0

我使用 Python

API_Version=9.73.04

from ibapi import wrapper
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract as IBcontract
from threading import Thread
import queue
import datetime
from ibapi.utils import iswrapper #just for decorator
from ibapi.common import *
from ibapi.contract import *
from ibapi.ticktype import *

class TestApp(wrapper.EWrapper, EClient):

    def __init__(self):
        wrapper.EWrapper.__init__(self)
        EClient.__init__(self, wrapper=self)


    @iswrapper
    def historicalData(self, reqId:int, bar: BarData):
        print("HistoricalData. ", reqId, " Date:", bar.date, "Open:", bar.open,
              "High:", bar.high, "Low:", bar.low, "Close:", bar.close, "Volume:", bar.volume)
        self.done = True





def main():
    t = time()
    max_amount_per_Iter = 70 #max number per iter to save cost
    max_Iter = ceil(len(all_data)/max_amount_per_Iter)-1
    for i in range (0,max_Iter):
        print('====================round : ',i+1,'===============================')
        app = TestApp()
        app.connect("127.0.0.1", 7496, clientId=i)
        print("serverVersion:%s connectionTime:%s" % (app.serverVersion(),app.twsConnectionTime()))
        for j in range (0,min(max_amount_per_Iter,len(all_data)-i*max_amount_per_Iter)):
            print(j+i*70)
            app.done = False
            app.i = j+i*max_amount_per_Iter

            contract = Contract()
            contract.symbol = all_data.iloc[app.i,0]
            contract.secType = all_data.iloc[app.i,1]
            contract.currency = all_data.iloc[app.i,3]
            contract.exchange = all_data.iloc[app.i,2]
            queryTime = (datetime.datetime.today() - datetime.timedelta(days=180)).strftime("%Y%m%d %H:%M:%S")

            print('i=', i)
            app.reqHistoricalData(app.i, contract, queryTime,"1 W", "1 day", "Adjusted_Last", 1, 1, False, [])
            i+1
            app.run()

        sleep(1)
        app.disconnect()
        sleep(0.02)
        print('=========End round : ',i+1,'with time :',time() - t,'==============')

if __name__ == "__main__":
    main()

作为

app.reqHistoricalData(app.i, contract, queryTime,"1 W", "1 day", "Adjusted_Last", 1, 1, False, [])

它返回没有数据的错误。

ERROR:root:ERROR 0 321 Error validating request:-'bm' : cause - What to show value of ADJUSTED_LAST rejected.

“Adjusted_Last”的链接

错误代码链接

我尝试了各种方法。但我是 Python 新手,所以我什至不知道如何开始搜索。因为 all_data 是一个 pd.DataFame,我用它来保留标准普尔 500 指数成分股的股票,我将使用这些价格来计算回报。

然后我计划获得一个我持有的股票头寸并系统地交易它们。

我知道我有关于 app.done = True 的另一个问题,我想我可以自己处理。但我无法弄清楚这一点。谢谢!

4

1 回答 1

1

我想我必须更新Click_Here

TWS 应用程序不是 API。

现在的错误是

ERROR:root:ERROR 0 321 Error validating request:-'bA' : cause - End date not supported with adjusted last

我有邮件询问 IB 团队。他告诉我将 endDateTime 留空。这是工作!!

app.reqHistoricalData(app.i, contract, "","1 W", "1 day", "Adjusted_Last", 1, 1, False, [])
于 2017-08-16T09:42:02.647 回答