Module: IB::Setup

Defined in:
lib/ib/setup-orientdb.rb

Class Method Summary collapse

Class Method Details

.clear_databaseObject



106
107
108
109
# File 'lib/ib/setup-orientdb.rb', line 106

def self.clear_database 
	IB::Account.delete  all: true
   IB::Contract.delete all: true
end

.connect(tws:, orientdb:, kind: :connection) ⇒ Object

connects to a running interactive brokers TWS or Gateway and to an active OrientDB-Server.

Parameter tws: host: (default ‘localhost’)

port: (default 4002 , specify 4001/4002 (Gateway) or 7496/7497 ())

Parameter orientdb: server: ( default: ‘localhost’ )

user: 
password:
database: ( default: 'temp' )

The parameter are transmitted to IB::Connection and OrientDB without further checking

It returns the active Connection

The optional block is evaluated in the context of the initialized, but not connected IB::Connection.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ib/setup-orientdb.rb', line 24

def self.connect tws:, orientdb:, kind: :connection
	project_root = File.expand_path('../..', __FILE__)
	ActiveOrient::Init.connect **orientdb
	ActiveOrient::Model.keep_models_without_file = false
	#IB::Model = V   --> Dynamic Assignment Error, this does the same:
	IB.const_set( :Model, V )

 	TG.connect { project_root + '/models'}

	## There are two locations of modelfiles
	## 1. ib-api-model files
	## 2. ib.orientdb-model files
	model_dir =[ (Pathname.new( `gem which ib-api`.to_s[0..-2]).dirname + "models/").to_s ,
																 project_root + '/models']
	ActiveOrient::Init.define_namespace { IB  }
	ActiveOrient::OrientDB.new model_dir: model_dir

	init_database 
	require 'ib/messages'

	target = (kind == :connection) ? IB::Connection : IB::OrientGateway
	if block_given? 
		target.new( **tws )  { |c| yield c  }
	else
		target.new **tws 
	end

	require 'ib/extensions'

end

.init_database(db = V.db) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ib/setup-orientdb.rb', line 55

def self.init_database db= V.db
		vertices= db.class_hierarchy( base_class: 'V' ).flatten
		return if vertices.include? "ib_contract"
     init_timegraph
    	ActiveOrient::Init.define_namespace { IB }

		### Vertices
	  V.create_class            :contract	, :account  
		IB::Account.create_class  :advisor, :user
		IB::Advisor.create_class  :demo_advisor
		IB::User.create_class     :demo_user
		V.create_class            :portfolio_value, :account_value,  :underlying, :contract_detail, :bar
		IB::Contract.create_class :option, :future, :stock, :forex, :index, :bag
		IB::Bag.create_class      :spread
					
	  IB::Contract.create_property :con_id, type: :integer, index: :unique
	  IB::Contract.create_property :bars, type: :link_list,  index: :notunique, 
																									 linked_class: IB::Bar
	  IB::Contract.create_property :contract_detail, type: :link,  index: :notunique, 
																									 linked_class: IB::ContractDetail

		IB::Account.create_property  :account, type: :string, index: :unique
		IB::Account.create_property  :last_access, type: :date
		IB::User.create_property     :watchlist, type: :map
		IB::PortfolioValue.create_property  :position, type: :decimal
		IB::PortfolioValue.create_property  :market_price, type: :decimal
		IB::PortfolioValue.create_property  :market_value, type: :decimal
		IB::PortfolioValue.create_property  :average_cost, type: :decimal
		IB::PortfolioValue.create_property  :realized_pnl, type: :decimal
		IB::PortfolioValue.create_property  :unrealized_pnl, type: :decimal
	  IB::PortfolioValue.create_property  :contract, type: :link, index: :notunique, linked_class: IB::Contract

    	ActiveOrient::Init.define_namespace { HC }
		V.create_class :portfolio 
		HC::Portfolio.create_property :account, type: :link,  index: :notunique, linked_class: IB::Account
		HC::Portfolio.create_property :positions, type: :link_list, linked_class: IB::PortfolioValue
		HC::Portfolio.create_property :values, type:  :map 
		#
		## Edges   (Namespace HC)
		E.create_class :has_portfolio, :has_position,  :grid, :has_account, :has_strategy
		E.create_class  :d2F, :p2U, :my_user
		HC::GRID.uniq_index
end

.init_timegraphObject



99
100
101
102
103
104
# File 'lib/ib/setup-orientdb.rb', line 99

def self.init_timegraph
		## Initialize TimeGraph
		TG::Setup.init_database 
		TG::TimeGraph.populate 1980..2030
		TG.info
end