实现 Bollinger Bands
Bollinger Bands(布林带)是在价格的简单移动平均线之上和之下绘制的包络线。由于带宽基于标准差确定,它会随标的价格的波动性而自适应变化。
为更好地理解标准差参数对布林带的影响,您将基于同一数据集实现并绘制两组布林带。
您将使用已预加载为 bitcoin_data 的比特币历史价格数据。talib 库也已为您导入。
本练习是课程的一部分
Python 中的金融交易
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Define the Bollinger Bands with 1-sd
upper_1sd, mid_1sd, lower_1sd = ____(bitcoin_data['Close'],
____,
____,
timeperiod=20)
# Plot the upper and lower Bollinger Bands
plt.plot(bitcoin_data['Close'], color='green', label='Price')
plt.plot(____, color='tomato', label="Upper 1sd")
plt.plot(____, color='tomato', label='Lower 1sd')
# Customize and show the plot
plt.legend(loc='upper left')
plt.title('Bollinger Bands (1sd)')
plt.show()