虽然我已经从堆栈溢出的所有出色信息中解决了许多问题,但这是我的第一篇文章,对许多错误的格式表示歉意。在此先感谢您的协助。总结一下-我生成并绘制了带有非日期x轴(柱线号)的股票价格数据框。我需要保持此柱号-原始数据帧的长度-并在每次收到新的实时柱时增加1。我还没有找到使用FuncAnimation做到这一点的方法。
我正在处理从Metatrader 5导入的历史和实时交易数据。使用matplotlib提取数据并绘制图表很容易。但是,就像我读过的许多类似项目一样,我想消除交易时段之间的差距,而使用matplotlib.dates很难做到这一点。我的简单解决方案是在绘制数据栏之前将条码栏添加到数据框中,并将条码用作x轴。效果很好-直到我想使用FuncAnimation添加其他现场酒吧
这是将MetaTrader数据加载到数据帧中的简单功能:
def Get_Rates_Pos(Symbol, timeframe, get_from, lookback):
df = MT5CopyRatesFromPos(Symbol, timeframe, get_from , lookback)
df = pd.DataFrame(list(df), columns=['Time', 'Open', 'High', 'Low', 'Close', 'Tick_volume', 'Spread', 'Real_volume'])
df = df[['Open', 'High', 'Low', 'Close', 'Time', 'Tick_volume', 'Spread', 'Real_volume']]
df['Time'] = df['Time'].map(mdates.date2num)
return(df)
rates = Get_Rates_Pos(Symbol,tf,get_from,look_back)
rates.insert(0,'Barnum',range(1,1 + len(rates)))
candlestick_ohlc(ax1,rates.values,width = bw,colordown ='#ff0000', colorup ='#00cc00')
ax2.bar(rates ['Barnum'],rates ['Real_volume'])
我需要对出现的每个新柱进行计数,以便可以为其分配一个新的柱号并以正确的顺序在图表上进行绘制。我一直无法保持每次调用动画函数时都会增加的计数。
我也尝试过将原始数据帧-称为'rates'-传递给该函数并向其添加一行,但这也不起作用-原始数组的长度永远不会增加。这是FuncAnimation调用的函数-我事先知道我传递了太多参数,但是当我拨入动画时,这是一个正在进行的工作。
def Plot_Current(i, Symbol, rates, tf, get_from, look_back, bw, ax1, ax2, lp, bars):
current_bar = Get_Rates_Pos(Symbol, tf, get_from, look_back)
current_bar.insert(0, 'Barnum', range(1, 1 + len(current_bar)))
current_bar.Barnum = len(rates) + 1
if current_bar.at[0, 'Time'] > rates.at[len(rates)-1, 'Time']:
# this is the line that doesn't work as the original dataframe is passed in every time this function is called and never gets appended:
rates = rates.append(current_bar, ignore_index=True)
candlestick_ohlc(ax1, current_bar.values, width = bw, colordown='#ff0000', colorup='#00cc00')
ax2.bar(current_bar['Barnum'], current_bar['Real_volume'], width = bw*2)