require 'ib-api' ib = IB::Connection.new ib.activate_plugin 'spread-prototypes'
As for Order-Prototypes Spread-Prototypes
are provided to ease the definition of complex-positions. Spread-Prototypes
are designed to automate the creation of IB::Spread
-Contracts.
Spreads
IB-Spread
provides :add_leg
and :remove_leg
methods, to be used, for example, to roll an option:
ib = IB::Connection.new
ib.activate_plugin :spread_protoypes, :advanced_account, :process_orders, :order_prototypes
account = ib.clients.first # use the first account
o1 = Option.new( symbol: 'ENR', exchange: 'EUREX',
strike: 27, expiry: IB::Option.next_expiry, right: :put )
o2 = o1.merge expiry: 202409 ## use o1 as template
s = IB::Spread.new
s.add_leg o1.verify.first, action: :buy # orderlegs must include a valid con_id
s.add_leg o2.verify.first, action: :sell
s.legs.to_human
=> ["<Option: ENR 20240719 put 27.0 EUREX >", "<Option: ENR 202409 put 27.0 EUREX >"]
s.combo_legs.to_human
==> ["<ComboLeg: buy 1 con_id 700720068 at EUREX>",
"<ComboLeg: sell 1 con_id 702590621 at EUREX>"]
s.currency = 'EUR'; s.symbol = 'ENR'; s.exchange = 'EUREX'
account.preview order: Limit.order( action: :buy, size: 10, price: 3.5 ), contract: s
=> { :init_margin => 0.1807873e5,
:maint_margin => 0.1511598e5,
:equity_with_loan => 0.102879439e7,
:commission => 0.22e2,
:commission_currency => "EUR",
:warning => "" }
Straddle
IB::Straddle
simplifies the construction of this Combo
ib = IB::Connection.new
ib.activate_plugin :spread_protoypes, :symbols
spxw = IB::Straddle.fabricate IB::Symbols::Options.spxw.merge( right: :put, strike: 5500 )
spxw.to_human
=> "<Straddle SPX(5500.0)[Jul 2024]>"
spxw.legs.to_human
=> ["<Option: SPX 20240719 put 5500.0 SMART USD>",
"<Option: SPX 20240719 call 5500.0 SMART USD>"]
# or
> spy = IB::Straddle.build from: IB::Symbols::Index.spx,
strike: 5500,
expiry: spxw.legs.first.expiry,
trading_class: 'SPXW'
> spxw == 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
> ib.clients.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: 5000, p: 4800,
expiry: IB::Options.next_expiry
the_strangle.to_human
=> <Strangle Estx50(4800.0,5000.0)[Jul 2024]>
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.