选股:从沪深300中选取波动最大的前20支股票,指标是日均振幅,本次测试的时间为20130104-20141001,需计算2年前,即20110104-20121231的日均振幅,取最大的前20支。
入场:开始进入半仓,上升卖出,下跌买入
不同位置的仓位分别为0%、5%、15%、30%、50%、70%、85%、95%、100%
策略源码:
- function wg2(n1) % 网格交易法2:下跌8%卖出,上涨15%买入
- % 来源:https://www.ricequant.com/community/topic/539/#share-source-code_content_2867_538849
- %获取目标资产信息
- targetList = traderGetTargetList(); % 在RunBackTest中选择好的标的.
- %获取账户信息
- HandleList = traderGetHandleList();
- %=================================================================
- % 定义持有的账户为全局变量
- global h;
- if isempty(h)
- for i=1:length(targetList)
- h(i).OpenPrice=0;
- h(i).up1=0;
- h(i).up2=0;
- h(i).up3=0;
- h(i).up4=0;
- h(i).dn1=0;
- h(i).dn2=0;
- h(i).dn3=0;
- h(i).dn4=0;
- h(i).StockValue=0;
- h(i).Cash=100000000/length(targetList);
- h(i).GridNow=0;
- h(i).GridLast=0;
- h(i).bar=0;
- end
- end
- % 金额
- Each=100000000/length(targetList); % 为每只股票的总资金
- EachOpen=Each/2; % 为每只股票一开始进入为半仓
- k=n1;
- tc=[0.05;0.15;0.3;0.5;0.7;0.85;0.95;1]; % 不同位置对应的头寸
- %------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- for i = 1:length(targetList)-1 % 每个股票过一遍
- % lags为策略需要往前获取多少天
- lags=15;
- %策略中每次取数据的长度
- 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)<15
- continue;
- end
- if time(end)>=datenum('1-Jan-2013')
- if length(close)<15
- continue;
- end
- %------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- [marketposition,~,~]=traderGetAccountPosition(HandleList(1),targetList(i).Market,targetList(i).Code);
- % 连续5日下跌,不操作
- a1=sum(close(end-4:end)<close(end-5:end-1))==5;
- if a1
- continue;
- end
- % 大于5日平均或10日平均1.5倍以上,不操作
- ma5=tsmovavg(close,'s',5,1);
- ma10=tsmovavg(close,'s',10,1);
- a2=close(end)>1.5*ma5(end);
- a3=close(end)>1.5*ma10(end);
- if a2 || a3
- continue;
- end
-
- % 指数止损,前一天跌幅大于4%
- [time1,open1,high1,low1,close1,volume1,turnover1,openinterest1] = traderGetKData('sse','000001','day',1, 0-lags, 0,false,'FWard');
- stoploss=close1(end)/close1(end-1)<0.96;
- if marketposition>0 && stoploss && barnum>=h(i).bar
- orderID1=traderSell(HandleList(1),targetList(i).Market,targetList(i).Code,'all',0,'market','sell1');
- sharenum=marketposition;
- if orderID1~=0
- [~,~,price] = traderGetAccountPosition(HandleList(1),targetList(i).Market,targetList(i).Code); % 记录开仓的价格
- h(i).Cash=h(i).Cash+sharenum*price;
- continue;
- end
- end
-
-
- % 开仓,半仓
- if marketposition ==0 && h(i).Cash>100000000/length(targetList)/10
- sharenum=floor(h(i).Cash/2/close(end));
- orderID1=traderBuy(HandleList(1),targetList(i).Market,targetList(i).Code,sharenum,0,'market','buy1'); % 开多单
- if orderID1~=0
- [~,~,price] = traderGetAccountPosition(HandleList(1),targetList(i).Market,targetList(i).Code); % 记录开仓的价格
- h(i).OpenPrice=price;
- h(i).up1=price*1.15;
- h(i).up2=price*1.15^2;
- h(i).up3=price*1.15^3;
- h(i).up4=price*1.15^4;
- h(i).dn1=price*0.92;
- h(i).dn2=price*0.92^2;
- h(i).dn3=price*0.92^3;
- h(i).dn4=price*0.92^4;
- h(i).StockValue=sharenum*price;
- h(i).Cash=Each-sharenum*price;
- h(i).bar=barnum;
- end
- end
-
- if marketposition>0
- if close(end)>h(i).up3 && close(end)<h(i).up4
- h(i).GridNow=1;
- end
- if close(end)>h(i).up2 && close(end)<h(i).up3
- h(i).GridNow=2;
- end
- if close(end)>h(i).up1 && close(end)<h(i).up2
- h(i).GridNow=3;
- end
- if close(end)>h(i).dn1 && close(end)<h(i).up1
- h(i).GridNow=4;
- end
- if close(end)>h(i).dn2 && close(end)<h(i).dn1
- h(i).GridNow=5;
- end
- if close(end)>h(i).dn3 && close(end)<h(i).dn2
- h(i).GridNow=6;
- end
- if close(end)>h(i).dn4 && close(end)<h(i).dn3
- h(i).GridNow=7;
- end
- if close(end)<h(i).dn4
- h(i).GridNow=8;
- end
-
- if close(end-1)>h(i).up3 && close(end-1)<h(i).up4
- h(i).GridLast=1;
- end
- if close(end-1)>h(i).up2 && close(end-1)<h(i).up3
- h(i).GridLast=2;
- end
- if close(end-1)>h(i).up1 && close(end-1)<h(i).up2
- h(i).GridLast=3;
- end
- if close(end-1)>h(i).dn1 && close(end-1)<h(i).up1
- h(i).GridLast=4;
- end
- if close(end-1)>h(i).dn2 && close(end-1)<h(i).dn1
- h(i).GridLast=5;
- end
- if close(end-1)>h(i).dn3 && close(end-1)<h(i).dn2
- h(i).GridLast=6;
- end
- if close(end-1)>h(i).dn4 && close(end-1)<h(i).dn3
- h(i).GridLast=7;
- end
- if close(end-1)<h(i).dn4
- h(i).GridLast=8;
- end
-
- end
-
- [marketposition,~,~]=traderGetAccountPosition(HandleList(1),targetList(i).Market,targetList(i).Code);
- if marketposition>0 && h(i).GridLast>h(i).GridNow && barnum>=h(i).bar % 价格上升了,要卖
- sharenum=floor(((tc(h(i).GridLast,1)-tc(h(i).GridNow,1))*Each)/close(end));
- orderID1=traderSell(HandleList(1),targetList(i).Market,targetList(i).Code,sharenum,0,'market','sell1');
- if orderID1~=0
- [~,~,price] = traderGetAccountPosition(HandleList(1),targetList(i).Market,targetList(i).Code); % 记录开仓的价格
- h(i).Cash=h(i).Cash+sharenum*price;
- end
- end
-
- if marketposition>0 && h(i).GridLast<h(i).GridNow && barnum>=h(i).bar % 价格跌了,要买
- sharenum=floor(((-tc(h(i).GridLast,1)+tc(h(i).GridNow,1))*Each)/close(end));
- if close(end)*sharenum<=h(i).Cash
- orderID1=traderBuy(HandleList(1),targetList(i).Market,targetList(i).Code,sharenum,0,'market','sell1');
- if orderID1~=0
- [~,~,price] = traderGetAccountPosition(HandleList(1),targetList(i).Market,targetList(i).Code); % 记录开仓的价格
- h(i).Cash=h(i).Cash-sharenum*price;
- end
- end
- end
-
- if marketposition>0 && close(end)>h(i).up4 && barnum>h(i).bar % 价格升到区间上面,空仓
- orderID1=traderSell(HandleList(1),targetList(i).Market,targetList(i).Code,'all',0,'market','sell1');
- sharenum=marketposition;
- if orderID1~=0
- [~,~,price] = traderGetAccountPosition(HandleList(1),targetList(i).Market,targetList(i).Code); % 记录开仓的价格
- h(i).Cash=h(i).Cash+sharenum*price;
- end
- end
-
-
- end
- end
- end
更多免费策略源码下载请登录DigQuant社区-策略资源下载~


雷达卡


京公网安备 11010802022788号







