- 精华
- 0
- 在线时间
- 37 小时
- UID
- 247176
- 积分
- 162
- 帖子
- 72
- 阅读权限
- 0
- 注册时间
- 2017-2-14
- 最后登录
- 2017-5-18
- 精华
- 0
- UID
- 247176
- 积分
- 162
- 帖子
- 72
- 主题
- 68
- 阅读权限
- 0
- 注册时间
- 2017-2-14
- 最后登录
- 2017-5-18
|
策略原理:
构造收盘与开盘之间的绝对值均线corange
最高与最低之间的绝对值均线hlrange
做多条件:当前价格减开盘价大于corange(end-1)
最高价减最低价大于hlrange(end-1)
最低价出现在近期
回测曲线:
策略代码:
function Strategy1(default_unit,default_exitway,freq)%
targetList = traderGetTargetList();
%获取目标资产信息
HandleList = traderGetHandleList();
%获取账户句柄
global entry;
for k=1:length(targetList);
%--------------------仓位、K线、当前bar的提取-----------------------------%
%获取当前仓位
[marketposition,~,~]=traderGetAccountPosition(HandleList(1),targetList(k).Market,targetList(k).Code);
%策略中每次取数据的长度
dlags=20;
lags=80;
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 || length(Dclose)<dlags
continue;
end
% 虚拟交易所初始手数
%-------------------------交易逻辑-------------------------------%
%----------入场信号--------------------%
TRvalue=TR(close,high,low);
ATR=ma(TRvalue,20);
corange=ma(abs(close-open),20);
hlrange=ma(high-low,20);
len1=25;
mlow=low(end-len1+1:end);
[~,mi_num]=min(mlow);
bcon1=mi_num>=len1-6 && mi_num<=len1-2;
bcon2=close(end)-open(end)>corange(end-1);
bcon3=high(end)-low(end)>hlrange(end-1);
s(1).buycon=bcon1 && bcon2 && bcon3;
mhigh=high(end-len1+1:end);
[~,ma_num]=max(mhigh);
scon1=ma_num>=len1-6 && ma_num<=len1-2;
scon2=open(end)-close(end)>corange(end-1);
scon3=high(end)-low(end)>hlrange(end-1);
s(1).sellshortcon=scon1 && scon2 && scon3;
%------------被动出场操作------------------%
%找到未平仓的订单
remain=remainorder(entry,k);
%对未平仓的订单进行平仓判断及操作
for i=1:length(remain.entrybar);
% 进仓以来的bar个数
barsinceentry=barnum-remain.entrybar(i);
backlen=20; % 回溯的长度(进仓bar之前)
longstopcon=0;
shortstopcon=0;
% 回溯的信息提取
[backtime,backopen,backhigh,backlow,backclose,~,~,~] = traderGetKData(targetList(k).Market,targetList(k).Code,'min',freq, 0-barsinceentry-backlen, 0,false,'FWard');
% 根据出场方式计算出场条件
if remain.entryexitway(i)==1;
AFinitial=0;
AFparam=0.02;
AFmax=0.2;
Firstbarmultp=1; %影响第一根bar的止损价,调高表示可忍受的回撤越多
[longstopcon,shortstopcon,exitline]=exit1(backopen,backhigh,backlow,backclose,remain.entrydirection(i),backlen,AFinitial,AFparam,AFmax,Firstbarmultp);
elseif remain.entryexitway(i)==2;
initialATRparam=2;
AF=0.02;
minATRparam=1;
[longstopcon,shortstopcon,exitline]=exit2(backopen,backhigh,backlow,backclose,remain.entrydirection(i),backlen,initialATRparam,AF,minATRparam);
elseif remain.entryexitway(i)==3;
[longstopcon,shortstopcon,exitline]=exit3(backopen,backhigh,backlow,backclose,remain.entrydirection(i),backlen);
elseif remain.entryexitway(i)==4
startpoint=10;
percent=0.3;
TRmutlp=1;
[longstopcon,shortstopcon,exitline]=exit4(backopen,backhigh,backlow,backclose,remain.entrydirection(i),backlen,startpoint,percent,TRmutlp);
elseif remain.entryexitway(i)==5;
stdlen=19;
initialstdparam=2;
minstdparam=1;
AF=0.2;
[longstopcon,shortstopcon,exitline]=exit5(backopen,backhigh,backlow,backclose,remain.entrydirection(i),backlen,stdlen,initialstdparam,minstdparam,AF);
elseif remain.entryexitway(i)==6;
% if remain.entrydirection(i)==1;
% if barsinceentry==20
% longstopcon=1;
% end;
% elseif remain.entrydirection(i)==-1;
% if barsinceentry==20
% shortstopcon=1;
% end;
% end;
if remain.entrydirection(i)==1;
if high(end)>max(high(end-20:end-1))
longstopcon=1;
end;
elseif remain.entrydirection(i)==-1;
if low(end)<min(low(end-20:end-1))
shortstopcon=1;
end;
end;
end;
% 出场执行
if longstopcon
orderID1=traderDirectSell(HandleList(1),targetList(k).Market,targetList(k).Code,remain.entryunit(i),0,'market','totalbuy');
entry.record{k}(remain.num(i))=0;
end;
if shortstopcon
orderID1=traderDirectBuy(HandleList(1),targetList(k).Market,targetList(k).Code,remain.entryunit(i),0,'market','totalbuy');
entry.record{k}(remain.num(i))=0;
end;
end;
%---------------------------加仓--------------------------------%
%----------------策略1----------------------%
%再次找到未平仓的订单
remain=remainorder(entry,k);
% 找到策略i的marketposition
s=mptaking(s,remain);
%---------------------------入场操作--------------------------------%
%----------------策略1----------------------%
if s(1).buycon && s(1).marketposition==0
buyunit=default_unit;
orderID1=traderDirectBuy(HandleList(1),targetList(k).Market,targetList(k).Code,buyunit,0,'market','totalbuy');
[~]=entryalter(k,barnum,1,1,buyunit,default_exitway,1);
% 合约号,barnum,方向,开关,手数,出场,策略
end;
if s(1).sellshortcon && s(1).marketposition==0
sellshortunit=default_unit;
orderID1=traderDirectSell(HandleList(1),targetList(k).Market,targetList(k).Code,sellshortunit,0,'market','totalbuy');
[~]=entryalter(k,barnum,-1,1,sellshortunit,default_exitway,1);
% 合约号,barnum,方向,开关,手数,出场,策略
end;
end
end
更多免费策略源码下载请登录atrader社区-策略服务下载,http://www.atrader.com.cn/stra.php
底部钓鱼交易系统源码下载,http://www.atrader.com.cn/stra.php?mod=model&pid=140 |
附件: 你需要登录才可以下载或查看附件。没有帐号?注册
|