Class: IB::Messages::Incoming::OpenOrder

Inherits:
Object
  • Object
show all
Defined in:
lib/ib/messages/incoming/open_order.rb

Instance Method Summary collapse

Instance Method Details

#contractObject



100
101
102
103
104
# File 'lib/ib/messages/incoming/open_order.rb', line 100

def contract
  @contract ||= IB::Contract.build(
      @data[:contract].merge(:underlying => underlying)
  )
end

#filled?(value) ⇒ Boolean

Check if given value was set by TWS to something vaguely “positive”

Returns:

  • (Boolean)


208
209
210
211
212
213
214
215
216
217
# File 'lib/ib/messages/incoming/open_order.rb', line 208

def filled? value
  case value
    when String
      !value.empty?
    when Float, Integer
      value > 0
    else
      !!value # to_bool
  end
end

#loadObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/ib/messages/incoming/open_order.rb', line 112

def load
  super

  load_map [27, [proc { | | filled?(@data[:order][:delta_neutral_order_type]) },
                 # As of client v.52, we receive delta... params in openOrder
                 [:order, :delta_neutral_con_id, :int],
                 [:order, :delta_neutral_settling_firm, :string],
                 [:order, :delta_neutral_clearing_account, :string],
                 [:order, :delta_neutral_clearing_intent, :string]]
           ],
           [:order, :continuous_update, :int],
           [:order, :reference_price_type, :int],
           [:order, :trail_stop_price, :decimal_max],

           # As of client v.56, we receive trailing_percent in openOrder
           [30, [:order, :trailing_percent, :decimal_max]], # Never! 28 currently

           [:order, :basis_points, :decimal_max],
           [:order, :basis_points_type, :int_max],
           [:contract, :legs_description, :string],

           # As of client v.55, we receive orderComboLegs (price) in openOrder
           [29, [:contract, :legs, :array, proc do |_|
             IB::ComboLeg.new :con_id => socket.read_int,
                              :ratio => socket.read_int,
                              :action => socket.read_string,
                              :exchange => socket.read_string,
                              :open_close => socket.read_int,
                              :short_sale_slot => socket.read_int,
                              :designated_location => socket.read_string,
                              :exempt_code => socket.read_int
           end],

            # Order keeps received leg prices in a separate Array for some reason ?!
            [:order, :leg_prices, :array, proc { |_| socket.read_decimal_max }],
           ],
           # As of client v.51, we can receive smartComboRoutingParams in openOrder
           [26, [:smart_combo_routing_params, :hash]],

           [:order, :scale_init_level_size, :int_max],
           [:order, :scale_subs_level_size, :int_max],
           [:order, :scale_price_increment, :decimal_max],

           # As of client v.54, we can receive scale order fields
           [28, [proc { | | filled?(@data[:order][:scale_price_increment]) },
                 [:order, :scale_price_adjust_value, :decimal_max],
                 [:order, :scale_price_adjust_interval, :int_max],
                 [:order, :scale_profit_offset, :decimal_max],
                 [:order, :scale_auto_reset, :boolean],
                 [:order, :scale_init_position, :int_max],
                 [:order, :scale_init_fill_qty, :decimal_max],
                 [:order, :scale_random_percent, :boolean]]
           ],

           # As of client v.49/50, we can receive hedgeType, hedgeParam, optOutSmartRouting
           [25,
            [:order, :hedge_type, :string],
            [proc { | | filled?(@data[:order][:hedge_type]) },
             [:order, :hedge_param, :string],
            ],
            [:order, :opt_out_smart_routing, :boolean]
           ],

           [:order, :clearing_account, :string],
           [:order, :clearing_intent, :string],
           [:order, :not_held, :boolean],
           [:underlying_present, :boolean],

           [proc { | | filled?(@data[:underlying_present]) },
            [:underlying, :con_id, :int],
            [:underlying, :delta, :decimal],
            [:underlying, :price, :decimal]
           ],

           [:order, :algo_strategy, :string],

           # TODO: Test Order with algo_params, scale and legs!
           [proc { | | filled?(@data[:order][:algo_strategy]) },
            [:order, :algo_params, :hash]
           ],

           [:order, :what_if, :boolean],

           [:order_state, :status, :string],
           # IB uses weird String with Java Double.MAX_VALUE to indicate no value here
           [:order_state, :init_margin, :decimal_max], # :string],
           [:order_state, :maint_margin, :decimal_max], # :string],
           [:order_state, :equity_with_loan, :decimal_max], # :string],
           [:order_state, :commission, :decimal_max], # May be nil!
           [:order_state, :min_commission, :decimal_max], # May be nil!
           [:order_state, :max_commission, :decimal_max], # May be nil!
           [:order_state, :commission_currency, :string],
           [:order_state, :warning_text, :string]
end

#local_idObject Also known as: order_id

Accessors to make OpenOrder API-compatible with OrderStatus message



77
78
79
# File 'lib/ib/messages/incoming/open_order.rb', line 77

def local_id
  order.local_id
end

#orderObject



87
88
89
# File 'lib/ib/messages/incoming/open_order.rb', line 87

def order
  @order ||= IB::Order.new @data[:order].merge(:order_state => order_state)
end

#order_stateObject



91
92
93
94
95
96
97
98
# File 'lib/ib/messages/incoming/open_order.rb', line 91

def order_state
  @order_state ||= IB::OrderState.new(
      @data[:order_state].merge(
          :local_id => @data[:order][:local_id],
          :perm_id => @data[:order][:perm_id],
          :parent_id => @data[:order][:parent_id],
          :client_id => @data[:order][:client_id]))
end

#statusObject



83
84
85
# File 'lib/ib/messages/incoming/open_order.rb', line 83

def status
  order.status
end

#to_humanObject



219
220
221
# File 'lib/ib/messages/incoming/open_order.rb', line 219

def to_human
  "<OpenOrder: #{contract.to_human} #{order.to_human}>"
end

#underlyingObject Also known as: under_comp



106
107
108
# File 'lib/ib/messages/incoming/open_order.rb', line 106

def underlying
  @underlying = @data[:underlying_present] ? IB::Underlying.new(@data[:underlying]) : nil
end