Pandas标绘财务数据绝对奇怪的行为,长短信号标绘

2024-04-25 16:44:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个非常奇怪的问题,我能够解决一个快速和肮脏的方式,但我想了解背后的原因。你知道吗

我遵循了这个教程:https://www.datacamp.com/community/tutorials/finance-python-trading#backtesting

他们给了这个密码:

# Import `pyplot` module as `plt`
import matplotlib.pyplot as plt

# Initialize the plot figure
fig = plt.figure()

# Add a subplot and label for y-axis
ax1 = fig.add_subplot(111,  ylabel='Price in $')

# Plot the closing price
aapl['Close'].plot(ax=ax1, color='r', lw=2.)

# Plot the short and long moving averages
signals[['short_mavg', 'long_mavg']].plot(ax=ax1, lw=2.)

# Plot the buy signals
ax1.plot(signals.loc[signals.positions == 1.0].index, 
         signals.short_mavg[signals.positions == 1.0],
         '^', markersize=10, color='m')

# Plot the sell signals
ax1.plot(signals.loc[signals.positions == -1.0].index, 
         signals.short_mavg[signals.positions == -1.0],
         'v', markersize=10, color='k')

# Show the plot
plt.show()

要生成此图表: Tutorials's plot

我完全复制了代码并遵循了教程,只是有一点不同的输入数据(另一个股票)。你知道吗

但是我得到这个图表:

My Plot

现在,如果我用代码来绘制买入和卖出信号,并将其向上移动到ax1=。。。 一切都是顺其自然。我试着改变参数,也只是把轴的全部数据,用1填充序列,得到一条直线。它产生了几乎相同的图形。你知道吗

有人知道为什么会发生这种情况吗?如果有,如何避免?你知道吗

我假设它与matplotlib的某种更新有关,因为它在datacamp环境中工作。你知道吗

非常感谢您的帮助。谢谢!你知道吗


Tags: theplotmatplotlibas教程pltcolorshort