策略名称:CCI择时
策略思路:
I 为正值时,视为多头市场;为负值时,视为空头市场;
2.常态行情时,CCI 波动于±100 的间;强势行情,CCI 会超出±100 ;
I>100 时,买进,直到CCI
回测曲线:
QQ图片020533.png (83.9 KB, 下载次数: 1)
-2-20 10:27 上传
策略代码:
functionCCI1(n)%顺势指标
%获取目标资产信息
targetList=traderGetTargetList();%在RunBackTest中选择好的标的.
%获取账户信息
HandleList=traderGetHandleList();
%=================================================================
%RunBackTest的参数设置
%n=20;%CCI的参数
%=================================================================
%定义持有的账户为全局变量
globalholdingList;
ifisempty(holdingList)%判断cc是否为空值
holdingList(1).Market=0;
holdingList(1).Code=0;
holdingList(1).OpenBar=0;
holdingList(1).OpenPrice=0;
holdingList(1).Sharebum=0;
end
%定义可用金额
globalavailable;
ifisempty(available)%判断cc是否为空值
available=100000000;
end
%定义初始金额,每只股票的最大仓位为初始资金的1/300
initial=100000000;
initialeach=initial/length(targetList)*2;
k1=0;
%------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
fori=1:length(targetList)%每个股票过一遍
%lags为策略需要往前获取多少天
lags=30;
%策略中每次取数据的长度
barnum=traderGetCurrentBar(targetList(i).Market,targetList(i).Code);%K线的序号,后面会增加,前面的值对应的日期固定.
%数据长度限制,排除了前lags根k线
if(barnum
continue;
end
%策略开始部分
[time,open,high,low,close,volume,turnover,openinterest]=traderGetKData(targetList(i).Market,targetList(i).Code,'day',1,0-lags,0,false,'FWard');
iflength(close)<30
continue;
end
TP=(high+low+close)/3;
MATP=mean(TP(end-n+1:end));
a1=abs(TP(end-n+1:end)-MATP);
meanDev=mean(a1);
CCI=(TP-MATP)/(0.015*meanDev);
a=CCI(end)>100&&CCI(end-1)<100;%CCI上穿100,买入
ifa
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
%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%判断股票池里面的股票是否要买入,买入多少手
ifk1~=0
fori=1:length(stockList)
%等权重
sharenum=floor(initialeach/stockList(i).Price/100)*100;
%设置每只股票的交易手数,使得最大的亏损不大于初始账户的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);
ifmarketposition==0&&(sharenum*stockList(i).Price)<=1*available
orderID1=traderBuy(HandleList(1),stockList(i).Market,stockList(i).Code,sharenum,0,'market','buy1');%开多单
iforderID1~=0
[~,~,price]=traderGetAccountPosition(HandleList(1),stockList(i).Market,stockList(i).Code);%记录开仓的价格
available=available-sharenum*price;
ifholdingList(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;
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;
holdingList=[holdingList,holdingList1];
end
end
end
end
end
%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%出场设置
ifholdingList(1).Code~=0
fori=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);
TP=(high1+low1+close1)/3;
MATP=mean(TP(end-n+1:end));
a1=abs(TP(end-n+1:end)-MATP);
meanDev=mean(a1);
CCI=(TP-MATP)/(0.015*meanDev);
%出场条件设定,当CCI指标回到100,并距离前次上穿100在m天之内,我们卖出.或者下穿-100
b1=CCI(end-1)>100&&CCI(end)<=100;
b2=holdingList(1).OpenBar-barnum<6;
exitlong1=b1&&b2;
exitlong2=CCI(end-1)>-100&&CCI(end)
exitlong=exitlong1||exitlong2;
ifbarnum>=holdingList(i).OpenBar&&exitlong
orderID2=traderPositionTo(HandleList(1),holdingList(i).Market,holdingList(i).Code,0,0,'market','sell1');
iforderID2~=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;
fori=1:length(holdingList)
ifholdingList(i).Sharebum~=0
holdingList2=[holdingList2,holdingList(i)];
end
end
holdingList=holdingList2(2:end);
end
%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
end
更多免费策略下载,请登录Atrader社区-策略服务下载,ww /stra.php
CCI择时策略代码下载:w /stra.php?mod=model&pid=88