Data
通過 IbPy 檢索歷史數據的問題
我在從 IB 檢索 14 天的歷史數據時遇到問題。我最近從 MATLAB 切換到 Python,所以我有很多事情要做。我哪裡錯了?感謝所有幫助。
import sys import pandas as pd from time import sleep, strftime from ib.ext.Contract import Contract #Make connection conn = Connection.create(port=7496, clientId=999) conn.connect() def var2(msg): print(msg) conn.register(var2, message.historicalData) def var3(msg): print(msg) if __name__ == '__main__': #conn = ibConnection() conn.register(var2, 'UpdateAccountValue') conn.register(var3, message.tickSize, message.tickPrice) conn.connect() def inner(): qqqq = Contract() qqqq.m_secType = "STK" qqqq.m_symbol = "ABN" qqqq.m_currency = "EUR" qqqq.m_exchange = "AEB" endtime = strftime('%Y%m%d %H:%M:%S') conn.reqHistoricalData(1,qqqq,endtime,"14 D","1 min","BID",1,1) data = reqHistoricalData conn.disconnect()
您的程式碼中有幾個錯誤,其中一個很明顯是有一個
inner()
函式但沒有呼叫它。這是我根據您的程式碼清理的範例。我在程式碼中添加了一些註釋來解釋發生了什麼。另請參閱我對您原始文章的評論。from time import sleep, strftime from ib.ext.Contract import Contract def historical_data_handler(msg): # The response data callback function # print (msg.reqId, msg.date, msg.open, msg.close, msg.high, msg.low) print(msg) if __name__ == '__main__': # Establish IB connection, make sure you have the correct port, clientId conn = ibConnection(host='127.0.0.1', port=7496, clientId=999) # Register the response callback function and type of data to be returned conn.register(historical_data_handler, message.historicalData) conn.connect() # Establish a Contract object and the params for the request req = Contract() req.m_secType = "STK" req.m_symbol = "ABN" req.m_currency = "EUR" req.m_exchange = "AEB" endtime = strftime('%Y%m%d %H:%M:%S') conn.reqHistoricalData(1,req,endtime,"14 D","1 min","BID",1,1) # Make sure the connection don't get disconnected prior the response data return sleep(5) conn.disconnect()