To use Spread Prototypes, include the ib-extensions Gem in the Gemfile and note
 
require 'ib-api' 
require 'ib/spread-prototypes' 
in your scripts.

As for Order-Prototypes Spread-Prototypes are provided to ease the definition of complex-positions. Spread-Prototypesautomate the creation of IB::Spread-Contracts.

Spreads

IB-Spread provides :add_legand :remove_leg methods, to be used, for example, to roll an option:

#( in perfom in the gateway-console )
> account =  G.active_accounts.first
> o1 = Option.new( symbol: 'DAI', exchange: 'DTB',
                   strike: 50, expiry: 20181221, right: :put )
> o2 = o1.merge expiry: 201902   ## use o1 as template 
> s =  IB::Spread.new
> s.add_leg o1, action: :buy
> s.add_leg o2, action: :sell
> s.legs.to_human
 => ["<Option: DAI 20181221 put 50.0 DTB EUR>", "<Option: DAI 20190215 put 50.0 DTB EUR>"] 
 > s.combo_legs.to_human
  => ["<ComboLeg: buy 1 con_id 299669864 at DTB>", "<ComboLeg: sell 1 con_id 342146619 at DTB>"] 
> s.currency = 'EUR'; s.symbol = 'DAI'; s.exchange = 'DTB' 
> account.preview order: Limit.order( action: :buy, size: 10, price: 3.5  ), contract: s
  => {:init_margin=>0.993409e5, :maint_margin=>0.9620193e5, :equity_with_loan=>0.95495394e6, :commission=>0.22e2, :commission_currency=>"EUR", :warning=>""} 

Straddle

IB::Straddle simplifies the construction of this Combo

> spx= IB::Straddle.fabricate IB::Option.new( symbol: 'SPX', exchange: 'CBOE', strike: 2400, expiry: 201903, trading_class: 'SPX', right: :put )
> spx.to_human
  => "<Straddle SPX(2400.0)[Mar 2019]>"
> spx.legs.to_human
  => ["<Option: SPX 20190314 put 2400.0 CBOE USD>", "<Option: SPX 20190314 call 2400.0 CBOE USD>"] 
					
# or
> spy = IB::Straddle.build from: IB::Symbols::Index.spx, 
                           strike: 2400, 
                           expiry: 201903, 
                           trading_class: 'SPX'
> spx == spy			 => true 

IB::Straddle.fabricate and IB::Straddle.build create IB::Spread-Contract Objects which can be used in Watchlists orplace_order

> the_order =  Limit.order size: 3, action: :sell, price: 245
> G.active_accounts.first.place order: the_order, contract: spy

Strangle

IB::Strangle simplifies the definition of Strangles. An Underlying is required (Index, Future, Stock). Compared to IB::Straddles the strike is split to :p and :c

the_strangle = IB::Strangle.build from: IB::Symbols::Index.stoxx, 
                                  c: 3000, p: 3200, 
                                  expiry: 201903
the_strangle.to_human
 => "<Strangle Estx50(3200.0,3000.0)[Mar 2019]>

Calendar

As for straddles, Calendar-Spreads are build either form a sample or »in situ« using underlying, strike, front- + back-month. To buy a Calendar-Spread means: The front month is sold!

> calendar = IB::Calendar.build from: IB::Symbols::Index.stoxx, strike: 3000, 
                                front: 201812, back: 201903,
                                trading_class: 'OESX'
> calendar.to_human           
 => "<Calendar Estx50 put(3000.0)[-1 :Dec 2018 |+|1 :Mar 2019  >" 
 

builds a put Calendar-Spread, selling the front-month and buying the back-month.

Alternatively the Spread may be defined relative to a master-option:

> master = calendar.legs.first   # front-option
> m_calendar = IB::Calendar.fabricate master, back: 201903
> m_calendar.to_human
  => "<Calendar ESTX50 put(3000.0)[-1 :Dec 2018 |+|1 :Mar 2019  >" 
> m_calendar.combo_legs == calendar.combo_legs        => true

Rolling of Futures

In Fabricate-Mode the Calendar-Spread can be used to roll Futures:

> calendar =  IB::Calendar.fabricate IB::Symbols::Futures.es, '3m'
> calendar.to_human 
 => "<Calendar ES none(0.0)[1 :Dec 2020 |+|-1 :Mar 2021  >" 
> calendar.market_price                    ==>  9.75

# or to roll a short future
> es =  IB::Calendar.fabricate IB::Symbols::Futures.es.merge(expiry: 202106), '-3m'
> es.to_human
 => "<Calendar ES none(0.0)[1 :Jun 2021 |+|-1 :Mar 2021  >" 
> es.market_price                         ==> -10.0
			 

Vertical

Verticals are defined straightforward

# using build
> the_v = IB::Vertical.build from: IB::Symbols::Index.stoxx, buy: 3200, sell: 3000 
> the_v.to_human
  => "<Vertical Estx50 put(1 :3200.0 |+|-1 :3000.0  )[Dec 2018]>" 
# or in fabricate-mode
> new_v = IB::Vertical.fabricate sell: the_v.legs.first, buy: 2400 
> new_v.to_human
  => "<Vertical ESTX50 put(-1 :3200.0 |+|1 :2400.0  )[Dec 2018]>"

Stock Spreads

Stock Spreads are combined buy and sell orders with us-stocks as unterlyings.

They can only be fabricated:

> the_bag = IB::StockSpread.fabricate 'MSFT', 'WFC', ratio: [1, -8]  
#or
> msft =  IB::Stock.new( symbol: :msft )
> wfc =  IB::Stock.new( symbol: :wfc )
> the_bag = IB::StockSpread.fabricate ', msft, wfc ratio: [1, -8]  


> the_bag.to_human
 "<StockSpread MSFT:1 , WFC:-8 (USD )>" 

> the_bag.market_price    => 21.56
> the_bag.symbol          => "MSFT,WFC" 

Additional attributes can be added through the fabrication process. Thus, IB::StockSpread.fabricate 'MSFT', 'WFC', ratio: [1, -8], exchange: 'NYSE' sets the exchange for the combo.

Stock-Spreads must be ordered with the NonGuaranteed flag set. Use the IB::Combo prototype for this purpose.

Tags: