简体   繁体   中英

Interactive Brokers Python Data Request

I am running the following below code but my request to get the snapshot price returns nothing on the python console. Do I need a print method? Sorry, I am not familiar with stupid oop, done only procedural and functional.

Thank you


from ibapi.client   import EClient
from ibapi.wrapper  import EWrapper
from ibapi.common   import *
from ibapi.contract import *

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

my_connection = TestApp()

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

ym = Contract()
ym.symbol          = "YM"
ym.secType         = "FUT"
ym.ContractMonth   = "JUN18"
ym.primaryExchange = "ECBOT"
ym.currency        = "USD"

my_connection.reqMktData(1000, contract = ym , genericTickList = "9", snapshot = True, regulatorySnapshot = False, mktDataOptions = [])`

You need to override the callback functions in the EWrapper class to define how you would like handle returned data:

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.ticktype import TickType
from ibapi.common import *

from threading import Timer

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

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

    def nextValidId(self, orderId ):
        self.nextOrderId = orderId
        self.start()

    def tickPrice(self, reqId: TickerId, tickType: TickType, price: float, attrib: TickAttrib):
        print("TickPrice. TickerId:", reqId, "tickType:", tickType,
              "Price:", price, "CanAutoExecute:", attrib.canAutoExecute,
              "PastLimit:", attrib.pastLimit, end=' ')

    def tickSize(self, reqId: TickerId, tickType: TickType, size: int):
        print("TickSize. TickerId:", reqId, "TickType:", tickType, "Size:", size)

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

        self.reqMarketDataType(4)
        self.reqMktData(1, contract, "", False, False, [])

    def stop(self):
        self.done = True
        self.disconnect()

def main():
    app = TestApp()
    app.nextOrderId = 0
    app.connect("127.0.0.1", 7497, 1)

    Timer(5, app.stop).start()
    app.run()

if __name__ == "__main__":
    main()

Also note that the futures contract is defined incorrectly, there are examples at:

http://interactivebrokers.github.io/tws-api/basic_contracts.html#fut

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