请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  量化平台 帖子:3352762 新帖:37

基于凸组合原理构造的新均线策略研究

醒掌天下权发表于:5 月 10 日 06:48回复(1)

本文参考了广发证券2015年7月16日《均线交叉策略的另类研究》

移动平均线是重要的技术分析指标,一般而说,当短期均线穿过长期均线时为“金叉信号”,此时可以买入股票,当长期均线穿过短期均线时为“死叉信号”,此时应该卖出股票。然而技术分析并非总是有效,本研究发现根据金叉死叉进行买卖并不一定获得正收益。

下面我们来研究新的均线策略:

首先了解凸组合的概念:设向量空间有一组有限数据x1,x2...xn,则定义x为这些数据点的一个凸组合如果x满足

x=α1x1 α2x2 ... αnxnx=α1x1 α2x2 ... αnxn

x=α1x1α2x2...αnxn

<br>x=α1x1 α2x2 ... αnxn<br>


x= \alpha_1x_1 \alpha_2x_2 ... \alpha_nx_n

其中实数αi(i=1,2......n)αi(i=1,2......n)

αi(i=1,2......n)

αi(i=1,2......n)

\alpha_i(i=1,2... ...,n)

满足α1 α2 ... αn=1α1 α2 ... αn=1α1α2...αn=1α1 α2 ... αn=1

\alpha_1 \alpha_2 ... \alpha_n=1

提出的改进均线交叉策略,主要是改变传统策略的平仓条件,开仓条件仍由传统的金叉死叉确定,平仓条件则是由开仓价格与当前价格来确定。若当前价格不小于开仓价与当前价的凸组合,则继续持有多单,无需平仓;若当前价格不大于开仓价与当前价的凸组合,则继续持有空单,无需平仓。

根据研究,我们发现新的均线策略无论是收益和回撤的表现均大幅提高。

本文参考了广发证券2015年7月16日《均线交叉策略的另类研究》

移动平均线是重要的技术分析指标,一般而说,当短期均线穿过长期均线时为“金叉信号”,此时可以买入股票,当长期均线穿过短期均线时为“死叉信号”,此时应该卖出股票。然而技术分析并非总是有效,本研究发现根据金叉死叉进行买卖并不一定获得正收益。

下面我们来研究新的均线策略:

首先了解凸组合的概念:设向量空间有一组有限数据x1,x2...xn,则定义x为这些数据点的一个凸组合如果x满足

$ x= \alpha_1x_1+\alpha_2x_2+...+\alpha_nx_n $

其中实数$\alpha_i(i=1,2... ...,n)$满足$\alpha_1+\alpha_2+...+\alpha_n=1$

提出的改进均线交叉策略,主要是改变传统策略的平仓条件,开仓条件仍由传统的金叉死叉确定,平仓条件则是由开仓价格与当前价格来确定。若当前价格不小于开仓价与当前价的凸组合,则继续持有多单,无需平仓;若当前价格不大于开仓价与当前价的凸组合,则继续持有空单,无需平仓。

根据研究,我们发现新的均线策略无论是收益和回撤的表现均大幅提高。

import pandas as pd
hs300=get_price('IF8888.CCFX', start_date='2016-01-01', end_date='2016-10-31', frequency='10m', fields='close', skip_paused=False, fq='pre')
datastore=pd.DataFrame()#ma=20timeline=list([5,10,20,30,60,120,240])ma=20for ma in timeline:tempt11=[]for i in arange(len(hs300)-ma):    tempt11.append(float(hs300[i:(i+ma)].mean()))datastore[str(ma)]=tempt11[((timeline[-1])-ma):]datastore.index=hs300.index[timeline[-1]:]
def gain_se(slow='5',fast='10'):##交易函数##输入:短周期均线,长周期均线##返回:总收益,最大回撤,se_type=[]for i in arange(1,len(datastore)):   if datastore[slow].iloc[i-1]>datastore[fast].iloc[i-1] and datastore[slow].iloc[i]<datastore[fast].iloc[i]:se_type.append(-1)elif datastore[slow].iloc[i-1]<datastore[fast].iloc[i-1] and datastore[slow].iloc[i]>datastore[fast].iloc[i]:se_type.append(1)else: se_type.append(0)rate=(hs300[timeline[-1]:].diff(1)[1:]/hs300[timeline[-1]:])[1:]tempt13=[]tempt12=0for i in arange(len(se_type)):    if se_type[i]==-1:tempt12=-1elif se_type[i]==1:tempt12=1tempt13.append(tempt12)rate22=[tempt13[i]*list(rate['close'])[i] for i in arange(len(tempt13))]##计算累计收益率tempt23=1cumreturn=[]for i in arange(len(rate22)):tempt23=(rate22[i]+1)*tempt23cumreturn.append(tempt23)##计算中收益以及回撤finalreturn=cumreturn[-1]-cumreturn[0]huichestore=[]for i in arange(len(cumreturn)):for j in arange(i,len(cumreturn)):huichestore.append(cumreturn[i]-cumreturn[j])huiche=max(huichestore)##画累计收益图#plt.plot(cumreturn)##返回总收益和回撤return finalreturn,huiche
ratestore=pd.DataFrame()time=[str(timeline[i]) for i in arange(len(timeline))]for i in arange(len(time)):for j in arange(i+1,len(time)):ratestore[time[i]+'+'+time[j]]=gain_se(time[i],time[j])ratestore.index=['总收益','回撤']ratestore

