设为首页收藏本站

 找回密码
 注册
查看: 2793|回复: 0

网格资金管理(多空)策略源码 [复制链接]

精华
0
UID
247176
积分
162
帖子
72
主题
68
阅读权限
0
注册时间
2017-2-14
最后登录
2017-5-18
发表于 2017-2-28 15:09:17 |显示全部楼层
策略原理:
          将资金分为N份,采取随机抛点的形式入场,止损为10%,止盈为11%
          如果该份资金获利超过11%,则上移止盈止损线,且启动下一份资金抛点入场。
          同时在原策略的基础上加上空头入场

策略代码:
function  Strategy1(freq)%
targetList  =  traderGetTargetList();  
%获取目标资产信息
HandleList  =  traderGetHandleList();
%获取账户句柄
global  record;
for  k=1:length(targetList);
        
        %--------------------仓位、K线、当前bar的提取-----------------------------%
        %获取当前仓位
        [marketposition,~,~]=traderGetAccountPosition(HandleList(1),targetList(k).Market,targetList(k).Code);
        %策略中每次取数据的长度
        lags=121;
        dlags=31;
        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,~,~]  =  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(close,high,low);
%          ATR=ma(TRvalue,50);
%          p=5;
%          stoplossratio=p*mean(ATR(end-20:end-1))/mean(Dclose(end-20:end-1));                              %  止损触发比例
        stoplossratio=0.02;
        stopearnratio=stoplossratio*1.1;                                                                                                    %  止盈触发比例
        %  未平仓的订单提取
        remain_num=find(record.isopen==1);
        remain.isopen=record.isopen(remain_num);
        remain.isearn=record.isearn(remain_num);
        remain.pivotprice=record.pivotprice(remain_num);
        remain.entrybar=record.entrybar(remain_num);
        remain.unit=record.unit(remain_num);
        remain.direction=record.direction(remain_num);
%          remain.stoplossratio=record.stoplossratio(remain_num);
%          remain.stopearnratio=record.stopearnratio(remain_num);
        %  计算购买手数
        [ValidCash,MarketCap,OrderFrozen,MarginFrozen,PositionProfit]  =  traderGetAccountInfo(HandleList(1));
        [~,~,Multiple,~,~,~,~,~,~]  =  traderGetFutureInfo(targetList(k).Market,targetList(k).Code);
        remain_share=10-length(remain_num);
        con1=0;
        if  remain_share~=0;
                con1=1;      %  表示仍有可用资金购买
                openunit=fix(ValidCash/remain_share/close(end)/Multiple);
        end;
        %  判断当前是多头持仓还是空头持仓
        posdir=0;  
        if  ~isempty(remain.direction)
                if  remain.direction(end)>0
                        posdir=1;
                elseif  remain.direction(end)<0
                        posdir=-1;
                end;
        end;
        %  对未平仓的订单进行平仓或者调整止损线
        for  i=1:length(remain_num)
                index=remain_num(i);
                if  remain.direction(i)==1
                        if  close(end)<remain.pivotprice(i)-stoplossratio*remain.pivotprice(i)  %  触发止损线
                                orderID3=traderDirectSell(HandleList(1),targetList(k).Market,targetList(k).Code,remain.unit(i),0,'market','sell');
                                if  orderID3==0
                                        continue;
                                end;
                                record.isopen(index)=0;
                        elseif  close(end)>remain.pivotprice(i)+stopearnratio*remain.pivotprice(i)  %  触发止盈线
%                                  record.stoplossratio(index)=(close(end)-record.pivotprice(index))/close(end);
%                                  record.stopearnratio(index)=record.stoplossratio(index)*1.1;
                                record.pivotprice(index)=close(end);
                                record.isearn(index)=2;
                        end;
                elseif  remain.direction(i)==-1
                        if  close(end)>remain.pivotprice(i)+stoplossratio*remain.pivotprice(i)  %  触发止损线
                                orderID4=traderDirectBuy(HandleList(1),targetList(k).Market,targetList(k).Code,remain.unit(i),0,'market','buy');
                                if  orderID4==0
                                        continue;
                                end;
                                record.isopen(index)=0;
                        elseif  close(end)<remain.pivotprice(i)-stopearnratio*remain.pivotprice(i)  %  触发止盈线
%                                  record.stoplossratio(index)=(record.pivotprice(index)-close(end))/close(end);
%                                  record.stopearnratio(index)=record.stoplossratio(index)*1.1;
                                record.pivotprice(index)=close(end);
                                record.isearn(index)=2;
                        end;
                end;
        end;
        %  入场条件
%          ma0=ma(close,5);
        ma1=ma(close,20);
        std1=stdev(close,20);
        con2=isempty(find(remain.isearn==1,1));                            %  所有头寸都是盈利的才考虑进新的头寸
        bcon=close(end)>ma1(end)+2*std1(end)  &&  close(end-1)>ma1(end-1)+2*std1(end-1);
        scon=close(end)<ma1(end)-2*std1(end)  &&  close(end-1)<ma1(end-1)-2*std1(end-1);
        buycon=con1  &&  con2  &&  bcon  &&  posdir>=0;
        sellshortcon=con1  &&  con2  &&  scon  &&  posdir<=0;
        %---------------------------入场操作--------------------------------%
        if  buycon
                orderID1=traderDirectBuy(HandleList(1),targetList(k).Market,targetList(k).Code,openunit,0,'market','buy');
                if  orderID1==0
                        continue;
                end;
                record.pivotprice=[record.pivotprice,close(end)];
                record.isearn=[record.isearn,1];
                record.isopen=[record.isopen,1];
                record.unit=[record.unit,openunit];
                record.entrybar=[record.entrybar,barnum];
                record.direction=[record.direction,1];
%                  record.stoplossratio=[record.stoplossratio,stoplossratio];
%                  record.stopearnratio=[record.stopearnratio,stopearnratio];
        end;
        if  sellshortcon
                orderID2=traderDirectSell(HandleList(1),targetList(k).Market,targetList(k).Code,openunit,0,'market','sell');
                if  orderID2==0
                        continue;
                end;
                record.pivotprice=[record.pivotprice,close(end)];
                record.isearn=[record.isearn,1];
                record.isopen=[record.isopen,1];
                record.unit=[record.unit,openunit];
                record.entrybar=[record.entrybar,barnum];
                record.direction=[record.direction,-1];
%                  record.stoplossratio=[record.stoplossratio,stoplossratio];
%                  record.stopearnratio=[record.stopearnratio,stopearnratio];
        end;
end
end

更多免费策略源码下载:http://www.atrader.com.cn/stra.php

网格资金管理策略源码下载:http://www.atrader.com.cn/stra.php?mod=model&pid=151
您需要登录后才可以回帖 登录 | 注册

bottom

静态版|手机版|联系我们|交易开拓者 ( 粤ICP备07044698   

GMT+8, 2024-3-29 09:43

Powered by Discuz! X2 LicensedChrome插件扩展

© 2011-2012 交易开拓者 Inc.

回顶部