如何解决在不间断的白天运行 reqHisorical 数据并更新 csv 文件
我正处于学习曲线的起点。已经有req历史数据下载csv文件中的req历史数据。我想让这个过程在后台运行,每分钟更新我在 CsV 中的历史数据。我希望它在后台不间断运行,直到主动脉。现在我仍然需要手动更新它,我很想自动化它以解放我的思想和双手...... 缺少什么?
真诚的谢谢你...
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
def print_to_file(*args):
with open('mnq.CsV','a') as fh:
fh.write(' '.join(map(str,args)))
fh.write('\n')
fh.close()
print = print_to_file
class TestApp(EWrapper,EClient):
def __init__(self):
EClient.__init__(self,self)
open('mnq.csv','w')
# Layout = "{!s:1} {!s:2} {!s:3} {!s:4}} "
# print(Layout.format("DateTime","High;","Close;","Volume "))
def historicalData(self,reqId,bar):
print( bar.date.replace(' ',''),";",bar.high,bar.low,bar.volume)
def main():
app = TestApp()
app.connect("127.0.0.1",7497,0)
# define MNQ
contract = Contract()
contract.symbol = "MNQ"
contract.secType = "FUT"
contract.exchange = "GLOBEX"
contract.currency = "USD"
contract.lastTradeDateOrContractMonth = "202103"
app.reqHistoricalData(1,contract,"","86400 S","1 min","TradES",1,False,[])
app.run()
if __name__ == "__main__":
main()
这是我最新的代码。 我的问号 def historyDataUpdate(self,bar) 中的内容: 仅用于处理 csv ???????
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
#import pandas as pd
class MyWrapper(EWrapper):
def __init__(self):
open('mnq.csv','w')
self.data = []
self.df=None
def nextValidId(self,orderId: int):
print("Setting nextValidOrderId: %d",orderId)
self.nextValidOrderId = orderId
self.start()
def historicalDataUpdate(self,bar):
???????
def error(self,errorCode,errorString):
print("Error. Id: "," Code: "," Msg: ",errorString)
open('mnq.csv','w')
def print_to_file(*args):
with open('mnq.csv','a') as fh:
fh.write(' '.join(map(str,args)))
fh.write('\n')
fh.close()
print = print_to_file
def start(self):
wrap = MyWrapper()
app = EClient(wrap)
app.connect("127.0.0.1",True,[])
# print(wrap.df)
# wrap.df.to_csv("mnq.csv")
app.disconnect()
解决方法
如果您想连续保存数据而不是将其保存在数据框中,那么逻辑有点不同。
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from time import time,ctime
class MyWrapper(EWrapper):
def __init__(self):
self.bars = {} # store in a dict(map) with date->bar so you can overwrite same date
def nextValidId(self,id):
self.start = time()
print(ctime(self.start))
# this is when IB recommends starting requests
reqData()
def error(self,reqId,errorCode,errorString):
# always print errors and turn on api logging in TWS
print("Error. Id: "," Code: "," Msg: ",errorString)
def print_to_file(self):
with open('mnq.csv','a') as fh:
for val in self.bars.values():
fh.write("%s,%f,%d\n" % (val.date,val.high,val.low,val.volume))
self.bars.clear()
def historicalData(self,bar):
self.bars[bar.date] = bar #the last one will be incomplete
def historicalDataEnd(self,reqId: int,start: str,end: str):
pass #can't write yet since last bar is incomplete
def historicalDataUpdate(self,bar):
if not self.bars.get(bar.date):
self.print_to_file() # a new bar has arrived so last bar is finished
self.bars[bar.date] = bar # now there is only ever one bar in dict
# decide when to quit,a timer would be better
if time() - self.start > 30:
app.disconnect()
wrap = MyWrapper()
app = EClient(wrap)
def reqData():
# define MNQ
contract = Contract()
contract.symbol = "MNQ"
contract.secType = "FUT"
contract.exchange = "GLOBEX"
contract.currency = "USD"
contract.lastTradeDateOrContractMonth = "202103"
# use dateformat 2 for UTC. Always store UTC on disk.
app.reqHistoricalData(1,contract,"","100 S","5 secs","TRADES",2,True,[])
app.connect("127.0.0.1",7497,123)
app.run() # blocks until disconnect()
app.disconnect() #never gets called
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。