5+105+205+305+605+1205+24010+2010+3010+6010+120...20+3020+6020+12020+24030+6030+12030+24060+12060+240120+240
总收益0.0149330.040870-0.212217-0.220009-0.110054-0.1069040.206872-0.156236-0.171456-0.222049...-0.159388-0.143848-0.131586-0.165854-0.022663-0.010057-0.249950-0.155161-0.142461-0.231228
回撤0.2040590.1560190.2442760.2898140.2063720.1834450.1970010.2264800.2982740.276466...0.2134240.2322220.2539730.1949510.1700780.1655940.2830280.2172170.2403420.324851

2 rows × 21 columns

上述为不同时间周期组合而来的收益以及回撤,可以发现均线策略并不一定总能获得正收益¶

下面我们来研究新的均线策略:¶

首先了解凸组合的概念:设向量空间有一组有限数据x1,x2...xn,则定义x为这些数据点的一个凸组合如果x满足

$ x= \alpha_1x_1+\alpha_2x_2+...+\alpha_nx_n $

其中实数$\alpha_i(i=1,2... ...,n)$满足$\alpha_1+\alpha_2+...+\alpha_n=1$

提出的改进均线交叉策略,主要是改变传统策略的平仓条件,开仓条件仍由传统的金叉死叉确定,平仓条件则是由开仓价格与当前价格来确定。若当前价格不小于开仓价与当前价的凸组合,则继续持有多单,无需平仓;若当前价格不大于开仓价与当前价的凸组合,则继续持有空单,无需平仓。

def gain_se2(slow='5',fast='10'):##交易函数##输入:短周期均线,长周期均线##返回:总收益,最大回撤,se_type=[]for i in arange(1,len(datastore)):   if datastore[slow].iloc[i-1]>datastore[fast].iloc[i-1] and datastore[slow].iloc[i]<datastore[fast].iloc[i]:se_type.append(-1)elif datastore[slow].iloc[i-1]<datastore[fast].iloc[i-1] and datastore[slow].iloc[i]>datastore[fast].iloc[i]:se_type.append(1)else: se_type.append(0)rate=(hs300[timeline[-1]:].diff(1)[1:]/hs300[timeline[-1]:])[1:]tempt13=[]tempt12=0for i in arange(len(se_type)):if tempt12==0:if se_type[i]==-1:##死叉并且空仓需要判断uu1=i##记录死叉位置tempt12=-1##开空单elif se_type[i]==1:##金叉uu2=i##记录金叉位置tempt12=1##开多单elif tempt12<0:if se_type[i]==-1:##死叉位置更新uu1=iif hs300[timeline[-1]+1:]['close'][uu1]>=hs300[timeline[-1]+1:]['close'][i]:tempt12=-1##假如价格没有超过死叉位置价格继续持仓else:tempt12=0elif tempt12>0:if se_type[i]==1:##金叉位置更新uu1=iif hs300[timeline[-1]+1:]['close'][uu2]<=hs300[timeline[-1]+1:]['close'][i]:tempt12=1##假如价格没有超过金叉位置价格继续持仓else:tempt12=0tempt13.append(tempt12)rate22=[tempt13[i]*list(rate['close'])[i] for i in arange(len(tempt13))]##计算累计收益率tempt23=1cumreturn=[]for i in arange(len(rate22)):tempt23=(rate22[i]+1)*tempt23cumreturn.append(tempt23)##计算中收益以及回撤finalreturn=cumreturn[-1]-cumreturn[0]huichestore=[]for i in arange(len(cumreturn)):for j in arange(i,len(cumreturn)):huichestore.append(cumreturn[i]-cumreturn[j])huiche=max(huichestore)##画累计收益图#plt.plot(cumreturn)##返回总收益和回撤return finalreturn,huiche
ratestore2=pd.DataFrame()time=[str(timeline[i]) for i in arange(len(timeline))]for i in arange(len(time)):for j in arange(i+1,len(time)):ratestore2[time[i]+'+'+time[j]]=gain_se2(time[i],time[j])ratestore2.index=['总收益','回撤']ratestore2

5+105+205+305+605+1205+24010+2010+3010+6010+120...20+3020+6020+12020+24030+6030+12030+24060+12060+240120+240
总收益0.3945140.4004820.2200210.2024720.1500640.0912900.2824720.3894850.1866040.074751...0.0762520.0498940.1207890.0803340.1624260.1002290.0340780.0967160.0736810.049507
回撤0.0992460.1208880.0846500.0595280.0364690.0757180.0889830.0964080.0823310.081913...0.0746740.0957580.0967450.0484490.1003390.0653270.0941960.0946670.0744960.067056

2 rows × 21 columns

import seaborn as sns  import numpy as npimport matplotlib.pyplot as pltplt.figure(figsize=(15,5))y1 = ratestore.iloc[0]*100y2 = ratestore.iloc[1]*100index = np.arange(len(y1))bar_width = 0.3ax1=plt.bar(2+index, y1, width=0.3,color='steelblue',label='总收益率')ax2=plt.bar(2+index+bar_width, y2, width=0.3 , color='goldenrod',label='最大回撤')plt.xticks(2+index+0.35,list(ratestore.columns),rotation=28)plt.xlabel('传统均线策略不同周期均线组合')plt.legend()plt.show()
import numpy as npimport matplotlib.pyplot as pltplt.figure(figsize=(15,5))y1 = ratestore2.iloc[0]*100y2 = ratestore2.iloc[1]*100index = np.arange(len(y1))bar_width = 0.3ax1=plt.bar(2+index, y1, width=0.3,color='steelblue',label='总收益率')ax2=plt.bar(2+index+bar_width, y2, width=0.3 , color='goldenrod',label='最大回撤')plt.xticks(2+index+0.35,list(ratestore2.columns),rotation=28)plt.xlabel('新的均线策略不同周期均线组合')plt.legend()plt.show()
 
 

全部回复

0/140

达人推荐

量化课程

    移动端课程