全部版块 我的主页
论坛 金融投资论坛 六区 金融学(理论版) 量化投资
3454 2
2017-03-21
均线通道突破+加仓
策略原理:
         20均线为中轴,上下一个单位的标准差构成一个均线通道
         多头入场:价格突破通道上轨,且成为近期高点
         空头入场:价格突破通道下轨,且成为近期低点
         加仓:价格每变动2倍ATR
         出场:动态跟踪止损

回测曲线(由Auto-Trader提供回测报告)

均线通道+突破+加仓.png

策略源码:

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=11;    lags=150;    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    % 虚拟交易所初始手数    totalunit=0;        %-------------------------交易逻辑-------------------------------%    %----------入场信号--------------------%%     len=80;%     p=0.3;    ma0=ma(close,5);    ma1=ma(close,20);    std1=stdev(close,20);    upline=ma1+std1;    dnline=ma1-std1;%     HC=max(Dclose(end-len:end-1));%     LC=min(Dclose(end-len:end-1));%     HH=max(Dhigh(end-len:end-1));%     LL=min(Dlow(end-len:end-1));%     range=max(HH-LC,HC-LL);%     upline=Dopen(end)+range*p1;%     dnline=Dopen(end)-range*p2;%     HH=max(high(end-len+1:end));%     LL=min(low(end-len+1:end));%     con1=close(end)<LL+p*(HH-LL);%     con2=close(end)>HH-p*(HH-LL);%     con3=ma0(end)>ma1(end) && ma0(end-1)<ma1(end-1);%     con4=ma0(end)<ma1(end) && ma0(end-1)>ma1(end-1);%     s(1).buycon=con1 && con3;%     s(1).sellshortcon=con2 && con4;    len1=20;    con1=ma0(end)>upline(end);    con2=ma0(end)<dnline(end);    con3=close(end)>max(close(end-len1+1:end-1));    con4=close(end)<min(close(end-len1+1:end-1));    s(1).buycon=con1 && con3;    s(1).sellshortcon=con2 && con4;    %----------主动出场信号----------------%    s(1).sellcon=0;    s(1).buytocovercon=0;          %------------被动出场操作------------------%    %找到未平仓的订单    remain=remainorder(entry,k);    %对未平仓的订单进行平仓判断及操作    for i=1:length(remain.entrybar);        % 进仓以来的bar个数        barsinceentry=barnum-remain.entrybar(i);        backlen=10;    % 回溯的长度(进仓bar之前)        % 回溯的信息提取        [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;            initialATRparam=1.4;            AF=0.02;            minATRparam=1;            [longstopcon,shortstopcon,exitline]=exit2(backopen,backhigh,backlow,backclose,remain.entrydirection(i),backlen,initialATRparam,AF,minATRparam);        end;        % 出场执行        if longstopcon            totalunit=totalunit-remain.entryunit(i);            entry.record{k}(remain.num(i))=0;        end;        if shortstopcon            totalunit=totalunit+remain.entryunit(i);            entry.record{k}(remain.num(i))=0;        end;    end;        %{    %------------------- 主动出场操作 --------------------%    %再次找到未平仓的订单    remain=remainorder(entry,k);    % 找到策略i的marketposition    s=mptaking(s,remain);    %----------------策略1----------------------%    if s(1).sellcon && s(1).marketposition>0        totalunit=totalunit-abs(s(2).marketposition);        % 把已经平掉的订单的开关关掉        for j=1:length(s(1).num)            entry.record{k}(remain.num(s(1).num(j)))=0;        end;    end;    if s(1).buytocovercon && s(1).marketposition<0        totalunit=totalunit+abs(s(2).marketposition);        % 把已经平掉的订单的开关关掉        for j=1:length(s(1).num)            entry.record{k}(remain.num(s(1).num(j)))=0;        end;    end;    %----------------策略2----------------------%    if s(2).sellcon && s(2).marketposition>0        totalunit=totalunit-s(2).marketposition;        % 把已经平掉的订单的开关关掉        for j=1:length(s(2).num)            entry.record{k}(remain.num(s(2).num(j)))=0;        end;    end;    if s(2).buytocovercon && s(2).marketposition<0        totalunit=totalunit+s(2).marketposition;        % 把已经平掉的订单的开关关掉        for j=1:length(s(2).num)            entry.record{k}(remain.num(s(2).num(j)))=0;        end;    end;    %}        %---------------------------加仓--------------------------------%    %----------------策略1----------------------%    %再次找到未平仓的订单    remain=remainorder(entry,k);    % 找到策略i的marketposition    s=mptaking(s,remain);    % 找到最近的加仓点和加仓价格    if s(1).marketposition~=0;        lastentrybar=max(remain.entrybar(s(1).num));        lastbarsinceentry=barnum-lastentrybar;        lastentryprice=open(end-lastbarsinceentry+1);        vector=remain.entrydirection(s(1).num);        lastdirection=vector(end);        TRvalue=TR(close,high,low);        ATR=ma(TRvalue,10);        s(1).addbuycon=0;        s(1).addsellshortcon=0;        if lastdirection>0            s(1).addbuycon=close(end)-lastentryprice>2*ATR(end);        elseif lastdirection<0            s(1).addsellshortcon=lastentryprice-close(end)>2*ATR(end);        end;        %---------------加仓操作-------------------------%        if s(1).addbuycon && s(1).marketposition>=default_unit && s(1).marketposition<default_unit*2            addbuyunit=default_unit*0.5;            totalunit=totalunit+addbuyunit;            [~]=entryalter(k,barnum,1,1,addbuyunit,5,1);            % 合约号,barnum,方向,开关,手数,出场,策略        end;        if s(1).addsellshortcon && s(1).marketposition<=-default_unit && s(1).marketposition>-default_unit*2            addsellshortunit=default_unit*0.5;            totalunit=totalunit-addsellshortunit;            [~]=entryalter(k,barnum,-1,1,addsellshortunit,5,1);            % 合约号,barnum,方向,开关,手数,出场,策略        end;    end;    %---------------------------入场操作--------------------------------%    %----------------策略1----------------------%    if s(1).buycon && s(1).marketposition==0        buyunit=default_unit;        totalunit=totalunit+buyunit;        [~]=entryalter(k,barnum,1,1,buyunit,default_exitway,1);        % 合约号,barnum,方向,开关,手数,出场,策略    end;        if s(1).sellshortcon && s(1).marketposition==0        sellshortunit=default_unit;        totalunit=totalunit-sellshortunit;        [~]=entryalter(k,barnum,-1,1,sellshortunit,default_exitway,1);        % 合约号,barnum,方向,开关,手数,出场,策略    end;         %{    %----------------策略2----------------------%    if s(2).buycon && s(2).marketposition==0        buyunit=default_unit;        totalunit=totalunit+buyunit;        [~]=entryalter(k,barnum,1,1,buyunit,default_exitway,2);        % 合约号,barnum,方向,开关,手数,出场,策略    end;    if s(2).sellshortcon && s(2).marketposition==0        sellshortunit=default_unit;        totalunit=totalunit-sellshortunit;        [~]=entryalter(k,barnum,-1,1,sellshortunit,default_exitway,2);        % 合约号,barnum,方向,开关,手数,出场,策略    end;      %}    %---------------------- 虚拟交易所最终执行买卖 ------------------------------%    if totalunit>0        orderID1=traderDirectBuy(HandleList(1),targetList(k).Market,targetList(k).Code,abs(totalunit),0,'market','totalbuy');    elseif totalunit<0        orderID2=traderDirectSell(HandleList(1),targetList(k).Market,targetList(k).Code,abs(totalunit),0,'market','totalsell');    end;endend




更多免费策略源码下载请登录DigQuant社区-策略资源获取~


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2017-3-21 16:35:28
期待下一篇~~已关注楼主~~~
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-3-21 18:59:16
感谢支持~
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群