十大经典日内策略之空中花园(附源码分享)
今天为大家介绍十大经典日内策略系列的第三个——空中花园。空中花园的策略思路非常简单粗暴,同样的确定今天的交易通道。但是通道的确定依据的仅为今天第一根bar信息:上轨=第一根bar的最高价;下轨=第一根bar的最低价。入场条件:和所有突破通道的策略一样,价格突破上轨做多,价格突破下轨做空。但是为了保证赢面,限制了今天开盘的状态,只在今天是高开或者低开的时候,才进行轨道变化,否则沿用的仍为上一次的通道。
出场条件:同样,反手出场。同时,收盘前,主动出场。
下为源代码:
[code]
function kzhy(freq)%
targetList = traderGetTargetList();
%获取目标资产信息
HandleList = traderGetHandleList();
%获取账户句柄
global upline;
global dnline;
if isempty(upline)||isempty(dnline)
upline=zeros(length(targetList),1);%记录今天通道的上轨
dnline=zeros(length(targetList),1);%记录今天通道的下轨
end
for k=1:length(targetList);
%--------------------仓位、K线、当前bar的提取-----------------------------%
%获取当前仓位
[marketposition,~,~]=traderGetAccountPosition(HandleList(1),targetList(k).Market,targetList(k).Code);
%策略中每次取数据的长度
lags=40;
dlags=31;
barnum=traderGetCurrentBar(targetList(k).Market,targetList(k).Code);
%数据长度限制
if(barnum<lags)
continue;
end
%获取K线数据
[time,open,high,low,close,volume,~,~] = traderGetKData(targetList(k).Market,targetList(k).Code,'min',freq, 0-lags, 0,false,'FWard');
[Dtime,Dopen,Dhigh,Dlow,Dclose,Dvolume,~,~] = traderGetKData(targetList(k).Market,targetList(k).Code,'day',1, 0-dlags, 0,false,'FWard');
if length(close)<lags || length(Dclose)<dlags
continue;
end;
%--------------------- 止损/入场条件计算 ------------------------------------%
%计算出场时间
gettime=datevec(time);
numtime=gettime(end,4)*100+gettime(end,5);
cont=0;
if numtime>=0900&&numtime<1400||numtime>=2100&&numtime<=2400||numtime>0000&&numtime<0100
cont=1;
end
if floor(time(end))~=floor(time(end-1))
if open(end)>Dopen(end-1)||open(end)<Dopen(end-1)*0.99
upline=Dhigh(end);
dnline=Dlow(end);
end
end
buycon=close(end)>upline && cont; % 突破range上轨道做多(每天只做一手)
sellshortcon=close(end)<dnline && cont;% 突破 range下轨道做空(每天只做一手)
shareNum=10;
%--------------------------------------仓位操作-----------------%
%-----------------入场--------------------------%
if marketposition==0
if buycon
traderBuy(HandleList(1),targetList(k).Market,targetList(k).Code,shareNum,0,'market','buy');%开多单
elseif sellshortcon
traderSellShort(HandleList(1),targetList(k).Market,targetList(k).Code,shareNum,0,'market','sellshort');%开空单
end
end
if cont~=0
%------------------------------多头出场----------------------------%
if marketposition>0
if sellshortcon
traderPositionTo(HandleList(1),targetList(k).Market,targetList(k).Code,0,0,'market','sell');
end
end
if marketposition<0
if buycon
traderPositionTo(HandleList(1),targetList(k).Market,targetList(k).Code,0,0,'market','sell');
end
end
end
%-------------------------------------------------------------日内平仓-----------------------------------%
if cont==0
traderPositionTo(HandleList(1),targetList(k).Market,targetList(k).Code,0,0,'market','sell');
end
end
end
function ATRValue=ATR(High,Low,Close,Length)
ATRValue=zeros(length(High),1);
TRValue=zeros(length(High),1);
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);
ATRValue=MA(TRValue,Length);
end
function MAValue=MA(Price,Length)
MAValue=zeros(length(Price),1);
for i=Length:length(Price)
MAValue(i)=sum(Price(i-Length+1:i))/Length;
end
MAValue(1:Length-1)=Price(1:Length-1);
end
%-------分割线---------%
clear
clc;
%-----------获取targetList-----------------%
targetList(1).Market = 'CFFEX';
targetList(1).Code = 'IF0000';
freq=3;
%-------------------参数设定与全局变量----------------------%
for i=1:length(targetList)
%-----------------------回测----------------------------------%
% 在回测时设置初始资本10000000000元、手续费率0.000026、无风险利率0.02、滑价0、默认1下一个bar的开盘价、默认0成交价、默认0直接成交
% traderSetBacktest(1000000,0.000026,0.02,0,1,0,0);
AccountList(1) = {'FutureBackReplay'};
traderRunBacktest('kzhy',@kzhy,{freq},AccountList,targetList(i),'min',freq,20150101,20160815,'FWard');
end
[/code] 謝謝 {:smile:} 学习学习
页:
[1]