This repository was archived by the owner on Aug 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathoption_data
More file actions
executable file
·64 lines (57 loc) · 2.78 KB
/
Copy pathoption_data
File metadata and controls
executable file
·64 lines (57 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
#!/usr/bin/env ruby
#
# This script subscribes to market data for a list of Options
require 'bundler/setup'
require 'ib-ruby'
COUNT_OF_EVENTS = 20
# Definition of what we want market data for. We have to keep track of what ticker id
# corresponds to what symbol ourselves, because the ticks don't include any other
# identifying information. The choice of ticker ids is, as far as I can tell, arbitrary.
contracts = [ IB::Symbols::Options.ge20, IB::Symbols::Options.z750, IB::Symbols::Options.spy75,
IB::Symbols::Options.aapl200 ]
list_of_ids =[]
counter = 0
contracts = [ IB::Option.new( symbol: 'ESTX50', exchange:'DTB', multiplier: 10, currency: 'EUR', strike: 3200, expiry: '20180518', right: :put) ,IB::Symbols::Options.ge20 ]
# First, connect to IB TWS. Arbitrary :client_id is used to identify your script
ib = IB::Connection.new( client_id: 1116) do | gw| #, :port => 7496 # TWS
## Subscribe to TWS alerts/errors
gw.subscribe(:Alert) { |msg| puts msg.to_human }
# set the log level
gw.logger.level = Logger::FATAL
# Subscribe to Ticker... events. The code passed in the block will be executed when
# any message of that type is received, with the received message as its argument.
# In this case, we just print out the tick.
#
# (N.B. The description field is not from IB TWS. It is defined
# locally in forex.rb, and is just arbitrary text.)
gw.subscribe(:TickPrice, :TickSize, :TickOption, :TickString, :TickRequestParameters) do |msg|
what = contracts[msg.ticker_id].description || contracts[msg.ticker_id].osi
puts "#{msg.ticker_id}: #{what}: #{msg.to_human}"
counter = counter + 1
if counter == COUNT_OF_EVENTS
puts "Cancelling market data subscription.."
list_of_ids.each { |id| ib.send_message :CancelMarketData, :id => id }
puts "Generally, the TWS sends additional TickEvents after the subscription is cancelled ... \n\n"
Thread.new do
sleep 1
puts "\n******** Press <Enter> to quit *********\n\n"
end
end
end
end
# Now we actually request market data for the symbols we're interested in.
#
# ### ticklist-values : lib/ib/messages/outgoing/request_marketdata.rb
ib.send_message :RequestMarketDataType, :market_data_type => :delayed
list_of_ids = contracts.map do |contract|
ib.send_message :RequestMarketData, :contract => contract , tick_list: [100,101,106]
end
# we modify contracts: { ticker_id => Contract } ( Will be used in the subscrption thread )
contracts = list_of_ids.zip( contracts ).to_h
puts "\nSubscribed to market data"
puts "\n******** Press <Enter> to cancel subscriptions *********\n\n"
STDIN.gets
puts "Cancelling market data subscription.."
contracts.each_with_index { | contract, i| ib.send_message :CancelMarketData, :id => i+1 }
puts "\n******** Press <Enter> to quit.. *********\n\n"
STDIN.gets