楼主: ghjktdf
1489 0

[源码分享] 【每日一策】Matlab量化交易策略之 基于均线和形态高低点 [推广有奖]

  • 1关注
  • 1粉丝

硕士生

19%

还不是VIP/贵宾

-

威望
0
论坛币
166 个
通用积分
0
学术水平
1 点
热心指数
2 点
信用等级
1 点
经验
1340 点
帖子
225
精华
0
在线时间
39 小时
注册时间
2017-2-16
最后登录
2017-6-26

楼主
ghjktdf 发表于 2017-4-6 14:26:27 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
基于均线和形态高低点
策略原理:
    多头信号:出现连续上涨的两根阳线同时满足短均线上穿长均线
    空头信号:出现连续下跌的两根阳线同时满足短均线下穿长均线


回测曲线(由Auto-trader提供回测报告)

基于均线和形态高低点.png


策略源码:

  1. function Strategy1(default_unit,default_exitway,freq)%

  2. targetList = traderGetTargetList();
  3. %获取目标资产信息
  4. HandleList = traderGetHandleList();
  5. %获取账户句柄

  6. global entry;
  7. for k=1:length(targetList);
  8.    
  9.     %--------------------仓位、K线、当前bar的提取-----------------------------%
  10.     %获取当前仓位
  11.     [marketposition,~,~]=traderGetAccountPosition(HandleList(1),targetList(k).Market,targetList(k).Code);
  12.    
  13.     %策略中每次取数据的长度
  14.     dlags=10;
  15.     lags=30;
  16.     barnum=traderGetCurrentBar(targetList(k).Market,targetList(k).Code);
  17.    
  18.     %数据长度限制
  19.     if(barnum<lags)
  20.         continue;
  21.     end
  22.    
  23.     %获取K线数据
  24.     [time,open,high,low,close,volume,turnover,openinterest] = traderGetKData(targetList(k).Market,targetList(k).Code,'min',freq, 0-lags, 0,false,'FWard');
  25. %     [Dtime,Dopen,Dhigh,Dlow,Dclose,Dvolume,Dturnover,Dopeninterest] = traderGetKData(targetList(k).Market,targetList(k).Code,'day',1,0-dlags, 0,false,'FWard');
  26.     if length(close)<lags
  27.         continue;
  28.     end
  29.     % 虚拟交易所初始手数
  30.     totalunit=0;
  31.    
  32.     %-------------------------交易逻辑-------------------------------%
  33.     %----------入场信号--------------------%
  34.     ma0=ma(close,5);
  35.     ma1=ma(close,20);
  36.     range=high-low;
  37.     positivebar=zeros(length(close),1);
  38.     negativebar=zeros(length(close),1);
  39.     for i=1:length(close)
  40.         if close(i)>high(i)-0.25*range(i)
  41.             positivebar(i)=1;
  42.         elseif close(i)<low(i)+0.25*range(i)
  43.             negativebar(i)=1;
  44.         end;
  45.     end;
  46.     s(1).buycon=positivebar(end) && negativebar(end-1) && ma0(end)>ma1(end);
  47.     s(1).sellshortcon=negativebar(end) && positivebar(end-1) && ma0(end)<ma1(end);
  48. %     s(1).buycon=con1 && close(end)>max(mclose);
  49. %     s(1).sellshortcon=con1 && close(end)<min(mclose);
  50.    
  51.     %----------主动出场信号----------------%
  52.     s(1).sellcon=0;
  53.     s(1).buytocovercon=0;
  54. %     s(1).sellcon=close(end)>dnline(end) && close(end-1)<dnline(end-1);
  55. %     s(1).buytocovercon=close(end)<upline(end) && close(end-1)>upline(end-1);
  56.     %------------被动出场操作------------------%
  57.     %找到未平仓的订单
  58.     remain=remainorder(entry,k);
  59.     %对未平仓的订单进行平仓判断及操作
  60.     for i=1:length(remain.entrybar);
  61.         % 进仓以来的bar个数
  62.         barsinceentry=barnum-remain.entrybar(i);
  63.         backlen=30;    % 回溯的长度(进仓bar之前)
  64.         % 回溯的信息提取
  65.         [backtime,backopen,backhigh,backlow,backclose,~,~,~] = traderGetKData(targetList(k).Market,targetList(k).Code,'min',freq, 0-barsinceentry-backlen, 0,false,'FWard');
  66.         % 根据出场方式计算出场条件
  67.         longstopcon=0;
  68.         shortstopcon=0;
  69.         if remain.entryexitway(i)==1;
  70.             AFinitial=0;
  71.             AFparam=0.02;
  72.             AFmax=0.2;
  73.             Firstbarmultp=1;  %影响第一根bar的止损价,调高表示可忍受的回撤越多
  74.             [longstopcon,shortstopcon,exitline]=exit1(backopen,backhigh,backlow,backclose,remain.entrydirection(i),backlen,AFinitial,AFparam,AFmax,Firstbarmultp);
  75.         elseif remain.entryexitway(i)==2;
  76.             ATRparam=2;
  77.             [longstopcon,shortstopcon,exitline]=exit2(backopen,backhigh,backlow,backclose,remain.entrydirection(i),backlen,ATRparam);
  78.         elseif remain.entryexitway(i)==3;
  79.             [longstopcon,shortstopcon,exitline]=exit3(backopen,backhigh,backlow,backclose,remain.entrydirection(i),backlen);
  80.         elseif remain.entryexitway(i)==4
  81.             startpoint=10;
  82.             percent=0.3;
  83.             TRmutlp=1;
  84.             [longstopcon,shortstopcon,exitline]=exit4(backopen,backhigh,backlow,backclose,remain.entrydirection(i),backlen,startpoint,percent,TRmutlp);
  85.         end;
  86.         % 出场执行
  87.         if longstopcon
  88.             totalunit=totalunit-remain.entryunit(i);
  89.             entry.record{k}(remain.num(i))=0;
  90.         end;
  91.         if shortstopcon
  92.             totalunit=totalunit+remain.entryunit(i);
  93.             entry.record{k}(remain.num(i))=0;
  94.         end;
  95.     end;
  96.     %------------------- 主动出场操作 --------------------%
  97. %     %再次找到未平仓的订单
  98.     remain=remainorder(entry,k);
  99.     % 找到策略i的marketposition
  100.     s=mptaking(s,remain);
  101.     %----------------策略1----------------------%
  102.     if s(1).sellcon && s(1).marketposition>0
  103.         totalunit=totalunit-abs(s(1).marketposition);
  104.         % 把已经平掉的订单的开关关掉
  105.         for j=1:length(s(1).num)
  106.             entry.record{k}(remain.num(s(1).num(j)))=0;
  107.         end;
  108.     end;
  109.     if s(1).buytocovercon && s(1).marketposition<0
  110.         totalunit=totalunit+abs(s(1).marketposition);
  111.         % 把已经平掉的订单的开关关掉
  112.         for j=1:length(s(1).num)
  113.             entry.record{k}(remain.num(s(1).num(j)))=0;
  114.         end;
  115.     end;
  116.    
  117.     %---------------------------入场操作--------------------------------%
  118.     %再次找到未平仓的订单
  119.     remain=remainorder(entry,k);
  120.     % 找到策略i的marketposition
  121.     s=mptaking(s,remain);
  122.     %----------------策略1----------------------%
  123.     if s(1).buycon && s(1).marketposition==0
  124.         buyunit=default_unit;
  125.         totalunit=totalunit+buyunit;
  126.         [~]=entryalter(k,barnum,1,1,buyunit,default_exitway,1);
  127.         % 合约号,barnum,方向,开关,手数,出场,策略
  128.     end;
  129.    
  130.     if s(1).sellshortcon && s(1).marketposition==0
  131.         sellshortunit=default_unit;
  132.         totalunit=totalunit-sellshortunit;
  133.         [~]=entryalter(k,barnum,-1,1,sellshortunit,default_exitway,1);
  134.         % 合约号,barnum,方向,开关,手数,出场,策略
  135.     end;  
  136.    
  137.     %---------------------- 虚拟交易所最终执行买卖 ------------------------------%
  138.     if totalunit>0
  139.         orderID1=traderDirectBuy(HandleList(1),targetList(k).Market,targetList(k).Code,abs(totalunit),0,'market','totalbuy');
  140.     elseif totalunit<0
  141.         orderID2=traderDirectSell(HandleList(1),targetList(k).Market,targetList(k).Code,abs(totalunit),0,'market','totalsell');
  142.     end;
  143. end
  144. end
复制代码

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




二维码

扫码加我 拉你入群

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

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


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

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