To use the features described, include the ib-extensions Gem in the Gemfile and use
 
require 'ib-api' 
require 'ib/order-prototypes' 
in your scripts.

Interactive Brokers offers more than 60 ordertypes to its clients.

There might be a reason for each and everyone. But it’s virtually impossible to know all of them, and even for less common, but very useful ordertypes, one must think carefully which attributes apply and/or are required.

IB-Ruby implements Order-Prototypes to simplify the ordering process.

Order-Prototypes define the order. For Placement look here.

List of Prototypes

  • Market, MarketIfTouched, MarketOnClose, MarketOnOpen, AtAuction
  • Limit, Discretionary, Sweep2Fill, LimitIfTouched, LimitOnClose, LimitOnOpen
  • SimpleStop, StopLimit, StopProtected, TrailingStop, TrailingStopLimit
  • ForexLimit
  • Volatility
  • Combo
  • Pegged2Primary, Pegged2Market, Pegged2Stock, Pegged2Benchmark

Commandline autocomplete

A handy command line autocomplete mechanism is provided.

> o = Limit.order
		(...) 
IB::ArgumentError (IB::Limit.order -> A necessary field is missing: action: 
--> {"B"=>:buy, "S"=>:sell, "T"=>:short, "X"=>:short_exempt})

> o = Limit.order action: :buy
		(...)
IB::ArgumentError (IB::Limit.order -> A necessary field is missing: total_quantity: 
--> also aliased as :size)

> o = Limit.order action: :buy, size: 100
		(...)
 IB::ArgumentError (IB::Limit.order -> A necessary field is missing: limit_price: 
 --> also aliased as :price)

> o = Limit.order action: :buy, size: 100, price: 15
  => #<IB::Order:0x0000000002fa5fd0 @attributes={:tif=>"GTC", :order_type=>"LMT", :side=>"B", :total_quantity=>100, :limit_price=>15, :created_at=>2018-03-22 08:50:57 +0000, :active_start_time=>"", :active_stop_time=>"", :algo_strategy=>"", :algo_id=>"", :auction_strategy=>0, :conditions=>[], :continuous_update=>0, :designated_location=>"", :display_size=>0, :discretionary_amount=>0, :etrade_only=>true, :exempt_code=>-1, :ext_operator=>"", :firm_quote_only=>true, :not_held=>false, :oca_type=>0, :open_close=>1, :opt_out_smart_routing=>false, :origin=>0, :outside_rth=>false, :parent_id=>0, :random_size=>false, :random_price=>false, :scale_auto_reset=>false, :scale_random_percent=>false, :scale_table=>"", :short_sale_slot=>0, :solicided=>false, :transmit=>true, :trigger_method=>0, :what_if=>false, :leg_prices=>[], :algo_params=>{}, :combo_params=>[], :soft_dollar_tier_params=>{"name"=>"", "val"=>"", "display_name"=>""}}, @order_states=[#<IB::OrderState:0x0000000002f9b670 @attributes={:status=>"New", :filled=>0, :remaining=>0, :price=>0, :average_price=>0, :created_at=>2018-03-22 08:50:57 +0000}>]> 

Summary Method

Order-Prototypes provide a summary class-method

> print Limit.summary
  A Limit order is an order to buy or sell at a specified price or better. 
  The Limit order ensures that if the order fills, it will not fill at a 
  price less favorable than your limit price, but it does not guarantee a fill. 
  It appears in the orderbook.

Special treatment of :size

The :size attribute is primarily an alias for :total_quantity.

However, as in real live, size can be a negative number. If the size-attribute is specified without noticing any :action, the sign is interpreted as intention for the :action, ie.

> Limit.order( price: 34, size:  5 ) ==>  IB:.Order attributes: action: :buy, total_quantity: 5, limit_price: 34
> Limit.order( price: 24, size: -5 ) ==> IB:.Order attributes: action: :sell, total_quantity: 5, limit_price: 34

Additional Arguments

Order-Prototypes are just a wrapper for IB::Order.new. Other arguments(properties) are specified as usual

>  order = Limit.order price: 15, size:  100  
>  C.place_order order, IB::Symbols::Stocks.wfc 
I, [2018-03-22T09:00:50.168223 #1501]  INFO -- : TWS Error 436: You must specify an allocation (either a single account, group, or profile).

> order = Limit.order  size: 100, price: 15, account: 'DU167349', transmit: true
> C.place_order order, IB::Symbols::Stocks.wfc 
I, [2018-03-22T09:03:23.601694 #1501]  INFO -- : <OrderStatus: <OrderState: Submitted #13/1114784592 from 2000 filled 0.0/100.0 at 0.0/0.0 why_held >>

Examples

In the example-section Order-Prototypes are used to demonstrate the placement-process

Tags: