Module: IB::Vertical

Extended by:
SpreadPrototype
Defined in:
lib/ib/spread_prototypes/vertical.rb

Class Method Summary collapse

Methods included from SpreadPrototype

build, defaults, initialize_spread, optional, parameters, requirements

Class Method Details

.build(from:, **fields) ⇒ Object

Build Vertical out of an Underlying

-----------------------------------------
Needed attributes: :strikes, :expiry, right

Optional: :trading_class, :multiplier

 Call with 
 IB::Straddle.build from: IB::Contract, buy: a_strike,  sell: a_stike, right: {put or call}, expiry: yyyymmm(dd)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ib/spread_prototypes/vertical.rb', line 39

def build from:, **fields
  underlying = if from.is_a?  IB::Option
                 fields[:right] = from.right unless fields.key?(:right) 
                 fields[:sell] = from.strike unless fields.key(:sell)
                 fields[:buy] = from.strike unless fields.key?(:buy)
                 fields[:expiry] = from.expiry unless fields.key?(:expiry)
                 fields[:trading_class] = from.trading_class unless fields.key?(:trading_class) || from.trading_class.empty?
                 fields[:multiplier] = from.multiplier unless fields.key?(:multiplier) || from.multiplier.to_i.zero?
                 details =  from.verify.first.contract_detail 
                 IB::Contract.new( con_id: details.under_con_id, 
                                  currency: from.currency, 
                                  exchange: from.exchange)
                                 .verify.first
                                 .essential
               else
                 from
               end
  kind = { :buy => fields.delete(:buy), :sell => fields.delete(:sell) }
  error "Specifiaction of :buy and :sell nessesary, got: #{kind.inspect}" if kind.values.any?(nil)
  initialize_spread( underlying ) do | the_spread |
    leg_prototype  = IB::Option.new underlying.attributes
                        .slice( :currency, :symbol, :exchange)
                        .merge(defaults)
                        .merge( fields )
                        .merge( local_symbol: "" )
    leg_prototype.sec_type = 'FOP' if underlying.is_a?(IB::Future)
    the_spread.add_leg  leg_prototype.merge(strike: kind[:sell]), action: :sell
    the_spread.add_leg  leg_prototype.merge(strike: kind[:buy] ), action: :buy
    error "Initialisation of Legs failed" if the_spread.legs.size != 2
    the_spread.description =  the_description( the_spread )
  end
end

.defaultsObject



72
73
74
75
# File 'lib/ib/spread_prototypes/vertical.rb', line 72

def defaults
super.merge expiry: IB::Symbols::Futures.next_expiry, 
      right: :put
end

.fabricate(master, buy: 0, sell: 0) ⇒ Object

Fabricate a Vertical from a Master-Option

-----------------------------------------
If one Leg is known, the other is build by flipping the right and adjusting the strike by distance

 Call with 
 IB::Vertical.fabricate  an_option, buy: {another_strike},  (or) , :sell{another_strike}


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ib/spread_prototypes/vertical.rb', line 15

def fabricate master, buy: 0, sell: 0

  error "Argument must be an option" unless [:option, :futures_option].include? master.sec_type
  error "Unable to fabricate Vertical. Either :buy or :sell must be specified " if buy.zero? && sell.zero?

  buy =  master.strike if buy.zero? 
  sell =  master.strike if sell.zero? 
  initialize_spread( master ) do | the_spread |
    the_spread.add_leg master.essential.merge(strike: sell, local_symbol: "", con_id: 0), action: :sell
    the_spread.add_leg master.essential.merge(strike: buy, local_symbol: "", con_id: 0), action: :buy
    error "Initialisation of Legs failed" if the_spread.legs.size != 2
    the_spread.description =  the_description( the_spread )
  end
end

.the_description(spread) ⇒ Object



78
79
80
81
# File 'lib/ib/spread_prototypes/vertical.rb', line 78

def the_description spread
  x= [ spread.combo_legs.map(&:weight) , spread.legs.map( &:strike )].transpose
  "<Vertical #{spread.symbol} #{spread.legs.first.right}(#{x.map{|w,strike| "#{w} :#{strike} "}.join( '|+|' )} )[#{Date.parse(spread.legs.first.last_trading_day).strftime("%b %Y")}]>"
end