reqHistoricalData()使用IBpy返回空值?

时间:2015-05-08 18:23:38

标签: python ibrokers interactive-brokers ibpy

我正在尝试使用IBpy从某些乐器返回历史数据,但是当我尝试文档中的代码时,我得到一个空结果。

我设法使用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())

知道可能出现什么问题吗?

1 个答案:

答案 0 :(得分:1)

您需要注册历史数据信息。

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