ib-ruby 0.7.4 → 0.7.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. data/.gitignore +3 -0
  2. data/HISTORY +8 -0
  3. data/README.md +2 -2
  4. data/Rakefile +15 -0
  5. data/TODO +7 -2
  6. data/VERSION +1 -1
  7. data/bin/account_info +1 -1
  8. data/bin/cancel_orders +1 -1
  9. data/bin/contract_details +1 -1
  10. data/bin/depth_of_market +1 -1
  11. data/bin/fa_accounts +1 -1
  12. data/bin/fundamental_data +42 -0
  13. data/bin/historic_data +1 -1
  14. data/bin/historic_data_cli +1 -1
  15. data/bin/list_orders +1 -2
  16. data/bin/market_data +1 -1
  17. data/bin/option_data +1 -1
  18. data/bin/place_combo_order +1 -1
  19. data/bin/place_order +1 -1
  20. data/bin/template +1 -4
  21. data/bin/tick_data +2 -2
  22. data/bin/time_and_sales +1 -1
  23. data/lib/ib-ruby.rb +4 -0
  24. data/lib/ib-ruby/connection.rb +50 -34
  25. data/lib/ib-ruby/constants.rb +232 -37
  26. data/lib/ib-ruby/db.rb +25 -0
  27. data/lib/ib-ruby/extensions.rb +51 -1
  28. data/lib/ib-ruby/messages/abstract_message.rb +0 -8
  29. data/lib/ib-ruby/messages/incoming.rb +18 -493
  30. data/lib/ib-ruby/messages/incoming/abstract_message.rb +100 -0
  31. data/lib/ib-ruby/messages/incoming/alert.rb +34 -0
  32. data/lib/ib-ruby/messages/incoming/contract_data.rb +82 -0
  33. data/lib/ib-ruby/messages/incoming/delta_neutral_validation.rb +20 -0
  34. data/lib/ib-ruby/messages/incoming/execution_data.rb +59 -0
  35. data/lib/ib-ruby/messages/incoming/historical_data.rb +55 -0
  36. data/lib/ib-ruby/messages/incoming/market_depths.rb +44 -0
  37. data/lib/ib-ruby/messages/incoming/open_order.rb +32 -16
  38. data/lib/ib-ruby/messages/incoming/order_status.rb +67 -0
  39. data/lib/ib-ruby/messages/incoming/portfolio_value.rb +39 -0
  40. data/lib/ib-ruby/messages/incoming/real_time_bar.rb +32 -0
  41. data/lib/ib-ruby/messages/incoming/scanner_data.rb +49 -0
  42. data/lib/ib-ruby/messages/outgoing.rb +25 -223
  43. data/lib/ib-ruby/messages/outgoing/abstract_message.rb +61 -0
  44. data/lib/ib-ruby/messages/outgoing/bar_requests.rb +149 -0
  45. data/lib/ib-ruby/messages/outgoing/place_order.rb +24 -0
  46. data/lib/ib-ruby/models.rb +4 -0
  47. data/lib/ib-ruby/models/bar.rb +31 -14
  48. data/lib/ib-ruby/models/combo_leg.rb +48 -23
  49. data/lib/ib-ruby/models/contracts.rb +2 -2
  50. data/lib/ib-ruby/models/contracts/bag.rb +11 -7
  51. data/lib/ib-ruby/models/contracts/contract.rb +90 -66
  52. data/lib/ib-ruby/models/contracts/option.rb +16 -7
  53. data/lib/ib-ruby/models/execution.rb +34 -18
  54. data/lib/ib-ruby/models/model.rb +15 -7
  55. data/lib/ib-ruby/models/model_properties.rb +101 -44
  56. data/lib/ib-ruby/models/order.rb +176 -187
  57. data/lib/ib-ruby/models/order_state.rb +99 -0
  58. data/lib/ib-ruby/symbols/forex.rb +10 -10
  59. data/lib/ib-ruby/symbols/futures.rb +6 -6
  60. data/lib/ib-ruby/symbols/stocks.rb +3 -3
  61. data/spec/account_helper.rb +4 -5
  62. data/spec/combo_helper.rb +4 -4
  63. data/spec/db.rb +18 -0
  64. data/spec/ib-ruby/messages/{incoming_spec.rb → incoming/alert_spec.rb} +1 -0
  65. data/spec/ib-ruby/messages/incoming/open_order_spec.rb +100 -0
  66. data/spec/ib-ruby/messages/incoming/order_status_spec.rb +74 -0
  67. data/spec/ib-ruby/messages/{outgoing_spec.rb → outgoing/account_data_spec.rb} +0 -0
  68. data/spec/ib-ruby/messages/outgoing/market_data_type_spec.rb +44 -0
  69. data/spec/ib-ruby/models/bag_spec.rb +97 -0
  70. data/spec/ib-ruby/models/bar_spec.rb +45 -0
  71. data/spec/ib-ruby/models/combo_leg_spec.rb +56 -40
  72. data/spec/ib-ruby/models/contract_spec.rb +134 -170
  73. data/spec/ib-ruby/models/execution_spec.rb +35 -50
  74. data/spec/ib-ruby/models/option_spec.rb +127 -0
  75. data/spec/ib-ruby/models/order_spec.rb +89 -68
  76. data/spec/ib-ruby/models/order_state_spec.rb +55 -0
  77. data/spec/integration/contract_info_spec.rb +4 -6
  78. data/spec/integration/fundamental_data_spec.rb +41 -0
  79. data/spec/integration/historic_data_spec.rb +4 -4
  80. data/spec/integration/market_data_spec.rb +1 -3
  81. data/spec/integration/orders/attached_spec.rb +8 -10
  82. data/spec/integration/orders/combo_spec.rb +2 -2
  83. data/spec/integration/orders/execution_spec.rb +0 -1
  84. data/spec/integration/orders/placement_spec.rb +1 -3
  85. data/spec/integration/orders/valid_ids_spec.rb +1 -2
  86. data/spec/message_helper.rb +1 -1
  87. data/spec/model_helper.rb +211 -0
  88. data/spec/order_helper.rb +44 -37
  89. data/spec/spec_helper.rb +36 -23
  90. data/spec/v.rb +7 -0
  91. data/tasks/doc.rake +1 -1
  92. metadata +116 -12
  93. data/spec/integration/orders/open_order +0 -98
