How to get Historical Data and Streaming? #22
-
I see from the examples how to get EOD data. How to get historical data based on other timeframes, such as 1 sec, 5 sec, or 1 minute? Also, does ib-api support getting streaming data? Is there any documentation or examples? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi, Other timeframes are easily fetched by The trading-system -gem extents the IB::Contract-class to fetch the data: module IB
class Contract
def request_historic_data back: "10 D", interval: :hour1
ib = Connection.current
ib.send_message IB::Messages::Outgoing::RequestHistoricalData.new(
contract: self,
# end_date_time: Time.now.to_ib, # set only if keep_up_todate is 0
duration: back, # ?
bar_size: interval, # IB::BAR_SIZES.key(:hour)?
what_to_show: :trades,
use_rth: 1,
keep_up_todate: 1) # if set to `1` here, comment end_date_time
end
end
end Valid timeframes are defined in lib/ib/constants.rb If you call it with a contract, it returns the request_id. ib = IB::Connection.current
ge = IB::Stock.new symbol: 'GE'
ge_id = nil
q = Queue.new
ib.subscribe(IB::Messages::Incoming::HistoricalData) do |msg|
if msg.request_id == ge_id
q.push msg.results
end
end
ge_id = ge.request_historic_data
# serialize historic data
ge_results = q.pop # anthing is transmitted once Streaming data are received through subcribing to IB::Messages::Incoming::HistoricalDataUpdate events. For a complete example, look into https://github.com/ib-ruby/ib-trading-system/blob/main/lib/trading-system/base.rb hope it helps. |
Beta Was this translation helpful? Give feedback.
Hi,
you are right, documentation is mostly about EOD-Data.
Other timeframes are easily fetched by
IB:Messages::Outgoing:RequestHistoricalData
The trading-system -gem extents the IB::Contract-class to fetch the data: