Skip to content

Commit

Permalink
Removed old server version support in PlaceOrder Outgoing message, fi…
Browse files Browse the repository at this point in the history
…xed minor bugs that prevented replication of Orders between accounts.
  • Loading branch information
arvicco committed Nov 15, 2012
1 parent 76e7619 commit 03c179f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 67 deletions.
2 changes: 1 addition & 1 deletion example/account_info
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
require 'ib-ruby'

# First, connect to IB TWS.
ib = IB::Connection.new :client_id => 1112, :port => 7496 # TWS
ib = IB::Connection.new :client_id => 1112 #, :port => 7496 # TWS

# Set log level
log.level = Logger::FATAL
Expand Down
3 changes: 2 additions & 1 deletion lib/ib/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ module IB
'OCA' => :one_cancels_all, # One-Cancels-All
'VOL' => :volatility, # Volatility
'SCALE' => :scale, # Scale
'NONE' => :no_order # Used to indicate no hedge in :delta_neutral_order_type
'NONE' => :none, # Used to indicate no hedge in :delta_neutral_order_type
'None' => :none, # Used to indicate no hedge in :delta_neutral_order_type
}.freeze

# Valid security types (sec_type attribute of IB::Contract)
Expand Down
10 changes: 5 additions & 5 deletions lib/ib/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Messages
# This gem supports incoming/outgoing IB messages compatible with the following
# IB client/server versions:
CLIENT_VERSION = 59 # 59? Maximal client version implemented
SERVER_VERSION = 60 # 38? 53? 62? Minimal server version required
SERVER_VERSION = 62 # 38? 53? 62? Minimal server version required
end
end

Expand Down Expand Up @@ -44,10 +44,10 @@ module Messages
// 29 = can receive trail stop limit price in open order and can place them: API 8.91
// 30 = can receive extended bond contract def, new ticks, and trade count in bars
// 31 = can receive EFP extensions to scanner and market data, and combo legs on open orders
// ; can receive RT bars
// ; can receive RT bars
// 32 = can receive TickType.LAST_TIMESTAMP
// ; can receive "whyHeld" in order status messages
// 33 = can receive ScaleNumComponents and ScaleComponentSize is open order messages
// ; can receive "whyHeld" in order status messages
// 33 = can receive ScaleNumComponents and ScaleComponentSize is open order messages
// 34 = can receive whatIf orders / order state
// 35 = can receive contId field for Contract objects
// 36 = can receive outsideRth field for Order objects
Expand Down Expand Up @@ -78,7 +78,7 @@ module Messages
// 51 = can receive smartComboRoutingParams in openOrder
// 52 = can receive deltaNeutralConId, deltaNeutralSettlingFirm, deltaNeutralClearingAccount and deltaNeutralClearingIntent in openOrder
// 53 = can receive orderRef in execution
// 54 = can receive scale order fields (PriceAdjustValue, PriceAdjustInterval, ProfitOffset, AutoReset,
// 54 = can receive scale order fields (PriceAdjustValue, PriceAdjustInterval, ProfitOffset, AutoReset,
// InitPosition, InitFillQty and RandomPercent) in openOrder
// 55 = can receive orderComboLegs (price) in openOrder
// 56 = can receive trailingPercent in openOrder
Expand Down
75 changes: 29 additions & 46 deletions lib/ib/messages/outgoing/place_order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ module Outgoing
# Data format is { :id => int: local_id,
# :contract => Contract,
# :order => Order }
PlaceOrder = def_message [3, 38] # v.38 is NOT properly supported by API yet ?!
PlaceOrder = def_message [3, 38]

class PlaceOrder

def encode server
# Old server version supports no enhancements
#@version = 31 if server[:server_version] <= 60
def encode

order = @data[:order]
contract = @data[:contract]
Expand All @@ -21,14 +19,7 @@ def encode server
contract.serialize_long(:con_id, :sec_id),

# main order fields
case order.side
when :short
'SSHORT'
when :short_exempt
'SSHORTX'
else
order.side.to_sup
end,
(order.side == :short ? 'SSHORT' : order.side == :short_exempt ? 'SSHORTX' : order.side.to_sup),
order.quantity,
order[:order_type], # Internal code, 'LMT' instead of :limit
order.limit_price,
Expand All @@ -49,18 +40,14 @@ def encode server
order.hidden || false,
contract.serialize_legs(:extended),

# This is specific to PlaceOrder v.38, NOT supported by API yet!
## Support for per-leg prices in Order
if server[:server_version] >= 61 && contract.bag?
#order.leg_prices.empty? ? 0 : [order.leg_prices.size] + order.leg_prices
[contract.legs.size] + contract.legs.map { |_| nil }
else
[]
end,

