[Python] Getting correct volume using IB API

Discussão em 'Python' iniciado por Stack, Fevereiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to get the volume of a stock using interactive brokers API, but it gets an incorrect number. I read about lots so I tried to multiple by 100, but still, the numbers are not related to reality. for example:

    real volume: 32.2 million, script gets volume: 169151
    real volume: 48.82 million, script gets volume: 214656
    real volume: 3.29 million, script gets volume: 19171
    real volume: 3.81 million, script gets volume: 20394
    real volume: 5.12 million, script gets volume: 22483
    real volume: 632.82K, script gets volume: 4447
    real volume: 1.01 million, script gets volume: 8946
    real volume: 1.08 million, script gets volume: 5088
    real volume: 2.18 million, script gets volume: 7841
    real volume: 1.09 million, script gets volume: 2431
    real volume: 652.74K, script gets volume: 4736

    Here is how I get the volume in my code:

    class IBapi(EWrapper, EClient):
    def __init__(self):
    EClient.__init__(self, self)
    self.historical_data = []
    self.data_ready = False
    self.shown_errors = set()

    def error(self, reqId, errorCode, errorString):
    if errorCode in [2104, 2106, 2158, 2176]:
    return
    if errorCode not in self.shown_errors:
    print(f"Error {errorCode}: {errorString}")
    self.shown_errors.add(errorCode)

    def historicalData(self, reqId, bar):
    self.historical_data.append({
    'Date': bar.date,
    'Open': bar.open,
    'High': bar.high,
    'Low': bar.low,
    'Close': bar.close,
    'Volume': int(bar.volume * 100)
    })

    def historicalDataEnd(self, reqId, start, end):
    self.data_ready = True


    Here is how I create the contract:

    contract.symbol = ticker_name
    contract.secType = "STK"
    contract.exchange = "SMART"
    contract.currency = "USD"
    return contract


    What am I doing wrong? how to get from IB API the correct volume, which appears in all the other sites with volume information?

    Continue reading...

Compartilhe esta Página