Module: IB::Butterfly

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

Class Method Summary collapse

Methods included from SpreadPrototype

build, defaults, initialize_spread, optional, parameters, requirements

Class Method Details

.build(from:, front:, back:, **options) ⇒ Object



53
54
55
56
# File 'lib/ib/spread_prototypes/butterfly.rb', line 53

def  build from: , front:, back:,  **options
  underlying_attributes =  { expiry: IB::Symbols::Futures.next_expiry, right: :put }.merge( from.attributes.slice( :symbol, :currency, :exchange, :strike )).merge( options )
  fabricate  IB::Option.new( underlying_attributes), front: front, back: back
end

.defaultsObject



63
64
65
66
# File 'lib/ib/spread_prototypes/butterfly.rb', line 63

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

.fabricate(master, front:, back:) ⇒ Object

where :strike defines the center of the Spread.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ib/spread_prototypes/butterfly.rb', line 23

def  fabricate master, front:, back:

  error "fabrication is based on a master option. Please specify as first argument" unless master.is_a?(IB::Option)
  strike = master.strike
  master.right = :put unless master.right == :call
  l= master.verify
  if l.empty?
    error "Invalid Parameters. No Contract found #{master.to_human}" 
  elsif l.size > 1
    error "ambigous contract-specification: #{l.map(&:to_human).join(';')}"
    available_trading_classes = l.map( &:trading_class ).uniq
    if available_trading_classes.size >1
      error "Refine Specification with trading_class: #{available_trading_classes.join('; ')} "
    else
      error "Respecify expiry, verification reveals #{l.size} contracts  (only 1 is allowed)"
    end
  end

  initialize_spread( master ) do | the_spread |
    strikes = [front, master.strike, back]
    strikes.zip([1, -2, 1]).each do |strike, ratio|
      action = ratio >0 ?  :buy : :sell
          leg =  IB::Option.new( master.attributes.merge( strike: strike )).verify.first.essential
      the_spread.add_leg  leg,  action: action,  ratio: ratio.abs 
    end
    the_spread.description =  the_description( the_spread )
    the_spread.symbol = master.symbol
  end
end

.requirementsObject



69
70
71
72
73
# File 'lib/ib/spread_prototypes/butterfly.rb', line 69

def requirements
  super.merge back: "the strike of the lower bougth option",
              front: "the strike of the upper bougth option"

end

.the_description(spread) ⇒ Object



58
59
60
61
# File 'lib/ib/spread_prototypes/butterfly.rb', line 58

def the_description spread
      x= [ spread.combo_legs.map(&:weight) , spread.legs.map( &:strike )].transpose
"<Butterfly #{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