码迷,mamicode.com
首页 > 其他好文 > 详细

量化交易——双均线策略(金叉死叉)

时间:2020-04-13 19:49:13      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:pytho   png   art   fir   简便   nan   pen   重置   lib   

一、双均线概念

  均线:对于每一个交易日,都可以计算出前N天的移动平均值,然后把这些平均值连起来,成为一条线,就叫做N日移动平均线。移动平均线常用线有5日、10日、30日、60日、120日的指标。

    5日和10日的是短线操作参照指标,称作均线指标;

    30日和60日的是中期均线指标,称作均线指标;

    120日和240日的是长期均线指标,称作均线指标。

  金叉:短期均线上穿长期均线,买入信号。

  死叉:短期均线下穿长期均线,卖出信号。

  交易策略:金叉买入,死叉卖出

二、金叉死叉交易练习

1、使用tushare包获取某股票的历史行情

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt    # Python绘图和数据可视化的工具包
import tushare as ts


ts.get_k_data("601318", start="2004-01-30").to_csv(601318.csv)
df = pd.read_csv(601318.csv, index_col="date", parse_dates=[date])[[open, close, high, low]]
print(df)
"""
              open   close    high     low
date                                      
2007-03-01  21.254  19.890  21.666  19.469
2007-03-02  19.979  19.728  20.166  19.503
...            ...     ...     ...     ...
2020-01-22  85.000  85.220  85.480  83.830
2020-01-23  84.010  83.490  84.560  82.480

[3076 rows x 4 columns]
"""

2、使用pandas包计算该股票历史数据的5日和30均线

  5日均线则前4天没有均线,30日均线则前29天没有均线。

(1)普通方法

df[ma5] = np.nan     # 创建5日均值列
df[ma30] = np.nan    # 创建30日均值列
print(df)
"""
              open   close    high     low  ma5  ma30
date                                                 
2007-03-01  21.254  19.890  21.666  19.469  NaN   NaN
2007-03-02  19.979  19.728  20.166  19.503  NaN   NaN
...            ...     ...     ...     ...  ...   ...
2020-01-22  85.000  85.220  85.480  83.830  NaN   NaN
2020-01-23  84.010  83.490  84.560  82.480  NaN   NaN
"""
for i in range(4, len(df)):    # 从第五个到最后一个
    # 当天和前四天的收盘价的平均值为5日均线值
    df.loc[df.index[i], ma5] = df["close"][i-4:i+1].mean()   # i=4是第五天,[0,5]
print(df)
"""
              open   close    high     low      ma5  ma30
date                                                     
2007-03-01  21.254  19.890  21.666  19.469      NaN   NaN
2007-03-02  19.979  19.728  20.166  19.503      NaN   NaN
2007-03-05  19.545  18.865  19.626  18.504      NaN   NaN
2007-03-06  18.704  19.235  19.554  18.597      NaN   NaN
2007-03-07  19.252  19.758  19.936  19.090  19.4952   NaN
...            ...     ...     ...     ...      ...   ...
2020-01-23  84.010  83.490  84.560  82.480  85.6320   NaN
"""

for i in range(29, len(df)):    # 从第30个到最后一个
    # 当天和前29天的收盘价的平均值为30日均线值
    df.loc[df.index[i], ma30] = df["close"][i-29:i+1].mean()
print(df)
"""
              open   close    high     low      ma5       ma30
date                                                          
2007-03-01  21.254  19.890  21.666  19.469      NaN        NaN
2007-03-02  19.979  19.728  20.166  19.503      NaN        NaN
2007-03-05  19.545  18.865  19.626  18.504      NaN        NaN
2007-03-06  18.704  19.235  19.554  18.597      NaN        NaN
2007-03-07  19.252  19.758  19.936  19.090  19.4952        NaN
...            ...     ...     ...     ...      ...        ...
2020-01-17  86.150  86.250  86.900  85.850  86.1780  85.292333
2020-01-20  88.300  87.600  88.700  87.350  86.4080  85.387667
2020-01-21  87.000  85.600  87.290  85.600  86.1620  85.452667
2020-01-22  85.000  85.220  85.480  83.830  86.0440  85.507000
2020-01-23  84.010  83.490  84.560  82.480  85.6320  85.488667
"""

(2)简单方法