@@ -1,94 +1,30 @@
1
- require 'ib-ruby/models/model'
2
-
3
- # TODO: Implement equals() according to the criteria in IB's Java client.
4
-
5
1
  module IB
6
2
  module Models
7
- class Order < Model
3
+ class Order < Model.for(:order)
4
+ include ModelProperties
8
5
 
9
6
  # General Notes:
10
7
  # 1. Placing Orders by con_id - When you place an order by con_id, you must
11
8
  # provide the con_id AND the exchange. If you provide extra fields when placing
12
9
  # an order by conid, the order may not work.
13
10
 
14
- # 2. Order IDs - Each order you place must have a unique Order ID. We recommend
15
- # that you increment your own Order IDs to avoid conflicts between orders placed
16
- # from your API application.
17
-
18
- # Constants used in Order objects. Drawn from Order.java
19
- Origin_Customer = 0
20
- Origin_Firm = 1
21
-
22
- Opt_Unknown = '?'
23
- Opt_Broker_Dealer = 'b'
24
- Opt_Customer = 'c'
25
- Opt_Firm = 'f'
26
- Opt_Isemm = 'm'
27
- Opt_Farmm = 'n'
28
- Opt_Specialist = 'y'
29
-
30
- OCA_Cancel_with_block = 1
31
- OCA_Reduce_with_block = 2
32
- OCA_Reduce_non_block = 3
33
-
34
- # Box orders consts:
35
- Box_Auction_Match = 1
36
- Box_Auction_Improvement = 2
37
- Box_Auction_Transparent = 3
38
-
39
- # Volatility orders consts:
40
- Volatility_Type_Daily = 1
41
- Volatility_Type_Annual = 2
42
- Volatility_Ref_Price_Average = 1
43
- Volatility_Ref_Price_BidOrAsk = 2
11
+ # 2. Order IDs - Each order you place must have a unique Order ID. Increment
12
+ # your own Order IDs to avoid conflicts between orders placed from your API application.
44
13
 
