66Camping . goes :Ibo
77
88module Ibo ::Helpers
9- def account_name account , allow_blank : false
10- the_name = if File . exists? ( 'tws_alias.yml' )
11- YAML . load_file ( 'tws_alias.yml' ) [ :user ] [ account . account ] rescue account . alias
12- else
13- account . alias # alias is set to account-number if no alias is given
14- end
15- allow_blank && ( the_name == account . account ) ? "" : the_name . presence || account . alias # return_value
16- end
9+ def account_name account , allow_blank : false
10+ the_name = if File . exists? ( 'tws_alias.yml' )
11+ YAML . load_file ( 'tws_alias.yml' ) [ :user ] [ account . account ] rescue account . alias
12+ else
13+ account . alias # alias is set to account-number if no alias is given
14+ end
15+ allow_blank && ( the_name == account . account ) ? "" : the_name . presence || account . alias # return_value
16+ end
17+
1718 def get_account account_id # returns an account-object
1819 initialize_gw . active_accounts . detect { |x | x . account == account_id }
1920 end
21+
2022 def initialize_gw
2123 if IB ::Gateway . current . nil? # only the first time ...
2224 host = File . exists? ( 'tws_alias.yml' ) ? YAML . load_file ( 'tws_alias.yml' ) [ :host ] : 'localhost'
23- # client_id 0 gets any open order
25+ # client_id 0 gets any open order
2426 gw = IB ::Gateway . new ( host : host , client_id : 0 , logger : Logger . new ( 'simple-monitor.log' ) )
2527 gw . logger . level = Logger ::WARN
2628 gw . logger . formatter = proc { |severity , datetime , progname , msg | "#{ datetime . strftime ( "%d.%m.(%X)" ) } #{ "%5s" % severity } ->#{ msg } \n " }
@@ -29,10 +31,11 @@ def initialize_gw
2931 end
3032 IB ::Gateway . current # return_value
3133 end
34+
3235 def all_contracts *sort
3336 sort = [ :sec_type ] if sort . empty?
3437 sort . map! { |x | x . is_a? ( Symbol ) ? x : x . to_sym }
35- initialize_gw . all_contracts . sort_by { |x | sort . map { |s | x . send ( s ) } }
38+ initialize_gw . all_contracts . sort_by { |x | sort . map { |s | x . send ( s ) } }
3639 end
3740end
3841
@@ -68,80 +71,79 @@ def get action
6871 end
6972
7073 class SelectAccount # < R '/Status'
71- def init_account_values
72- account_value = -> ( item ) do
73- initialize_gw . get_account_data ( @account )
74- @account . simple_account_data_scan ( item )
75- . map { |y | [ y . value , y . currency ] unless y . value . to_i . zero? }
76- . compact
77- . sort { |a , b | b . last == 'BASE' ? 1 : a . last <=> b . last } # put base element in front
78- end
79- { 'Cash' => account_value [ 'TotalCashBalance' ] ,
80- 'FuturesPNL' => account_value [ 'PNL' ] ,
81- 'FutureOptions' => account_value [ 'FutureOption' ] ,
82- 'Options' => account_value [ 'OptionMarket' ] ,
83- 'Stocks' => account_value [ 'StockMarket' ] } # return_value
84- end
85- def get action
86- @account = get_account action . split ( '/' ) . last
87- @account_values = init_account_values
88- render :show_account
89- end
90-
91- def post
92- @account = get_account @input [ 'account' ]
93- @contract = IB ::Stock . new
94- @account_values = init_account_values
95- render :contract_mask
74+ def init_account_values
75+ account_value = -> ( item ) do
76+ initialize_gw . get_account_data ( @account )
77+ @account . simple_account_data_scan ( item )
78+ . map { |y | [ y . value , y . currency ] unless y . value . to_i . zero? }
79+ . compact
80+ . sort { |a , b | b . last == 'BASE' ? 1 : a . last <=> b . last } # put base element in front
9681 end
82+ { 'Cash' => account_value [ 'TotalCashBalance' ] ,
83+ 'FuturesPNL' => account_value [ 'PNL' ] ,
84+ 'FutureOptions' => account_value [ 'FutureOption' ] ,
85+ 'Options' => account_value [ 'OptionMarket' ] ,
86+ 'Stocks' => account_value [ 'StockMarket' ] } # return_value
87+ end
88+ def get action
89+ @account = get_account action . split ( '/' ) . last
90+ @account_values = init_account_values
91+ render :show_account
9792 end
9893
99- class ContractX # < R '/contract/(\d+)/select'
100- def post account_id
101- # if symbol is specified, search for the contract, otherwise use predefined contract
102- # The contract itself is initialized after verifying
103- @account = get_account account_id
104- c = if input . include? ( 'predefined_contract' )
105- @account . contracts . detect { |x | x . con_id == input [ 'predefined_contract' ] . to_i }
106- else
107- @input [ :right ] = @input [ 'right' ] [ 0 ] . upcase if input [ 'sec_type' ] =='option'
108- @input [ :sec_type ] = @input [ 'sec_type' ] . to_sym
109- IB ::Contract . build @input #.reject{|x| ['right','sec_type'].include?(x) }
110- end
111- c . exchange = nil unless c . con_id . zero? # walkaround to enable verifying by con_id
112- count = c . verify { |y | @contract = y }
113- @message = if count . zero?
114- @contract ||= IB ::Stock . new
115- "Not a valid contract, details in Log"
116- else
117- ""
118- end
119- # include contract in contracts-collection of the account
120- @account . contracts . update_or_create ( @contract ) unless @contract . con_id . to_i . zero?
121- render :contract_mask
122- end
94+ def post
95+ @account = get_account @input [ 'account' ]
96+ @contract = IB ::Stock . new
97+ @account_values = init_account_values
98+ render :contract_mask
12399 end
100+ end
124101
125- class OrderXN
126- def get account_id , local_id # use get request to cancel an order
127- account = get_account account_id
128- order = account . orders . detect { |x | x . local_id == local_id . to_i }
129- IB ::Gateway . current . cancel_order order . local_id if order . is_a? IB ::Order
130- sleep 1
102+ class ContractX # < R '/contract/(\d+)/select'
103+ def post account_id
104+ # if symbol is specified, search for the contract, otherwise use predefined contract
105+ # The contract itself is initialized after verifying
106+ @account = get_account account_id
107+ c = if input . include? ( 'predefined_contract' )
108+ @account . contracts . detect { |x | x . con_id == input [ 'predefined_contract' ] . to_i }
109+ else
110+ @input [ :right ] = @input [ 'right' ] [ 0 ] . upcase if input [ 'sec_type' ] =='option'
111+ @input [ :sec_type ] = @input [ 'sec_type' ] . to_sym
112+ IB ::Contract . build @input #.reject{|x| ['right','sec_type'].include?(x) }
113+ end
114+ c . exchange = nil unless c . con_id . zero? # walkaround to enable verifying by con_id
115+ count = c . verify { |y | @contract = y }
116+ @message = if count . zero?
117+ @contract ||= IB ::Stock . new
118+ "Not a valid contract, details in Log"
119+ else
120+ ""
121+ end
122+ # include contract in contracts-collection of the account
123+ @account . contracts . update_or_create ( @contract ) unless @contract . con_id . to_i . zero?
124+ render :contract_mask
125+ end
126+ end
131127
132- redirect Index
133- end
128+ class OrderXN
129+ def get account_id , local_id # use get request to cancel an order
130+ account = get_account account_id
131+ order = account . orders . detect { |x | x . local_id == local_id . to_i }
132+ IB ::Gateway . current . cancel_order order . local_id if order . is_a? IB ::Order
133+ sleep 1
134+ redirect Index
135+ end
134136
135- def post account_id , con_id # use put request to place an order
136- account = get_account account_id
137- contract = account . contracts . detect { |x | x . con_id == con_id . to_i }
138- @input [ 'action' ] = @input . total_quantity . to_i > 0 ? :buy : :sell
139- @input . total_quantity = @input . total_quantity . to_i . abs
140-
141- account . place_order order : IB ::Order . new ( @input ) , contract :contract
142- redirect Index
143- end
137+ def post account_id , con_id # use put request to place an order
138+ account = get_account account_id
139+ contract = account . contracts . detect { |x | x . con_id == con_id . to_i }
140+ @input [ 'action' ] = @input . total_quantity . to_i > 0 ? :buy : :sell
141+ @input . total_quantity = @input . total_quantity . to_i . abs
142+
143+ account . place_order order : IB ::Order . new ( @input ) , contract :contract
144+ redirect Index
144145 end
146+ end
145147
146148 class Style < R '/styles\.css'
147149 STYLE = File . read ( __FILE__ ) . gsub ( /.*__END__/m , '' )
@@ -281,39 +283,38 @@ def contract_mask
281283 end # contract_mask
282284
283285 def _order_mask
284- negative_position = -> do # returns the negative position-size (if present) or ""
285- the_p_position = @account . portfolio_values . find { |p | p . contract . con_id == @contract . con_id }
286- the_p_position . present? ? -the_p_position . position . to_i : ""
287- end
288-
289- the_price = -> { @contract . misc . last } # holds the market price from the previous query
286+ negative_position = -> do # returns the negative position-size (if present) or ""
287+ the_p_position = @account . portfolio_values . find { |p | p . contract . con_id == @contract . con_id }
288+ the_p_position . present? ? -the_p_position . position . to_i : ""
289+ end
290+ the_price = -> { @contract . misc . last } # holds the market price from the previous query
290291
291- form action : R ( OrderXN , @account . account , @contract . con_id ) , method : 'post' do
292+ form action : R ( OrderXN , @account . account , @contract . con_id ) , method : 'post' do
292293
293- table do
294- tr . exited do
295- td
296- td @contract . symbol # {"#{@contract.to_human }" }
297- td ( colspan : 4 , align : 'left' ) { 'Order-Mask' }
298- end
299- tr do
300- td 'Size'
301- td { input type : :text , value : negative_position [ ] , name : 'total_quantity' } ;
302- td '(Primary) Price'
303- td { input type : :text , value : the_price [ ] , name : 'limit_price' } ;
304- td '(Aux) Price'
305- td { input type : :text , value : '' , name : 'aux_price' } ;
306- end
307- tr do
308- td 'Order Type'
309- td { select ( name : "order_type" , size :1 ) { IB ::ORDER_TYPES . each { |a , b | option ( value : a ) { b } } } }
310- td 'Validity'
311- td { select ( name : 'tif' , size :1 ) { [ "GTC" , "DAY" ] . each { |x | option x } } }
312- td { input :type => 'submit' , :class => 'submit' , :value => 'submit' }
313- end
294+ table do
295+ tr . exited do
296+ td
297+ td @contract . symbol # {"#{@contract.to_human }" }
298+ td ( colspan : 4 , align : 'left' ) { 'Order-Mask' }
299+ end
300+ tr do
301+ td 'Size'
302+ td { input type : :text , value : negative_position [ ] , name : 'total_quantity' } ;
303+ td '(Primary) Price'
304+ td { input type : :text , value : the_price [ ] , name : 'limit_price' } ;
305+ td '(Aux) Price'
306+ td { input type : :text , value : '' , name : 'aux_price' } ;
307+ end
308+ tr do
309+ td 'Order Type'
310+ td { select ( name : "order_type" , size :1 ) { IB ::ORDER_TYPES . each { |a , b | option ( value : a ) { b } } } }
311+ td 'Validity'
312+ td { select ( name : 'tif' , size :1 ) { [ "GTC" , "DAY" ] . each { |x | option x } } }
313+ td { input :type => 'submit' , :class => 'submit' , :value => 'submit' }
314314 end
315315 end
316- end
316+ end
317+ end
317318
318319 def show_index
319320 form action : R ( SelectAccount ) , method : 'post' do
0 commit comments