df[ma5] = df[close].rolling(5).mean()
df[ma30] = df[close].rolling(30).mean()
print(df)
"""
              open   close    high     low      ma5       ma30
date                                                          
2007-03-01  21.254  19.890  21.666  19.469      NaN        NaN
2007-03-02  19.979  19.728  20.166  19.503      NaN        NaN
2007-03-05  19.545  18.865  19.626  18.504      NaN        NaN
2007-03-06  18.704  19.235  19.554  18.597      NaN        NaN
2007-03-07  19.252  19.758  19.936  19.090  19.4952        NaN
...            ...     ...     ...     ...      ...        ...
2020-01-17  86.150  86.250  86.900  85.850  86.1780  85.292333
2020-01-20  88.300  87.600  88.700  87.350  86.4080  85.387667
2020-01-21  87.000  85.600  87.290  85.600  86.1620  85.452667
2020-01-22  85.000  85.220  85.480  83.830  86.0440  85.507000
2020-01-23  84.010  83.490  84.560  82.480  85.6320  85.488667
"""

3、使用matplotlib包可视化历史数据的收盘价和两条均线

df[[close, ma5, ma30]].plot()
plt.show()

  运行后显示效果:

  技术图片

4、分析输出所有金叉日期和死叉日期

  如果前一交易日五日均线小于30日均线,后一交易日五日均线大于30日均线,则说明是金叉;

  如果前一交易日五日均线大于30日均线,后一交易日五日均线小于30日均线,则说明是死叉。

(1)循环的解法

# dropna()删除含有空数据的全部行,axis参数可删除含义空数据的全部列
df = df.dropna()
gloden_cross = []    # 金叉
death_cross = []      # 死叉

for i in range(0, len(df)-1):
    if df[ma5][i] >= df[ma30][i] and df[ma5][i-1] <= df[ma30][i-1]:
        gloden_cross.append(df.index[i])
    if df[ma5][i] <= df[ma30][i] and df[ma5][i-1] >= df[ma30][i-1]:
        death_cross.append(df.index[i])

print(金叉日期, gloden_cross)
"""
金叉日期 [Timestamp(‘2007-06-14 00:00:00‘), Timestamp(‘2007-12-10 00:00:00‘),..., Timestamp(‘2020-01-02 00:00:00‘)]
"""
print(死叉日期, death_cross)
"""
死叉日期 [Timestamp(‘2007-06-04 00:00:00‘), Timestamp(‘2007-11-06 00:00:00‘),..., Timestamp(‘2019-12-23 00:00:00‘)]
"""

(2)简便算法

# dropna()删除含有空数据的全部行,axis参数可删除含义空数据的全部列
df = df.dropna()

sr1 = df[ma5] < df[ma30]
sr2 = df[ma5] >= df[ma30]

death_cross = df[sr1 & sr2.shift(1)].index
golden_cross = df[-(sr1 | sr2.shift(1))].index

print(金叉日期, golden_cross)
"""
金叉日期 DatetimeIndex([‘2007-04-12‘, ‘2007-06-14‘, ‘2007-12-10‘, ‘2008-04-23‘,..., ‘2020-01-02‘]
"""
print(死叉日期, death_cross)
"""
死叉日期 DatetimeIndex([‘2007-06-04‘, ‘2007-11-06‘, ‘2007-12-13‘, ‘2008-05-20‘,..., ‘2019-11-12‘, ‘2019-12-23‘]
"""

5、使用该策略的炒股收益率

  如果我从2010年1月1日起,初始资金为100000元,金叉尽量买入,死叉全部卖出,则到今天为止,我的炒股收益率?

# 炒股收益率
first_money = 100000
money = first_money    # 持有的资金
hold = 0               # 持有的股票
sr1 = pd.Series(1, index=golden_cross)
sr2 = pd.Series(0, index=death_cross)

sr = sr1.append(sr2).sort_index()    # 将两个表合并,并按时间排序
sr = sr[2010-01-01:]           # 从2010年1月1日开始

for i in range(0, len(sr)):
    p = df[open][sr.index[i]]      # 当天的开盘价
    if sr.iloc[i] == 1:
        # 金叉
        buy = money // (100 * p)   # 买多少手
        hold += buy * 100
        money -= buy * 100 * p
    else:
        # 死叉
        money += hold * p
        hold = 0    # 持有股票重置为0

# 计算最后一天股票市值加上持有的资金
p = df[open][-1]
now_money = hold * p + money
print(当前持有资产总额:, now_money)
print(盈亏情况:, now_money - first_money)
"""
当前持有资产总额: 551977.7999999997
盈亏情况: 451977.7999999997
"""

  需要注意的是:这里金叉死叉都是按照当天的收盘价计算的。但是如果得到当天的收盘价就已经无法进行交易了。因此要让策略可行,需要按照当天的开盘价计算。

 

量化交易——双均线策略(金叉死叉)

标签:pytho   png   art   fir   简便   nan   pen   重置   lib   

原文地址:https://www.cnblogs.com/xiugeng/p/12247248.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!