## Support for combo routing params in Order
if server[:server_version] >= 57 && contract.bag?
order.combo_params.empty? ? 0 : [order.combo_params.size] + order.combo_params.to_a
if contract.bag?
[
## Support for per-leg prices in Order
[contract.legs.size] + contract.legs.map { |_| nil },
## Support for combo routing params in Order
order.combo_params.empty? ? 0 : [order.combo_params.size] + order.combo_params.to_a
]
else
[]
end,
Expand All @@ -77,7 +64,7 @@ def encode server
order.designated_location, # only populate when short_sale_slot == 2 (Institutional)
order.exempt_code,
order[:oca_type],
order.rule_80a,
order[:rule_80a], #.to_sup[0..0],
order.settling_firm,
order.all_or_none || false,
order.min_quantity,
Expand All @@ -92,55 +79,51 @@ def encode server
order.stock_range_lower,
order.stock_range_upper,
order.override_percentage_constraints || false,
order.volatility, # Volatility orders
order[:volatility_type], #
order[:delta_neutral_order_type],
order.delta_neutral_aux_price, #
order.volatility, # Volatility orders
order[:volatility_type], # Volatility orders

# Support for delta neutral orders with parameters
if server[:server_version] >= 58 && order.delta_neutral_order_type
[order.delta_neutral_con_id,
if order.delta_neutral_order_type && order.delta_neutral_order_type != :none
[order[:delta_neutral_order_type],
order.delta_neutral_aux_price,
order.delta_neutral_con_id,
order.delta_neutral_settling_firm,
order.delta_neutral_clearing_account,
order[:delta_neutral_clearing_intent]
]
]
else
[]
['', '']
end,

order.continuous_update, # Volatility orders
order[:reference_price_type], # Volatility orders
order.continuous_update, # Volatility orders
order[:reference_price_type], # Volatility orders

order.trail_stop_price, # TRAIL_STOP_LIMIT stop price

# Support for trailing percent
server[:server_version] >= 62 ? order.trailing_percent : [],
order.trailing_percent, # Support for trailing percent

order.scale_init_level_size, # Scale Orders
order.scale_subs_level_size, # Scale Orders
order.scale_price_increment, # Scale Orders

# Support extended scale orders parameters
if server[:server_version] >= 60 && # MIN_SERVER_VER_SCALE_ORDERS3
order.scale_price_increment && order.scale_price_increment > 0
# Support for extended scale orders parameters
if order.scale_price_increment && order.scale_price_increment > 0
[order.scale_price_adjust_value,
order.scale_price_adjust_interval,
order.scale_profit_offset,
order.scale_auto_reset || false,
order.scale_init_position,
order.scale_init_fill_qty,
order.scale_random_percent || false
]
]
else
[]
end,

# TODO: Need to add support for hedgeType, not working ATM - beta only
# MIN_SERVER_VER_HEDGE_ORDERS
server[:server_version] >= 54 ? [order.hedge_type, order.hedge_param || []] : [],
# Support for hedgeType
order.hedge_type, # MIN_SERVER_VER_HEDGE_ORDERS
order.hedge_param || [],

#MIN_SERVER_VER_OPT_OUT_SMART_ROUTING
server[:server_version] >= 56 ? (order.opt_out_smart_routing || false) : [],
order.opt_out_smart_routing || false, # MIN_SERVER_VER_OPT_OUT_SMART_ROUTING

order.clearing_account,
order.clearing_intent,
Expand Down
2 changes: 1 addition & 1 deletion lib/models/ib/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def serialize_algo
# Placement
def place contract, connection
error "Unable to place order, next_local_id not known" unless connection.next_local_id
self.client_id = connection.server[:client_id]
self.client_id = connection.client_id
self.local_id = connection.next_local_id
connection.next_local_id += 1
self.placed_at = Time.now
Expand Down
18 changes: 5 additions & 13 deletions spec/account_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,11 @@ def verify_account

@ib = IB::Connection.new OPTS[:connection].merge(:logger => mock_logger)

received =
if @ib.server[:server_version] <= 60
@ib.send_message :RequestAccountData, :subscribe => true
@ib.wait_for :AccountValue, 5
raise "Unable to verify IB PAPER ACCOUNT" unless @ib.received? :AccountValue
p @ib.received[:AccountValue].first
@ib.received[:AccountValue].first.account_name
else
@ib.wait_for :ManagedAccounts, 5
raise "Unable to verify IB PAPER ACCOUNT" unless @ib.received?(:ManagedAccounts)
@ib.received[:ManagedAccounts].first.accounts_list
end
@ib.wait_for :ManagedAccounts, 5

raise "Unable to verify IB PAPER ACCOUNT" unless @ib.received?(:ManagedAccounts)

received = @ib.received[:ManagedAccounts].first.accounts_list

raise "Connected to wrong account #{received}, expected #{account}" if account != received

Expand Down Expand Up @@ -87,4 +80,3 @@ def verify_account
its(:to_human) { should =~ /AccountDownloadEnd/ }
end
end

0 comments on commit 03c179f

Please sign in to comment.