45
14
  # Main order fields
46
15
  prop :order_id, # int: Order id associated with client (volatile).
47
16
  :client_id, # int: The id of the client that placed this order.
48
17
  :perm_id, # int: TWS permanent id, remains the same over TWS sessions.
49
- :action, # String: Identifies the side: BUY/SELL/SSHORT
50
18
  :total_quantity, # int: The order quantity.
51
19
 
52
- :order_type, # String: Identifies the order type. Valid values are:
53
- # Limit Risk:
54
- # MTL Market-to-Limit
55
- # MKT PRT Market with Protection
56
- # QUOTE Request for Quote
57
- # STP Stop
58
- # STP LMT Stop Limit
59
- # TRAIL Trailing Stop
60
- # TRAIL LIMIT Trailing Stop Limit
61
- # TRAIL LIT Trailing Limit if Touched
62
- # TRAIL MIT Trailing Market If Touched
63
- # Speed of Execution:
64
- # MKT Market
65
- # MIT Market-if-Touched
66
- # MOC Market-on-Close MKTCLSL ?
67
- # MOO Market-on-Open
68
- # PEG MKT Pegged-to-Market
69
- # REL Relative
70
- # Price Improvement:
71
- # BOX TOP Box Top
72
- # LOC Limit-on-Close LMTCLS ?
73
- # LOO Limit-on-Open
74
- # LIT Limit if Touched
75
- # PEG MID Pegged-to-Midpoint
76
- # VWAP VWAP-Guaranteed
77
- # Advanced Trading:
78
- # OCA One-Cancels-All
79
- # VOL Volatility
80
- # SCALE Scale
81
- # Other (no abbreviation):
82
- # Bracket
83
- # At Auction
84
- # Discretionary
85
- # Sweep-to-Fill
86
- # Price Improvement Auction
87
- # Block
88
- # Hidden
89
- # Iceberg/Reserve
90
- # All-or-None
91
- # Fill-or-Kill
20
+ :order_type, # String: Order type.
21
+ # Limit Risk: MTL / MKT PRT / QUOTE / STP / STP LMT / TRAIL / TRAIL LIMIT / TRAIL LIT / TRAIL MIT
22
+ # Speed of Execution: MKT / MIT / MOC / MOO / PEG MKT / REL
23
+ # Price Improvement: BOX TOP / LOC / LOO / LIT / PEG MID / VWAP
24
+ # Advanced Trading: OCA / VOL / SCALE
25
+ # Other (no abbreviation): Bracket, Auction, Discretionary, Sweep-to-Fill,
26
+ # Price Improvement Auction, Block, Hidden, Iceberg/Reserve, All-or-None, Fill-or-Kill
27
+ # See 'ib-ruby/constants.rb' ORDER_TYPES for a complete list of valid values.
92
28
 
93
29
  :limit_price, # double: LIMIT price, used for limit, stop-limit and relative
94
30
  # orders. In all other cases specify zero. For relative
@@ -97,12 +33,6 @@ module IB
97
33
  :aux_price, # double: STOP price for stop-limit orders, and the OFFSET amount
98
34
  # for relative orders. In all other cases, specify zero.
99
35
 
100
- :tif, # String: Time to Market:
101
- # DAY
102
- # GAT Good-after-Time/Date
103
- # GTD Good-till-Date/Time
104
- # GTC Good-till-Canceled
105
- # IOC Immediate-or-Cancel
106
36
  :oca_group, # String: Identifies a member of a one-cancels-all group.
107
37
  :oca_type, # int: Tells how to handle remaining orders in an OCA group
108
38
  # when one order or part of an order executes. Valid values:
@@ -113,17 +43,13 @@ module IB
113
43
  # overfill protection. This means that only one order in
