请选择 进入手机版 | 继续访问电脑版

QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 5376|回复: 2

均线系统策略(附源码)

[复制链接]
字体大小: 正常 放大
sdfg123        

3

主题

13

听众

6

积分

升级  1.05%

该用户从未签到

自我介绍
发表于 2017-3-2 14:11 |显示全部楼层
|招呼Ta 关注Ta
策略原理:; F! P4 ^+ {8 S- _8 k% E
         多头入场:5日均线上穿65日均线且当前价格大于200日均线7 j1 r8 [8 t0 l. k) t9 [' x# O: h
         空头入场:5日均线下穿65日均线且当前价格小于200日均线
# y' _9 Z8 r4 ^+ J9 p         出场:动态跟踪止损出场
4 h, i7 P' I/ G- S0 L6 G" c+ I8 U! k4 A9 ~. _: [: |7 Z" O
策略代码:
# g" W5 S0 H1 d6 F4 J
function  Strategy1(default_unit,default_exitway,freq)%

" C+ ^  l9 \( H% C( q5 K
targetList  =  traderGetTargetList();  
%获取目标资产信息
HandleList  =  traderGetHandleList();
%获取账户句柄
global  entrybar;
for  k=1:length(targetList);

$ ~: S7 B( ]. |7 F1 P
        %--------------------仓位、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  legth(close)<lags  ||  length(Dclose)<dlags
                continue;
        end;        
        %-------------------------交易逻辑-------------------------------%
        %----------入场信号--------------------%
        TRvalue=TR(Dclose,Dhigh,Dlow);
        ATR=ma(TRvalue,10);
        ma2=ma(close,200);
        ma0=ma(close,5);
        ma1=ma(close,65);
        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
                sellcon=ma0(end)<ma1(end)  &&  ma0(end-1)>ma1(end-1);
                buytocovercon=ma0(end)>ma1(end)  &&  ma0(end-1)<ma1(end-1);
        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;
        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

! F- J% i; Q- o* \
3 |8 p; R, [0 G/ }- ~$ R  q
zan
sdfg123        

3

主题

13

听众

6

积分

升级  1.05%

该用户从未签到

自我介绍
回复

使用道具 举报

sdfg123        

3

主题

13

听众

6

积分

升级  1.05%

该用户从未签到

自我介绍
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2024-3-29 13:44 , Processed in 0.511871 second(s), 59 queries .

回顶部