楼主: 挖矿专家
1581 2

[源码分享] 【每日一策】Matlab量化交易策略之 等分网格股票交易2 [推广有奖]

  • 0关注
  • 74粉丝

讲师

22%

还不是VIP/贵宾

-

威望
0
论坛币
2016 个
通用积分
5.2622
学术水平
21 点
热心指数
21 点
信用等级
21 点
经验
6055 点
帖子
403
精华
0
在线时间
151 小时
注册时间
2017-2-8
最后登录
2017-6-27

楼主
挖矿专家 发表于 2017-4-5 15:04:40 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
策略原理
选股:从沪深300中选取波动最大的前20支股票,指标是日均振幅,本次测试的时间为20130104-20141001,需计算2年前,即20110104-20121231的日均振幅,取最大的前20支。
入场:开始进入半仓,上升卖出,下跌买入
不同位置的仓位分别为0%、5%、15%、30%、50%、70%、85%、95%、100%


策略源码:
  1. function wg2(n1) % 网格交易法2:下跌8%卖出,上涨15%买入
  2. % 来源:https://www.ricequant.com/community/topic/539/#share-source-code_content_2867_538849
  3. %获取目标资产信息
  4. targetList = traderGetTargetList(); % 在RunBackTest中选择好的标的.
  5. %获取账户信息
  6. HandleList = traderGetHandleList();
  7. %=================================================================
  8. % 定义持有的账户为全局变量
  9. global h;
  10. if isempty(h)
  11.     for i=1:length(targetList)
  12.         h(i).OpenPrice=0;
  13.         h(i).up1=0;
  14.         h(i).up2=0;
  15.         h(i).up3=0;
  16.         h(i).up4=0;
  17.         h(i).dn1=0;
  18.         h(i).dn2=0;
  19.         h(i).dn3=0;
  20.         h(i).dn4=0;
  21.         h(i).StockValue=0;
  22.         h(i).Cash=100000000/length(targetList);
  23.         h(i).GridNow=0;
  24.         h(i).GridLast=0;
  25.         h(i).bar=0;      
  26.     end
  27. end
  28. % 金额
  29. Each=100000000/length(targetList); % 为每只股票的总资金
  30. EachOpen=Each/2; % 为每只股票一开始进入为半仓
  31. k=n1;
  32. tc=[0.05;0.15;0.3;0.5;0.7;0.85;0.95;1]; % 不同位置对应的头寸
  33. %------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  34. for i = 1:length(targetList)-1 % 每个股票过一遍
  35.     % lags为策略需要往前获取多少天
  36.     lags=15;
  37.     %策略中每次取数据的长度
  38.     barnum=traderGetCurrentBar(targetList(i).Market,targetList(i).Code); % K线的序号,后面会增加,前面的值对应的日期固定.
  39.     % 数据长度限制,排除了前lags根k线
  40.     if(barnum<lags)
  41.         continue;
  42.     end
  43.    
  44.     % 策略开始部分
  45.     [time,open,high,low,close,volume,turnover,openinterest] = traderGetKData(targetList(i).Market,targetList(i).Code,'day',1, 0-lags, 0,false,'FWard');
  46.     if length(close)<15
  47.         continue;
  48.     end
  49.     if time(end)>=datenum('1-Jan-2013')
  50.         if length(close)<15
  51.             continue;
  52.         end
  53.         %------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  54.         [marketposition,~,~]=traderGetAccountPosition(HandleList(1),targetList(i).Market,targetList(i).Code);
  55.         % 连续5日下跌,不操作
  56.         a1=sum(close(end-4:end)<close(end-5:end-1))==5;
  57.         if a1
  58.             continue;
  59.         end
  60.         % 大于5日平均或10日平均1.5倍以上,不操作
  61.         ma5=tsmovavg(close,'s',5,1);
  62.         ma10=tsmovavg(close,'s',10,1);
  63.         a2=close(end)>1.5*ma5(end);
  64.         a3=close(end)>1.5*ma10(end);
  65.         if a2 || a3
  66.             continue;
  67.         end
  68.         
  69.         % 指数止损,前一天跌幅大于4%
  70.         [time1,open1,high1,low1,close1,volume1,turnover1,openinterest1] = traderGetKData('sse','000001','day',1, 0-lags, 0,false,'FWard');
  71.         stoploss=close1(end)/close1(end-1)<0.96;
  72.         if marketposition>0 && stoploss && barnum>=h(i).bar
  73.             orderID1=traderSell(HandleList(1),targetList(i).Market,targetList(i).Code,'all',0,'market','sell1');
  74.             sharenum=marketposition;
  75.             if orderID1~=0
  76.                 [~,~,price] = traderGetAccountPosition(HandleList(1),targetList(i).Market,targetList(i).Code); % 记录开仓的价格
  77.                 h(i).Cash=h(i).Cash+sharenum*price;
  78.                 continue;
  79.             end
  80.         end
  81.         
  82.    
  83.         % 开仓,半仓
  84.         if marketposition ==0 && h(i).Cash>100000000/length(targetList)/10
  85.             sharenum=floor(h(i).Cash/2/close(end));
  86.             orderID1=traderBuy(HandleList(1),targetList(i).Market,targetList(i).Code,sharenum,0,'market','buy1'); % 开多单
  87.             if orderID1~=0
  88.                 [~,~,price] = traderGetAccountPosition(HandleList(1),targetList(i).Market,targetList(i).Code); % 记录开仓的价格
  89.                 h(i).OpenPrice=price;
  90.                 h(i).up1=price*1.15;
  91.                 h(i).up2=price*1.15^2;
  92.                 h(i).up3=price*1.15^3;
  93.                 h(i).up4=price*1.15^4;
  94.                 h(i).dn1=price*0.92;
  95.                 h(i).dn2=price*0.92^2;
  96.                 h(i).dn3=price*0.92^3;
  97.                 h(i).dn4=price*0.92^4;
  98.                 h(i).StockValue=sharenum*price;
  99.                 h(i).Cash=Each-sharenum*price;
  100.                 h(i).bar=barnum;
  101.             end
  102.         end
  103.         
  104.         if marketposition>0
  105.             if close(end)>h(i).up3 && close(end)<h(i).up4
  106.                 h(i).GridNow=1;
  107.             end
  108.             if close(end)>h(i).up2 && close(end)<h(i).up3
  109.                 h(i).GridNow=2;
  110.             end
  111.             if close(end)>h(i).up1 && close(end)<h(i).up2
  112.                 h(i).GridNow=3;
  113.             end
  114.             if close(end)>h(i).dn1 && close(end)<h(i).up1
  115.                 h(i).GridNow=4;
  116.             end
  117.             if close(end)>h(i).dn2 && close(end)<h(i).dn1
  118.                 h(i).GridNow=5;
  119.             end
  120.             if close(end)>h(i).dn3 && close(end)<h(i).dn2
  121.                 h(i).GridNow=6;
  122.             end
  123.             if close(end)>h(i).dn4 && close(end)<h(i).dn3
  124.                 h(i).GridNow=7;
  125.             end
  126.             if close(end)<h(i).dn4
  127.                 h(i).GridNow=8;
  128.             end
  129.                      
  130.             if close(end-1)>h(i).up3 && close(end-1)<h(i).up4
  131.                 h(i).GridLast=1;
  132.             end
  133.             if close(end-1)>h(i).up2 && close(end-1)<h(i).up3
  134.                 h(i).GridLast=2;
  135.             end
  136.             if close(end-1)>h(i).up1 && close(end-1)<h(i).up2
  137.                 h(i).GridLast=3;
  138.             end
  139.             if close(end-1)>h(i).dn1 && close(end-1)<h(i).up1
  140.                 h(i).GridLast=4;
  141.             end
  142.             if close(end-1)>h(i).dn2 && close(end-1)<h(i).dn1
  143.                 h(i).GridLast=5;
  144.             end
  145.             if close(end-1)>h(i).dn3 && close(end-1)<h(i).dn2
  146.                 h(i).GridLast=6;
  147.             end
  148.             if close(end-1)>h(i).dn4 && close(end-1)<h(i).dn3
  149.                 h(i).GridLast=7;
  150.             end
  151.             if close(end-1)<h(i).dn4
  152.                 h(i).GridLast=8;
  153.             end
  154.             
  155.         end  
  156.         
  157.         [marketposition,~,~]=traderGetAccountPosition(HandleList(1),targetList(i).Market,targetList(i).Code);   
  158.         if marketposition>0 && h(i).GridLast>h(i).GridNow && barnum>=h(i).bar % 价格上升了,要卖
  159.             sharenum=floor(((tc(h(i).GridLast,1)-tc(h(i).GridNow,1))*Each)/close(end));
  160.             orderID1=traderSell(HandleList(1),targetList(i).Market,targetList(i).Code,sharenum,0,'market','sell1');
  161.             if orderID1~=0
  162.                 [~,~,price] = traderGetAccountPosition(HandleList(1),targetList(i).Market,targetList(i).Code); % 记录开仓的价格
  163.                 h(i).Cash=h(i).Cash+sharenum*price;
  164.             end
  165.         end
  166.         
  167.         if marketposition>0 && h(i).GridLast<h(i).GridNow && barnum>=h(i).bar % 价格跌了,要买
  168.             sharenum=floor(((-tc(h(i).GridLast,1)+tc(h(i).GridNow,1))*Each)/close(end));
  169.             if close(end)*sharenum<=h(i).Cash
  170.                 orderID1=traderBuy(HandleList(1),targetList(i).Market,targetList(i).Code,sharenum,0,'market','sell1');
  171.                 if orderID1~=0
  172.                     [~,~,price] = traderGetAccountPosition(HandleList(1),targetList(i).Market,targetList(i).Code); % 记录开仓的价格
  173.                     h(i).Cash=h(i).Cash-sharenum*price;
  174.                 end
  175.             end
  176.         end
  177.         
  178.         if marketposition>0 && close(end)>h(i).up4 && barnum>h(i).bar % 价格升到区间上面,空仓
  179.             orderID1=traderSell(HandleList(1),targetList(i).Market,targetList(i).Code,'all',0,'market','sell1');
  180.             sharenum=marketposition;
  181.             if orderID1~=0
  182.                 [~,~,price] = traderGetAccountPosition(HandleList(1),targetList(i).Market,targetList(i).Code); % 记录开仓的价格
  183.                 h(i).Cash=h(i).Cash+sharenum*price;
  184.             end
  185.         end
  186.         
  187.         
  188.     end
  189.     end
  190. end
复制代码


更多免费策略源码下载请登录DigQuant社区-策略资源下载~



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:股票交易

已有 1 人评分经验 论坛币 收起 理由
fantuanxiaot + 66 + 66 精彩帖子

总评分: 经验 + 66  论坛币 + 66   查看全部评分

沙发
ghjktdf 发表于 2017-4-6 10:29:56
感谢分享

藤椅
65425856 发表于 2017-4-7 11:19:53
感谢分享

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

本版微信群
加好友,备注jr
拉您进交流群
GMT+8, 2026-1-28 08:51