十大经典日内策略之空中花园(附源码分享)

今天为大家介绍十大经典日内策略系列的第三个——空中花园。空中花园的策略思路非常简单粗暴,同样的确定今天的交易通道。但是通道的确定依据的仅为今天第一根bar信息:上轨=第一根bar的最高价;下轨=第一根bar的最低价。
       入场条件:和所有突破通道的策略一样,价格突破上轨做多,价格突破下轨做空。但是为了保证赢面,限制了今天开盘的状态,只在今天是高开或者低开的时候,才进行轨道变化,否则沿用的仍为上一次的通道。
      出场条件:同样,反手出场。同时,收盘前,主动出场。  
       下为源代码:

  1. function kzhy(freq)%
  2. targetList = traderGetTargetList();
  3. %获取目标资产信息
  4. HandleList = traderGetHandleList();
  5. %获取账户句柄
  6. global upline;
  7. global dnline;
  8. if isempty(upline)||isempty(dnline)
  9. upline=zeros(length(targetList),1);%记录今天通道的上轨
  10. dnline=zeros(length(targetList),1);%记录今天通道的下轨
  11. end
  12. for k=1:length(targetList);
  13. %--------------------仓位、K线、当前bar的提取-----------------------------%
  14. %获取当前仓位
  15. [marketposition,~,~]=traderGetAccountPosition(HandleList(1),targetList(k).Market,targetList(k).Code);
  16. %策略中每次取数据的长度
  17. lags=40;
  18. dlags=31;
  19. barnum=traderGetCurrentBar(targetList(k).Market,targetList(k).Code);
  20. %数据长度限制
  21. if(barnum<lags)
  22. continue;
  23. end
  24. %获取K线数据
  25. [time,open,high,low,close,volume,~,~] = traderGetKData(targetList(k).Market,targetList(k).Code,'min',freq, 0-lags, 0,false,'FWard');
  26. [Dtime,Dopen,Dhigh,Dlow,Dclose,Dvolume,~,~] = traderGetKData(targetList(k).Market,targetList(k).Code,'day',1, 0-dlags, 0,false,'FWard');
  27. if length(close)<lags || length(Dclose)<dlags
  28. continue;
  29. end;
  30. %--------------------- 止损/入场条件计算 ------------------------------------%
  31. %计算出场时间
  32. gettime=datevec(time);
  33. numtime=gettime(end,4)*100+gettime(end,5);
  34. cont=0;
  35. if numtime>=0900&&numtime<1400||numtime>=2100&&numtime<=2400||numtime>0000&&numtime<0100
  36. cont=1;
  37. end
  38. if floor(time(end))~=floor(time(end-1))
  39. if open(end)>Dopen(end-1)||open(end)<Dopen(end-1)*0.99
  40. upline=Dhigh(end);
  41. dnline=Dlow(end);
  42. end
  43. end
  44. buycon=close(end)>upline && cont; % 突破range上轨道做多(每天只做一手)
  45. sellshortcon=close(end)<dnline && cont;% 突破 range下轨道做空(每天只做一手)
  46. shareNum=10;
  47. %--------------------------------------仓位操作-----------------%
  48. %-----------------入场--------------------------%
  49. if  marketposition==0
  50. if buycon
  51. traderBuy(HandleList(1),targetList(k).Market,targetList(k).Code,shareNum,0,'market','buy');%开多单
  52. elseif sellshortcon
  53. traderSellShort(HandleList(1),targetList(k).Market,targetList(k).Code,shareNum,0,'market','sellshort');%开空单
  54. end
  55. end
  56. if cont~=0
  57. %------------------------------多头出场----------------------------%
  58. if marketposition>0
  59. if sellshortcon
  60. traderPositionTo(HandleList(1),targetList(k).Market,targetList(k).Code,0,0,'market','sell');
  61. end
  62. end
  63. if marketposition<0
  64. if buycon
  65. traderPositionTo(HandleList(1),targetList(k).Market,targetList(k).Code,0,0,'market','sell');
  66. end
  67. end
  68. end
  69. %-------------------------------------------------------------日内平仓-----------------------------------%
  70. if cont==0
  71. traderPositionTo(HandleList(1),targetList(k).Market,targetList(k).Code,0,0,'market','sell');
  72. end
  73. end
  74. end
  75. function ATRValue=ATR(High,Low,Close,Length)
  76. ATRValue=zeros(length(High),1);
  77. TRValue=zeros(length(High),1);
  78. TRValue(2:end)=max([High(2:end)-Low(2:end) abs(High(2:end)-Close(1:end-1)) abs(Low(2:end)-Close(1:end-1))],[],2);
  79. ATRValue=MA(TRValue,Length);
  80. end
  81. function MAValue=MA(Price,Length)
  82. MAValue=zeros(length(Price),1);
  83. for i=Length:length(Price)
  84. MAValue(i)=sum(Price(i-Length+1:i))/Length;
  85. end
  86. MAValue(1:Length-1)=Price(1:Length-1);
  87. end
  88. %-------分割线---------%
  89. clear
  90. clc;
  91. %-----------获取targetList-----------------%
  92. targetList(1).Market = 'CFFEX';
  93. targetList(1).Code = 'IF0000';
  94. freq=3;
  95. %-------------------参数设定与全局变量----------------------%
  96. for i=1:length(targetList)
  97. %-----------------------回测----------------------------------%
  98. % 在回测时设置初始资本10000000000元、手续费率0.000026、无风险利率0.02、滑价0、默认1下一个bar的开盘价、默认0成交价、默认0直接成交
  99. % traderSetBacktest(1000000,0.000026,0.02,0,1,0,0);
  100. AccountList(1) = {'FutureBackReplay'};
  101. traderRunBacktest('kzhy',@kzhy,{freq},AccountList,targetList(i),'min',freq,20150101,20160815,'FWard');
  102. end

复制代码

策略表现:
权益曲线

多头表现:

空头表现:

策略绩效:

(0)

相关推荐