import akshare as ak import pandas as pd import mplfinance as mpf #从网上获取某只股票的历史行情代码 df = ak.stock_zh_index_daily(symbol="sh000688") #将本地存储的数据读到DataFrame中 #df = pd.read_csv('./sh000688.csv') df.head()
Out[274]:
date | open | high | low | close | volume | |
---|---|---|---|---|---|---|
0 | 2019-12-31 | 1000.000 | 1000.000 | 1000.000 | 1000.000 | 151620533 |
1 | 2020-01-02 | 1005.617 | 1019.910 | 1005.617 | 1019.723 | 219385276 |
2 | 2020-01-03 | 1021.669 | 1029.071 | 1018.681 | 1024.482 | 216058348 |
3 | 2020-01-06 | 1019.999 | 1040.140 | 1019.999 | 1027.277 | 266301181 |
4 | 2020-01-07 | 1025.663 | 1043.083 | 1019.858 | 1043.083 | 267592167 |
In [275]:
# 设置索引,否则无法正确绘图 df.index = pd.to_datetime(df.date) df.sort_index() df.head()
Out[275]:
date | open | high | low | close | volume | |
---|---|---|---|---|---|---|
date | ||||||
2019-12-31 | 2019-12-31 | 1000.000 | 1000.000 | 1000.000 | 1000.000 | 151620533 |
2020-01-02 | 2020-01-02 | 1005.617 | 1019.910 | 1005.617 | 1019.723 | 219385276 |
2020-01-03 | 2020-01-03 | 1021.669 | 1029.071 | 1018.681 | 1024.482 | 216058348 |
2020-01-06 | 2020-01-06 | 1019.999 | 1040.140 | 1019.999 | 1027.277 | 266301181 |
2020-01-07 | 2020-01-07 | 1025.663 | 1043.083 | 1019.858 | 1043.083 | 267592167 |
In [276]:
#计算10日均线和50日均线(收盘价) df['ma10'] = df['close'].rolling(10).mean() df['ma50'] = df['close'].rolling(50).mean() df.head(60)
Out[276]:
date | open | high | low | close | volume | ma10 | ma50 | |
---|---|---|---|---|---|---|---|---|
date | ||||||||
2019-12-31 | 2019-12-31 | 1000.000 | 1000.000 | 1000.000 | 1000.000 | 151620533 | NaN | NaN |
2020-01-02 | 2020-01-02 | 1005.617 | 1019.910 | 1005.617 | 1019.723 | 219385276 | NaN | NaN |
2020-01-03 | 2020-01-03 | 1021.669 | 1029.071 | 1018.681 | 1024.482 | 216058348 | NaN | NaN |
2020-01-06 | 2020-01-06 | 1019.999 | 1040.140 | 1019.999 | 1027.277 | 266301181 | NaN | NaN |
2020-01-07 | 2020-01-07 | 1025.663 | 1043.083 | 1019.858 | 1043.083 | 267592167 | NaN | NaN |
2020-01-08 | 2020-01-08 | 1035.836 | 1059.436 | 1034.513 | 1051.160 | 320762650 | NaN | NaN |
2020-01-09 | 2020-01-09 | 1060.580 | 1081.404 | 1060.580 | 1078.312 | 284342095 | NaN | NaN |
2020-01-10 | 2020-01-10 | 1082.310 | 1088.543 | 1076.438 | 1076.438 | 251547487 | NaN | NaN |
2020-01-13 | 2020-01-13 | 1076.762 | 1105.848 | 1069.779 | 1105.848 | 246625919 | NaN | NaN |
2020-01-14 | 2020-01-14 | 1107.776 | 1133.899 | 1107.776 | 1118.306 | 326246382 | 1054.4629 | NaN |
2020-01-15 | 2020-01-15 | 1113.252 | 1120.346 | 1105.575 | 1114.766 | 242046421 | 1065.9395 | NaN |
2020-01-16 | 2020-01-16 | 1116.508 | 1161.477 | 1116.508 | 1154.303 | 308528799 | 1079.3975 | NaN |
2020-01-17 | 2020-01-17 | 1159.372 | 1252.556 | 1159.372 | 1238.664 | 587359671 | 1100.8157 | NaN |
2020-01-20 | 2020-01-20 | 1239.439 | 1270.359 | 1208.690 | 1240.509 | 520259197 | 1122.1389 | NaN |
2020-01-21 | 2020-01-21 | 1233.646 | 1247.841 | 1194.797 | 1195.014 | 420678873 | 1137.3320 | NaN |
2020-01-22 | 2020-01-22 | 1178.784 | 1247.610 | 1157.290 | 1244.525 | 479474004 | 1156.6685 | NaN |
2020-01-23 | 2020-01-23 | 1219.233 | 1262.436 | 1177.094 | 1202.102 | 442593437 | 1169.0475 | NaN |
2020-02-03 | 2020-02-03 | 1018.276 | 1082.789 | 1015.597 | 1030.550 | 546654316 | 1164.4587 | NaN |
2020-02-04 | 2020-02-04 | 1049.956 | 1153.232 | 1049.956 | 1153.232 | 528620605 | 1169.1971 | NaN |
2020-02-05 | 2020-02-05 | 1166.973 | 1240.368 | 1166.973 | 1203.613 | 586021339 | 1177.7278 | NaN |
2020-02-06 | 2020-02-06 | 1191.731 | 1251.843 | 1191.731 | 1238.757 | 558998166 | 1190.1269 | NaN |
2020-02-07 | 2020-02-07 | 1240.038 | 1307.757 | 1234.234 | 1307.757 | 566478458 | 1205.4723 | NaN |
2020-02-10 | 2020-02-10 | 1310.526 | 1336.526 | 1293.430 | 1322.250 | 593290694 | 1213.8309 | NaN |
2020-02-11 | 2020-02-11 | 1318.420 | 1318.420 | 1266.214 | 1283.572 | 503753086 | 1218.1372 | NaN |
2020-02-12 | 2020-02-12 | 1274.796 | 1360.549 | 1274.796 | 1348.702 | 476390579 | 1233.5060 | NaN |
2020-02-13 | 2020-02-13 | 1360.129 | 1409.684 | 1353.045 | 1360.709 | 614405501 | 1245.1244 | NaN |
2020-02-14 | 2020-02-14 | 1359.373 | 1382.955 | 1305.974 | 1327.224 | 531016642 | 1257.6366 | NaN |
2020-02-17 | 2020-02-17 | 1332.965 | 1381.553 | 1332.965 | 1381.553 | 496988892 | 1292.7369 | NaN |
2020-02-18 | 2020-02-18 | 1381.583 | 1404.712 | 1372.526 | 1399.384 | 490560871 | 1317.3521 | NaN |
2020-02-19 | 2020-02-19 | 1398.781 | 1412.169 | 1373.444 | 1373.444 | 478319039 | 1334.3352 | NaN |
2020-02-20 | 2020-02-20 | 1377.756 | 1390.019 | 1321.734 | 1348.338 | 422226619 | 1345.2933 | NaN |
2020-02-21 | 2020-02-21 | 1337.557 | 1373.667 | 1337.557 | 1346.863 | 447545498 | 1349.2039 | NaN |
2020-02-24 | 2020-02-24 | 1341.988 | 1359.227 | 1328.747 | 1351.710 | 419998820 | 1352.1499 | NaN |
2020-02-25 | 2020-02-25 | 1312.479 | 1371.576 | 1312.479 | 1367.368 | 461159876 | 1360.5295 | NaN |
2020-02-26 | 2020-02-26 | 1348.075 | 1361.006 | 1286.345 | 1288.242 | 501581402 | 1354.4835 | NaN |
2020-02-27 | 2020-02-27 | 1293.187 | 1293.187 | 1251.451 | 1264.941 | 503282338 | 1344.9067 | NaN |
2020-02-28 | 2020-02-28 | 1206.858 | 1231.094 | 1187.096 | 1191.055 | 513208397 | 1331.2898 | NaN |
2020-03-02 | 2020-03-02 | 1202.959 | 1259.076 | 1194.937 | 1252.624 | 497973601 | 1318.3969 | NaN |
2020-03-03 | 2020-03-03 | 1278.269 | 1332.835 | 1272.063 | 1285.034 | 512293927 | 1306.9619 | NaN |
2020-03-04 | 2020-03-04 | 1268.834 | 1277.686 | 1226.802 | 1255.723 | 381952408 | 1295.1898 | NaN |
2020-03-05 | 2020-03-05 | 1277.706 | 1277.706 | 1246.714 | 1264.788 | 431261562 | 1286.8348 | NaN |
2020-03-06 | 2020-03-06 | 1246.096 | 1255.074 | 1237.623 | 1243.373 | 326121340 | 1276.4858 | NaN |
2020-03-09 | 2020-03-09 | 1216.035 | 1223.246 | 1171.859 | 1171.859 | 387270267 | 1258.5007 | NaN |
2020-03-10 | 2020-03-10 | 1148.397 | 1216.328 | 1135.209 | 1216.328 | 406492040 | 1243.3967 | NaN |
2020-03-11 | 2020-03-11 | 1218.516 | 1233.426 | 1184.710 | 1184.710 | 424850927 | 1233.0435 | NaN |
2020-03-12 | 2020-03-12 | 1161.922 | 1177.360 | 1151.785 | 1167.065 | 280538369 | 1223.2559 | NaN |
2020-03-13 | 2020-03-13 | 1103.956 | 1187.701 | 1103.956 | 1175.282 | 338330986 | 1221.6786 | NaN |
2020-03-16 | 2020-03-16 | 1188.078 | 1200.464 | 1115.084 | 1115.084 | 318118708 | 1207.9246 | NaN |
2020-03-17 | 2020-03-17 | 1132.949 | 1141.388 | 1079.838 | 1131.547 | 297267785 | 1192.5759 | NaN |
2020-03-18 | 2020-03-18 | 1145.302 | 1173.234 | 1115.019 | 1115.019 | 299956033 | 1178.5055 | 1208.04424 |
2020-03-19 | 2020-03-19 | 1116.263 | 1133.695 | 1091.240 | 1128.973 | 278503183 | 1164.9240 | 1210.62370 |
2020-03-20 | 2020-03-20 | 1140.092 | 1148.090 | 1114.291 | 1127.369 | 235596103 | 1153.3236 | 1212.77662 |
2020-03-23 | 2020-03-23 | 1092.501 | 1096.400 | 1037.528 | 1039.884 | 283047441 | 1140.1261 | 1213.08466 |
2020-03-24 | 2020-03-24 | 1067.014 | 1071.340 | 1020.249 | 1060.843 | 236834177 | 1124.5776 | 1213.75598 |
2020-03-25 | 2020-03-25 | 1097.309 | 1110.099 | 1094.163 | 1101.319 | 249632576 | 1116.2385 | 1214.92070 |
2020-03-26 | 2020-03-26 | 1093.331 | 1111.484 | 1091.004 | 1095.338 | 211377899 | 1109.0658 | 1215.80426 |
2020-03-27 | 2020-03-27 | 1109.542 | 1110.537 | 1082.932 | 1084.505 | 229201253 | 1099.9881 | 1215.92812 |
2020-03-30 | 2020-03-30 | 1062.899 | 1062.899 | 1021.499 | 1035.827 | 226087562 | 1092.0624 | 1215.11590 |
2020-03-31 | 2020-03-31 | 1048.736 | 1055.268 | 1027.224 | 1029.749 | 170367165 | 1081.8826 | 1213.59392 |
2020-04-01 | 2020-04-01 | 1026.575 | 1060.708 | 1026.575 | 1037.927 | 175056079 | 1074.1734 | 1211.98634 |
In [277]:
# 设置mplfinance的蜡烛颜色,up为阳线颜色,down为阴线颜色 my_color = mpf.make_marketcolors(up='r', down='g', edge='inherit', wick='inherit', volume='inherit') # 设置图表的背景色 my_style = mpf.make_mpf_style(marketcolors=my_color, figcolor='(0.82, 0.83, 0.85)', gridcolor='(0.82, 0.83, 0.85)')
In [278]:
# 取半年的数据 df = df[51:51+180] df.head()
Out[278]:
date | open | high | low | close | volume | ma10 | ma50 | |
---|---|---|---|---|---|---|---|---|
date | ||||||||
2020-03-20 | 2020-03-20 | 1140.092 | 1148.090 | 1114.291 | 1127.369 | 235596103 | 1153.3236 | 1212.77662 |
2020-03-23 | 2020-03-23 | 1092.501 | 1096.400 | 1037.528 | 1039.884 | 283047441 | 1140.1261 | 1213.08466 |
2020-03-24 | 2020-03-24 | 1067.014 | 1071.340 | 1020.249 | 1060.843 | 236834177 | 1124.5776 | 1213.75598 |
2020-03-25 | 2020-03-25 | 1097.309 | 1110.099 | 1094.163 | 1101.319 | 249632576 | 1116.2385 | 1214.92070 |
2020-03-26 | 2020-03-26 | 1093.331 | 1111.484 | 1091.004 | 1095.338 | 211377899 | 1109.0658 | 1215.80426 |
In [279]:
# 画K线,蜡烛图 mpf.plot(df,type='candle',style=my_style,ylabel='price(RMB)')
In [280]:
# 画带成交量的蜡烛图 mpf.plot(df,type='candle',style=my_style,ylabel='price(RMB)',volume=True,ylabel_lower="vol")
In [281]:
df[["ma10","ma50"]]
Out[281]:
ma10 | ma50 | |
---|---|---|
date | ||
2020-03-20 | 1153.3236 | 1212.77662 |
2020-03-23 | 1140.1261 | 1213.08466 |
2020-03-24 | 1124.5776 | 1213.75598 |
2020-03-25 | 1116.2385 | 1214.92070 |
2020-03-26 | 1109.0658 | 1215.80426 |
... | ... | ... |
2020-12-08 | 1398.0271 | 1416.62582 |
2020-12-09 | 1395.6945 | 1416.12486 |
2020-12-10 | 1393.1819 | 1414.85046 |
2020-12-11 | 1390.7011 | 1414.21116 |
2020-12-14 | 1389.6592 | 1414.43194 |
180 rows × 2 columns
In [282]:
plot_add = [mpf.make_addplot(df[["ma10","ma50"]])]
In [283]:
# 画MA10 MA50 mpf.plot(df,type='candle',style=my_style,ylabel='Price(RMB)',volume=True,ylabel_lower="Volumn",addplot=plot_add)
In [284]:
#ma10 = df.iloc[:,7] ma10 = df['ma10'] ma50 = df['ma50'] s1 = ma10 < ma50 s2 = ma10 > ma50 death_ex = s1 & s2.shift(1) #判断死叉的条件,一组BOOL值 death_date = df.loc[death_ex].index golden_ex = -(s1 | s2.shift(1)) #判断金叉的条件,一组BOOL值 golden_date = df.loc[golden_ex].index death_date
Out[284]:
DatetimeIndex(['2020-08-24', '2020-10-27', '2020-11-30'], dtype='datetime64[ns]', name='date', freq=None)
In [285]:
g = pd.Series(data=np.nan,index=df.index) g[golden_date] = df.loc[golden_date]['close'] g[golden_date]
Out[285]:
date 2020-05-07 1224.105 2020-10-19 1412.148 2020-11-05 1475.375 dtype: float64
In [286]:
d = pd.Series(data=np.nan,index=df.index) d[death_date] = df.loc[death_date]['close'] d
Out[286]:
date 2020-03-20 NaN 2020-03-23 NaN 2020-03-24 NaN 2020-03-25 NaN 2020-03-26 NaN .. 2020-12-08 NaN 2020-12-09 NaN 2020-12-10 NaN 2020-12-11 NaN 2020-12-14 NaN Length: 180, dtype: float64
In [287]:
df[['ma10','ma50']]
Out[287]:
ma10 | ma50 | |
---|---|---|
date | ||
2020-03-20 | 1153.3236 | 1212.77662 |
2020-03-23 | 1140.1261 | 1213.08466 |
2020-03-24 | 1124.5776 | 1213.75598 |
2020-03-25 | 1116.2385 | 1214.92070 |
2020-03-26 | 1109.0658 | 1215.80426 |
... | ... | ... |
2020-12-08 | 1398.0271 | 1416.62582 |
2020-12-09 | 1395.6945 | 1416.12486 |
2020-12-10 | 1393.1819 | 1414.85046 |
2020-12-11 | 1390.7011 | 1414.21116 |
2020-12-14 | 1389.6592 | 1414.43194 |
180 rows × 2 columns
In [288]:
g.to_frame(name='golden')
Out[288]:
golden | |
---|---|
date | |
2020-03-20 | NaN |
2020-03-23 | NaN |
2020-03-24 | NaN |
2020-03-25 | NaN |
2020-03-26 | NaN |
... | ... |
2020-12-08 | NaN |
2020-12-09 | NaN |
2020-12-10 | NaN |
2020-12-11 | NaN |
2020-12-14 | NaN |
180 rows × 1 columns
In [289]:
g.to_frame(name='golden').loc['2020-05-07']
Out[289]:
golden 1224.105 Name: 2020-05-07 00:00:00, dtype: float64
In [290]:
plot_add=[mpf.make_addplot(df[['ma10','ma50']]), mpf.make_addplot(g.to_frame(name='golden'),type='scatter',markersize=100,marker='^',color='r'), mpf.make_addplot(d.to_frame(name='death'),type='scatter',markersize=100,marker='v',color='b')]
In [291]:
mpf.plot(df,type='candle',style=my_style,ylabel='Price(RMB)',volume=True,ylabel_lower="Volumn",addplot=plot_add)
In [ ]:
In [ ]: