问题描述
如何实时更新图表而不是一次又一次地重新打印? 我用过plot和破折号。
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)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)