wboayue / rust-ibapi

An implementation of the Interactive Brokers API for Rust
MIT License
45 stars 21 forks source link

Wrong start date in HistoricalData struct #93

Closed maystorm closed 1 year ago

maystorm commented 1 year ago

There may be a bug in the ibapi::client::Client::historical_data_ending_now() method regarding the start date in the returned ibapi::market_data::historical::HistoricalData struct. E.g., when calling the method as follows (asking for the last 7 days):

historical_data_ending_now(&contract, 7.days(), BarSize::Day, WhatToShow::MidPoint, true)

the following data is returned:

start_date: 2023-06-20 20:26:08.0 +02:00:00, end_date: 2023-06-27 20:26:08.0 +02:00:00
0: Bar { date: 2023-06-16 0:00:00.0 +00:00:00, open: 434.655, high: 437.18, low: 426.605, close: 427.05, volume: -1.0, wap: -1.0, count: -1 }
1: Bar { date: 2023-06-20 0:00:00.0 +00:00:00, open: 429.79, high: 439.895, low: 426.75, close: 438.035, volume: -1.0, wap: -1.0, count: -1 }
2: Bar { date: 2023-06-21 0:00:00.0 +00:00:00, open: 435.255, high: 436.13, low: 420.845, close: 430.44, volume: -1.0, wap: -1.0, count: -1 }
3: Bar { date: 2023-06-22 0:00:00.0 +00:00:00, open: 422.53, high: 434.255, low: 422.4, close: 430.16, volume: -1.0, wap: -1.0, count: -1 }
4: Bar { date: 2023-06-23 0:00:00.0 +00:00:00, open: 424.62, high: 428.075, low: 420.175, close: 422.09, volume: -1.0, wap: -1.0, count: -1 }
5: Bar { date: 2023-06-26 0:00:00.0 +00:00:00, open: 424.61, high: 427.635, low: 401.02, close: 406.38, volume: -1.0, wap: -1.0, count: -1 }
6: Bar { date: 2023-06-27 0:00:00.0 +00:00:00, open: 407.95, high: 417.05, low: 404.505, close: 416.495, volume: -1.0, wap: -1.0, count: -1 }

Seven TRADING days are correctly returned with "2023-06-16" as the first day, however, the start day says "2023-06-20".

It seems that HistoricalData::start is simply filled by subtracting 7 days from the current date and not looking for the first day returned by TWS which would be the correct behaviour.

wboayue commented 1 year ago

I noticed this during development and it is a little strange. But that is the result from the API. With the current implementation if the message version is less than 2, both the start and end date default to the current date. Otherwise, the start and end date are read from the message.