erdewit / ib_insync

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

ib.reqHistoricalData method does not return a volume #521

Closed bossdown123 closed 1 year ago

bossdown123 commented 1 year ago

from ib_insync import *

ib = IB()
ib.connect('127.0.0.1', 4001, clientId=1)

contract = Stock('SPY', 'SMART', 'USD')
bars = ib.reqHistoricalData(
    contract, endDateTime='', durationStr='30 D',
    barSizeSetting='1 min', whatToShow='MIDPOINT', useRTH=False)

df = util.df(bars)
print(df)
#df.to_csv(str(contract), sep='\t')

returns

          date     open     high      low    close  volume  average  barCount
0   2022-09-26  367.100  370.205  363.025  365.095    -1.0     -1.0        -1
mattsta commented 1 year ago

You are correct! It works as advertised.

You requested MIDPOINT data, so what's the volume of a midpoint? Undefined.

All the possible combinations are in the API documentation: https://interactivebrokers.github.io/tws-api/historical_bars.html#hd_what_to_show

erdewit commented 1 year ago

The intention was probably whatToShow='TRADES'.

keshav-pudaruth-shift-blue commented 1 year ago

I solved this one after one week. The key parts are:

  1. whatToShow='TRADES'
  2. contract = Stock('SPY', 'CBOE', 'USD')

Using the SMART exchange will default to exchanges that do not report volumes.

Hope it helps.