114
44
  # the group will be routed at a time to remove the
115
45
  # possibility of an overfill.
116
- :transmit, # bool:if false, order will be created but not transmitted.
117
46
  :parent_id, # int: The order ID of the parent (original) order, used
118
47
  # for bracket (STP) and auto trailing stop (TRAIL) orders.
119
- :block_order, # bool: the order is an ISE Block order.
120
- :sweep_to_fill, # bool: the order is a Sweep-to-Fill order.
121
48
  :display_size, # int: publicly disclosed order size for Iceberg orders.
122
49
 
123
50
  :trigger_method, # Specifies how Simulated Stop, Stop-Limit and Trailing
124
51
  # Stop orders are triggered. Valid values are:
125
- # 0 - Default, "double bid/ask" method will be used for OTC stocks
126
- # and US options orders, "last" method will be used all others.
52
+ # 0 - Default, "double bid/ask" for OTC/US options, "last" otherswise.
127
53
  # 1 - "double bid/ask" method, stop orders are triggered based on
128
54
  # two consecutive bid or ask prices.
129
55
  # 2 - "last" method, stops are triggered based on the last price.
@@ -141,29 +67,16 @@ module IB
141
67
  # and the spread between the bid and ask must be less than
142
68
  # 0.1% of the midpoint
143
69
 
144
- :what_if, # bool: Use to request pre-trade commissions and margin
145
- # information. If set to true, margin and commissions data is received
146
- # back via the OrderState() object for the openOrder() callback.
147
- :not_held, # public boolean m_notHeld; // Not Held
148
- :outside_rth, # bool: allows orders to also trigger or fill outside
149
- # of regular trading hours. (WAS: ignore_rth)
150
- :hidden, # bool: the order will not be visible when viewing
151
- # the market depth. Only for ISLAND exchange.
152
70
  :good_after_time, # Indicates that the trade should be submitted after the
153
71
  # time and date set, format YYYYMMDD HH:MM:SS (seconds are optional).
154
72
  :good_till_date, # Indicates that the trade should remain working until the
155
73
  # time and date set, format YYYYMMDD HH:MM:SS (seconds are optional).
156
74
  # You must set the :tif to GTD when using this string.
157
75
  # Use an empty String if not applicable.
158
- :override_percentage_constraints, # bool: Precautionary constraints defined on
159
- # the TWS Presets page ensure that your price and size order values are reasonable.
160
- # Orders sent from the API are also validated against these safety constraints,
161
- # unless this parameter is set to True.
162
76
 
163
77
  :rule_80a, # Individual = 'I', Agency = 'A', AgentOtherMember = 'W',
164
78
  # IndividualPTIA = 'J', AgencyPTIA = 'U', AgentOtherMemberPTIA = 'M',
165
79
  # IndividualPT = 'K', AgencyPT = 'Y', AgentOtherMemberPT = 'N'
166
- :all_or_none, # bool: yes=1, no=0
167
80
  :min_quantity, # int: Identifies a minimum quantity order type.
168
81
  :percent_offset, # double: percent offset amount for relative (REL)orders only
169
82
  :trail_stop_price, # double: for TRAILLIMIT orders only
@@ -174,7 +87,6 @@ module IB
174
87
  :fa_group, :fa_profile, :fa_method, :fa_percentage,
175
88
 
176
89
  # Institutional orders only!
177
- :open_close, # String: O=Open, C=Close
178
90
  :origin, # 0=Customer, 1=Firm
179
91
  :order_ref, # String: Order reference. Customer defined order ID tag.
180
92
  :short_sale_slot, # 1 - you hold the shares,
@@ -194,10 +106,7 @@ module IB
194
106
  # SMART routing only
195
107
  :discretionary_amount, # double: The amount off the limit price
196
108
  # allowed for discretionary orders.
197
- :etrade_only, # bool: Trade with electronic quotes.
198
- :firm_quote_only, # bool: Trade with firm quotes.
199
109
  :nbbo_price_cap, # double: Maximum Smart order distance from the NBBO.
