
等级: 标准版
- 注册:
- 2022-6-27
- 曾用名:
|

楼主 |
发表于 2022-10-14 18:05
|
显示全部楼层
下面是计算开仓手数以及开仓条件的代码,不知道哪里有问题,请帮看一下,谢谢!
///////////////////////////////////////////////////////////////////////////////////////////开仓方式 【百分比】【固定手数】
if TACCOUNT(20)>0 then //登录了交易账户(模拟/实际交易账户),获取账户的流动资产(动态权益)值
begin
if Rest_Limit=0 then //未开启资金限制
begin Chart_Asset:=TACCOUNT(20); end
else //开启资金限制
begin Chart_Asset:=Rest_Funds*10000; end
end
else //未登录任何账户(模拟/实际交易账户)
begin
if Rest_Limit=0 then //未开启资金限制
begin Chart_Asset:=ASSET; end //PEL编辑框内,费率设置里的虚拟资产值
else //开启资金限制
begin Chart_Asset:=Rest_Funds*10000; end //限制可开仓金额设置为固定值=Rest_Funds*10000
end
////////////////////////////////////////////每手开仓成本
Lots_Cost:=(MULTIPLIER*Close*TACCOUNT(41));
////////////////////////////////////////////持仓市值
Hold_Asset:=Lots_Cost*HOLDING,NODRAW;
////////////////////////////////////////////开仓手数计算
S_Theory_Loss:=ABS(CCC-Short_SP)*10,NODRAW;
L_Theory_Loss:=ABS(CCC-Long_SP)*10,NODRAW;
if Rest_Limit=0 then //未启用资金限制
begin
if Per_Lots=0 then //未启用百分比 [实盘/模拟/PEL虚拟资金相同]
begin Ex_Lots:=Fix_Lots; end //固定手数开仓
else //百分比手数开仓
begin
if S_Open_Cond>0 and HOLDING=0 then
begin
if S_Theory_Loss*INTPART(Chart_Asset*(Per_Lots*0.01)/Lots_Cost) > INTPART(Chart_Asset*(Per_Lots*0.01)*(Rest_Loss*0.01)) then
begin Ex_Lots:=INTPART(INTPART(Chart_Asset*(Per_Lots*0.01)*(Rest_Loss*0.01))/S_Theory_Loss); end
else
begin Ex_Lots:=INTPART(Chart_Asset*(Per_Lots*0.01)/Lots_Cost); end
end
if L_Open_Cond>0 and HOLDING=0 then
begin
if L_Theory_Loss*INTPART(Chart_Asset*(Per_Lots*0.01)/Lots_Cost) > INTPART(Chart_Asset*(Per_Lots*0.01)*(Rest_Loss*0.01)) then
begin Ex_Lots:=INTPART(INTPART(Chart_Asset*(Per_Lots*0.01)*(Rest_Loss*0.01))/L_Theory_Loss); end
else
begin Ex_Lots:=INTPART(Chart_Asset*(Per_Lots*0.01)/Lots_Cost); end
end
end
end
else //Rest_Limit=1 启用资金限制
begin
if Rest_Per=100 then //不启用安全垫 无论是否登录账户,都按照限制资金最大量开仓[用于回测限制资金满仓结果]
begin
if INTPART(Rest_Funds*10000/Lots_Cost)>=1 then
begin
if S_Open_Cond>0 and HOLDING=0 then
begin
if S_Theory_Loss*INTPART(Rest_Funds*10000/Lots_Cost) > INTPART((Rest_Funds*10000)*(Rest_Loss*0.01)) then
begin Ex_Lots:=INTPART(INTPART((Rest_Funds*10000)*(Rest_Loss*0.01))/S_Theory_Loss); end
else
begin Ex_Lots:=INTPART((Rest_Funds*10000)/Lots_Cost); end
end
if L_Open_Cond>0 and HOLDING=0 then
begin
if L_Theory_Loss*INTPART(Rest_Funds*10000/Lots_Cost) > INTPART((Rest_Funds*10000)*(Rest_Loss*0.01)) then
begin Ex_Lots:=INTPART(INTPART((Rest_Funds*10000)*(Rest_Loss*0.01))/L_Theory_Loss); end
else
begin Ex_Lots:=INTPART((Rest_Funds*10000)/Lots_Cost); end
end
end
else
begin Ex_Lots:=0; end //注意:当计算出的手数为0时,PEL会认为0为按照现有资金全部开仓,因此需要在开仓条件里附件,Ex_Lots<>0时才可以开仓
end
else //Rest_Per<100 启用安全垫 [回测/实盘/模拟 安全垫交易]2种情况 1账户开启 2PEL虚拟资金
begin
if TACCOUNT(20)>0 then //账户开启时
begin
if TACCOUNT(20)*(Rest_Per*0.01)<(Rest_Funds*10000) then //账户资金*安全垫 < 限制使用资金 → 使用安全垫资金
begin
if INTPART(TACCOUNT(20)*(Rest_Per*0.01)/Lots_Cost)>=1 then
begin
if S_Open_Cond>0 and HOLDING=0 then
begin
if S_Theory_Loss*INTPART(TACCOUNT(20)*(Rest_Per*0.01)/Lots_Cost) > TACCOUNT(20)*(Rest_Per*0.01)*(Rest_Loss*0.01) then
begin Ex_Lots:=INTPART(TACCOUNT(20)*(Rest_Per*0.01)*(Rest_Loss*0.01)/S_Theory_Loss); end
else
begin Ex_Lots:=INTPART(TACCOUNT(20)*(Rest_Per*0.01)/Lots_Cost); end
end
if L_Open_Cond>0 and HOLDING=0 then
begin
if L_Theory_Loss*INTPART(TACCOUNT(20)*(Rest_Per*0.01)/Lots_Cost) > TACCOUNT(20)*(Rest_Per*0.01)*(Rest_Loss*0.01) then
begin Ex_Lots:=INTPART(TACCOUNT(20)*(Rest_Per*0.01)*(Rest_Loss*0.01)/L_Theory_Loss); end
else
begin Ex_Lots:=INTPART(TACCOUNT(20)*(Rest_Per*0.01)/Lots_Cost); end
end
end
else begin Ex_Lots:=0; end //注意:当计算出的手数为0时,PEL会认为0为按照现有资金全部开仓,因此需要在开仓条件里附件,Ex_Lots<>0时才可以开仓
end
else //使用 限制使用资金
begin
if INTPART((Rest_Funds*10000)/Lots_Cost)>=1 then
begin
if S_Open_Cond>0 and HOLDING=0 then
begin
if S_Theory_Loss*INTPART((Rest_Funds*10000)/Lots_Cost) > (Rest_Funds*10000)*(Rest_Loss*0.01) then
begin Ex_Lots:=INTPART((Rest_Funds*10000)*(Rest_Loss*0.01)/S_Theory_Loss); end
else
begin Ex_Lots:=INTPART((Rest_Funds*10000)/Lots_Cost); end
end
if L_Open_Cond>0 and HOLDING=0 then
begin
if L_Theory_Loss*INTPART((Rest_Funds*10000)/Lots_Cost) > (Rest_Funds*10000)*(Rest_Loss*0.01) then
begin Ex_Lots:=INTPART((Rest_Funds*10000)*(Rest_Loss*0.01)/L_Theory_Loss); end
else
begin Ex_Lots:=INTPART((Rest_Funds*10000)/Lots_Cost); end
end
end
else begin Ex_Lots:=0; end //注意:当计算出的手数为0时,PEL会认为0为按照现有资金全部开仓,因此需要在开仓条件里附件,Ex_Lots<>0时才可以开仓
end
end
else //TACCOUNT(20)<=0 账户未开启 即PEL虚拟资金
begin
if Asset*(Rest_Per*0.01)<(Rest_Funds*10000) then //虚拟资金*安全垫百分比 < 限制资金 使用安全垫资金
begin
if INTPART(Asset*(Rest_Per*0.01)/Lots_Cost)>=1 then
begin
if S_Open_Cond>0 and HOLDING=0 then
begin
if S_Theory_Loss*INTPART(Asset*(Rest_Per*0.01)/Lots_Cost) > Asset*(Rest_Per*0.01)*(Rest_Loss*0.01) then
begin Ex_Lots:=INTPART(Asset*(Rest_Per*0.01)*(Rest_Loss*0.01)/S_Theory_Loss); end
else
begin Ex_Lots:=INTPART(Asset*(Rest_Per*0.01)/Lots_Cost); end
end
if L_Open_Cond>0 and HOLDING=0 then
begin
if L_Theory_Loss*INTPART(Asset*(Rest_Per*0.01)/Lots_Cost) > Asset*(Rest_Per*0.01)*(Rest_Loss*0.01) then
begin Ex_Lots:=INTPART(Asset*(Rest_Per*0.01)*(Rest_Loss*0.01)/L_Theory_Loss); end
else
begin Ex_Lots:=INTPART(Asset*(Rest_Per*0.01)/Lots_Cost); end
end
end
else begin Ex_Lots:=0; end //注意:当计算出的手数为0时,PEL会认为0为按照现有资金全部开仓,因此需要在开仓条件里附件,Ex_Lots<>0时才可以开仓
end
else //虚拟资金*安全垫百分比 >= 限制资金 使用限制资金
begin
if INTPART((Rest_Funds*10000)/Lots_Cost)>=1 then
begin
if S_Open_Cond>0 and HOLDING=0 then
begin
if S_Theory_Loss*INTPART((Rest_Funds*10000)/Lots_Cost) > (Rest_Funds*10000)*(Rest_Loss*0.01) then
begin Ex_Lots:=INTPART((Rest_Funds*10000)*(Rest_Loss*0.01)/S_Theory_Loss); end
else
begin Ex_Lots:=INTPART((Rest_Funds*10000)/Lots_Cost); end
end
if L_Open_Cond>0 and HOLDING=0 then
begin
if L_Theory_Loss*INTPART((Rest_Funds*10000)/Lots_Cost) > (Rest_Funds*10000)*(Rest_Loss*0.01) then
begin Ex_Lots:=INTPART((Rest_Funds*10000)*(Rest_Loss*0.01)/L_Theory_Loss); end
else
begin Ex_Lots:=INTPART((Rest_Funds*10000)/Lots_Cost); end
end
end
else begin Ex_Lots:=0; end //注意:当计算出的手数为0时,PEL会认为0为按照现有资金全部开仓,因此需要在开仓条件里附件,Ex_Lots<>0时才可以开仓
end
end
end
end
Excute_Lots:Ex_Lots,NODRAW;
//+------------------------------------------------------------------+
//| 确认条件 |
//+------------------------------------------------------------------+
Long_Cond:=L_Open_Cond>0 and Ex_Lots>0,COLORRED,NODRAW;
Short_Cond:S_Open_Cond>0 and Ex_Lots>0,COLORGREEN,NODRAW;
//+------------------------------------------------------------------+
//| 开平仓执行 |
//+------------------------------------------------------------------+
多:=ref(Long_Cond,1)>0 and HOLDING=0 and OOO>=ref(Min(CCC,OOO),1),NODRAW;
空:=ref(Short_Cond,1)>0 and HOLDING=0 and OOO<=ref(Max(CCC,OOO),1),NODRAW;
if ISLASTBAR then
begin
清多i:SELL(多清=1,0,marketr);
清空i:SELLSHORT(空清=1,0,marketr);
开多i:BUY(多=1,Excute_Lots,marketr),COLORRED;
开空i:BUYSHORT(空=1,Excute_Lots,marketr),COLORGREEN;
end
else
begin
清多:SELL(多清=1,0,LIMITR,Open),COLORGREEN;
清空:SELLSHORT(空清=1,0,LIMITR,Open),COLORGREEN;
开多:BUY(多=1,Excute_Lots,LIMITR,Open),COLORRED;
开空:BUYSHORT(空=1,Excute_Lots,LIMITR,Open),COLORGREEN;
if 开空 then
begin
Open_Price:=OOO;
Last_Lots:=Excute_Lots;
end
if 开多 then
begin
Open_Price:=OOO;
Last_Lots:=Excute_Lots;
end
end
持:HOLDING,COLORYELLOW,NODRAW; |
|