How can I update the chart in real-time instead of reprinting it again and again? I have used plotly and dash.
import ibapi
import pandas
import plotly.graph_objects as go
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
import threading
import time
class IBapi(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
self.data = [] #Initialize variable to store candle
def historicalData(self, reqId, bar):
print(f'Time: {bar.date} High: {bar.high} Low: {bar.low} Open: {bar.open} Close :
{bar.close}')
self.data.append([bar.date, bar.high, bar.low, bar.open, bar.close])
def run_loop():
app.run()
app = IBapi()
app.connect('127.0.0.1', 7496, 123)
#Start the socket in a thread
api_thread = threading.Thread(target=run_loop, daemon=True)
api_thread.start()
time.sleep(1)
#contract object
eurusd_contract = Contract()
eurusd_contract.symbol = 'EUR'
eurusd_contract.secType = 'CASH'
eurusd_contract.exchange = 'IDEALPRO'
eurusd_contract.currency = 'USD'
i=1
while i==1:
app.reqHistoricalData(1, eurusd_contract, '', '2 D', '1 hour', 'BID', 1, 2, False, [])
time.sleep(5) #sleep to allow enough time for data to be returned
df = pandas.DataFrame(app.data, index=None, columns=['DateTime','High', 'Low', 'Open',
'Close'])
df['DateTime'] = pandas.to_datetime(df['DateTime'],unit='s')
df.to_csv('EURUSD_Hourly.csv')
#print(df)
time.sleep(5)
fig = go.Figure(data=[
go.Candlestick(
x=df['DateTime'],
open=df['Open'],
high=df['High'],
low=df['Low'],
close=df['Close'])])
fig = fig.update_layout(title= 'INTERACTIVE CHART',
yaxis_title = eurusd_contract.symbol)
fig.show()
time.sleep(10)