200
- :opt_out_smart_routing, # Australian exchange only, default false
201
110
 
202
111
  # BOX or VOL ORDERS ONLY
203
112
  :auction_strategy, # For BOX exchange only. Valid values:
@@ -216,6 +125,7 @@ module IB
216
125
  # underlying stock price range.
217
126
 
218
127
  # VOLATILITY ORDERS ONLY:
128
+ # http://www.interactivebrokers.com/en/general/education/pdfnotes/PDF-VolTrader.php
219
129
  :volatility, # double: What the price is, computed via TWSs Options
220
130
  # Analytics. For VOL orders, the limit price sent to an
221
131
  # exchange is not editable, as it is the output of a
@@ -233,6 +143,7 @@ module IB
233
143
  :delta_neutral_order_type, # String: Enter an order type to instruct TWS
234
144
  # to submit a delta neutral trade on full or partial execution of the
235
145
  # VOL order. For no hedge delta order to be sent, specify NONE.
146
+ # Valid values - LMT, MKT, MTL, REL, MOC
236
147
  :delta_neutral_aux_price, # double: Use this field to enter a value if
237
148
  # the value in the deltaNeutralOrderType field is an order
238
149
  # type that requires an Aux price, such as a REL order.
@@ -276,83 +187,103 @@ module IB
276
187
  :scale_price_adjust_value,
277
188
  :scale_price_adjust_interval,
278
189
  :scale_profit_offset,
279
- :scale_auto_reset,
280
190
  :scale_init_position,
281
191
  :scale_init_fill_qty,
