策略原理:
通过前N日的收盘价和前N日的开盘价构造俩条均线
以收盘均线初次上穿开盘均线的时刻记录一个高点,以该高点构造通道
突破买入,跌破卖出
动态跟踪止损出场
回测曲线(由Auto-trader提供回测报告)
策略源码:
- function Strategy2(default_unit,default_exitway,freq)%
- global entry;
- targetList = traderGetTargetList();
- %获取目标资产信息
- HandleList = traderGetHandleList();
- %获取账户信息
- for k=1:length(targetList);
- %--------------------仓位、K线、当前bar的提取-----------------------------%
- %获取当前仓位
- [marketposition,~,~]=traderGetAccountPosition(HandleList(1),targetList(k).Market,targetList(k).Code);
- %策略中每次取数据的长度
- dlags=10;
- lags=300;
- barnum=traderGetCurrentBar(targetList(k).Market,targetList(k).Code);
- %数据长度限制
- if(barnum<lags)
- continue;
- end
- %获取K线数据
- [time,open,high,low,close,volume,turnover,openinterest] = traderGetKData(targetList(k).Market,targetList(k).Code,'min',freq, 0-lags, 0,false,'FWard');
- % [Dtime,Dopen,Dhigh,Dlow,Dclose,Dvolume,Dturnover,Dopeninterest] = traderGetKData(targetList(k).Market,targetList(k).Code,'day',1,0-dlags, 0,false,'FWard');
- if length(close)<lags
- continue;
- end
- %------------------前n根bar不开仓-----------------------%
- % 虚拟交易所初始手数
- totalunit=0;
- %-------------------------交易逻辑-------------------------------%
- len=10;
- p=0.5;
- TRvalue=TR(close,high,low);
- diff=ma(close,len)-ma(open,len);
- con1=0;
- for i=2:length(diff)
- if diff(end-i+1)>0 && diff(end-i)<0
- highvalue=high(end-i+1);
- con1=1;
- break;
- end;
- end;
- con2=high(end)>highvalue+p*mean(TRvalue(end-len+1:end));
- buycon=con1 && con2;
- con3=0;
- for i=2:length(diff)
- if diff(end-i+1)<0 && diff(end-i)>0
- lowvalue=low(end-i+1);
- con3=1;
- break;
- end;
- end;
- con4=low(end)<lowvalue-p*mean(TRvalue(end-len+1:end));
- sellshortcon=con3 && con4;
- sellcon=diff(end)<0 && diff(end-1)>=0;
- buytocovercon=diff(end)>0 && diff(end-1)<=0;
- % sellshortcon=ma1(end)>upline(end) && close(end)<close(end-1) && close(end-1)<close(end-2);
- % buycon=ma1(end)<dnline(end) && close(end)>close(end-1) && close(end-1)>close(end-2);
- % buytocovercon=ma1(end)<dnline(end) && ma1(end-1)>dnline(end-1);
- % sellcon=ma1(end)>upline(end) && ma1(end-1)<=upline(end-1);
- %------------被动出场操作------------------%
- barsinceentry=barnum-entry.bar;
- backlen=30; % 回溯的长度(进仓bar之前)
- % 回溯的信息提取
- [backtime,backopen,backhigh,backlow,backclose,~,~,~] = traderGetKData(targetList(k).Market,targetList(k).Code,'min',freq, 0-barsinceentry-backlen, 0,false,'FWard');
- % 根据出场方式计算出场条件
- longstopcon=0;
- shortstopcon=0;
- if entry.exitway==1;
- AFinitial=0;
- AFparam=0.02;
- AFmax=0.2;
- Firstbarmultp=2; %影响第一根bar的止损价,调高表示可忍受的回撤越多
- [longstopcon,shortstopcon,exitline]=exit1(backopen,backhigh,backlow,backclose,entry.direction,backlen,AFinitial,AFparam,AFmax,Firstbarmultp);
- elseif entry.exitway==2;
- ATRparam=2;
- [longstopcon,shortstopcon,exitline]=exit2(backopen,backhigh,backlow,backclose,entry.direction,backlen,ATRparam);
- elseif entry.exitway==3;
- [longstopcon,shortstopcon,exitline]=exit3(backopen,backhigh,backlow,backclose,entry.direction,backlen);
- elseif entry.exitway==4
- startpoint=10;
- percent=0.3;
- TRmutlp=1;
- [longstopcon,shortstopcon,exitline]=exit4(backopen,backhigh,backlow,backclose,entry.direction,backlen,startpoint,percent,TRmutlp);
- end;
- % 出场执行
- if marketposition>0 && longstopcon
- totalunit=totalunit-entry.unit;
- entryinitial;
- end;
- if marketposition<0 && shortstopcon
- totalunit=totalunit+entry.unit;
- entryinitial;
- end;
- %---------------------------主动出场操作--------------------------------%
- if marketposition>0 && sellcon
- sellunit=default_unit;
- totalunit=totalunit-sellunit;
- entryinitial;
- end;
- if marketposition<0 && buytocovercon
- buytocoverunit=default_unit;
- totalunit=totalunit+buytocoverunit;
- entryinitial;
- end;
- %---------------------------入场操作--------------------------------%
- if marketposition==0 && buycon
- buyunit=default_unit;
- totalunit=totalunit+buyunit;
- [~]=entryalter2(k,barnum,1,1,default_unit,default_exitway);
- % 合约号,barnum,方向,开关,手数,出场
- end;
- if marketposition==0 && sellshortcon
- sellshortunit=default_unit;
- totalunit=totalunit-sellshortunit;
- [~]=entryalter2(k,barnum,-1,1,default_unit,default_exitway);
- % 合约号,barnum,方向,开关,手数,出场
- end;
- %---------------------- 虚拟交易所最终执行买卖 ------------------------------%
- if totalunit>0
- orderID1=traderDirectBuy(HandleList(1),targetList(k).Market,targetList(k).Code,abs(totalunit),0,'market','totalbuy');
- elseif totalunit<0
- orderID2=traderDirectSell(HandleList(1),targetList(k).Market,targetList(k).Code,abs(totalunit),0,'market','totalsell');
- end;
- end
- end
更多免费策略源码下载请登录DigQuant社区-策略资源下载~


雷达卡


京公网安备 11010802022788号







