[英]Interactive Brokers Python Data Request
我正在運行以下代碼,但是獲取快照價格的請求在python控制台上未返回任何內容。 我需要打印方法嗎? 抱歉,我對愚蠢的oop不熟悉,只執行了過程和功能。
謝謝
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 = [])`
您需要覆蓋EWrapper類中的回調函數,以定義處理返回數據的方式:
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()
另請注意,期貨合約定義不正確,在以下位置有示例:
http://interactivebrokers.github.io/tws-api/basic_contracts.html#fut
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.