matlab 突破交易策略,【每日一策】Matlab量化交易策略之 突破系统

function Strategy1(default_unit,default_exitway,freq)%

targetList = traderGetTargetList();

%获取目标资产信息

HandleList = traderGetHandleList();

%获取账户句柄

global entry;

for k=1:length(targetList);

%--------------------仓位、K线、当前bar的提取-----------------------------%

%获取当前仓位

[marketposition,~,~]=traderGetAccountPosition(HandleList(1),targetList(k).Market,targetList(k).Code);

%策略中每次取数据的长度

dlags=20;

lags=300;

barnum=traderGetCurrentBar(targetList(k).Market,targetList(k).Code);

%数据长度限制

if(barnum

continue;

end

%获取K线数据

[time,open,high,low,close,volume,turnover,openinterest] = traderGetKData(targetList(k).Market,targetList(k).Code,'min',freq, 0-lags, 0,false,'FWard');

%     [time1,open1,high1,low1,close1,volume1,turnover1,openinterest1] = traderGetKData(targetList(k).Market,targetList(k).Code,'min',freq*4, 0-lags1, 0,false,'FWard');

[Dtime,Dopen,Dhigh,Dlow,Dclose,Dvolume,Dturnover,Dopeninterest] = traderGetKData(targetList(k).Market,targetList(k).Code,'day',1,0-dlags, 0,false,'FWard');

if length(close)

continue;

end

% 虚拟交易所初始手数

totalunit=0;

%-------------------------交易逻辑-------------------------------%

%----------入场信号--------------------%

buy0=high(end)>max(high(end-100:end-1));

sellshort0=low(end)

dma=ma(Dclose,20);

con1=Dclose(end)>dma(end);

s(1).buycon=buy0 && con1;

s(1).sellshortcon=sellshort0 && ~con1;

%------------被动出场操作------------------%

%找到未平仓的订单

remain=remainorder(entry,k);

%对未平仓的订单进行平仓判断及操作

for i=1:length(remain.entrybar);

% 进仓以来的bar个数

barsinceentry=barnum-remain.entrybar(i);

backlen=20;    % 回溯的长度(进仓bar之前)

longstopcon=0;

shortstopcon=0;

% 回溯的信息提取

[backtime,backopen,backhigh,backlow,backclose,~,~,~] = traderGetKData(targetList(k).Market,targetList(k).Code,'min',freq, 0-barsinceentry-backlen, 0,false,'FWard');

% 根据出场方式计算出场条件

if remain.entryexitway(i)==1;

AFinitial=0;

AFparam=0.02;

AFmax=0.2;

Firstbarmultp=1;  %影响第一根bar的止损价,调高表示可忍受的回撤越多

[longstopcon,shortstopcon,exitline]=exit1(backopen,backhigh,backlow,backclose,remain.entrydirection(i),backlen,AFinitial,AFparam,AFmax,Firstbarmultp);

elseif remain.entryexitway(i)==2;

initialATRparam=2;

AF=0.02;

minATRparam=1;

[longstopcon,shortstopcon,exitline]=exit2(backopen,backhigh,backlow,backclose,remain.entrydirection(i),backlen,initialATRparam,AF,minATRparam);

elseif remain.entryexitway(i)==3;

[longstopcon,shortstopcon,exitline]=exit3(backopen,backhigh,backlow,backclose,remain.entrydirection(i),backlen);

elseif remain.entryexitway(i)==4

startpoint=10;

percent=0.3;

TRmutlp=1;

[longstopcon,shortstopcon,exitline]=exit4(backopen,backhigh,backlow,backclose,remain.entrydirection(i),backlen,startpoint,percent,TRmutlp);

elseif remain.entryexitway(i)==5;

stdlen=19;

initialstdparam=2;

minstdparam=1;

AF=0.2;

[longstopcon,shortstopcon,exitline]=exit5(backopen,backhigh,backlow,backclose,remain.entrydirection(i),backlen,stdlen,initialstdparam,minstdparam,AF);

elseif remain.entryexitway(i)==6;

longstopcon1=0;

shortstopcon1=0;

if remain.entrydirection(i)==1;

longstopcon1=low(end)

elseif remain.entrydirection(i)==-1

shortstopcon1=high(end)>max(high(end-20:end-1));

end;

stdlen=19;

initialstdparam=2;

minstdparam=1;

AF=0.1;

[longstopcon2,shortstopcon2,exitline]=exit5(backopen,backhigh,backlow,backclose,remain.entrydirection(i),backlen,stdlen,initialstdparam,minstdparam,AF);

longstopcon2=0;

shortstopcon2=0;

longstopcon=longstopcon1 || longstopcon2;

shortstopcon=shortstopcon1 || shortstopcon2;

end;

% 出场执行

if longstopcon

totalunit=totalunit-remain.entryunit(i);

orderID1=traderDirectSell(HandleList(1),targetList(k).Market,targetList(k).Code,remain.entryunit(i),0,'market','totalbuy');

entry.record{k}(remain.num(i))=0;

end;

if shortstopcon

totalunit=totalunit+remain.entryunit(i);

orderID1=traderDirectBuy(HandleList(1),targetList(k).Market,targetList(k).Code,remain.entryunit(i),0,'market','totalbuy');

entry.record{k}(remain.num(i))=0;

end;

end;

%---------------------------加仓--------------------------------%

%----------------策略1----------------------%

%再次找到未平仓的订单

remain=remainorder(entry,k);

% 找到策略i的marketposition

s=mptaking(s,remain);

% 找到最近的加仓点和加仓价格

%{

if s(1).marketposition~=0;

lastentrybar=max(remain.entrybar(s(1).num));

lastbarsinceentry=barnum-lastentrybar;

lastentryprice=open(end-lastbarsinceentry+1);

vector=remain.entrydirection(s(1).num);

lastdirection=vector(end);

TRvalue=TR(close,high,low);

ATR=2*ma(TRvalue,10);

s(1).addbuycon=0;

s(1).addsellshortcon=0;

if lastdirection>0

s(1).addbuycon=close(end)-lastentryprice>ATR(end) && high(end)>max(high(end-20:end-1)) && con1;

elseif lastdirection<0

s(1).addsellshortcon=lastentryprice-close(end)>ATR(end) && low(end)

end;

%---------------加仓操作-------------------------%

if s(1).addbuycon && s(1).marketposition>=2 && s(1).marketposition<4

addbuyunit=default_unit*0.5;

orderID1=traderDirectBuy(HandleList(1),targetList(k).Market,targetList(k).Code,addbuyunit,0,'market','totalbuy');

[~]=entryalter(k,barnum,1,1,addbuyunit,2,1);

% 合约号,barnum,方向,开关,手数,出场,策略

end;

if s(1).addsellshortcon && s(1).marketposition<=-2 && s(1).marketposition>-4

addsellshortunit=default_unit*0.5;

orderID1=traderDirectSell(HandleList(1),targetList(k).Market,targetList(k).Code,addsellshortunit,0,'market','totalbuy');

[~]=entryalter(k,barnum,-1,1,addsellshortunit,2,1);

% 合约号,barnum,方向,开关,手数,出场,策略

end;

end;

%}

%---------------------------入场操作--------------------------------%

%----------------策略1----------------------%

if s(1).buycon && s(1).marketposition==0

buyunit=default_unit;

orderID1=traderDirectBuy(HandleList(1),targetList(k).Market,targetList(k).Code,buyunit,0,'market','totalbuy');

[~]=entryalter(k,barnum,1,1,buyunit,default_exitway,1);

% 合约号,barnum,方向,开关,手数,出场,策略

end;

if s(1).sellshortcon && s(1).marketposition==0

sellshortunit=default_unit;

orderID1=traderDirectSell(HandleList(1),targetList(k).Market,targetList(k).Code,sellshortunit,0,'market','totalbuy');

[~]=entryalter(k,barnum,-1,1,sellshortunit,default_exitway,1);

% 合约号,barnum,方向,开关,手数,出场,策略

end;

end

end

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值