Python 通过 akshare 绘制中国平安均线并显示买卖点
继续上篇,通过 akshare 绘制中国平台的均线,并在 5日线上穿 10日线的时候,显示买入信号,在5日线下穿 10日均线的时候,显示卖出信号
环境:
python3.9(miniconda3)、Pycharm、Win10
需要用的模块或者工具库:
akshare(1.11.91)、pandas(2.1.1)、numpy(1.26.2)、matplotlib(3.8.2)、mplfinance(0.12.10b0)
绘制K线图流程如下:
-
获取中国平安数据
import akshare as ak pingan = ak.stock_zh_a_daily(symbol="sh601318", adjust="qfq")
date open high ... amount outstanding_share turnover 0 2007-03-01 17.58 17.92 ... 9.500303e+09 5.750000e+08 0.343936 1 2007-03-02 16.52 16.68 ... 1.981445e+09 5.750000e+08 0.073921 2 2007-03-05 16.16 16.23 ... 1.880503e+09 5.750000e+08 0.072904 3 2007-03-06 15.47 16.17 ... 1.337719e+09 5.750000e+08 0.051779 4 2007-03-07 15.92 16.49 ... 1.317764e+09 5.750000e+08 0.049994 ... ... ... ... ... ... ... 4030 2023-12-29 40.06 40.44 ... 1.372848e+09 1.076266e+10 0.003169 4031 2024-01-02 40.30 40.31 ... 1.740486e+09 1.076266e+10 0.004066 4032 2024-01-03 39.47 39.66 ... 1.432855e+09 1.076266e+10 0.003380 4033 2024-01-04 39.36 39.58 ... 1.819794e+09 1.076266e+10 0.004343 4034 2024-01-05 38.85 39.58 ... 1.955115e+09 1.076266e+10 0.004638 [4035 rows x 9 columns]
-
获取过去 30天的数据
data = pingan.reset_index().iloc[-30:, :6]
index date open high low close 4005 4005 2023-11-24 43.00 43.03 42.63 42.69 4006 4006 2023-11-27 42.60 42.64 41.61 41.85 4007 4007 2023-11-28 41.80 42.06 41.58 41.67 4008 4008 2023-11-29 41.67 41.73 40.50 40.75 4009 4009 2023-11-30 40.67 41.07 40.62 40.80 4010 4010 2023-12-01 40.65 41.18 40.30 41.02 4011 4011 2023-12-04 40.85 41.27 40.31 40.38 4012 4012 2023-12-05 40.12 40.17 39.51 39.53 4013 4013 2023-12-06 39.50 39.75 39.29 39.53 4014 4014 2023-12-07 39.36 39.72 38.88 39.34 4015 4015 2023-12-08 39.25 39.63 39.19 39.19 4016 4016 2023-12-11 39.00 39.71 38.56 39.60 4017 4017 2023-12-12 39.42 39.81 39.25 39.79 4018 4018 2023-12-13 39.70 39.70 38.89 38.89 4019 4019 2023-12-14 39.23 39.47 38.69 38.70 4020 4020 2023-12-15 39.15 39.56 38.73 38.79 4021 4021 2023-12-18 38.40 38.85 38.31 38.61 4022 4022 2023-12-19 38.58 38.98 38.40 38.71 4023 4023 2023-12-20 38.71 38.90 38.43 38.45 4024 4024 2023-12-21 38.26 38.97 38.13 38.80 4025 4025 2023-12-22 38.76 39.38 38.68 39.10 4026 4026 2023-12-25 39.05 39.28 38.91 39.18 4027 4027 2023-12-26 39.18 39.18 38.82 39.10 4028 4028 2023-12-27 39.10 39.16 38.68 39.00 4029 4029 2023-12-28 39.00 40.35 38.85 40.26 4030 4030 2023-12-29 40.06 40.44 40.05 40.30 4031 4031 2024-01-02 40.30 40.31 39.46 39.47 4032 4032 2024-01-03 39.47 39.66 39.19 39.40 4033 4033 2024-01-04 39.36 39.58 38.66 38.90 4034 4034 2024-01-05 38.85 39.58 38.68 38.97
-
把 date 类型修改为 datetime 并设置为索引
data['date'] = pd.to_datetime(data['date']) data.set_index("date", inplace=True)
index open high low close date 2023-11-24 4005 43.00 43.03 42.63 42.69 2023-11-27 4006 42.60 42.64 41.61 41.85 2023-11-28 4007 41.80 42.06 41.58 41.67 2023-11-29 4008 41.67 41.73 40.50 40.75 2023-11-30 4009 40.67 41.07 40.62 40.80 2023-12-01 4010 40.65 41.18 40.30 41.02 2023-12-04 4011 40.85 41.27 40.31 40.38 2023-12-05 4012 40.12 40.17 39.51 39.53 2023-12-06 4013 39.50 39.75 39.29 39.53 2023-12-07 4014 39.36 39.72 38.88 39.34 2023-12-08 4015 39.25 39.63 39.19 39.19 2023-12-11 4016 39.00 39.71 38.56 39.60 2023-12-12 4017 39.42 39.81 39.25 39.79 2023-12-13 4018 39.70 39.70 38.89 38.89 2023-12-14 4019 39.23 39.47 38.69 38.70 2023-12-15 4020 39.15 39.56 38.73 38.79 2023-12-18 4021 38.40 38.85 38.31 38.61 2023-12-19 4022 38.58 38.98 38.40 38.71 2023-12-20 4023 38.71 38.90 38.43 38.45 2023-12-21 4024 38.26 38.97 38.13 38.80 2023-12-22 4025 38.76 39.38 38.68 39.10 2023-12-25 4026 39.05 39.28 38.91 39.18 2023-12-26 4027 39.18 39.18 38.82 39.10 2023-12-27 4028 39.10 39.16 38.68 39.00 2023-12-28 4029 39.00 40.35 38.85 40.26 2023-12-29 4030 40.06 40.44 40.05 40.30 2024-01-02 4031 40.30 40.31 39.46 39.47 2024-01-03 4032 39.47 39.66 39.19 39.40 2024-01-04 4033 39.36 39.58 38.66 38.90 2024-01-05 4034 38.85 39.58 38.68 38.97
-
删除 index 列数据
data.drop(columns=['index'], inplace=True)
open high low close date 2023-11-24 43.00 43.03 42.63 42.69 2023-11-27 42.60 42.64 41.61 41.85 2023-11-28 41.80 42.06 41.58 41.67 2023-11-29 41.67 41.73 40.50 40.75 2023-11-30 40.67 41.07 40.62 40.80 2023-12-01 40.65 41.18 40.30 41.02 2023-12-04 40.85 41.27 40.31 40.38 2023-12-05 40.12 40.17 39.51 39.53 2023-12-06 39.50 39.75 39.29 39.53 2023-12-07 39.36 39.72 38.88 39.34 2023-12-08 39.25 39.63 39.19 39.19 2023-12-11 39.00 39.71 38.56 39.60 2023-12-12 39.42 39.81 39.25 39.79 2023-12-13 39.70 39.70 38.89 38.89 2023-12-14 39.23 39.47 38.69 38.70 2023-12-15 39.15 39.56 38.73 38.79 2023-12-18 38.40 38.85 38.31 38.61 2023-12-19 38.58 38.98 38.40 38.71 2023-12-20 38.71 38.90 38.43 38.45 2023-12-21 38.26 38.97 38.13 38.80 2023-12-22 38.76 39.38 38.68 39.10 2023-12-25 39.05 39.28 38.91 39.18 2023-12-26 39.18 39.18 38.82 39.10 2023-12-27 39.10 39.16 38.68 39.00 2023-12-28 39.00 40.35 38.85 40.26 2023-12-29 40.06 40.44 40.05 40.30 2024-01-02 40.30 40.31 39.46 39.47 2024-01-03 39.47 39.66 39.19 39.40 2024-01-04 39.36 39.58 38.66 38.90 2024-01-05 38.85 39.58 38.68 38.97
-
生成日均线
data['1'] = data.close data['5'] = data.close.rolling(5).mean() data['10'] = data.close.rolling(10).mean()
open high low close 1 5 10 date 2023-11-24 43.00 43.03 42.63 42.69 42.69 NaN NaN 2023-11-27 42.60 42.64 41.61 41.85 41.85 NaN NaN 2023-11-28 41.80 42.06 41.58 41.67 41.67 NaN NaN 2023-11-29 41.67 41.73 40.50 40.75 40.75 NaN NaN 2023-11-30 40.67 41.07 40.62 40.80 40.80 41.552 NaN 2023-12-01 40.65 41.18 40.30 41.02 41.02 41.218 NaN 2023-12-04 40.85 41.27 40.31 40.38 40.38 40.924 NaN 2023-12-05 40.12 40.17 39.51 39.53 39.53 40.496 NaN 2023-12-06 39.50 39.75 39.29 39.53 39.53 40.252 NaN 2023-12-07 39.36 39.72 38.88 39.34 39.34 39.960 40.756 2023-12-08 39.25 39.63 39.19 39.19 39.19 39.594 40.406 2023-12-11 39.00 39.71 38.56 39.60 39.60 39.438 40.181 2023-12-12 39.42 39.81 39.25 39.79 39.79 39.490 39.993 2023-12-13 39.70 39.70 38.89 38.89 38.89 39.362 39.807 2023-12-14 39.23 39.47 38.69 38.70 38.70 39.234 39.597 2023-12-15 39.15 39.56 38.73 38.79 38.79 39.154 39.374 2023-12-18 38.40 38.85 38.31 38.61 38.61 38.956 39.197 2023-12-19 38.58 38.98 38.40 38.71 38.71 38.740 39.115 2023-12-20 38.71 38.90 38.43 38.45 38.45 38.652 39.007 2023-12-21 38.26 38.97 38.13 38.80 38.80 38.672 38.953 2023-12-22 38.76 39.38 38.68 39.10 39.10 38.734 38.944 2023-12-25 39.05 39.28 38.91 39.18 39.18 38.848 38.902 2023-12-26 39.18 39.18 38.82 39.10 39.10 38.926 38.833 2023-12-27 39.10 39.16 38.68 39.00 39.00 39.036 38.844 2023-12-28 39.00 40.35 38.85 40.26 40.26 39.328 39.000 2023-12-29 40.06 40.44 40.05 40.30 40.30 39.568 39.151 2024-01-02 40.30 40.31 39.46 39.47 39.47 39.626 39.237 2024-01-03 39.47 39.66 39.19 39.40 39.40 39.686 39.306 2024-01-04 39.36 39.58 38.66 38.90 38.90 39.666 39.351 2024-01-05 38.85 39.58 38.68 38.97 38.97 39.408 39.368
-
生成买卖信号
# 生成买卖信号 data['Signal'] = 0 # 短期均线上穿长期均线,产生买入信号 data.loc[data['5'] > data['10'], 'Signal'] = 1 # 短期均线下穿长期均线,产生卖出信号 data.loc[data['5'] < data['10'], 'Signal'] = -1
open high low close 1 5 10 Signal date 2023-11-24 43.00 43.03 42.63 42.69 42.69 NaN NaN 0 2023-11-27 42.60 42.64 41.61 41.85 41.85 NaN NaN 0 2023-11-28 41.80 42.06 41.58 41.67 41.67 NaN NaN 0 2023-11-29 41.67 41.73 40.50 40.75 40.75 NaN NaN 0 2023-11-30 40.67 41.07 40.62 40.80 40.80 41.552 NaN 0 2023-12-01 40.65 41.18 40.30 41.02 41.02 41.218 NaN 0 2023-12-04 40.85 41.27 40.31 40.38 40.38 40.924 NaN 0 2023-12-05 40.12 40.17 39.51 39.53 39.53 40.496 NaN 0 2023-12-06 39.50 39.75 39.29 39.53 39.53 40.252 NaN 0 2023-12-07 39.36 39.72 38.88 39.34 39.34 39.960 40.756 -1 2023-12-08 39.25 39.63 39.19 39.19 39.19 39.594 40.406 -1 2023-12-11 39.00 39.71 38.56 39.60 39.60 39.438 40.181 -1 2023-12-12 39.42 39.81 39.25 39.79 39.79 39.490 39.993 -1 2023-12-13 39.70 39.70 38.89 38.89 38.89 39.362 39.807 -1 2023-12-14 39.23 39.47 38.69 38.70 38.70 39.234 39.597 -1 2023-12-15 39.15 39.56 38.73 38.79 38.79 39.154 39.374 -1 2023-12-18 38.40 38.85 38.31 38.61 38.61 38.956 39.197 -1 2023-12-19 38.58 38.98 38.40 38.71 38.71 38.740 39.115 -1 2023-12-20 38.71 38.90 38.43 38.45 38.45 38.652 39.007 -1 2023-12-21 38.26 38.97 38.13 38.80 38.80 38.672 38.953 -1 2023-12-22 38.76 39.38 38.68 39.10 39.10 38.734 38.944 -1 2023-12-25 39.05 39.28 38.91 39.18 39.18 38.848 38.902 -1 2023-12-26 39.18 39.18 38.82 39.10 39.10 38.926 38.833 1 2023-12-27 39.10 39.16 38.68 39.00 39.00 39.036 38.844 1 2023-12-28 39.00 40.35 38.85 40.26 40.26 39.328 39.000 1 2023-12-29 40.06 40.44 40.05 40.30 40.30 39.568 39.151 1 2024-01-02 40.30 40.31 39.46 39.47 39.47 39.626 39.237 1 2024-01-03 39.47 39.66 39.19 39.40 39.40 39.686 39.306 1 2024-01-04 39.36 39.58 38.66 38.90 38.90 39.666 39.351 1 2024-01-05 38.85 39.58 38.68 38.97 38.97 39.408 39.368 1
-
设置画板
plt.figure(figsize=(10, 6))
-
绘制均线
plt.plot(data['1'], alpha=0.5, label='M1') plt.plot(data['5'], alpha=0.5, label='M5') plt.plot(data['10'], alpha=0.5, label='M10')
-
绘制买卖信号
plt.scatter(data[data['Signal'] == 1].index, data[data['Signal'] == 1]['5'], marker='^', color='r', label='Buy Signal') plt.scatter(data[data['Signal'] == -1].index, data[data['Signal'] == -1]['10'], marker='v', color='g', label='Sell Signal')
-
设置 X Y 轴信息
plt.xticks(rotation=45, size=8) plt.title(f"{symbol} 中国平安股价走势") plt.xlabel("日期") plt.ylabel("价格")
-
绘制 label 和显示图表
plt.legend() plt.subplots_adjust(bottom=0.25) plt.show(block=True)
股票中国平台的 K线绘制如下:
直面挑战,躬身入局