金字塔决策交易系统

 找回密码
 

微信登录

微信扫一扫,快速登录

搜索
查看: 277|回复: 0

后台移动止盈的范例

[复制链接]

1

主题

4046

帖子

4046

积分

Rank: 8Rank: 8

等级: 超级版主

注册:
2021-5-24
曾用名:
发表于 2024-7-16 15:41 | 显示全部楼层 |阅读模式
本帖最后由 资深技术05 于 2024-9-5 08:50 编辑

策略可以单独运行,也可以直接复制到现有策略中运行。本策略包含固定的止损,以及可设置阈值的回撤止盈.

注意:1.不支持锁仓情况下运行,如需锁仓运行,需调整代码中的全局变量名称以使得多空分开记录数据。
2.暂停程序运行会导致 程序的数据无法及时有效更新.例如暂停期间行情出现新高或者新低的情况。   

[PEL] 复制代码
input:移动止盈(60,-1,100,0.1);//设置为负值时候表示不启用 回撤止盈
input:阈值(4,-1,100,0.1);//设置为负值时候表示不启用阈值。这个阈值表示:至少盈利多少(百分比)后才能触发回撤止盈。如果参数设置为负,则表示无论盈利多少,回撤到指定百分比即平仓
input:固定止损(2,1,100,0.1);

//path是日志输出的所在目录
path:='c:\'&formulaname;
//超全局变量的字符串
maxp_str:=stklabel&'_'&'max';//用来记录开盘 后最大盈利的价格 不区分多空,仅区分品种
dk_str:=stklabel&'_'&'dk';//用来记录持仓的方向

//移动止损模块部分,单边有效,不支持锁仓模式

//初始写值
if tbuyholdingex( '','' ,1)>0 and extgbdata(dk_str)<=0  then begin
extgbdataset(maxp_str,tavgenterpriceex2('','',0));
extgbdataset(dk_str,1);
end

if tsellholdingex( '','' ,1)>0 and extgbdata(dk_str)>=0 then begin
extgbdataset(maxp_str,tavgenterpriceex2('','',1));
extgbdataset(dk_str,-1);
end

//表示监控开始后的最高价
if dynainfo(  7)>extgbdata(maxp_str) and extgbdata(dk_str)>0 and tbuyholdingex( '','' ,1)>0  //多空的方向 
then begin
extgbdataset(maxp_str,dynainfo(  7));
debugfile(path&stklabel&'.txt',stklabel&'的最高价更新:%.2f',dynainfo(  7)); 
end

//表示监控开始后的最低价
if dynainfo(  7)<extgbdata(maxp_str) and extgbdata(dk_str)<0 and tsellholdingex( '','' ,1)>0 //多空的方向 
then begin
extgbdataset(maxp_str,dynainfo(  7));
debugfile(path&stklabel&'.txt',stklabel&'的最低价更新:%.2f',dynainfo(  7)); 
end

maxp:=extgbdata(maxp_str);
多最大盈利:=maxp-tavgenterpriceex2('','',0);//根据持仓期间最高价计算
空最大盈利:=tavgenterpriceex2('','',1)-maxp;//根据持仓期间最低价计算

//回撤止盈部分

if  移动止盈>0 then 
begin         
if tbuyholdingex( '','' ,1)>0 and ((阈值>0 and maxp>=tavgenterpriceex2('','',0)*(1+(阈值/100))) or 阈值<0)  and (c-tavgenterpriceex2('','',0))<=(1-移动止盈/100)*多最大盈利 then 
begin 
debugfile(path&stklabel&'.txt',stklabel&'多回撤止盈',0); 
debugfile(path&stklabel&'.txt',stklabel&'最新价:%.2f',c); 
debugfile(path&stklabel&'.txt',stklabel&'持仓均价:%.2f',tavgenterpriceex2('','',0)); 
debugfile(path&stklabel&'.txt',stklabel&'最大盈利价格:%.2f',maxp); 
tsell(1,0,mkt),allowrepeat;        
extgbdataset(maxp_str,0);
extgbdataset(dk_str,0);
end 

if tsellholdingex( '','' ,1)<0 and ((阈值>0 and maxp<=tavgenterpriceex2('','',1)*(1-(阈值/100))) or 阈值<0) and  (tavgenterpriceex2('','',1)-c)<=(1-移动止盈/100)*空最大盈利 then 
begin 
debugfile(path&stklabel&'.txt',stklabel&'空回撤止盈',0); 
debugfile(path&stklabel&'.txt',stklabel&'最新价:%.2f',c); 
debugfile(path&stklabel&'.txt',stklabel&'持仓均价:%.2f',tavgenterpriceex2('','',1)); 
debugfile(path&stklabel&'.txt',stklabel&'最大盈利价格:%.2f',maxp);         
tsellshort(1,0,mkt),allowrepeat;                 
extgbdataset(maxp_str,0);
extgbdataset(dk_str,0);
end 

end 


//固定止损部分

if tbuyholdingex( '','' ,1)>0  and c<=tavgenterpriceex2('','',0)*(1-(固定止损/100)) then 
begin 
debugfile(path&stklabel&'.txt',stklabel&'多固定止损',0); 
debugfile(path&stklabel&'.txt',stklabel&'最新价:%.2f',c); 
debugfile(path&stklabel&'.txt',stklabel&'持仓均价:%.2f',tavgenterpriceex2('','',0)); 
tsell(1,0,mkt),allowrepeat;        
extgbdataset(maxp_str,0);
extgbdataset(dk_str,0);
end 

if tsellholdingex( '','' ,1)>0 and c>=tavgenterpriceex2('','',1)*(1+(固定止损/100)) then 
begin 
debugfile(path&stklabel&'.txt',stklabel&'空固定止损',0); 
debugfile(path&stklabel&'.txt',stklabel&'最新价:%.2f',c); 
debugfile(path&stklabel&'.txt',stklabel&'持仓均价:%.2f',tavgenterpriceex2('','',1)); 
tsellshort(1,0,mkt),allowrepeat;                 
extgbdataset(maxp_str,0);
extgbdataset(dk_str,0);
end 



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 微信登录

本版积分规则

手机版|小黑屋|上海金之塔信息技术有限公司 ( 沪ICP备13035422号 )

GMT+8, 2024-9-19 12:11 , Processed in 0.251710 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表