简体   繁体   中英

interactive brokers api python - pull bid/ask trading book vendor

I'm using the IB API in order to automatically pull real time data of the full daily bid/ask trading book vendor. I am unable to figure out how to print on the screen this data yet, would appreciate some enlightenment

Here is my code sample for pulling frozen delayed market data:

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.ticktype import TickTypeEnum


class TestApp(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self, self)

    def error(self, reqId, errorCode, errorString):
        print("Error: ", reqId, " ", errorCode, " ", errorString)

    def tickPrice(self, reqId, tickType, price, attrib):
        print("Tick Price. Ticker Id:", reqId, "tickType:", TickTypeEnum.to_str(tickType), "Price:", price, end=' ')

    def tickSize(self, reqId, tickType, size):
        print("Tick Size. Ticker Id:", reqId, "tickType:", TickTypeEnum.to_str(tickType), "Size:", size)
        #if tickType == 2 and reqId == 1:
        #   print('The current ask price is: ', price)

def main():
    app = TestApp()

    app.connect("127.0.0.1", 7497, 0)

    contract = Contract()
    contract.symbol = "AAPL"
    contract.secType = "STK"
    contract.exchange = "SMART"
    contract.currency = "USD"
    contract.primaryExchange = "NASDAQ"


    try:
        app.reqMarketDataType(1)  # switch to delayed-frozen data if live is not available
        app.reqMktData(1, contract, "", False, False, [])
        
    except Exception:
        marketData=0
    
        
    else:
        app.reqMarketDataType(4)  # switch to delayed-frozen data if live is not available
        app.reqMktData(1, contract, "", False, False, [])
        marketData = -1
    
    finally:
        print (marketData)



    app.run()


if __name__ == "__main__":
    main()
def tickPrice(self, reqId, tickType, price, attrib):
    if tickType == 1 and reqId == 2:
        print('The current ask price is: ', price)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM