AAPL.csv 数据文件
Date,Close,Volume,Open,High,Low
06/23/2023,$186.68,53117000,$185.55,$187.56,$185.01
06/22/2023,$187.00,51245330,$183.74,$187.045,$183.67
06/21/2023,$183.96,49515700,$184.90,$185.41,$182.5901
06/20/2023,$185.01,49799090,$184.41,$186.10,$184.41
06/16/2023,$184.92,101256200,$186.73,$186.99,$184.27
06/15/2023,$186.01,65433170,$183.96,$186.52,$183.78
06/14/2023,$183.95,57462880,$183.37,$184.39,$182.02
06/13/2023,$183.31,54929130,$182.80,$184.15,$182.44
06/12/2023,$183.79,54755000,$181.27,$183.89,$180.97
06/09/2023,$180.96,48899970,$181.50,$182.23,$180.63
06/08/2023,$180.57,50214880,$177.895,$180.84,$177.46
06/07/2023,$177.82,61944620,$178.44,$181.21,$177.32
06/06/2023,$179.21,64848370,$179.965,$180.12,$177.43
06/05/2023,$179.58,121946500,$182.63,$184.951,$178.035
06/02/2023,$180.95,61996910,$181.03,$181.78,$179.26
06/01/2023,$180.09,68901810,$177.70,$180.12,$176.9306
05/31/2023,$177.25,99625290,$177.325,$179.35,$176.76
05/30/2023,$177.30,55964400,$176.96,$178.99,$176.57
05/26/2023,$175.43,54834980,$173.32,$175.77,$173.11
05/25/2023,$172.99,56058260,$172.41,$173.895,$171.69文章来源:https://www.toymoban.com/news/detail-828863.html
demo.py
import matplotlib.pyplot as plt
import mplfinance as mpf
import pandas as pd
plt.rcParams['font.family'] = ['SimHei'] # 设置中文字体
plt.rcParams['axes.unicode_minus'] = False # 设置负号显示
# 读取股票数据
df = pd.read_csv('AAPL.csv', index_col='Date', parse_dates=True)
# 清洗数据
df['Close'] = df['Close'].str.replace('$', '').astype(float)
df['Open'] = df['Open'].str.replace('$', '').astype(float)
df['High'] = df['High'].str.replace('$', '').astype(float)
df['Low'] = df['Low'].str.replace('$', '').astype(float)
ma5 = df['MA5'] = df['Close'].rolling(5, min_periods=1).mean()
ma20 = df['MA20'] = df['Close'].rolling(20, min_periods=1).mean()
# 添加移动平均线参数
ap0 = [
mpf.make_addplot(ma5, color="b", width=1.5),
mpf.make_addplot(ma20, color="y", width=1.5),
]
market_colors = mpf.make_marketcolors(up='red', down='green', )
my_style = mpf.make_mpf_style(marketcolors=market_colors)
# 绘制K线图
mpf.plot(df, type='candle',
figratio=(10, 4),
mav=(10, 20),
volume=True,
# 显示非交易日期
# show_nontrading=True,
addplot=ap0,
style=my_style)
mpf.show()
显示
文章来源地址https://www.toymoban.com/news/detail-828863.html
到了这里,关于python绘制k线图均线图的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!