量化投资02—手绘K线

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]:

dateopenhighlowclosevolume02019-12-311000.0001000.0001000.0001000.00015162053312020-01-021005.6171019.9101005.6171019.72321938527622020-01-031021.6691029.0711018.6811024.48221605834832020-01-061019.9991040.1401019.9991027.27726630118142020-01-071025.6631043.0831019.8581043.083267592167

In [275]:

设置索引,否则无法正确绘图
df.index = pd.to_datetime(df.date)
df.sort_index()
df.head()

Out[275]:

dateopenhighlowclosevolumedate2019-12-312019-12-311000.0001000.0001000.0001000.0001516205332020-01-022020-01-021005.6171019.9101005.6171019.7232193852762020-01-032020-01-031021.6691029.0711018.6811024.4822160583482020-01-062020-01-061019.9991040.1401019.9991027.2772663011812020-01-072020-01-071025.6631043.0831019.8581043.083267592167

In [276]:

#计算10日均线和50日均线(收盘价)
df['ma10'] = df['close'].rolling(10).mean()
df['ma50'] = df['close'].rolling(50).mean()
df.head(60)

Out[276]:

dateopenhighlowclosevolumema10ma50date2019-12-312019-12-311000.0001000.0001000.0001000.000151620533NaNNaN2020-01-022020-01-021005.6171019.9101005.6171019.723219385276NaNNaN2020-01-032020-01-031021.6691029.0711018.6811024.482216058348NaNNaN2020-01-062020-01-061019.9991040.1401019.9991027.277266301181NaNNaN2020-01-072020-01-071025.6631043.0831019.8581043.083267592167NaNNaN2020-01-082020-01-081035.8361059.4361034.5131051.160320762650NaNNaN2020-01-092020-01-091060.5801081.4041060.5801078.312284342095NaNNaN2020-01-102020-01-101082.3101088.5431076.4381076.438251547487NaNNaN2020-01-132020-01-131076.7621105.8481069.7791105.848246625919NaNNaN2020-01-142020-01-141107.7761133.8991107.7761118.3063262463821054.4629NaN2020-01-152020-01-151113.2521120.3461105.5751114.7662420464211065.9395NaN2020-01-162020-01-161116.5081161.4771116.5081154.3033085287991079.3975NaN2020-01-172020-01-171159.3721252.5561159.3721238.6645873596711100.8157NaN2020-01-202020-01-201239.4391270.3591208.6901240.5095202591971122.1389NaN2020-01-212020-01-211233.6461247.8411194.7971195.0144206788731137.3320NaN2020-01-222020-01-221178.7841247.6101157.2901244.5254794740041156.6685NaN2020-01-232020-01-231219.2331262.4361177.0941202.1024425934371169.0475NaN2020-02-032020-02-031018.2761082.7891015.5971030.5505466543161164.4587NaN2020-02-042020-02-041049.9561153.2321049.9561153.2325286206051169.1971NaN2020-02-052020-02-051166.9731240.3681166.9731203.6135860213391177.7278NaN2020-02-062020-02-061191.7311251.8431191.7311238.7575589981661190.1269NaN2020-02-072020-02-071240.0381307.7571234.2341307.7575664784581205.4723NaN2020-02-102020-02-101310.5261336.5261293.4301322.2505932906941213.8309NaN2020-02-112020-02-111318.4201318.4201266.2141283.5725037530861218.1372NaN2020-02-122020-02-121274.7961360.5491274.7961348.7024763905791233.5060NaN2020-02-132020-02-131360.1291409.6841353.0451360.7096144055011245.1244NaN2020-02-142020-02-141359.3731382.9551305.9741327.2245310166421257.6366NaN2020-02-172020-02-171332.9651381.5531332.9651381.5534969888921292.7369NaN2020-02-182020-02-181381.5831404.7121372.5261399.3844905608711317.3521NaN2020-02-192020-02-191398.7811412.1691373.4441373.4444783190391334.3352NaN2020-02-202020-02-201377.7561390.0191321.7341348.3384222266191345.2933NaN2020-02-212020-02-211337.5571373.6671337.5571346.8634475454981349.2039NaN2020-02-242020-02-241341.9881359.2271328.7471351.7104199988201352.1499NaN2020-02-252020-02-251312.4791371.5761312.4791367.3684611598761360.5295NaN2020-02-262020-02-261348.0751361.0061286.3451288.2425015814021354.4835NaN2020-02-272020-02-271293.1871293.1871251.4511264.9415032823381344.9067NaN2020-02-282020-02-281206.8581231.0941187.0961191.0555132083971331.2898NaN2020-03-022020-03-021202.9591259.0761194.9371252.6244979736011318.3969NaN2020-03-032020-03-031278.2691332.8351272.0631285.0345122939271306.9619NaN2020-03-042020-03-041268.8341277.6861226.8021255.7233819524081295.1898NaN2020-03-052020-03-051277.7061277.7061246.7141264.7884312615621286.8348NaN2020-03-062020-03-061246.0961255.0741237.6231243.3733261213401276.4858NaN2020-03-092020-03-091216.0351223.2461171.8591171.8593872702671258.5007NaN2020-03-102020-03-101148.3971216.3281135.2091216.3284064920401243.3967NaN2020-03-112020-03-111218.5161233.4261184.7101184.7104248509271233.0435NaN2020-03-122020-03-121161.9221177.3601151.7851167.0652805383691223.2559NaN2020-03-132020-03-131103.9561187.7011103.9561175.2823383309861221.6786NaN2020-03-162020-03-161188.0781200.4641115.0841115.0843181187081207.9246NaN2020-03-172020-03-171132.9491141.3881079.8381131.5472972677851192.5759NaN2020-03-182020-03-181145.3021173.2341115.0191115.0192999560331178.50551208.044242020-03-192020-03-191116.2631133.6951091.2401128.9732785031831164.92401210.623702020-03-202020-03-201140.0921148.0901114.2911127.3692355961031153.32361212.776622020-03-232020-03-231092.5011096.4001037.5281039.8842830474411140.12611213.084662020-03-242020-03-241067.0141071.3401020.2491060.8432368341771124.57761213.755982020-03-252020-03-251097.3091110.0991094.1631101.3192496325761116.23851214.920702020-03-262020-03-261093.3311111.4841091.0041095.3382113778991109.06581215.804262020-03-272020-03-271109.5421110.5371082.9321084.5052292012531099.98811215.928122020-03-302020-03-301062.8991062.8991021.4991035.8272260875621092.06241215.115902020-03-312020-03-311048.7361055.2681027.2241029.7491703671651081.88261213.593922020-04-012020-04-011026.5751060.7081026.5751037.9271750560791074.17341211.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]:

dateopenhighlowclosevolumema10ma50date2020-03-202020-03-201140.0921148.0901114.2911127.3692355961031153.32361212.776622020-03-232020-03-231092.5011096.4001037.5281039.8842830474411140.12611213.084662020-03-242020-03-241067.0141071.3401020.2491060.8432368341771124.57761213.755982020-03-252020-03-251097.3091110.0991094.1631101.3192496325761116.23851214.920702020-03-262020-03-261093.3311111.4841091.0041095.3382113778991109.06581215.80426

In [279]:

画K线,蜡烛图
mpf.plot(df,type='candle',style=my_style,ylabel='price(RMB)')

量化投资02---手绘K线

In [280]:

画带成交量的蜡烛图
mpf.plot(df,type='candle',style=my_style,ylabel='price(RMB)',volume=True,ylabel_lower="vol")

量化投资02---手绘K线

In [281]:

df[["ma10","ma50"]]

Out[281]:

ma10ma50date2020-03-201153.32361212.776622020-03-231140.12611213.084662020-03-241124.57761213.755982020-03-251116.23851214.920702020-03-261109.06581215.80426………2020-12-081398.02711416.625822020-12-091395.69451416.124862020-12-101393.18191414.850462020-12-111390.70111414.211162020-12-141389.65921414.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)

量化投资02---手绘K线

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]:

ma10ma50date2020-03-201153.32361212.776622020-03-231140.12611213.084662020-03-241124.57761213.755982020-03-251116.23851214.920702020-03-261109.06581215.80426………2020-12-081398.02711416.625822020-12-091395.69451416.124862020-12-101393.18191414.850462020-12-111390.70111414.211162020-12-141389.65921414.43194

180 rows × 2 columns

In [288]:

g.to_frame(name='golden')

Out[288]:

goldendate2020-03-20NaN2020-03-23NaN2020-03-24NaN2020-03-25NaN2020-03-26NaN……2020-12-08NaN2020-12-09NaN2020-12-10NaN2020-12-11NaN2020-12-14NaN

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)

量化投资02---手绘K线

In [ ]:

undefined

In [ ]:

undefined

Original: https://blog.csdn.net/turui/article/details/127179175
Author: turui
Title: 量化投资02—手绘K线

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/677747/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球