erdewit / ib_insync

Python sync/async framework for Interactive Brokers API
BSD 2-Clause "Simplified" License
2.81k stars 753 forks source link

reqHistoricalData returns -1 #651

Closed Roh-codeur closed 11 months ago

Roh-codeur commented 11 months ago

hi

I am running the below piece of code. Not sure whether the issue is with IBKR or the lib. it seems to return valid values in barUpdate callback, however, the historical seem to have Volume as -1. is this something you can help with pls?

    contract = Stock("BP.", exchange="SMART", currency="GBP")
    print(ib.qualifyContracts(contract))

    def onBarUpdate(bars, hasNewBar):
        print(util.df(bars))
        print("")

    bars = ib.reqHistoricalData(
        contract,
        endDateTime='',
        durationStr='1 D',
        barSizeSetting='1 hour',
        whatToShow='TRADES',
        useRTH=True,
        keepUpToDate=True,
        formatDate=1)
    ib.sleep(5)
    print(util.df(bars))
    bars.updateEvent += onBarUpdate

Output:


                       date   open   high  ...     volume  average  barCount
0 2023-10-18 09:00:00+02:00  558.9  559.7  ...  2471785.0   5.5816      1518
[1 rows x 8 columns]
                       date   open   high  ...     volume   average  barCount
0 2023-10-18 09:00:00+02:00  558.9  559.7  ...  2475416.0  5.581588      1525
[1 rows x 8 columns]
                       date   open   high  ...     volume   average  barCount
0 2023-10-18 09:00:00+02:00  558.9  559.7  ...  2477353.0  5.581583      1527
[1 rows x 8 columns]
                       date    open    high  ...  volume  average  barCount
0 2023-10-17 09:30:00-04:00  175.69  176.99  ...    -1.0     -1.0        -1
1 2023-10-17 10:00:00-04:00  176.37  177.23  ...    -1.0     -1.0        -1
2 2023-10-17 11:00:00-04:00  177.20  177.85  ...    -1.0     -1.0        -1
3 2023-10-17 12:00:00-04:00  178.08  178.43  ...    -1.0     -1.0        -1
4 2023-10-17 13:00:00-04:00  177.78  178.36  ...    -1.0     -1.0        -1
5 2023-10-17 14:00:00-04:00  177.12  177.54  ...    -1.0     -1.0        -1
6 2023-10-17 15:00:00-04:00  176.74  177.21  ...    -1.0     -1.0        -1
[7 rows x 8 columns]
Roh-codeur commented 11 months ago

I had a look at the docs

https://github.com/erdewit/ib_insync/blob/d31241f2fcb16f5a61dc075d6f458721cb95eebd/README.rst#L72

Looks like for historical data it could return -1, like it does in your example. My guess is this is related to Paper account

erdewit commented 11 months ago

-1 seems to denote a "no data" condition. A bit strange for volume but it is what IB sends.

Roh-codeur commented 11 months ago

thanks mate! strange indeed. am wondering if this related to Paper account vs real account, when requesting historical data. current data has non-negative values for Volume. I am using Paper account.