Class: IB::Option
- Inherits:
-
Object
- Object
- IB::Option
- Defined in:
- lib/ib/models/option.rb,
lib/ib/option-greeks.rb
Instance Method Summary collapse
-
#request_greeks(delayed: true, what: :model, thread: false) ⇒ Object
Possible values for Parameter :what –> :all :model, :bid, :ask, :bidask, :last.
- #roll(expiry, strike) ⇒ Object
Instance Method Details
#request_greeks(delayed: true, what: :model, thread: false) ⇒ Object
Possible values for Parameter :what –> :all :model, :bid, :ask, :bidask, :last
15 16 17 18 19 20 21 22 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ib/option-greeks.rb', line 15 def request_greeks delayed: true, what: :model, thread: false tws = Connection.current # get the initialized ib-ruby instance # define requested tick-attributes request_data_type = IB::MARKET_DATA_TYPES.rassoc( delayed ? :frozen_delayed : :frozen ).first # possible types = [ [ :delayed_model_option , :model_option ] , [:delayed_last_option , :last_option ], # [ :delayed_bid_option , :bid_option ], [ :delayed_ask_option , :ask_option ] ] tws. :RequestMarketDataType, :market_data_type => request_data_type tickdata = [] self.greek = OptionDetail.new if greek.nil? greek.updated_at = Time.now queue = Queue.new #keep the method-call running until the request finished #and cancel subscriptions to the message handler # method returns the (running) thread th = Thread.new do the_id = nil # subscribe to TickPrices s_id = tws.subscribe(:TickSnapshotEnd) { |msg| queue.push(true) if msg.ticker_id == the_id } e_id = tws.subscribe(:Alert){|x| queue.push(false) if [200,353].include?( x.code) && x.error_id == the_id } t_id = tws.subscribe( :TickSnapshotEnd, :TickPrice, :TickString, :TickSize, :TickGeneric, :MarketDataType ) {|msg| msg } # TWS Error 200: No security definition has been found for the request # TWS Error 354: Requested market data is not subscribed. sub_id = tws.subscribe(:TickOption ) do |msg| #, :TickSize, :TickGeneric do |msg| if msg.ticker_id == the_id # && tickdata.is_a?(Array) # do nothing if tickdata have already gathered case msg.type when /ask/ greek.ask_price = msg.option_price unless msg.option_price.nil? tickdata << msg if [ :all, :ask, :bidask ].include?( what ) when /bid/ greek.bid_price = msg.option_price unless msg.option_price.nil? tickdata << msg if [ :all, :bid, :bidask ].include?( what ) when /last/ tickdata << msg if msg.type =~ /last/ when /model/ # transfer attributs from TickOption to OptionDetail bf =[ :option_price, :implied_volatility, :under_price, :pv_dividend ] (bf + msg.greeks.keys).each{ |a| greek.send( a.to_s+"=", msg.send( a)) } tickdata << msg if [ :all, :model ].include?( what ) end queue.push(true) if tickdata.is_a?(IB::Messages::Incoming::TickOption) || (tickdata.size == 2 && what== :bidask) || (tickdata.size == 4 && what == :all) end end # if sub_id # initialize »the_id« that is used to identify the received tick messages # by firing the market data request the_id = tws. :RequestMarketData, contract: self , snapshot: true result = queue.pop # reduce :close_price delayed_close to close a.s.o if result == false Connection.logger.info{ "#{to_human} --> No Marketdata received " } else self.misc = tickdata if thread # store internally if in thread modus end tws.unsubscribe sub_id, s_id, e_id, t_id end # thread if thread th # return thread else th.join greek end end |
#roll(expiry, strike) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/ib/models/option.rb', line 3 def roll expiry, strike if strike.to_i > 2000 expiry, strike = strike, expiry # exchange values if input occurs in wrong direction end new_option = Option.new( invariant_attributes.merge( con_id: nil, trading_class: '', last_trading_day: nil, local_symbol: "", expiry: expiry, strike: strike )) n_o = new_option.verify.first # get con_id target = IB::Spread.new exchange: exchange, symbol: symbol, currency: currency target.add_leg self, action: :buy target.add_leg n_o, action: :sell rescue NoMethodError Connection.logger.error "Rolling not possible. #{new_option.to_human} could not be verified" nil end |