我使用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而没有数据。
ERROR:root:ERROR 0 321 Error validating request:-'bm' : cause - What to show value of ADJUSTED_LAST rejected.
我尝试了各种方法。但我是Python的新手,所以我甚至都不知道如何开始搜索。 for all_data是一个pd.DataFame,我用来保存S& P 500成分股,我会用这些价格来计算回报。
然后我计划获得我所持有的股票头寸并系统地交易它们。
我知道我有另一个关于app.done = True的问题,我想我可以自己处理它。但我无法弄清楚这一点。 谢谢!
答案 0 :(得分:0)
我想我必须更新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, [])