本帖最后由 资深技术05 于 2025-9-8 10:49 编辑
参考这里范例,根据具体需求做下调整即可。不过这种逻辑通常还要考虑到和其他策略协调的问题,比如其他策略在平仓期间也可能持续开仓,甚至有撤单逻辑之类的。
[PEL] 复制代码
as:extgbdata(taccount( 1));//记录资产的全局变量
dt:extgbdata(taccount( 1)+'_date');//记录数据更新日期的全局变量
isclear:extgbdata(taccount( 1)+'_isclear');//标记是否应该清仓的全局变量
if tholdcount('')=0 then //如果没有持仓了,清空掉之前的数据
begin
extgbdataset(taccount( 1),0);
extgbdataset(taccount( 1)+'_date',date);//记录数据更新的日期
extgbdataset(taccount( 1)+'_isclear',0); //隔日后清仓标记也重置掉
end
//更新到最新日期的数据,用来处理隔日的问题
if date>dt and tholdcount('')>0 then
begin
extgbdataset(taccount( 1),taccount( 6));
extgbdataset(taccount( 1)+'_date',date);//记录数据更新的日期
extgbdataset(taccount( 1)+'_isclear',0); //隔日后清仓标记也重置掉
end
//始终记录最大的动态权益
if tholdcount('')>0 and taccount( 6)>as and isclear=0 then
begin
extgbdataset(taccount( 1),taccount( 6));
extgbdataset(taccount( 1)+'_date',date);//记录数据更新的日期
end
as:=extgbdata(taccount( 1));
//回撤逻辑在这里调整即可
if as/taccount( 6)>=1.01 and tholdcount('')>0 and isclear=0 then
begin
extgbdataset(taccount( 1)+'_isclear',1);
end
isclear:=extgbdata(taccount( 1)+'_isclear');//标记是否应该清仓的全局变量
if isclear and tbuyholdingex('','',1)>0 then tsell(1,0,mkt);//每次只能平当前品种,所以必须监控账户栏实际持仓才能全平 |