This repository has been archived by the owner on Aug 10, 2021. It is now read-only.
/
HistoricToCSV
executable file
·111 lines (89 loc) · 3.94 KB
/
HistoricToCSV
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env ruby -w
#
# Copyright (C) 2009 Wes Devauld
#
# This library is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2.1 of the
# License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA
#
# TODO Fix the Historical command line client
#
require File.expand_path(
File.join(File.dirname(__FILE__), '..', 'lib', 'ib-ruby'))
### Configurable Options
# if Quiet == false, status data will be printed to STDERR
Quiet = false
# How long to wait when no messages are received from TWS before
# exiting, in seconds
Timeout = 10
SymbolToRequest = IB::Symbols::Forex[:gbpusd]
### end options
#
# 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.
#
# Note that as of 4/07 there is no historical data available for forex spot.
#
@market =
{
123 => SymbolToRequest
}
# To determine when the timeout has passed.
@last_msg_time = Time.now.to_i + 2
# Connect to IB TWS.
ib = IB::IB.new
# Uncomment this for verbose debug messages:
# IB::IBLogger.level = Logger::Severity::DEBUG
#
# Now, subscribe to HistoricalData incoming events. The code
# passed in the block will be executed when a message of that type is
# received, with the received message as its argument. In this case,
# we just print out the data.
#
# Note that we have to look the ticker id of each incoming message
# up in local memory to figure out what it's for.
#
# (N.B. The description field is not from IB TWS. It is defined
# locally in forex.rb, and is just arbitrary text.)
ib.subscribe(IB::IncomingMessages::HistoricalData, lambda {|msg|
STDERR.puts @market[msg.data[:req_id]].description + ": " + msg.data[:item_count].to_s("F") + " items:" unless Quiet
msg.data[:history].each { |datum|
@last_msg_time = Time.now.to_i
STDERR.puts " " + datum.to_s("F") unless Quiet
STDOUT.puts "#{datum.date},#{datum.open.to_s("F")},#{datum.high.to_s("F")},#{datum.low.to_s("F")},#{datum.close.to_s("F")},#{datum.volume}"
}
})
# Now we actually request historical data for the symbols we're
# interested in. TWS will respond with a HistoricalData message,
# which will be received by the code above.
@market.each_pair {|id, contract|
msg = IB::OutgoingMessages::RequestHistoricalData.new({
:ticker_id => id,
:contract => contract,
:end_date_time => Time.now.to_ib,
:duration => (360).to_s, # how long before end_date_time to request in seconds - this means 1 day
:bar_size => IB::OutgoingMessages::RequestHistoricalData::BarSizes.index(:hour),
:what_to_show => :trades,
:use_RTH => 0,
:format_date => 2
})
ib.dispatch(msg)
}
while true
exit(0) if Time.now.to_i > @last_msg_time + Timeout
sleep 1
end