双均线策略制定

需求:双均线策略制定

  • 使用tushare包获取某股票的历史行情数据
  • 计算该股票历史数据的5日均线和60日均线
    • 什么是均线:
      • 对于每一个交易日,都可以计算出前N天的移动平均值,然后把这些移动平均值连起来,成为一条线,就叫做N日移动平均线。移动平均线常用线有5天、10天、30天、60天、120天和240天的指标。
      • 5天和10天的是短线操作的参照指标,称作日均线指标;
      • 30天和60天的是中期均线指标,称作季均线指标;
      • 120天和240天的是长期均线指标,称作年均线指标。
    • 均线计算方法:MA=(C1+C2+C3+…+Cn)/N C:某日收盘价;N:移动平均周期(天数)
1
2
3
4
import tushare as ts
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
1
2
3
4
5
6
7
8
"""
df = ts.get_k_data(code='600519',start='2015-01-01')
df.to_csv('moutai.csv')
"""
df = pd.read_csv('moutai.csv').drop(labels='Unnamed: 0',axis=1)
df['date'] = pd.to_datetime(df['date'])
df.set_index('date',inplace=True)
df.head()

open close high low volume code
date
2015-01-05 161.056 172.013 173.474 160.266 94515.0 600519
2015-01-06 169.872 168.029 172.047 166.492 55020.0 600519
2015-01-07 166.509 163.876 169.448 161.370 54797.0 600519
2015-01-08 164.776 162.874 165.218 161.498 40525.0 600519
2015-01-09 161.719 161.642 166.280 161.472 53982.0 600519
1
2
3
4
5
6
7
8
9
ma5 = df['close'].rolling(5).mean()   # rolling滑动窗口尺寸为5
ma10 = df['close'].rolling(10).mean()
ma30 = df['close'].rolling(30).mean()
ma60 = df['close'].rolling(60).mean()
ma120 = df['close'].rolling(120).mean()
ma240 = df['close'].rolling(240).mean()
%matplotlib inline
plt.plot(ma5[110:330])
plt.plot(ma30[110:330])
[<matplotlib.lines.Line2D at 0x2a1aa5f9eb8>]

2325vj.jpg

  • 分析输出所有金叉日期和死叉日期
    • 股票分析技术中的金叉和死叉,可以简单解释为:
      • 1.分析指标中的两根线,一根为短时间内的指标线,另一根为较长时间的指标线。
      • 2.如果短时间的指标线方向拐头向上,并且穿过了较长时间的指标线,这种状态叫“金叉”;
      • 3.如果短时间的指标线方向拐头向下,并且穿过了较长时间的指标线,这种状态叫“死叉”;
      • 4.一般情况下,出现金叉后,操作趋向买入;死叉则趋向卖出。当然,金叉和死叉只是分析指标之一,要和其他很多指标配合使用,才能增加操作的准确性。
        参考讲解:https://www.bilibili.com/video/BV1Z64y1S7y3?p=15
1
2
3
4
5
6
7
8
9
10
11
ma5 = ma5[30:]
ma30 = ma30[30:]
df = df[30:]
s1 = ma5 < ma30
s2 = ma5 > ma30
death_ex = s1 & s2.shift(1) # 判定死叉条件
death_date = df.loc[death_ex].index # death_ex为Boolean型,用作索引直接提取True的对应行数据,再取行索引号即死叉对应日期
print('死叉日期:', death_date)
golden_ex = ~(s1|s2.shift(1))
golden_date = df.loc[golden_ex].index
print('金叉日期:', golden_date)
死叉日期: DatetimeIndex(['2015-06-17', '2015-07-17', '2015-09-28', '2015-11-26',
               '2015-12-10', '2016-01-05', '2016-08-05', '2016-08-18',
               '2016-11-21', '2017-07-06', '2017-09-08', '2017-11-29',
               '2018-02-05', '2018-03-27', '2018-06-28', '2018-07-23',
               '2018-07-31', '2018-10-15', '2018-12-25', '2019-05-10',
               '2019-07-19', '2019-11-28', '2020-01-03', '2020-02-28',
               '2020-03-18', '2020-08-10', '2020-09-21', '2020-10-27',
               '2021-03-01', '2021-04-15'],
              dtype='datetime64[ns]', name='date', freq=None)
金叉日期: DatetimeIndex(['2015-02-16', '2015-07-15', '2015-09-16', '2015-10-09',
               '2015-12-03', '2015-12-21', '2016-02-22', '2016-08-11',
               '2016-10-13', '2016-11-25', '2017-07-24', '2017-09-18',
               '2017-12-15', '2018-03-16', '2018-05-09', '2018-07-18',
               '2018-07-25', '2018-09-20', '2018-12-04', '2019-01-03',
               '2019-06-14', '2019-08-13', '2020-01-02', '2020-02-19',
               '2020-03-03', '2020-04-02', '2020-08-19', '2020-10-14',
               '2020-11-05', '2021-04-02', '2021-04-16'],
              dtype='datetime64[ns]', name='date', freq=None)
  • 如果从2017年1月1日开始,初始资金为10W元,金叉尽量买入,死叉全部卖出,则迄今为止炒股收益率如何?
    • 1.买卖股票的单价使用开盘价
    • 2.买卖股票的时机
    • 3.最终手里会有剩余的股票没有卖出去(如果最有一次是金叉即买入没有卖出则出现剩余,需估量其价值,剩余股票单价使用最后一天的收盘价计算)
1
2
3
4
l1 = pd.Series(1,index=golden_date)    # 1作为金叉标识
l2 = pd.Series(0,index=death_date) # 0作为死叉标识
l = l1.append(l2)
print(l)
date
2015-02-16    1
2015-07-15    1
2015-09-16    1
2015-10-09    1
2015-12-03    1
             ..
2020-08-10    0
2020-09-21    0
2020-10-27    0
2021-03-01    0
2021-04-15    0
Length: 61, dtype: int64
1
2
3
4
l = l.sort_index()  # 根据日期排序即出现金叉死叉交错
print(l)
l = l['2017':'2021'] # 根据需求切出2017年至今的金叉死叉
print(l)
date
2015-02-16    1
2015-06-17    0
2015-07-15    1
2015-07-17    0
2015-09-16    1
             ..
2020-11-05    1
2021-03-01    0
2021-04-02    1
2021-04-15    0
2021-04-16    1
Length: 61, dtype: int64
date
2017-07-06    0
2017-07-24    1
2017-09-08    0
2017-09-18    1
2017-11-29    0
2017-12-15    1
2018-02-05    0
2018-03-16    1
2018-03-27    0
2018-05-09    1
2018-06-28    0
2018-07-18    1
2018-07-23    0
2018-07-25    1
2018-07-31    0
2018-09-20    1
2018-10-15    0
2018-12-04    1
2018-12-25    0
2019-01-03    1
2019-05-10    0
2019-06-14    1
2019-07-19    0
2019-08-13    1
2019-11-28    0
2020-01-02    1
2020-01-03    0
2020-02-19    1
2020-02-28    0
2020-03-03    1
2020-03-18    0
2020-04-02    1
2020-08-10    0
2020-08-19    1
2020-09-21    0
2020-10-14    1
2020-10-27    0
2020-11-05    1
2021-03-01    0
2021-04-02    1
2021-04-15    0
2021-04-16    1
dtype: int64
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
first_money = 100000    # 本金不变
money = first_money # 可变,买股票花的钱和卖股票赚的钱都在该变量中操作
hold = 0 # 持有股票的数量(股数:100股=1手)
for i in range(0,len(l)):
if l[i] == 1: # 金叉时间,基于10W单价尽多买入
time = l.index[i] # 获取金叉时间
p = df.loc[time]['open'] # 取出该时间对应的开盘价,即当前单价
hand_count = money // (p*100) # 10W能买入多少手
hold = hand_count * 100 # 股数
money -= (hold*p) # 将买股票的钱从money中减去
else: # 死叉时间,将买入的股票卖出去
death_time = l.index[i]
p_death = df.loc[death_time]['open'] # 卖股票的单价
money += (p_death * hold) # 卖股票的收入加进money中
hold = 0 # 股票卖出后,注意hold要清空
# 判定最后一次是金叉还是死叉可以判断l[i]最后一位是1还是0,但也可以使用hold判断,hold=0则最后一次为死叉反之金叉。
last_money = hold * df['close'][-1] # 剩余股票的价值,若为金叉有剩余价值若为死叉该值为0,不用再进行判断直接加上即可。
# 总收益
final_money = money + last_money - first_money
print(final_money)
198082.0
  • 金融量化实用平台:聚宽joinQuant
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2019-2022 Woody
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信