I'm trying to download historical data from TWS API and keep the data up to date with keepuptodate=True. So far I have managed to download historical data from TWS API and store it in a Dataframe.
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
import threading
import time
class TradingApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self,self)
def historicalData(self, reqId, bar):
print("HistoricalData. ReqId:", reqId, "BarData.", bar)
def websocket_con():
app.run()
app = TradingApp()
app.connect("127.0.0.1", 7497, clientId=1)
con_thread = threading.Thread(target=websocket_con, daemon=True)
con_thread.start()
time.sleep(1) # some latency added to ensure that the connection is established
def usTechStk(symbol,sec_type="STK",currency="USD",exchange="ISLAND"):
contract = Contract()
contract.symbol = symbol
contract.secType = sec_type
contract.currency = currency
contract.exchange = exchange
return contract
def histData(req_num,contract,duration,candle_size):
app.reqHistoricalData(reqId=req_num,
contract=contract,
endDateTime='',
durationStr=duration,
barSizeSetting=candle_size,
whatToShow='TRADES',
useRTH=0,
formatDate=1,
keepUpToDate=True,
chartOptions=[])
tickers = ["META","AMZN","INTC"]
for ticker in tickers:
print(ticker)
histData(tickers.index(ticker),usTechStk(ticker),'2 D', '1 min')
time.sleep(10)
I have also managed to keep histrcial data up to date with.
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
import threading
import time
class TradingApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self,self)
def historicalData(self, reqId, bar):
print("HistoricalData. ReqId:", reqId, "BarData.", bar)
def historicalDataUpdate(self, reqId, bar):
print("HistoricalDataUpdate. ReqId:", reqId, "BarData.", bar)
def websocket_con():
app.run()
app = TradingApp()
app.connect("127.0.0.1", 7497, clientId=1)
con_thread = threading.Thread(target=websocket_con, daemon=True)
con_thread.start()
time.sleep(1)
contract = Contract()
contract.symbol = "META"
contract.secType = "STK"
contract.currency = "USD"
contract.exchange = "ISLAND"
app.reqHistoricalData(reqId=1,
contract=contract,
endDateTime='',
durationStr='2 D',
barSizeSetting='1 min',
whatToShow='TRADES',
useRTH=0,
formatDate=1,
keepUpToDate=True,
chartOptions=[])
time.sleep(5)
app.historicalDataUpdate(reqId=1,bar='BarData') # EClient function to request contract details
How do I get the dataframes in the first block of code to update with the streaming data?