reqHistoricalData() 使用 IBpy 返回空值?
reqHistoricalData() returning empty value using IBpy?
我正在尝试使用 IBpy return 来自某些仪器的历史数据,但是当我尝试文档中的代码时,我得到了一个空结果。
我设法使用 R Ibroker 让它工作,但我真的更愿意使用 Python API.
让它工作
这是我正在测试的代码。
from time import sleep, strftime
from time import sleep
from ib.ext.Contract import Contract
from ib.opt import ibConnection, message
def my_account_handler(msg):
print(msg)
def my_tick_handler(msg):
print(msg)
if __name__ == '__main__':
con = ibConnection()
con.register(my_account_handler, 'UpdateAccountValue')
con.register(my_tick_handler, message.tickSize, message.tickPrice)
con.connect()
def inner():
qqqq = Contract()
qqqq.m_secType = "CASH"
qqqq.m_symbol = "MSFT"
qqqq.m_currency = "USD"
qqqq.m_exchange = "IDEALPRO"
endtime = strftime('%Y%m%d %H:%M:%S')
con.reqHistoricalData(1,qqqq,endtime,"5 D","1 hour","MIDPOINT",1,1)
sleep(10)
inner()
sleep(5)
print('disconnected', con.disconnect())
知道可能出了什么问题吗?
您需要注册历史数据消息
con.register(my_hist_data_handler, message.historicalData)
然后定义你想用它做什么
def my_hist_data_handler(msg):
print(msg)
另请注意 MSFT(或 QQQ)是股票
qqqq.m_secType = "STK" #cash is for forex
qqqq.m_symbol = "MSFT" #use less confusing var name
qqqq.m_currency = "USD"
qqqq.m_exchange = "SMART" #for stocks usually
我正在尝试使用 IBpy return 来自某些仪器的历史数据,但是当我尝试文档中的代码时,我得到了一个空结果。
我设法使用 R Ibroker 让它工作,但我真的更愿意使用 Python API.
让它工作这是我正在测试的代码。
from time import sleep, strftime
from time import sleep
from ib.ext.Contract import Contract
from ib.opt import ibConnection, message
def my_account_handler(msg):
print(msg)
def my_tick_handler(msg):
print(msg)
if __name__ == '__main__':
con = ibConnection()
con.register(my_account_handler, 'UpdateAccountValue')
con.register(my_tick_handler, message.tickSize, message.tickPrice)
con.connect()
def inner():
qqqq = Contract()
qqqq.m_secType = "CASH"
qqqq.m_symbol = "MSFT"
qqqq.m_currency = "USD"
qqqq.m_exchange = "IDEALPRO"
endtime = strftime('%Y%m%d %H:%M:%S')
con.reqHistoricalData(1,qqqq,endtime,"5 D","1 hour","MIDPOINT",1,1)
sleep(10)
inner()
sleep(5)
print('disconnected', con.disconnect())
知道可能出了什么问题吗?
您需要注册历史数据消息
con.register(my_hist_data_handler, message.historicalData)
然后定义你想用它做什么
def my_hist_data_handler(msg):
print(msg)
另请注意 MSFT(或 QQQ)是股票
qqqq.m_secType = "STK" #cash is for forex
qqqq.m_symbol = "MSFT" #use less confusing var name
qqqq.m_currency = "USD"
qqqq.m_exchange = "SMART" #for stocks usually