[PEL] 复制代码
input:P1(4.5,0.01,1000,0.01),P2(5,0.01,1000,0.01);//定义开仓品种的价格区间
input:总资金(500000,1,1000000,1),策略资金(30000,1,1000000,1),单品种资金(3000,1,1000000,1);
INPUT:最大开仓品种数量(5,1,100,1);
globalvariable:拟占用资金:=0;//用来累计下单但是可能未成交的品种的资金占用
股票池名称:='g1';
状态池名称:='c1';
//资金分配
if not(islastbar) then exit;
name:=formulaname&'_bk';//自定义的板块的名称
nm:=stkcount(name);//自定义的板块中的品种数量
当前策略资金:=0;//从账户栏中 统计自选股品种 当前的投入总金额
path:='C:\'&FORMULANAME&'_日志.txt';//日志输出路径
debugfile(path,'nm:%.2f',nm);
当前已有的持仓数量:0;
uncompleted:=0;//判断当前自选股中的品种是否有未成交
for i=1 to nm do
begin
hlabel:= stkfromblk(name,i);
//有持仓或者 开仓未成交 持仓品种数量+1
if tbuyholdingex('',hlabel,4)>0 then 当前已有的持仓数量:=当前已有的持仓数量+1;
//当完全成交时候
if tbuyholdingex('',hlabel,2)<>0 and tisremainex(1,'',hlabel)=0 then
begin
品种已占用的资金:=tbuyholdingex('',hlabel,2)*tavgenterpriceex2('',hlabel,0);
当前策略资金:=当前策略资金+品种已占用的资金;//累计当前的初始投入资金
debugfile(path,hlabel&'|已占用资金1:%.2f',品种已占用的资金);
debugfile(path,'当前策略资金:%.2f',当前策略资金);
continue;
end
tis:=tisremainex(1,'',hlabel);//是否有未成交
tis_p:tsubmitid(1,'',hlabel, 3,0);//未成交的价格
tis_v:tsubmitid(1,'',hlabel, 0,0);//未成交的数量
thd:tbuyholdingex('',hlabel,2);
tav:tavgenterpriceex2('',hlabel,0);
//一轮开仓后如果有开仓未成交,那么重新统计拟占用资金
if tis and uncompleted=0 then
begin
uncompleted:=1;
拟占用资金:=0;
debugfile(path,hlabel&'当前有未成交,重新统计拟占用资金',0);
end
//如果 品种不在持仓中了 且没有开仓未成交从板块中直接删除即可
if thd=0 and tis=0 then
begin
x:=deletefromblock(hlabel,name);
end
//如果有开仓未成交
if thd>=0 and tis=1 then
begin
//改品种未成交的资金+部分成交的资金占用
品种拟占用的资金:=tis_p*tis_v+thd*tav;
拟占用资金:=拟占用资金+品种拟占用的资金;
debugfile(path,hlabel&'|品种拟占用的资金:%.2f',品种拟占用的资金);
debugfile(path,'累计拟占用资金:%.2f',拟占用资金);
end
end
//没有 自选股品种中 没有开仓未成交时候重置 全局变量
if uncompleted=0 then
begin
拟占用资金:=0;
debugfile(path,'当前无未成交,拟占用资金重置为0',0);
end
//遍历股票池
for i=1 to tstockpoolcount(股票池名称,状态池名称) do
begin
if 当前已有的持仓数量>= 最大开仓品种数量 then CONTINUE;
//股票池中的品种代码
code:=tstockpoolinxlabel(股票池名称,状态池名称,i);
c1:=callstock(code,vtclose,6,0);//这个品种最新价
ss:=100*floor(单品种资金/(c1*100));//每支股票能买入的股数
单品种拟占用资金:ss*c1;
总资金控制:=taccount(28)+单品种拟占用资金+拟占用资金<总资金;//当前总市值+单品种拟占用资金 小于总资金限制
策略资金限制:当前策略资金+拟占用资金+单品种拟占用资金<策略资金;
资金控制:策略资金限制 and 总资金控制 and ss>0;
//如果资金条件不满足,直接遍历下一个品种
if not(资金控制) then continue;
kd:c1>P1 and c1<P2;
stknamex:=dynainfo2(219,code);
//剔除st
cond:strfind(stknamex,'ST',1)=0 and strfind(stknamex,'*ST',1)=0 ;
//该品种没有持仓+没有开仓未成交
if tbuyholdingex('',code,2)=0 and tglobalsubmitex(1,'',code,0)=0 and tisremainex(1,'',code)=0 and cond and kd then
begin
tbuy(1,ss,lmt,c1,0,'',code),allowrepeat;
拟占用资金:=拟占用资金+单品种拟占用资金;
debugfile(path,code&' 单品种拟占用资金:%.2f',单品种拟占用资金);
debugfile(path,code&' 开仓后累计拟占用资金:%.2f',拟占用资金);
x:=addtoblock(code,name);
end
end