1

我正在尝试使用 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())

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

4

1 回答 1

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
于 2015-05-11T20:11:39.270 回答