| 1 | from ibapi.client import * |
| 2 | from ibapi.wrapper import * |
| 3 | import time |
| 4 | import threading |
| 5 | from ibapi.ticktype import TickTypeEnum |
| 6 | |
| 7 | # Default Ports: |
| 8 | # TWS Live Account: 7946 |
| 9 | # TWS Paper Account: 7947 |
| 10 | # IB Gateway Live Account: 4001 |
| 11 | # IB Gateway Paper Account: 4002 |
| 12 | port = 4002 |
| 13 | |
| 14 | |
| 15 | class TestApp(EClient, EWrapper): |
| 16 | def __init__(self): |
| 17 | EClient.__init__(self, self) |
| 18 | |
| 19 | def nextValidId(self, orderId: OrderId): |
| 20 | self.orderId = orderId |
| 21 | |
| 22 | def nextId(self): |
| 23 | self.orderId += 1 |
| 24 | return self.orderId |
| 25 | |
| 26 | def error(self, reqId, errorTime, errorCode, errorString, advancedOrderReject=""): |
| 27 | print( |
| 28 | f"reqId: {reqId}, errorCode: {errorCode}, errorString: {errorString}, orderReject: {advancedOrderReject}" |
| 29 | ) |
| 30 | |
| 31 | def tickPrice(self, reqId, tickType, price, attrib): |
| 32 | print( |
| 33 | f"reqId: {reqId}, tickType: {TickTypeEnum.toStr(tickType)}, price: {price}, attrib: {attrib}" |
| 34 | ) |
| 35 | |
| 36 | def tickSize(self, reqId, tickType, size): |
| 37 | print(f"reqId: {reqId}, tickType: {TickTypeEnum.toStr(tickType)}, size: {size}") |
| 38 | |
| 39 | |
| 40 | app = TestApp() |
| 41 | app.connect("127.0.0.1", port, 0) |
| 42 | threading.Thread(target=app.run).start() |
| 43 | time.sleep(1) |
| 44 | |
| 45 | mycontract = Contract() |
| 46 | mycontract.symbol = "AAPL" |
| 47 | mycontract.secType = "STK" |
| 48 | mycontract.exchange = "SMART" |
| 49 | mycontract.currency = "USD" |
| 50 | |
| 51 | app.reqMarketDataType(3) |
| 52 | app.reqMktData(app.nextId(), mycontract, "", False, False, []) |