require 'ib-api' ib = IB::Connection.new ib.activate_plugin 'order-prototypes'
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.
Common Use
ib = IB::Connection.new
ib.activate_plugin :order_prototypes, :symbols, :process_orders
u = ib.clients.first
u.place contract: Symbols::Stocks.msft,
order: Limit.order( size: 100, price: 450 )
u.place contract: Symbols::Stocks.sie,
order: Adaptive.order( size: -100, price: 180 )
List of Prototypes
Market
,MarketIfTouched
,MarketOnClose
,MarketOnOpen
,AtAuction
Limit
,Discretionary
,Sweep2Fill
,LimitIfTouched
,LimitOnClose
,LimitOnOpen
Adaptive
,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, (...) >
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