首页 > 解决方案 > 交互式经纪人 api python - 拉出/询问交易书供应商

问题描述

我正在使用 IB API 来自动提取完整的每日买卖交易簿供应商的实时数据。我还无法弄清楚如何在屏幕上打印这些数据,希望能得到一些启示

这是我用于提取冻结的延迟市场数据的代码示例:

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()

标签: pythonib-api

解决方案


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

推荐阅读