Module: IB::Straddle

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

Class Method Summary collapse

Methods included from SpreadPrototype

build, defaults, initialize_spread, optional, parameters, requirements

Class Method Details

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

Build Straddle out of an Underlying

-----------------------------------------
Needed attributes: :strike, :expiry 

Optional: :trading_class, :multiplier

 Call with 
 IB::Spread::Straddle.build from: IB::Contract, strike: a_value, expiry: yyyymmm(dd)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ib/spread_prototypes/straddle.rb', line 38

def build from:, ** fields
	if  from.is_a?  IB::Option
		fabricate from.merge(fields)
	else
		initialize_spread( from ) do | the_spread |
			leg_prototype  = IB::Option.new from.attributes
			.slice( :currency, :symbol, :exchange)
			.merge(defaults)
           .merge( fields )

			leg_prototype.sec_type = 'FOP' if from.is_a?(IB::Future)
         the_spread.add_leg leg_prototype.merge( right: :put ).verify.first
         the_spread.add_leg leg_prototype.merge( right: :call ).verify.first
			error "Initialisation of Legs failed" if the_spread.legs.size != 2
			the_spread.description =  the_description( the_spread )
		end
	end
end

.defaultsObject



57
58
59
# File 'lib/ib/spread_prototypes/straddle.rb', line 57

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

.fabricate(master) ⇒ Object

Fabricate a Straddle from a Master-Option

-----------------------------------------
If one Leg is known, the other is simply build by flipping the right

 Call with 
 IB::Spread::Straddle.fabricate an_option


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

def fabricate master

	flip_right = ->(the_right){  the_right == :put ? :call : :put   }
	error "Argument must be a IB::Option" unless [ :option, :futures_option ].include?( master.sec_type )


	initialize_spread( master ) do | the_spread |
		the_spread.add_leg master.essential
		the_spread.add_leg( master.essential.merge( right: flip_right[master.right], local_symbol: "") )
		error "Initialisation of Legs failed" if the_spread.legs.size != 2
		the_spread.description =  the_description( the_spread )
	end
end

.requirementsObject



62
63
64
65
# File 'lib/ib/spread_prototypes/straddle.rb', line 62

def requirements
				super.merge strike: "the strike of both options",
			  expiry: "Expiry expressed as »yyyymm(dd)« (String or Integer) )"
end

.the_description(spread) ⇒ Object



68
69
70
# File 'lib/ib/spread_prototypes/straddle.rb', line 68

def the_description spread
 "<Straddle #{spread.symbol}(#{spread.legs.first.strike})[#{Date.parse(spread.legs.first.last_trading_day).strftime("%b %Y")}]>"
end