【发布时间】:2022-08-20 19:06:10
【问题描述】:
我这里有一个 MWE(最小工作示例),但我无法运行?可悲的是我找不到我的错误。
控制台告诉我的唯一一件事是有太多的滴答声。但我不明白为什么,因为只有 3 个月的数据。
import yfinance as yf
from stockstats import StockDataFrame
# Convert to StockDataFrame
# Need to pass a copy of candlestick_data to StockDataFrame.retype
# Otherwise the original candlestick_data will be modified
df = yf.download(\'SPY\',start=\'2017-04-23\', end = \'2017-07-01\')
stockstats = StockDataFrame.retype(df)
# 5-day exponential moving average on closing price
ema_5 = stockstats[\"close_5_ema\"]
# 20-day exponential moving average on closing price
ema_20 = stockstats[\"close_20_ema\"]
# 50-day exponential moving average on closing price
ema_50 = stockstats[\"close_50_ema\"]
# Upper Bollinger band
boll_ub = stockstats[\"boll_ub\"]
# Lower Bollinger band
boll_lb = stockstats[\"boll_lb\"]
# 7-day Relative Strength Index
rsi_7 = stockstats[\'rsi_7\']
# 14-day Relative Strength Index
rsi_14 = stockstats[\'rsi_14\']
import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import date2num, WeekdayLocator, DayLocator, DateFormatter, MONDAY
from mplfinance.original_flavor import candlestick_ohlc
# Create a new Matplotlib figure
fig, ax = plt.subplots()
# Prepare a candlestick plot
candlestick_ohlc(ax, df.values, width=0.6)
# Plot stock indicators in the same plot
ax.plot(df.index, ema_5, lw=1, label=\'EMA (5)\')
ax.plot(df.index, ema_20, lw=1, label=\'EMA (20)\')
ax.plot(df.index, ema_50, lw=1, label=\'EMA (50)\')
ax.plot(df.index, boll_ub, lw=2, linestyle=\"--\", label=\'Bollinger upper\')
ax.plot(df.index, boll_lb, lw=2, linestyle=\"--\", label=\'Bollinger lower\')
ax.xaxis.set_major_locator(WeekdayLocator(MONDAY)) # major ticks on
# the mondays
ax.xaxis.set_minor_locator(DayLocator()) # minor ticks on the days
ax.xaxis.set_major_formatter(DateFormatter(\'%Y-%m-%d\'))
ax.xaxis_date() # treat the x data as dates
# rotate all ticks to vertical
plt.setp(ax.get_xticklabels(), rotation=90, horizontalalignment=\'right\')
ax.set_ylabel(\'Price (US $)\') # Set y-axis label
# Limit the x-axis range from 2017-4-23 to 2017-7-1
datemin = datetime.date(2017, 4, 23)
datemax = datetime.date(2017, 7, 1)
ax.set_xlim(datemin, datemax)
plt.legend() # Show figure legend
plt.tight_layout()
plt.show()
标签: python matplotlib trading stockstats