Class: IB::Order

Inherits:
Object
  • Object
show all
Defined in:
lib/ib/gateway/order-handling.rb

Instance Method Summary collapse

Instance Method Details

#auto_adjustObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ib/gateway/order-handling.rb', line 119

def auto_adjust
  # lambda to perform the calculation
  adjust_price = ->(a,b) do
    a = BigDecimal( a, 5 ) 
    b = BigDecimal( b, 5 ) 
    _,o = a.divmod(b)
    a - o
  end
  # adjust_price[2.6896, 0.1].to_f     => 2.6
  # adjust_price[2.0896, 0.05].to_f    => 2.05
  # adjust_price[2.0896, 0.002].to_f   => 2.088


  error "No Contract provided to Auto adjust" unless contract.is_a? IB::Contract

  unless contract.is_a? IB::Bag
    # ensure that contract_details are present

    the_details = contract.contract_detail.present? ? contract.contract_detail : contract.verify.first.contract_detail
      # there are two attributes to consider: limit_price and aux_price
      # limit_price +  aux_price may be nil or an empty string. Then ".to_f.zero?" becomes true 
      self.limit_price= adjust_price.call(limit_price.to_f, the_details.min_tick) unless limit_price.to_f.zero?
      self.aux_price= adjust_price.call(aux_price.to_f, the_details.min_tick) unless aux_price.to_f.zero?
  end
end