回测曲线(由Auto-trader提供回测报告)
策略源码:
- function sjx1sarlossadd(freq) %
- %freq为输入频率
- %反手策略
- %双均线+SAR策略:双均线决定入场,SAR定出场。MA(5)>MA(10),做多,SAR由空转多,平空做多,SAR由多转空,平多做空。
- targetList = traderGetTargetList();
- HandleList = traderGetHandleList();
- global record;
- for i=1:length(targetList)
- marketposition=traderGetAccountPosition(HandleList(1),targetList(i).Market,targetList(i).Code);
- barnum=traderGetCurrentBar(targetList(i).Market,targetList(i).Code);
- len=90;
- dlen=90;
- [time,open,high,low,close,volume,turnover,openinterest] = traderGetKData(targetList(i).Market,targetList(i).Code,'min',freq, 0-len, 0,false,'FWard');
- [Dtime,Dopen,Dhigh,Dlow,Dclose,Dvolume,Dturnover,Dopeninterest] = traderGetKData(targetList(i).Market,targetList(i).Code,'day',1, 0-dlen, 0,false,'FWard');
- if length(close)<len+1||length(Dclose)<dlen+1
- continue;
- end
- [sarc,sarn,pos,turn]=SAR(high,low);
- ma5=MA(Dclose,5);
- ma20=MA(Dclose,20);
- con1=ma5(end)>ma20(end);%做多
- con2=ma5(end)<ma20(end);%做空
- N=barnum-record{i}.entrybar;
- if record{i}.entrybar==0
- N=15;
- end
-
- N=max(N,15);
- N=min(N,50);
- con20=sum(pos(end-N:end-4)==-1)>=fix(N*0.8)&&sum(pos(end-3:end)==1)>=3;%空转多,反转做多
- con10=sum(pos(end-N:end-4)==1)>=fix(N*0.8)&&sum(pos(end-3:end)==-1)>=3;%多转空,反转做空
-
- %--止损线
- ATR=ATR(Dhigh,Dlow,Dclose,20);
-
- %-满仓上移止损线
- if record{i}.m>4
- if close(end)>record{i}.entryp+2*ATR(end)&&(marketposition>0)
- record{i}.entryp= record{i}.entryp+2*ATR(end);
- elseif close(end)<record{i}.entryp-2*ATR(end)&&(marketposition<0)
- record{i}.entrp= record{i}.entryp-2*ATR(end);
- end
- end
- %入场
- sharenum=5;
- %---------------入场-------------%
- if con1&&(record{i}.m==0)&&(marketposition==0)
- order=traderBuy(HandleList(1),targetList(i).Market,targetList(i).Code,sharenum,0,'market','buy'); %%%%做多
- if order~=0
- record{i}.m= record{i}.m+1;
- record{i}.entryp=close(end);
- record{i}.entrybar=barnum;
- end
- end
-
- if con2&&(record{i}.m==0)&&(marketposition==0)
- order=traderSellShort(HandleList(1),targetList(i).Market,targetList(i).Code,sharenum,0,'market','buy'); %%%%做多
- if order~=0
- record{i}.m= record{i}.m+1;
- record{i}.entryp=close(end);
- record{i}.entrybar=barnum;
- end
- end
- %--加仓
- %-----------------加仓----------------%
- if close(end)>record{i}.entryp+2*ATR(end)&&(record{i}.m<=4)&&(marketposition>0)
- order=traderBuy(HandleList(1),targetList(i).Market,targetList(i).Code,sharenum,0,'market','buy'); %%%%做多
- if order~=0
- record{i}.m= record{i}.m+1;
- record{i}.entryp=close(end);
- record{i}.entrybar=barnum;
- end
- end
-
- if close(end)<record{i}.entryp-2*ATR(end)&&(record{i}.m<=4)&&(marketposition<0)
- order=traderSellShort(HandleList(1),targetList(i).Market,targetList(i).Code,sharenum,0,'market','buy'); %%%%做多
- if order~=0
- record{i}.m= record{i}.m+1;
- record{i}.entryp=close(end);
- record{i}.entrybar=barnum;
- end
- end
-
- %-------------------------止损出场--------------------%
- %做多平仓
- if marketposition>0&&record{i}.m~=0
- if close(end)<record{i}.entryp-0.5*ATR(end)||con10
- order= traderPositionTo(HandleList(1),targetList(i).Market,targetList(i).Code,0,0,'market','sell');
- if order~=0
- record{i}.m=0;
- record{i}.entryp=close(end);
-
- end
- end
- end
-
- if marketposition<0&&record{i}.m~=0
- if close(end)>record{i}.entryp+0.5*ATR(end)||con20
- order= traderPositionTo(HandleList(1),targetList(i).Market,targetList(i).Code,0,0,'market','sell');
- if order~=0
- record{i}.m=0;
- record{i}.entryp=close(end);
-
- end
- end
- end
-
-
-
- end
- end
- function ATRValue=ATR(High,Low,Close,Length)
- ATRValue=zeros(length(High),1);
- TRValue=zeros(length(High),1);
- TRValue(2:end)=max([High(2:end)-Low(2:end) abs(High(2:end)-Close(1:end-1)) abs(Low(2:end)-Close(1:end-1))],[],2);
- ATRValue=MA(TRValue,Length);
- end
- function MAValue=MA(Price,Length)
- MAValue=zeros(length(Price),1);
- for i=Length:length(Price)
- MAValue(i)=sum(Price(i-Length+1:i))/Length;
- end
- MAValue(1:Length-1)=Price(1:Length-1);
- end
更多免费策略源码下载请登录DigQuant社区-策略资源下载~


雷达卡




京公网安备 11010802022788号







