function Strategyn(freq)%
targetList = traderGetTargetList();
%获取目标资产信息
HandleList = traderGetHandleList();
%获取账户句柄
global record;
global state;
for k=1:length(targetList);
%--------------------仓位、K线、当前bar的提取-----------------------------%
%获取当前仓位
[marketposition,~,~]=traderGetAccountPosition(HandleList(1),targetList(k).Market,targetList(k).Code);
%策略中每次取数据的长度
lags=60;
dlags=21;
barnum=traderGetCurrentBar(targetList(k).Market,targetList(k).Code);
%数据长度限制
if(barnum
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)
continue;
end;
% 每天的第一个bar将state全局变量复原,state的作用是保证每天只buy or sellshort 一次(后续的加仓没有限制)
if floor(time(end))~=floor(time(end-1))
state(k)=0;
end;
% 未平仓的订单提取
remain_num=find(record{k}.isopen==1);
remain.isopen=record{k}.isopen(remain_num);
remain.isearn=record{k}.isearn(remain_num);
remain.pivotprice=record{k}.pivotprice(remain_num);
remain.entrybar=record{k}.entrybar(remain_num);
remain.unit=record{k}.unit(remain_num);
remain.direction=record{k}.direction(remain_num);
% ------------------- 计算购买手数 ----------------------------- %
% 提取剩余可用资金
[ValidCash,MarketCap,~,~,~] = traderGetAccountInfo(HandleList(1));
[~,~,Multiple,~,~,~,~,LongMargin,ShortMargin] = traderGetFutureInfo(targetList(k).Market,targetList(k).Code);
% 计算当前还有几分资金没使用
num=0; % isopen==1 表示未平仓的资金
for i=1:length(record)
aa=find(record{i}.isopen==1);
num=num+length(aa);
end;
n=5;
remain_share=n*length(targetList)-num; % 资金总共分为 n*length(targetList)份,已经用了num 份
% con1:当n份资金都使用完了,con1=0,不继续开仓,因为没有足够的资金了
con1=0;
if remain_share~=0;
con1=1;
%openunit=fix(MarketCap/close(end)/Multiple/length(targetList)/n); % 一份资金可以购买的手数(没有带杠杆买)
openunit=fix(ValidCash/close(end)/Multiple/length(targetList)/remain_share/LongMargin); % 一份资金可以购买的手数(带杠杆买)
end;
%------------------- 判断当前是多头持仓还是空头持仓 --------------------%
posdir=0;
if ~isempty(remain.direction)
if remain.direction(end)>0 % 有多头持仓只进多头,空头持仓只进空头,因此只需察看remain.direction数组的任意元素的值
posdir=1;
elseif remain.direction(end)<0
posdir=-1;
end;
end;
stds=std(Dclose(end-dlags+1:end-1));
means=mean(Dclose(end-dlags+1:end-1));
%---------------------------------对未平仓的订单进行平仓或者调整止损线--------------------------------------------%
for i=1:length(remain_num)
index=remain_num(i);
% conbar=barnum-remain.entrybar(i)>10; %十根bar后,仍无操作,强制平仓
if remain.direction(i)==1
if (close(end)
orderID3=traderDirectSell(HandleList(1),targetList(k).Market,targetList(k).Code,remain.unit(i),0,'market','sell');
if orderID3==0
continue;
end;
record{k}.isopen(index)=0;
elseif close(end)>remain.pivotprice(i)+2*stds % 触发止盈线
record{k}.pivotprice(index)=close(end);
record{k}.isearn(index)=2;
end;
elseif remain.direction(i)==-1
if (close(end)>remain.pivotprice(i)+stds) % 触发止损线
orderID4=traderDirectBuy(HandleList(1),targetList(k).Market,targetList(k).Code,remain.unit(i),0,'market','buy');
if orderID4==0
continue;
end;
record{k}.isopen(index)=0;
elseif close(end)
record{k}.pivotprice(index)=close(end);
record{k}.isearn(index)=2;
end;
end;
end;
% %--------------------- 止损/入场条件计算 ------------------------------------%
con2=isempty(find(remain.isearn==1,1)); % 所有头寸都是盈利的才考虑进新的头寸
upline=means+0.5*stds;
dnline=means-0.5*stds;
bcon=close(end)>upline && state(k)==0;
scon=close(end)
buycon=con1 && con2 && bcon && posdir==0; % 突破布林带上轨道做多(每天只做一手)
sellshortcon=con1 && con2 && scon && posdir==0; % 突破布林带下轨道做空(每天只做一手)
addbuycon=con1 && con2 && posdir>0; % 盈利了直接加仓
addsellshortcon=con1 && con2 && posdir<0; % 盈利了直接加仓
%---------------------------入场操作--------------------------------%
if buycon % 无仓时买进
orderID1=traderDirectBuy(HandleList(1),targetList(k).Market,targetList(k).Code,openunit,0,'market','buy');
if orderID1==0
continue;
end;
record{k}.pivotprice=[record{k}.pivotprice,close(end)];
record{k}.isearn=[record{k}.isearn,1];
record{k}.isopen=[record{k}.isopen,1];
record{k}.unit=[record{k}.unit,openunit];
record{k}.entrybar=[record{k}.entrybar,barnum];
record{k}.direction=[record{k}.direction,1];
state(k)=1; % state==1 则当天不再采取空仓买进的操作
end;
if sellshortcon % 无仓时卖空
orderID2=traderDirectSell(HandleList(1),targetList(k).Market,targetList(k).Code,openunit,0,'market','sell');
if orderID2==0
continue;
end;
record{k}.pivotprice=[record{k}.pivotprice,close(end)];
record{k}.isearn=[record{k}.isearn,1];
record{k}.isopen=[record{k}.isopen,1];
record{k}.unit=[record{k}.unit,openunit];
record{k}.entrybar=[record{k}.entrybar,barnum];
record{k}.direction=[record{k}.direction,-1];
state(k)=1; % state==1 则当天不再采取空仓卖空的操作
end;
if addbuycon % 多仓时买进
orderID1=traderDirectBuy(HandleList(1),targetList(k).Market,targetList(k).Code,openunit,0,'market','buy');
if orderID1==0
continue;
end;
record{k}.pivotprice=[record{k}.pivotprice,close(end)];
record{k}.isearn=[record{k}.isearn,1];
record{k}.isopen=[record{k}.isopen,1];
record{k}.unit=[record{k}.unit,openunit];
record{k}.entrybar=[record{k}.entrybar,barnum];
record{k}.direction=[record{k}.direction,1];
end;
if addsellshortcon % 空仓时卖空
orderID2=traderDirectSell(HandleList(1),targetList(k).Market,targetList(k).Code,openunit,0,'market','sell');
if orderID2==0
continue;
end;
record{k}.pivotprice=[record{k}.pivotprice,close(end)];
record{k}.isearn=[record{k}.isearn,1];
record{k}.isopen=[record{k}.isopen,1];
record{k}.unit=[record{k}.unit,openunit];
record{k}.entrybar=[record{k}.entrybar,barnum];
record{k}.direction=[record{k}.direction,-1];
end;
end
end