Im at the beginning of the learing curve. Already have req historical data downloading req historical data in csv file. I want to keep this process running in the background, updating my historical data in CsV every minute. I would like it to run uninterupped in the backround untill aorted. Now I still have to update it manualy and I would love to automate this to free my mind and hands..... What is missing?
Thank you sincerly...
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", 0, 1, False, [])
app.run()
if __name__ == "__main__":
main()
This is my latest code. questionmark for me what comes in def historicalDataUpdate(self, reqId, bar): for only handeling 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, reqId, bar):
???????
def error(self, reqId, errorCode, errorString):
print("Error. Id: ", reqId, " Code: ", errorCode, " 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", 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", 0, 1, True, [])
# print(wrap.df)
# wrap.df.to_csv("mnq.csv")
app.disconnect()