282
- :scale_random_percent
283
-
284
- # Some Order properties (received back from IB) are separated into
285
- # OrderState object. Here, they are lumped into Order proper: see OrderState.java
286
- # TODO: Extract OrderState object, for better record keeping
287
- prop :status, # String: Displays the order status.Possible values include:
288
- # PendingSubmit - indicates that you have transmitted the order, but
289
- # have not yet received confirmation that it has been accepted by the
290
- # order destination. NOTE: This order status is NOT sent back by TWS
291
- # and should be explicitly set by YOU when an order is submitted.
292
- # PendingCancel - indicates that you have sent a request to cancel
293
- # the order but have not yet received cancel confirmation from the
294
- # order destination. At this point, your order cancel is not confirmed.
295
- # You may still receive an execution while your cancellation request
296
- # is pending. NOTE: This order status is not sent back by TWS and
297
- # should be explicitly set by YOU when an order is canceled.
298
- # PreSubmitted - indicates that a simulated order type has been
299
- # accepted by the IB system and that this order has yet to be elected.
300
- # The order is held in the IB system until the election criteria are
301
- # met. At that time the order is transmitted to the order destination
302
- # as specified.
303
- # Submitted - indicates that your order has been accepted at the order
304
- # destination and is working.
305
- # � Cancelled - indicates that the balance of your order has been
306
- # confirmed canceled by the IB system. This could occur unexpectedly
307
- # when IB or the destination has rejected your order.
308
- # � ApiCancelled - canceled via API
309
- # Filled - indicates that the order has been completely filled.
310
- # Inactive - indicates that the order has been accepted by the system
311
- # (simulated orders) or an exchange (native orders) but that currently
312
- # the order is inactive due to system, exchange or other issues.
313
- :commission, # double: Shows the commission amount on the order.
314
- :commission_currency, # String: Shows the currency of the commission.
315
- #The possible range of the actual order commission:
316
- :min_commission,
317
- :max_commission,
318
- :warning_text, # String: Displays a warning message if warranted.
319
- :init_margin, # Float: The impact the order would have on your initial margin.
320
- :maint_margin, # Float: The impact the order would have on your maintenance margin.
321
- :equity_with_loan # Float: The impact the order would have on your equity
192
+ :scale_auto_reset => :bool,
193
+ :scale_random_percent => :bool
194
+
195
+ # Properties with complex processing logics
196
+ prop :tif, # String: Time in Force (time to market): DAY/GAT/GTD/GTC/IOC
197
+ :what_if => :bool, # Only return pre-trade commissions and margin info, do not place
198
+ :not_held => :bool, # Not Held
199
+ :outside_rth => :bool, # Order may trigger or fill outside of regular hours. (WAS: ignore_rth)
200
+ :hidden => :bool, # Order will not be visible in market depth. ISLAND only.
201
+ :transmit => :bool, # If false, order will be created but not transmitted.
202
+ :block_order => :bool, # This is an ISE Block order.
203
+ :sweep_to_fill => :bool, # This is a Sweep-to-Fill order.
204
+ :override_percentage_constraints => :bool,
205
+ # TWS Presets page constraints ensure that your price and size order values
206
+ # are reasonable. Orders sent from the API are also validated against these
207
+ # safety constraints, unless this parameter is set to True.
208
+ :all_or_none => :bool, # AON
209
+ :etrade_only => :bool, # Trade with electronic quotes.
210
+ :firm_quote_only => :bool, # Trade with firm quotes.
211
+ :opt_out_smart_routing => :bool, # Australian exchange only, default false
212
+ :open_close => PROPS[:open_close], # Originally String: O=Open, C=Close ()
213
+ # for ComboLeg compatibility: SAME = 0; OPEN = 1; CLOSE = 2; UNKNOWN = 3;
214
+ [:side, :action] => PROPS[:side] # String: Action/side: BUY/SELL/SSHORT/SSHORTX
215
+
216
+ # Some properties received from IB are separated into OrderState object,
217
+ # but they are still readable as Order properties through delegation.
218
+ #
219
+ # Properties arriving via OpenOrder message:
220
+ [:commission, # double: Shows the commission amount on the order.
221
+ :commission_currency, # String: Shows the currency of the commission.
222
+ :min_commission, # The possible min range of the actual order commission.
223
+ :max_commission, # The possible max range of the actual order commission.
224
+ :warning_text, # String: Displays a warning message if warranted.
225
+ :init_margin, # Float: The impact the order would have on your initial margin.
226
+ :maint_margin, # Float: The impact the order would have on your maintenance margin.
227
+ :equity_with_loan, # Float: The impact the order would have on your equity
228
+ :status, # String: Displays the order status. See OrderState for values
229
+ # Properties arriving via OrderStatus message:
230
+ :filled, # int
231
+ :remaining, # int
232
+ :average_fill_price, # double
233
+ :last_fill_price, # double
234
+ :why_held # String: comma-separated list of reasons for order to be held.
235
+ ].each { |property| define_method(property) { order_state.send(property) } }
322
236
 
323
237
  # Returned in OpenOrder for Bag Contracts
324
238
  # public Vector<OrderComboLeg> m_orderComboLegs
325
- attr_accessor :leg_prices, :combo_params
239
+ attr_accessor :leg_prices, :combo_params, :order_state
326
240
  alias order_combo_legs leg_prices
327
241
  alias smart_combo_routing_params combo_params
328
242
 
243
+ # TODO: :created_at, :placed_at, :modified_at accessors
244
+
245
+ # Order is not valid without correct :order_id
246
+ validates_numericality_of :order_id, :only_integer => true
247
+
329
248
  DEFAULT_PROPS = {:aux_price => 0.0,
249
+ :discretionary_amount => 0.0,
330
250
  :parent_id => 0,
331
- :tif => 'DAY',
332
- :outside_rth => false,
333
- :open_close => 'O',
334
- :origin => Origin_Customer,
335
- :transmit => true,
251
+ :tif => :day,
252
+ :order_type => :limit,
253
+ :open_close => :open,
254
+ :origin => :customer,
255
+ :short_sale_slot => :default,
256
+ :trigger_method => :default,
257
+ :oca_type => :none,
258
+ :auction_strategy => :none,
336
259
  :designated_location => '',
337
260
  :exempt_code => -1,
338
- :delta_neutral_order_type => '',
261
+ :display_size => 0,
262
+ :continuous_update => 0,
339
263
  :delta_neutral_con_id => 0,
340
- :delta_neutral_settling_firm => '',
341
- :delta_neutral_clearing_account => '',
342
- :delta_neutral_clearing_intent => '',
343
264
  :algo_strategy => '',
265
+ # TODO: Add simple defaults to prop ?
266
+ :transmit => true,
344
267
  :what_if => false,
268
+ :hidden => false,
269
+ :etrade_only => false,
270
+ :firm_quote_only => false,
271
+ :block_order => false,
272
+ :all_or_none => false,
273
+ :sweep_to_fill => false,
345
274
  :not_held => false,
275
+ :outside_rth => false,
346
276
  :scale_auto_reset => false,
347
277
  :scale_random_percent => false,
348
278
  :opt_out_smart_routing => false,
349
- :status => 'New' # Starting new Orders with this statu
279
+ :override_percentage_constraints => false,
350
280
  }
