def __init__(self, ip_addr='127.0.0.1', port=7497, clientId=1):

        # Wrapper Methods
        def nextValidId(reqId):
            q.put(reqId)

        def connectionClosed():
            print('CONNECTION HAS CLOSED')

        def error(reqId, errorCode: int, errorString: str):
            if errorCode != 2104 and errorCode != 2106:
                print("Error. Id: ", reqId, " Code: ", errorCode, " Msg: ",
                      errorString)
                if errorCode == 10167 and 'Displaying delayed market data' in errorString:
                    pass
                elif reqId != -1:
                    self.data_errors_q.put((errorString, reqId))
                if 'pacing violation' in errorString:
                    self.slowdown = True

        # Wrapper Methods End

        self.wrapper = EWrapper()
        self.client = EClient(self.wrapper)

        self.client.connect(ip_addr, port, clientId)
        self.ip_addr = ip_addr
        self.my_port = port
        self.my_clientId = clientId
        self.resetData()

        # Wrap wrapper methods
        self.wrap(error)
        self.wrap(connectionClosed)
        self.wrap(nextValidId)

        q = queue.Queue()
        self._thread = Thread(target=self.client.run)
        self._thread.start()
        # Once we get a reqID, we know we can start
        self._reqId = q.get()
示例#2
0
def cancel_data(request):
    # cancel_stock = TestApp()
    wrapper = EWrapper()
    cancel_data = EClient(wrapper)
    cancel_data.disconnect()
    return render(request, 'ib_api/cancel_data.html')
示例#3
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from ibapi.client import EClient
from ibapi.wrapper import EWrapper

app = EClient(EWrapper())
app.connect("127.0.0.1", 7497, clientId=0)
print("serverVersion:%s connectionTime:%s" %
      (app.serverVersion(), app.twsConnectionTime()))
if __name__ == '__main__':
    pass