在获得AD指标的数值后,利用其MA(5)和MA(20)的差值进行判断,如果差值由负转正,认为趋势为正,进行做多操作,反之认为趋势为负,进行做空操作。多头出场条件为此时趋势为负,空头出场条件为此时趋势为正。并利用ATR控制止损和加仓机制。
回测曲线(由Auto-trader提供回测报告):
策略源码:
- function ADMAlossadd(freq)
- 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=60;
- dlen=40;
- [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
- %----------------------------------基本用法------------------------------
- %1)视AD指标为领先指标,AD上升则买入,AD下降则卖出(等价于收盘价高于中
- %间价则买入,收盘价低于中间价则卖出)
- %2)计算Chaikin=AD的(m)日指数移动平均-AD的(n)日指数移动平均
- % Chaikin指标由负值向上穿越0轴时,为买进信号
- % Chaikin指标由正值向下穿越0轴时,为卖出信号
- ad=AD(high,low,close,volume);
- maad=MA(ad,5)-MA(ad,20);
- con1=maad(end-1)<0&&maad(end)>=0;
- con2=maad(end-1)>0&&maad(end)<=0;
-
- %%
- %止损线
- 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
-
- %%
- %-------------------------平仓止损--------------------%
- %做多平仓
- if marketposition>0&&record{i}.m~=0
- if close(end)<record{i}.entryp-0.5*ATR(end)
- 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)
- 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
- %%
- %入场
- sharenum=1;
- %---------------入场-------------%
- 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);
- 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);
- 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);
- 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);
- 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社区-策略资源下载~