Class: IB::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/ib/models/option.rb,
lib/ib/option-greeks.rb

Instance Method Summary collapse

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
84
85
# 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.send_message :RequestMarketDataType, :market_data_type =>  request_data_type
 tickdata = []

 self.greek = OptionDetail.new if greek.nil?
 greek.updated_at = Time.now

 #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
	 finalize= false
	 # subscribe to TickPrices
	 s_id = tws.subscribe(:TickSnapshotEnd) { |msg|	finalize = true	if msg.ticker_id == the_id }
	 e_id = tws.subscribe(:Alert){|x|  finalize = true if [200,353].include?( x.code) && x.error_id == the_id } 
	 # 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
			 tickdata =  tickdata &.first unless [:bidask, :all].include? what
			 finalize = 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.send_message :RequestMarketData,  contract: self , snapshot: true 

	 begin
		 # todo implement config-feature to set timeout in configuration   (DRY-Feature)
		 Timeout::timeout(5) do   # max 5 sec.
			 loop{ break if finalize ; sleep 0.05 } 
			 # reduce :close_price delayed_close  to close a.s.o 
			 self.misc =  tickdata if thread  # store internally if in thread modus
		 end
	 rescue Timeout::Error
		 Connection.logger.info{ "#{to_human} --> No Marketdata received " }
	 end
	 tws.unsubscribe sub_id, s_id, e_id
 end  # thread
 if thread
	 th		# return thread
 else
	 th.join
	 tickdata	# return 
 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