Class: Options::Strategies::Strategy

Inherits:
Object
  • Object
show all
Defined in:
lib/options/strategies.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Strategy

Returns a new instance of Strategy.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/options/strategies.rb', line 14

def initialize(options={})
  @options = options
  @index_name = @options[:index_name]
  @increment = eval("Multiplier::#{@index_name}")
  @spot_price = @options[:spot_price]
  @strike_price = @options.key?(:strike_price) ? @options[:strike_price] : @increment * ((@spot_price + @increment/2.0).to_i / @increment)
  @options[:strike_price] = @strike_price
  @ce_increment = 0
  @pe_increment = 0
  @default_lot_size = 1
  @input_lot_size = 1
  @options_data = []
  @legs = {OrderType::LONG => [], OrderType::SHORT => [], "index_name" => @index_name, "spot_price" => @spot_price, "strike_price" => @strike_price, "klass_name" => self.class.name.split('::').last}
end

Instance Attribute Details

#ce_incrementObject

Returns the value of attribute ce_increment.



12
13
14
# File 'lib/options/strategies.rb', line 12

def ce_increment
  @ce_increment
end

#default_lot_sizeObject

Returns the value of attribute default_lot_size.



12
13
14
# File 'lib/options/strategies.rb', line 12

def default_lot_size
  @default_lot_size
end

#incrementObject

Returns the value of attribute increment.



12
13
14
# File 'lib/options/strategies.rb', line 12

def increment
  @increment
end

#index_nameObject

Returns the value of attribute index_name.



12
13
14
# File 'lib/options/strategies.rb', line 12

def index_name
  @index_name
end

#input_lot_sizeObject

Returns the value of attribute input_lot_size.



12
13
14
# File 'lib/options/strategies.rb', line 12

def input_lot_size
  @input_lot_size
end

#legsObject

Returns the value of attribute legs.



12
13
14
# File 'lib/options/strategies.rb', line 12

def legs
  @legs
end

#option_type_klassesObject

Returns the value of attribute option_type_klasses.



12
13
14
# File 'lib/options/strategies.rb', line 12

def option_type_klasses
  @option_type_klasses
end

#options_dataObject

Returns the value of attribute options_data.



12
13
14
# File 'lib/options/strategies.rb', line 12

def options_data
  @options_data
end

#pe_incrementObject

Returns the value of attribute pe_increment.



12
13
14
# File 'lib/options/strategies.rb', line 12

def pe_increment
  @pe_increment
end

#strike_priceObject

Returns the value of attribute strike_price.



12
13
14
# File 'lib/options/strategies.rb', line 12

def strike_price
  @strike_price
end

Instance Method Details

#constructObject



61
62
63
64
65
66
67
68
69
# File 'lib/options/strategies.rb', line 61

def construct
  parse_option_type_klasses
  @default_lot_size = @input_lot_size * @default_lot_size
  @options_data.each do |option_data|
    option_data.size = @default_lot_size
    yield option_data
  end
  self
end

#long=(option_type) ⇒ Object



29
30
31
# File 'lib/options/strategies.rb', line 29

def long=(option_type)
  @legs[OrderType::LONG] << option_type.to_hash
end

#lot_size=(val) ⇒ Object



37
38
39
# File 'lib/options/strategies.rb', line 37

def lot_size=(val)
  @input_lot_size = val
end

#merge_legs(input_legs) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/options/strategies.rb', line 52

def merge_legs(input_legs)
  @legs.each do |k,v|
    if @legs[k].kind_of? Array
      @legs[k] << input_legs[k] unless input_legs[k].empty?
      @legs[k].flatten!
    end
  end
end

#parse_option_type_klassesObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/options/strategies.rb', line 41

def parse_option_type_klasses
  @option_type_klasses.each do |klass, option_strike_price|
    @options[:strike_price] = option_strike_price
    # puts "#{klass} --> #{option_strike_price} --> #{@options}"
    option = klass.new(@options)
    # puts "#{option}"
    @options_data << option
    @options[:strike_price] = @strike_price
  end
end

#short=(option_type) ⇒ Object



33
34
35
# File 'lib/options/strategies.rb', line 33

def short=(option_type)
  @legs[OrderType::SHORT] << option_type.to_hash
end

#to_hashObject



71
72
73
# File 'lib/options/strategies.rb', line 71

def to_hash
  self.legs
end

#to_jsonObject



75
76
77
# File 'lib/options/strategies.rb', line 75

def to_json
  self.legs.to_json
end