策略思路:
入场:价格突破50日高点,或者跌破50日低点
出场:多头情况下跌破20日低点或者低于上次入场价格下的N倍ATR
空头情况下突破20日高点或者高于上次入场价格上的N倍ATR
回测曲线:
策略代码:
function Turtle(len1,len2,len3,N)
targetList = traderGetTargetList();
HandleList = traderGetHandleList();
global s;
lens=60;
Plimit=4;
ShareNum=1;
n=length(targetList);
for j=1:n
[time,open,high,low,close,volume,turnover,openinterest] = traderGetKData(targetList(j).Market,targetList(j).Code,'day',1, 0-lens, 0,false,'FWard');
%---------------------策略计算与基本逻辑---------------%
if length(close)<lens+1 || volume(end)==0
continue;
end
value1 = high(end-lens+1:end) - low(end-lens+1:end);% 当日最高价减去当日最低价
value2 = abs(high(end-lens+1:end)-close(end-lens:end-1));% 当日最高价减去前日收盘价的绝对值
value3 = abs(low(end-lens+1:end)-close(end-lens:end-1));% 当日最低价减去前日收盘价的绝对值
TRlist=max(value1,max(value2,value3));
ATR_len3=mean(TRlist(end-len3+1:end));
% 四条轨道
High_len1=max(high(end-len1:end-1));
Low_len1=min(low(end-len1:end-1));
High_len2=max(high(end-len2:end-1));
Low_len2=min(low(end-len2:end-1));
mp= traderGetAccountPosition(HandleList,targetList(j).Market,targetList(j).Code);
con1=close(end)>High_len1;
con2=close(end)<Low_len1;
con3=close(end)>High_len2;
con4=close(end)<Low_len2;
% 进场
if mp==0
s(j).ShareList=[];
if con1
OrderID=traderDirectBuy(HandleList(1),targetList(j).Market,targetList(j).Code,ShareNum,0,'market','buy');
if OrderID~=0
s(j).openprice=traderOrderFilledPrice(HandleList(1),OrderID);
s(j).ShareList(end+1)=ShareNum;
end
elseif con2
OrderID=traderDirectSell(HandleList(1),targetList(j).Market,targetList(j).Code,ShareNum,0,'market','buy');
if OrderID~=0
s(j).openprice=traderOrderFilledPrice(HandleList(1),OrderID);
s(j).ShareList(end+1)=ShareNum;
end
end
end
% 加仓
if mp>0 && Plimit>length(s(j).ShareList) && close(end)>=( s(j).openprice+N*ATR_len3)
OrderID=traderDirectBuy(HandleList(1),targetList(j).Market,targetList(j).Code,ShareNum,0,'market','buy');
if OrderID~=0
s(j).openprice=traderOrderFilledPrice(HandleList(1),OrderID);
s(j).ShareList(end+1)=ShareNum;
end
end
if mp<0 && Plimit>length(s(j).ShareList) && close(end)<=(s(j).openprice-N*ATR_len3)
OrderID=traderDirectSell(HandleList(1),targetList(j).Market,targetList(j).Code,ShareNum,0,'market','sell');
if OrderID~=0
s(j).openprice=traderOrderFilledPrice(HandleList(1),OrderID);
s(j).ShareList(end+1)=ShareNum;
end
end
% 出场
if mp>0
if con4 || close(end)<( s(j).openprice-N*ATR_len3)
traderPositionTo(HandleList(1),targetList(j).Market,targetList(j).Code,0,0,'market','close');
end
end
if mp<0
if con3 || close(end)>(s(j).openprice+N*ATR_len3)
traderPositionTo(HandleList(1),targetList(j).Market,targetList(j).Code,0,0,'market','close');
end
end
end
atrader社区策略服务提供100度个免费策略源码下载 http://www.atrader.com.cn/stra.php
入场:价格突破50日高点,或者跌破50日低点
出场:多头情况下跌破20日低点或者低于上次入场价格下的N倍ATR
空头情况下突破20日高点或者高于上次入场价格上的N倍ATR
回测曲线:
策略代码:
function Turtle(len1,len2,len3,N)
targetList = traderGetTargetList();
HandleList = traderGetHandleList();
global s;
lens=60;
Plimit=4;
ShareNum=1;
n=length(targetList);
for j=1:n
[time,open,high,low,close,volume,turnover,openinterest] = traderGetKData(targetList(j).Market,targetList(j).Code,'day',1, 0-lens, 0,false,'FWard');
%---------------------策略计算与基本逻辑---------------%
if length(close)<lens+1 || volume(end)==0
continue;
end
value1 = high(end-lens+1:end) - low(end-lens+1:end);% 当日最高价减去当日最低价
value2 = abs(high(end-lens+1:end)-close(end-lens:end-1));% 当日最高价减去前日收盘价的绝对值
value3 = abs(low(end-lens+1:end)-close(end-lens:end-1));% 当日最低价减去前日收盘价的绝对值
TRlist=max(value1,max(value2,value3));
ATR_len3=mean(TRlist(end-len3+1:end));
% 四条轨道
High_len1=max(high(end-len1:end-1));
Low_len1=min(low(end-len1:end-1));
High_len2=max(high(end-len2:end-1));
Low_len2=min(low(end-len2:end-1));
mp= traderGetAccountPosition(HandleList,targetList(j).Market,targetList(j).Code);
con1=close(end)>High_len1;
con2=close(end)<Low_len1;
con3=close(end)>High_len2;
con4=close(end)<Low_len2;
% 进场
if mp==0
s(j).ShareList=[];
if con1
OrderID=traderDirectBuy(HandleList(1),targetList(j).Market,targetList(j).Code,ShareNum,0,'market','buy');
if OrderID~=0
s(j).openprice=traderOrderFilledPrice(HandleList(1),OrderID);
s(j).ShareList(end+1)=ShareNum;
end
elseif con2
OrderID=traderDirectSell(HandleList(1),targetList(j).Market,targetList(j).Code,ShareNum,0,'market','buy');
if OrderID~=0
s(j).openprice=traderOrderFilledPrice(HandleList(1),OrderID);
s(j).ShareList(end+1)=ShareNum;
end
end
end
% 加仓
if mp>0 && Plimit>length(s(j).ShareList) && close(end)>=( s(j).openprice+N*ATR_len3)
OrderID=traderDirectBuy(HandleList(1),targetList(j).Market,targetList(j).Code,ShareNum,0,'market','buy');
if OrderID~=0
s(j).openprice=traderOrderFilledPrice(HandleList(1),OrderID);
s(j).ShareList(end+1)=ShareNum;
end
end
if mp<0 && Plimit>length(s(j).ShareList) && close(end)<=(s(j).openprice-N*ATR_len3)
OrderID=traderDirectSell(HandleList(1),targetList(j).Market,targetList(j).Code,ShareNum,0,'market','sell');
if OrderID~=0
s(j).openprice=traderOrderFilledPrice(HandleList(1),OrderID);
s(j).ShareList(end+1)=ShareNum;
end
end
% 出场
if mp>0
if con4 || close(end)<( s(j).openprice-N*ATR_len3)
traderPositionTo(HandleList(1),targetList(j).Market,targetList(j).Code,0,0,'market','close');
end
end
if mp<0
if con3 || close(end)>(s(j).openprice+N*ATR_len3)
traderPositionTo(HandleList(1),targetList(j).Market,targetList(j).Code,0,0,'market','close');
end
end
end
atrader社区策略服务提供100度个免费策略源码下载 http://www.atrader.com.cn/stra.php