351
281
 
352
282
  def initialize opts = {}
353
283
  @leg_prices = []
354
284
  @algo_params = {}
355
285
  @combo_params = {}
286
+ @order_state = IB::OrderState.new
356
287
  super opts
357
288
  end
358
289
 
@@ -360,40 +291,51 @@ module IB
360
291
  # mixed with data from associated contract. Ugly mix, indeed.
361
292
  def serialize_with server, contract
362
293
  [contract.serialize_long(:con_id, :sec_id),
363
- action, # main order fields
294
+ # main order fields
295
+ case side
296
+ when :short
297
+ 'SSHORT'
298
+ when :short_exempt
299
+ 'SSHORTX'
300
+ else
301
+ side.to_sup
302
+ end,
364
303
  total_quantity,
365
- order_type,
304
+ self[:order_type], # Internal code, 'LMT' instead of :limit
366
305
  limit_price,
367
306
  aux_price,
368
- tif, # extended order fields
307
+ self[:tif],
369
308
  oca_group,
370
309
  account,
371
- open_close,
372
- origin,
310
+ open_close.to_sup[0..0],
311
+ self[:origin],
373
312
  order_ref,
374
313
  transmit,
375
314
  parent_id,
376
315
  block_order,
377
316
  sweep_to_fill,
378
317
  display_size,
379
- trigger_method,
318
+ self[:trigger_method],
380
319
  outside_rth, # was: ignore_rth
381
320
  hidden,
382
321
  contract.serialize_legs(:extended),
383
322
 
384
- # Support for per-leg prices in Order
385
- if server[:server_version] >= 61
386
- leg_prices.empty? ? 0 : [leg_prices.size] + leg_prices
387
- else
388
- []
389
- end,
390
-
391
- # Support for combo routing params in Order
392
- if server[:server_version] >= 57 && contract.sec_type == 'BAG'
393
- combo_params.empty? ? 0 : [combo_params.size] + combo_params.to_a
394
- else
395
- []
396
- end,
323
+ # This is specific to PlaceOrder v.38, NOT supported by API yet!
324
+ #
325
+ ## Support for per-leg prices in Order
326
+ #if server[:server_version] >= 61 && contract.bag?
327
+ # leg_prices.empty? ? 0 : [leg_prices.size] + leg_prices
328
+ #else
329
+ # []
330
+ #end,
331
+ #
332
+ ## Support for combo routing params in Order
333
+ #if server[:server_version] >= 57 && contract.bag?
334
+ # p 'Here!'
335
+ # combo_params.empty? ? 0 : [combo_params.size] + combo_params.to_a
336
+ #else
337
+ # []
338
+ #end,
397
339
 
398
340
  '', # deprecated shares_allocation field
399
341
  discretionary_amount,
@@ -403,10 +345,10 @@ module IB
403
345
  fa_method,
404
346
  fa_percentage,
405
347
  fa_profile,
406
- short_sale_slot, # 0 only for retail, 1 or 2 for institution (Institutional)
348
+ self[:short_sale_slot], # 0 only for retail, 1 or 2 for institution (Institutional)
407
349
  designated_location, # only populate when short_sale_slot == 2 (Institutional)
408
350
  exempt_code,
409
- oca_type,
351
+ self[:oca_type],
410
352
  rule_80a,
411
353
  settling_firm,
412
354
  all_or_none,
@@ -415,23 +357,66 @@ module IB
415
357
  etrade_only,
416
358
  firm_quote_only,
417
359
  nbbo_price_cap,
418
- auction_strategy,
360
+ self[:auction_strategy],
419
361
  starting_price,
420
362
  stock_ref_price,
421
363
  delta,
422
364
  stock_range_lower,
423
365
  stock_range_upper,
424
366
  override_percentage_constraints,
425
- volatility, # Volatility orders
426
- volatility_type, # Volatility orders
427
- delta_neutral_order_type, # Volatility orders
428
- delta_neutral_aux_price, # Volatility orders
429
- continuous_update, # Volatility orders
430
- reference_price_type, # Volatility orders
367
+ volatility, # Volatility orders
368
+ self[:volatility_type], #
369
+ self[:delta_neutral_order_type],
370
+ delta_neutral_aux_price, #
371
+
372
+ # Support for delta neutral orders with parameters
373
+ if server[:server_version] >= 58 && delta_neutral_order_type
374
+ [delta_neutral_con_id,
375
+ delta_neutral_settling_firm,
376
+ delta_neutral_clearing_account,
377
+ self[:delta_neutral_clearing_intent]
378
+ ]
379
+ else
380
+ []
381
+ end,
382
+
383
+ continuous_update, # Volatility orders
384
+ self[:reference_price_type], # Volatility orders
385
+
431
386
  trail_stop_price, # TRAIL_STOP_LIMIT stop price
387
+
388
+ # Support for trailing percent
389
+ server[:server_version] >= 62 ? trailing_percent : [],
390
+
432
391
  scale_init_level_size, # Scale Orders
433
392
  scale_subs_level_size, # Scale Orders
434
393
  scale_price_increment, # Scale Orders
394
+
395
+ # Support extended scale orders parameters
396
+ if server[:server_version] >= 60 &&
397
+ scale_price_increment && scale_price_increment > 0
398
+ [scale_price_adjust_value,
399
+ scale_price_adjust_interval,
400
+ scale_profit_offset,
401
+ scale_auto_reset,
402
+ scale_init_position,
403
+ scale_init_fill_qty,
404
+ scale_random_percent
405
+ ]
406
+ else
407
+ []
408
+ end,
409
+
410
+ # TODO: Need to add support for hedgeType, not working ATM - beta only
411
+ #if (m_serverVersion >= MIN_SERVER_VER_HEDGE_ORDERS) {
412
+ # send (order.m_hedgeType);
413
+ # if (!IsEmpty(order.m_hedgeType)) send (order.m_hedgeParam);
414
+ #}
415
+ #
416
+ #if (m_serverVersion >= MIN_SERVER_VER_OPT_OUT_SMART_ROUTING) {
417
+ # send (order.m_optOutSmartRouting);
418
+ #}
419
+
435
420
  clearing_account,
436
421
  clearing_intent,
437
422
  not_held,
@@ -483,9 +468,13 @@ module IB
483
468
  end
484
469
 
485
470
  def to_human
486
- "<Order: #{order_type} #{tif} #{action} #{total_quantity} #{status} #{limit_price}" +
487
- " id: #{order_id}/#{perm_id} from: #{client_id}/#{account}" +
488
- (commission ? " fee: #{commission}" : "") + ">"
471
+ "<Order: " + ((order_ref && order_ref != '') ? "#{order_ref} " : '') +
472
+ "#{self[:order_type]} #{self[:tif]} #{side} #{total_quantity} " +
473
+ "#{status} " + (limit_price ? "#{limit_price} " : '') +
474
+ ((aux_price && aux_price != 0) ? "/#{aux_price}" : '') +
475
+ "##{order_id}/#{perm_id} from #{client_id}" +
476
+ (account ? "/#{account}" : '') +
477
+ (commission ? " fee #{commission}" : '') + ">"
489
478
  end
490
479
  end # class Order
491
480
  end # module Models