设为首页收藏本站

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

BOLL择时策略(附源码) [复制链接]

精华
0
UID
247176
积分
162
帖子
72
主题
68
阅读权限
0
注册时间
2017-2-14
最后登录
2017-5-18
跳转到指定楼层
1#
发表于 2017-3-15 16:37:59 |只看该作者 |倒序浏览
策略原理:
         选股标的:沪深300所有成分股
         买入信号:价格上穿布林带中轨
         资金分配:等权重买入
         卖出信号:当股价跌破上轨或中轨时卖出

策略源码:

function BOLL1(n1,n2,n4) % BOLL布林线.
%获取目标资产信息
targetList = traderGetTargetList(); % 在RunBackTest中选择好的标的.
%获取账户信息
HandleList = traderGetHandleList();
%=================================================================
% RunBackTest的参数设置
% n1=20; % 中线的均线参数
% n2=2; % 标准差倍数
% n4=2; % 几倍ATR
%=================================================================
% 定义持有的账户为全局变量
global holdingList;
if isempty(holdingList) % 判断cc是否为空值
    holdingList(1).Market=0;
    holdingList(1).Code=0;
    holdingList(1).OpenBar=0;
    holdingList(1).OpenPrice=0;
    holdingList(1).Sharebum=0;
    holdingList(1).StopLoss=0;
end

% 定义可用金额
global available;
if isempty(available) % 判断cc是否为空值
    available=100000000;
end
% 定义初始金额,每只股票的最大仓位为初始资金的1/300
initial=100000000;
initialeach=initial/length(targetList);
k1=0;
%------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
for i = 1:length(targetList) % 每个股票过一遍   
    % lags为策略需要往前获取多少天
    lags=n1+n2;
    %策略中每次取数据的长度
    barnum=traderGetCurrentBar(targetList(i).Market,targetList(i).Code); % K线的序号,后面会增加,前面的值对应的日期固定.
    % 数据长度限制,排除了前lags根k线
    if(barnum<lags)
        continue;
    end
   
    % 策略开始部分
    [time,open,high,low,close,volume,turnover,openinterest] = traderGetKData(targetList(i).Market,targetList(i).Code,'day',1, 0-lags, 0,false,'FWard');
    if length(close)<n1+n2
        continue;
    end
    if time(end)>=datenum('1-Jan-2014')
        if length(close)<n1+n2
            continue;
        end

        % BOLL
        [mid,upper,lower] =traderBOLL(n1,n2,targetList(i).Market,targetList(i).Code,'day',1, 0-lags, 0,false,'FWard');

        % 买入信号:当股价向上穿越中轨是买入
        a=close(end-1)<mid(end-1) && close(end)>mid(end);

        if a
            k1=k1+1;
            stockList(k1).Market=targetList(i).Market;
            stockList(k1).Code=targetList(i).Code;
            stockList(k1).Price=close(end);
            [ATR1, ~]=traderATR(14,targetList(i).Market,targetList(i).Code,'day',1,0-lags,0,false,'FWard');
            stockList(k1).ATR=ATR1(end);
        end
    end
end
%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% 判断股票池里面的股票是否要买入,买入多少手
if k1~=0
    for i=1:length(stockList)
        % 等权重买入
        sharenum=floor(initialeach/stockList(i).Price);
        % 设置每只股票的交易手数,使得最大的亏损不大于初始账户的 n2%.
%         sharenum=floor(stockList(i).Price*initialeach/stockList(i).ATR/10000);
        % 获取当前bar序号
        barnum=traderGetCurrentBar(stockList(i).Market,stockList(i).Code);
        %获取当前仓位
        [marketposition,~,~]=traderGetAccountPosition(HandleList(1),stockList(i).Market,stockList(i).Code);
        if marketposition ==0 && (sharenum*stockList(i).Price)<=1*available && time(end)>=datenum('1-Jan-2014')
            orderID1=traderBuy(HandleList(1),stockList(i).Market,stockList(i).Code,sharenum,0,'market','buy1'); % 开多单
            if orderID1~=0
                [~,~,price] = traderGetAccountPosition(HandleList(1),stockList(i).Market,stockList(i).Code); % 记录开仓的价格
                available=available-sharenum*price;
                if holdingList(1).Market==0
                    holdingList(1).Market=stockList(i).Market;
                    holdingList(1).Code=stockList(i).Code;
                    holdingList(1).OpenBar=barnum+1;
                    holdingList(1).OpenPrice=price;
                    holdingList(1).Sharebum=sharenum;
                    holdingList(1).StopLoss=price-n4*stockList(i).ATR;
                else
                    holdingList1(1).Market=stockList(i).Market;
                    holdingList1(1).Code=stockList(i).Code;
                    holdingList1(1).OpenBar=barnum+1;
                    holdingList1(1).OpenPrice=price;
                    holdingList1(1).Sharebum=sharenum;
                    holdingList1(1).StopLoss=price-n4*stockList(i).ATR;
                    holdingList=[holdingList,holdingList1];
                end
            end
        end
    end
end

%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% 出场设置
if holdingList(1).Code~=0
    for i=1:length(holdingList)
        [ATR2, ~]=traderATR(14,holdingList(i).Market,holdingList(i).Code,'day',1,0-lags,0,false,'FWard');
        [time1,open1,high1,low1,close1,volume1,turnover1,openinterest1] = traderGetKData(holdingList(i).Market,holdingList(i).Code,'day',1, 0-lags, 0,false,'FWard');
        barnum=traderGetCurrentBar(holdingList(i).Market,holdingList(i).Code);        
        if barnum>holdingList(i).OpenBar && high1(end-1)>high1(end-2)
            holdingList(i).StopLoss=high1(end-1)-n4*ATR2(end-1);
        end
        exitlong1=close1(end)<holdingList(i).StopLoss;

        % MAEMA
        [mid,upper,lower] =traderBOLL(n1,n2,holdingList(i).Market,holdingList(i).Code,'day',1, 0-lags, 0,false,'FWard');
        % 卖出信号:当股价跌破上轨或中轨时卖出
        b1=close(end-1)>upper(end-1) && close(end)<upper(end);
        b2=close(end-1)>mid(end-1) && close(end)>mid(end);
        exitlong2=b1 || b2;

        exitlong=exitlong1 || exitlong2;
        if barnum>=holdingList(i).OpenBar && exitlong2
            orderID2=traderPositionTo(HandleList(1),holdingList(i).Market,holdingList(i).Code,0,0,'market','sell1');
            if orderID2~=0
                exitprice=traderOrderFilledPrice(HandleList(1),orderID2);
                available=available+exitprice*holdingList(i).Sharebum;
                holdingList(i).Sharebum=0;
            end
        end
        
    end
    % 删除已经卖出的股票
    holdingList2(1).Market=0;
    holdingList2(1).Code=0;
    holdingList2(1).OpenBar=0;
    holdingList2(1).OpenPrice=0;
    holdingList2(1).Sharebum=0;
    holdingList2(1).StopLoss=0;
    for i=1:length(holdingList)
        if holdingList(i).Sharebum~=0
            holdingList2=[holdingList2,holdingList(i)];
        end
    end
    holdingList=holdingList2(2:end);
end
%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

end

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

BOLL择时策略源码下载:http://www.digquant.com.cn/stra.php?mod=model&pid=115
您需要登录后才可以回帖 登录 | 注册

bottom

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

GMT+8, 2024-5-2 19:17

Powered by Discuz! X2 LicensedChrome插件扩展

© 2011-2012 交易开拓者 Inc.

回顶部