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



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

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 )
	puts underlying_attributes.inspect
	fabricate  IB::Option.new( underlying_attributes), front: front, back: back
end

.defaultsObject



65
66
67
68
# File 'lib/ib/spread_prototypes/butterfly.rb', line 65

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
52
# 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{|x| x.contract_detail= nil; l << x }
#					puts "master:  #{master.attributes.inspect}"
	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!.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



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

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



60
61
62
63
# File 'lib/ib/spread_prototypes/butterfly.rb', line 60

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