设为首页收藏本站

 找回密码
 注册
查看: 2693|回复: 0
打印 上一主题 下一主题

波动突破策略加止损 [复制链接]

精华
0
UID
247176
积分
162
帖子
72
主题
68
阅读权限
0
注册时间
2017-2-14
最后登录
2017-5-18
跳转到指定楼层
1#
发表于 2017-3-16 11:14:40 |只看该作者 |倒序浏览
策略思路:
从今天新开盘,确定今日的上轨和下轨,其中上轨由昨天的收盘价和过去10天的收盘价的标准差总和决定,下轨为二者之差。开盘价高于今日上轨,做多;反之做空。日内平仓。同时加入利用ATR控制的止损机制。

策略代码:

function jiabanLoss(freq) %
%freq为输入频率
%len为计算使用的长度
%band为波动乘数
%shareNum为操作的手数
%%%NEWS系统

targetList = traderGetTargetList();
HandleList = traderGetHandleList();
global p;
  global record;
for i=1:length(targetList)
    barnum=traderGetCurrentBar(targetList(i).Market,targetList(i).Code);
    marketposition=traderGetAccountPosition(HandleList(1),targetList(i).Market,targetList(i).Code);
   
    len=30;
    dlen=30;
    [time,open,high,low,close,volume,turnover,openinterest] = traderGetKData(targetList(i).Market,targetList(i).Code,'min',freq, 0-len, 0,false,'FWard');
    [Dtime,Dopen,Dhigh,Dlow,Dclose,Dvolume,Dturnover,Dopeninterest] = traderGetKData(targetList(i).Market,targetList(i).Code,'day',1, 0-dlen, 0,false,'FWard');
   
    if length(close)<len+1||length(Dclose)<dlen+1
        continue;
    end
    %----------------判断是否是今天的第一根bar--------------%
    if floor(time(end))~=floor(time(end-1))
       stds=std(close(end-10:end-1));
         p{i}.upline=close(end)+stds;
         p{i}.dnline=close(end)-1*stds;        
    end
     ATR=ATR(Dhigh,Dlow,Dclose,20);
    t0=rem(time(end),floor(time(end))); %时间求余,转化到分钟和小时上
    cont=(t0>=(14+40/60)/24)&&(t0<=(15+15/60)/24);%寻找时间大于14点40小于15点15的时间段的下标,日内平仓的信号
    con2=close(end)<p{i}.dnline;%跌低看涨,反手
    con1=close(end)>p{i}.upline;%高涨看跌,反手
    con10=cont;
    con20=cont;
    %-------------------------平仓止损--------------------%
    %做多平仓
    if marketposition>0&&record{i}.m~=0
        if close(end)<record{i}.entryp-0.5*ATR(end)||con10
            order= traderPositionTo(HandleList(1),targetList(i).Market,targetList(i).Code,0,0,'market','sell');
            if order~=0
                record{i}.m=0;
             record{i}.entryp=close(end);
               
            end
        end
    end
   
    if marketposition<0&&record{i}.m~=0
        if close(end)>record{i}.entryp+0.5*ATR(end)||con20
            order= traderPositionTo(HandleList(1),targetList(i).Market,targetList(i).Code,0,0,'market','sell');
            if order~=0
                record{i}.m=0;
           record{i}.entryp=close(end);
               
            end
        end
    end
   
  [~,~,Multiple,MinMove,~,~,~,LongMargin,~] = traderGetFutureInfo(targetList(i).Market,targetList(i).Code);
    [cash,~,~,~,~] = traderGetAccountInfo(HandleList(1));
    sharenum=cash/close(end)/LongMargin/7/Multiple;
    %---------------入场-------------%
    if con1&(record{i}.m==0)&(marketposition==0)
        order=traderBuy(HandleList(1),targetList(i).Market,targetList(i).Code,sharenum,0,'market','buy'); %%%%做多
        if order~=0
            record{i}.m=record{i}.m+1;
            record{i}.entryp=close(end);
            
        end
    end
   
    if con2&(record{i}.m==0)&(marketposition==0)
        order=traderSellShort(HandleList(1),targetList(i).Market,targetList(i).Code,sharenum,0,'market','buy'); %%%%做多
        if order~=0
            record{i}.m=record{i}.m+1;
            record{i}.entryp=close(end);            
        end
    end
   
   
end
end

function ATRValue=ATR(High,Low,Close,Length)
ATRValue=zeros(length(High),1);
TRValue=zeros(length(High),1);
TRValue(2:end)=max([High(2:end)-Low(2:end) abs(High(2:end)-Close(1:end-1)) abs(Low(2:end)-Close(1:end-1))],[],2);
ATRValue=MA(TRValue,Length);
end

function MAValue=MA(Price,Length)
MAValue=zeros(length(Price),1);
for i=Length:length(Price)
    MAValue(i)=sum(Price(i-Length+1:i))/Length;
end
MAValue(1ength-1)=Price(1ength-1);
end

更多免费策略源码下载请登录DigQuant社区-策略资源,http://www.digquant.com.cn/stra.php

波动突破策略加止损策略源码下载:http://www.digquant.com.cn/stra.php?mod=model&pid=104
您需要登录后才可以回帖 登录 | 注册

bottom

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

GMT+8, 2024-5-2 11:28

Powered by Discuz! X2 LicensedChrome插件扩展

© 2011-2012 交易开拓者 Inc.

回顶部