策略原理:
多头入场:5日均线上穿30日均线且当前价格大于100日均线
空头入场:5日均线下穿30日均线且当前价格小于100日均线
主动出场:
多头:5日均线下穿30日或者当前价格大于开仓价上N倍ATR
空头:5日均线上穿30日或者当前价格小于开仓价上N倍ATR
被动出场:跟踪止损出场
回测曲线:
策略代码:
function Strategy1(default_unit,default_exitway,freq)%
targetList = traderGetTargetList();
%获取目标资产信息
HandleList = traderGetHandleList();
%获取账户句柄
global entrybar;
for k=1:length(targetList);
%--------------------仓位、K线、当前bar的提取-----------------------------%
%获取当前仓位
[marketposition,~,~]=traderGetAccountPosition(HandleList(1),targetList(k).Market,targetList(k).Code);
%策略中每次取数据的长度
lags=200;
dlags=20;
barnum=traderGetCurrentBar(targetList(k).Market,targetList(k).Code);
%数据长度限制
if(barnum<lags)
continue;
end
if(barnum<dlags)
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,~,~] = traderGetKData(targetList(k).Market,targetList(k).Code,'day',1, 0-dlags, 0,false,'FWard');
if length(close)<lags || length(Dclose)<dlags
continue;
end;
%-------------------------交易逻辑-------------------------------%
%----------入场信号--------------------%
TRvalue=TR(Dclose,Dhigh,Dlow);
stoppercent=0.025;
ATR=ma(TRvalue,10);
ma0=ma(close,5);
ma1=ma(close,30);
ma2=ma(close,100);
buycon=ma0(end)>ma1(end) && ma0(end-1)<ma1(end-1) && close(end)>ma2(end);
sellshortcon=ma0(end)<ma1(end) && ma0(end-1)>ma1(end-1) && close(end)<ma2(end);
if default_exitway==1
barsinceentry=barnum-entrybar(k);
[~,entryopen,entryhigh,entrylow,entryclose,~,~,~] = traderGetKData(targetList(k).Market,targetList(k).Code,'min',freq, 0-barsinceentry, 0,false,'FWard');
longstop=close(end)<entryclose(1)-ATR(end-1);
shortstop=close(end)>entryclose(1)+ATR(end-1);
ATRparam=2;
sellcon1=ma0(end)<ma1(end) && ma0(end-1)>ma1(end-1) && close(end)>entryclose(1)+ATRparam*ATR(end-1);
buytocovercon1=ma0(end)>ma1(end) && ma0(end-1)<ma1(end-1) && close(end)<entryclose(1)-ATRparam*ATR(end-1);
sellcon=longstop || sellcon1;
buytocovercon=shortstop || buytocovercon1;
elseif default_exitway==2
TRvalue=TR(close,high,low);
ATR=ma(TRvalue,4);
barsinceentry=barnum-entrybar(k);
[~,entryopen,entryhigh,entrylow,entryclose,~,~,~] = traderGetKData(targetList(k).Market,targetList(k).Code,'min',freq, 0-barsinceentry, 0,false,'FWard');
range=4*ATR(end-1);
[sellcon,buytocovercon]=exitway1(entryopen,entryclose,entryhigh,entrylow,marketposition,range);
elseif default_exitway==3
barsinceentry=barnum-entrybar(k);
[~,entryopen,entryhigh,entrylow,entryclose,~,~,~] = traderGetKData(targetList(k).Market,targetList(k).Code,'min',freq, 0-barsinceentry, 0,false,'FWard');
enterprice=entryclose(1);
percent=0.5;
ATRparam=1;
sellcon=0;
buytocovercon=0;
if marketposition>0
stoplossprice=enterprice-ATRparam*ATR(end);
stopearnprice=enterprice+ATRparam*ATR(end);
[sellcon,buytocovercon]=exitway3(entryopen,entryclose,entryhigh,entrylow,marketposition,stoplossprice,stopearnprice,percent);
elseif marketposition<0
stoplossprice=enterprice+ATRparam*ATR(end);
stopearnprice=enterprice-ATRparam*ATR(end);
[sellcon,buytocovercon]=exitway3(entryopen,entryclose,entryhigh,entrylow,marketposition,stoplossprice,stopearnprice,percent);
end;
elseif default_exitway==4
barsinceentry=barnum-entrybar(k);
[~,entryopen,entryhigh,entrylow,entryclose,~,~,~] = traderGetKData(targetList(k).Market,targetList(k).Code,'min',freq, 0-barsinceentry, 0,false,'FWard');
range=2*ATR(end-1);
[sellcon1,buytocovercon1]=exitway1(entryopen,entryclose,entryhigh,entrylow,marketposition,range);
longstop=close(end)<entryclose(1)-ATR(end-1);
shortstop=close(end)>entryclose(1)+ATR(end-1);
ATRparam=2;
sellcon2=ma0(end)<ma1(end) && close(end)>entryclose(1)+ATRparam*ATR(end-1);
buytocovercon2=ma0(end)>ma1(end) && close(end)<entryclose(1)-ATRparam*ATR(end-1);
sellcon=(sellcon1 && sellcon2) || longstop;
buytocovercon=(buytocovercon1 && buytocovercon2) || shortstop;
end;
%---------------------------入场操作--------------------------------%
if sellcon && marketposition>0
orderID1=traderPositionTo(HandleList(1),targetList(k).Market,targetList(k).Code,0,0,'market','sell');
if orderID1==0
continue;
end;
end;
if buytocovercon && marketposition<0
orderID2=traderPositionTo(HandleList(1),targetList(k).Market,targetList(k).Code,0,0,'market','sell');
if orderID2==0
continue;
end;
end;
if buycon && marketposition<=0
buyunit=default_unit;
orderID3=traderBuy(HandleList(1),targetList(k).Market,targetList(k).Code,buyunit,0,'market','buy');
if orderID3==0
continue;
end;
entrybar(k)=barnum;
end;
if sellshortcon && marketposition>=0
sellshortunit=default_unit;
orderID4=traderSellShort(HandleList(1),targetList(k).Market,targetList(k).Code,sellshortunit,0,'market','sell');
if orderID4==0
continue;
end;
entrybar(k)=barnum;
end;
end
end
多头入场:5日均线上穿30日均线且当前价格大于100日均线
空头入场:5日均线下穿30日均线且当前价格小于100日均线
主动出场:
多头:5日均线下穿30日或者当前价格大于开仓价上N倍ATR
空头:5日均线上穿30日或者当前价格小于开仓价上N倍ATR
被动出场:跟踪止损出场
回测曲线:
策略代码:
function Strategy1(default_unit,default_exitway,freq)%
targetList = traderGetTargetList();
%获取目标资产信息
HandleList = traderGetHandleList();
%获取账户句柄
global entrybar;
for k=1:length(targetList);
%--------------------仓位、K线、当前bar的提取-----------------------------%
%获取当前仓位
[marketposition,~,~]=traderGetAccountPosition(HandleList(1),targetList(k).Market,targetList(k).Code);
%策略中每次取数据的长度
lags=200;
dlags=20;
barnum=traderGetCurrentBar(targetList(k).Market,targetList(k).Code);
%数据长度限制
if(barnum<lags)
continue;
end
if(barnum<dlags)
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,~,~] = traderGetKData(targetList(k).Market,targetList(k).Code,'day',1, 0-dlags, 0,false,'FWard');
if length(close)<lags || length(Dclose)<dlags
continue;
end;
%-------------------------交易逻辑-------------------------------%
%----------入场信号--------------------%
TRvalue=TR(Dclose,Dhigh,Dlow);
stoppercent=0.025;
ATR=ma(TRvalue,10);
ma0=ma(close,5);
ma1=ma(close,30);
ma2=ma(close,100);
buycon=ma0(end)>ma1(end) && ma0(end-1)<ma1(end-1) && close(end)>ma2(end);
sellshortcon=ma0(end)<ma1(end) && ma0(end-1)>ma1(end-1) && close(end)<ma2(end);
if default_exitway==1
barsinceentry=barnum-entrybar(k);
[~,entryopen,entryhigh,entrylow,entryclose,~,~,~] = traderGetKData(targetList(k).Market,targetList(k).Code,'min',freq, 0-barsinceentry, 0,false,'FWard');
longstop=close(end)<entryclose(1)-ATR(end-1);
shortstop=close(end)>entryclose(1)+ATR(end-1);
ATRparam=2;
sellcon1=ma0(end)<ma1(end) && ma0(end-1)>ma1(end-1) && close(end)>entryclose(1)+ATRparam*ATR(end-1);
buytocovercon1=ma0(end)>ma1(end) && ma0(end-1)<ma1(end-1) && close(end)<entryclose(1)-ATRparam*ATR(end-1);
sellcon=longstop || sellcon1;
buytocovercon=shortstop || buytocovercon1;
elseif default_exitway==2
TRvalue=TR(close,high,low);
ATR=ma(TRvalue,4);
barsinceentry=barnum-entrybar(k);
[~,entryopen,entryhigh,entrylow,entryclose,~,~,~] = traderGetKData(targetList(k).Market,targetList(k).Code,'min',freq, 0-barsinceentry, 0,false,'FWard');
range=4*ATR(end-1);
[sellcon,buytocovercon]=exitway1(entryopen,entryclose,entryhigh,entrylow,marketposition,range);
elseif default_exitway==3
barsinceentry=barnum-entrybar(k);
[~,entryopen,entryhigh,entrylow,entryclose,~,~,~] = traderGetKData(targetList(k).Market,targetList(k).Code,'min',freq, 0-barsinceentry, 0,false,'FWard');
enterprice=entryclose(1);
percent=0.5;
ATRparam=1;
sellcon=0;
buytocovercon=0;
if marketposition>0
stoplossprice=enterprice-ATRparam*ATR(end);
stopearnprice=enterprice+ATRparam*ATR(end);
[sellcon,buytocovercon]=exitway3(entryopen,entryclose,entryhigh,entrylow,marketposition,stoplossprice,stopearnprice,percent);
elseif marketposition<0
stoplossprice=enterprice+ATRparam*ATR(end);
stopearnprice=enterprice-ATRparam*ATR(end);
[sellcon,buytocovercon]=exitway3(entryopen,entryclose,entryhigh,entrylow,marketposition,stoplossprice,stopearnprice,percent);
end;
elseif default_exitway==4
barsinceentry=barnum-entrybar(k);
[~,entryopen,entryhigh,entrylow,entryclose,~,~,~] = traderGetKData(targetList(k).Market,targetList(k).Code,'min',freq, 0-barsinceentry, 0,false,'FWard');
range=2*ATR(end-1);
[sellcon1,buytocovercon1]=exitway1(entryopen,entryclose,entryhigh,entrylow,marketposition,range);
longstop=close(end)<entryclose(1)-ATR(end-1);
shortstop=close(end)>entryclose(1)+ATR(end-1);
ATRparam=2;
sellcon2=ma0(end)<ma1(end) && close(end)>entryclose(1)+ATRparam*ATR(end-1);
buytocovercon2=ma0(end)>ma1(end) && close(end)<entryclose(1)-ATRparam*ATR(end-1);
sellcon=(sellcon1 && sellcon2) || longstop;
buytocovercon=(buytocovercon1 && buytocovercon2) || shortstop;
end;
%---------------------------入场操作--------------------------------%
if sellcon && marketposition>0
orderID1=traderPositionTo(HandleList(1),targetList(k).Market,targetList(k).Code,0,0,'market','sell');
if orderID1==0
continue;
end;
end;
if buytocovercon && marketposition<0
orderID2=traderPositionTo(HandleList(1),targetList(k).Market,targetList(k).Code,0,0,'market','sell');
if orderID2==0
continue;
end;
end;
if buycon && marketposition<=0
buyunit=default_unit;
orderID3=traderBuy(HandleList(1),targetList(k).Market,targetList(k).Code,buyunit,0,'market','buy');
if orderID3==0
continue;
end;
entrybar(k)=barnum;
end;
if sellshortcon && marketposition>=0
sellshortunit=default_unit;
orderID4=traderSellShort(HandleList(1),targetList(k).Market,targetList(k).Code,sellshortunit,0,'market','sell');
if orderID4==0
continue;
end;
entrybar(k)=barnum;
end;
end
end