python绘制k线图均线图

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

demo.py

python 复制代码
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()

显示

相关推荐
_.Switch11 分钟前
Python 自动化运维持续优化与性能调优
运维·开发语言·python·缓存·自动化·运维开发
南猿北者16 分钟前
Docker Volume
运维·docker·容器
矛取矛求3 小时前
Linux如何更优质调节系统性能
linux
内核程序员kevin4 小时前
在Linux环境下使用Docker打包和发布.NET程序并配合MySQL部署
linux·mysql·docker·.net
kayotin4 小时前
Wordpress博客配置2024
linux·mysql·docker
Ztiddler5 小时前
【Linux Shell命令-不定期更新】
linux·运维·服务器·ssh
小小不董5 小时前
Oracle OCP认证考试考点详解082系列16
linux·运维·服务器·数据库·oracle·dba
IPdodo全球网络5 小时前
如何利用静态住宅IP优化Facebook商城的网络稳定性与运营效率
运维·服务器
a1denzzz5 小时前
Linux系统的网络设置
linux·服务器·网络
运维&陈同学6 小时前
【模块一】kubernetes容器编排进阶实战之k8s基础概念
运维·docker·云原生·容器·kubernetes·云计算