金字塔决策交易系统

 找回密码
 

微信登录

微信扫一扫,快速登录

搜索
查看: 3485|回复: 5

老师帮忙看一下 唐奇安和止盈止损案例拼合代码怎么写

[复制链接]

5

主题

18

帖子

18

积分

Rank: 1

等级: 新手上路

注册:
2021-10-13
曾用名:
发表于 2023-8-31 16:54 | 显示全部楼层 |阅读模式
//中间变量
INPUT:X(20,1,100,1),NMIN(10,1,100,1),SS(1,1,10000,1);
X周期高点:=REF(HHV(H,X),1);//X是参数,自行调整
X周期低点:=REF(LLV(L,X),1);
手数:=SS;
开仓时间:=TIME>OPENTIME(1) AND TIME<CLOSETIME(0)-NMIN*100;
平仓时间:=TIME>=CLOSETIME(0)-NMIN*100;
{NMIN为参数,CLOSETIME(0)-NMIN*100表示 收盘时间-提前N分钟 N由NMIN控制}

//交易条件:
开多平空条件:=High>=X周期高点 and 开仓时间 and holding<=0;
开空平多条件:=Low<=X周期低点 and 开仓时间 and holding>=0;

//交易系统
收盘平多:sell(平仓时间 and holding>0, 0, thisclose);
收盘平空:sellshort(平仓时间 and holding<0,0,thisclose);

平空:sellshort(开多平空条件 and holding<0, 手数,marketr,X周期高点);
平多:sell(开空平多条件 and holding>0,手数,marketr,X周期低点);
开空:buyshort(开空平多条件 and holding=0,手数,marketr,X周期低点);
开多:buy(开多平空条件 and holding=0, 手数,marketr,X周期高点);
//使用逐K线模式时
VARIABLE:DUOKONG=0;
TSFS:=0;     {停损反手,1-反手 0-不反手}
ORDVOL:=1;     {开仓手数}
ZSDS:=10;     {止损点数}
//以下变量用于移动止盈,,移动止盈只能保证你尽可能稳健盈利,却不能保证盈利最大化,如果不想使用,可是将总开关设置为0
VARIABLE:YDZY=1;     {移动止盈总开关,1为开,0为关}
VARIABLE:HIGHPRICE=0,LOWPRICE=0; {保存开仓后的最高价、最低价}
VARIABLE:DHCDS=3,KHCDS=3;   {多、空单回撤点数}
VARIABLE:DYLDS=7,KYLDS=7;   {多、空单盈利点数,当盈利点数达到这个标准后,如果回撤达到上面的参数值就平仓}
VARIABLE:Z3B1:=1;     {赚3保1开关}
DUOKONG:=1;
//持有空单


  //如果盈利超过10点之后回撤达到3点,就平仓,保住盈利
  IF ENTERPRICE-LOWPRICE>=KYLDS THEN BEGIN
   IF L-LOWPRICE>=KHCDS THEN BEGIN  //回撤超过3点
    空赢:SELLSHORT(1,0,MARKET);
   END



//赚3保1
IF Z3B1=1 THEN BEGIN
  IF ENTERPRICE-LOWPRICE>=3 THEN BEGIN
   IF ENTERPRICE-L<=1.2 THEN BEGIN
    空保:SELLSHORT(1,0,MARKET);
   END
  END
END
END
//开多单
IF HOLDING=0 THEN BEGIN
IF DUOKONG=1 OR DUOKONG=2 THEN BEGIN
  IF BUY1 THEN BEGIN
   多开:BUY(1=1,ORDVOL,MARKET);
   HIGHPRICE:=ENTERPRICE;  //将开仓价保存到最高价
  END
END
END
//持有多单
IF HOLDING>0 THEN BEGIN
IF SELL1 THEN BEGIN  {平多单}
  多平:SELL(1,0,MARKET),ORDERQUEUE;
END

//多单管理
//止损
IF ENTERPRICE-C>=ZSDS THEN BEGIN
  多损:SELL(1,0,MARKET),ORDERQUEUE;
END
//移动止盈
IF YDZY=1 THEN BEGIN
  //每周期判断是否创新高
  IF H>HIGHPRICE THEN BEGIN
   HIGHPRICE:=H;
  END

  //如果盈利超过10点之后回撤达到3点,就平仓,保住盈利
  IF HIGHPRICE-ENTERPRICE>=DYLDS THEN BEGIN
   IF HIGHPRICE-H>=DHCDS THEN BEGIN  //回撤超过3点
    多赢:SELL(1,0,MARKET);
   END
  END
END
//赚3保1
IF Z3B1=1 THEN BEGIN
  IF HIGHPRICE-ENTERPRICE>=3 THEN BEGIN
   IF H-ENTERPRICE<=1.2 THEN BEGIN
    多保:SELL(1,0,MARKET);
   END
  END
END
END
//开空单
IF HOLDING=0 THEN BEGIN
IF DUOKONG=1 OR DUOKONG=3 THEN BEGIN
  IF SELL1 THEN BEGIN
   空开:BUYSHORT(1=1,ORDVOL,MARKET);
   LOWPRICE:=ENTERPRICE;
  END
END
END
当前持仓:HOLDING,COLORGRAY,LINETHICK0;
当前资产:ASSET,NOAXIS,COLORGRAY;//输出当前资产,但不影响坐标最高最低值

回复

使用道具 举报

21

主题

1万

帖子

1万

积分

Rank: 8Rank: 8

等级: 超级版主

注册:
2021-5-18
曾用名:
FireScript
发表于 2023-9-1 09:41 | 显示全部楼层
第二段代码思路是回撤止盈的逻辑。源代码写的不是很好,下面代码按照思路直接重构了一次,没有再参考你给的第二段代码了。
[PEL] 复制代码


//中间变量
input:x(20,1,100,1),nmin(10,1,100,1),ss(1,1,10000,1);
//p1 是回撤点数;p2是回撤前最大盈利点数的条件;
input:p1(3,1,300,1),p2(7,1,500,1),3b1(1,0,1,1);

x周期高点:=ref(hhv(h,x),1);//x是参数,自行调整
x周期低点:=ref(llv(l,x),1);
手数:=ss;
开仓时间:=time>opentime(1) and time<closetime(0)-nmin*100;
平仓时间:=time>=closetime(0)-nmin*100;
{nmin为参数,closetime(0)-nmin*100表示 收盘时间-提前n分钟 n由nmin控制}

//交易条件:
开多平空条件:=high>=x周期高点 and 开仓时间 and holding<=0;
开空平多条件:=low<=x周期低点 and 开仓时间 and holding>=0;


variable:maxprofit=0;//有仓位时最大获利幅度
//开多
if 开多平空条件 and holding<=0 then
begin
  sellshort(1,holding,market);
  buy(1,手数,limit,c);
  maxprofit:=0;
end


//开空
if 开空平多条件 and holding>=0 then
begin
  sell(1,holding,market);
  buyshort(1,手数,limit,c);
  maxprofit:=0;
end


//判断当前持仓状态下的最大盈利
win:=0;
win2:=0;

if holding > 0 and enterbars > 0 then
begin
  win:=(h-enterprice); //记录最大盈利
  if win>maxprofit then maxprofit:=win; 
 
  win2:=(maxprofit-win); //最大盈利后的回调幅度
end
if holding < 0 and enterbars > 0 then
begin
  win:=(enterprice-l); //记录最大盈利
  if win>maxprofit then maxprofit:=win;
 
win2:=(maxprofit-win); //最大盈利后的回调
end


//回撤止盈
if win2>=p1*mindiff and maxprofit>=p2*mindiff then 
begin
if holding>0 then 
begin 
多止赢:sell(1,0,limit,c);        
end 
        

if holding<0 then 
begin 
空止赢:sellshort(1, 0,limit,c);        
end 
         
end 

//
//赚3保1:最高盈利大于3点,回撤到1点以下平仓
if  3b1  and win>3*mindiff  then 
begin
if  h-enterprice<=1.2*mindiff and holding>0 then 
begin 
多保:sell(1,0,market);
maxprofit:=0;
end 

if enterprice-l<=1.2*mindiff and  holding<0 then  
begin 
空保:sellshort(1,0,market);                
maxprofit:=0;
end
end 



if 平仓时间 and holding<>0 then 
begin 
收盘平多:sell(平仓时间 and holding>0, 0, thisclose);
收盘平空:sellshort(平仓时间 and holding<0,0,thisclose);
maxprofit:=0;
end
金字塔提供一对一VIP专业技术指导服务,技术团队实时响应您的日常使用问题与策略编写。联系电话:021-20339086
回复

使用道具 举报

5

主题

18

帖子

18

积分

Rank: 1

等级: 新手上路

注册:
2021-10-13
曾用名:
 楼主| 发表于 2023-9-1 16:10 | 显示全部楼层
老师  刚刚测试了一下  回撤止盈有效  但是 赚3保1  好像无效      删除了回撤止盈  单独测试赚3保1无效
回复

使用道具 举报

21

主题

1万

帖子

1万

积分

Rank: 8Rank: 8

等级: 超级版主

注册:
2021-5-18
曾用名:
FireScript
发表于 2023-9-1 17:16 | 显示全部楼层
//赚3保1:最高盈利大于3点,回撤到1点以下平仓
if  3b1  and maxprofit>3*mindiff  then
begin
if  h-enterprice<=1.2*mindiff and holding>0 then
begin
多保:sell(1,0,market);
maxprofit:=0;
end

if enterprice-l<=1.2*mindiff and  holding<0 then
begin
空保:sellshort(1,0,market);               
maxprofit:=0;
end
end

这里写错了 改下就行了。
金字塔提供一对一VIP专业技术指导服务,技术团队实时响应您的日常使用问题与策略编写。联系电话:021-20339086
回复

使用道具 举报

5

主题

18

帖子

18

积分

Rank: 1

等级: 新手上路

注册:
2021-10-13
曾用名:
 楼主| 发表于 2023-9-4 21:35 | 显示全部楼层
技术009 发表于 2023-9-1 17:16
//赚3保1:最高盈利大于3点,回撤到1点以下平仓
if  3b1  and maxprofit>3*mindiff  then
begin

经过今晚的实盘测试   赚3保1问题解决了   
但是又出现了新的问题
使用赚3保1   保本后  随着价格像波动到之前开仓位置又重新这方向的  反复被打  保本变的没了意义了   应该是持仓holding的问题吧   应该怎么修改?
以前止盈止损后不会在开之前方向的单
平空:sellshort(开多平空条件 and holding<0, 手数,marketr,X周期高点);
平多:sell(开空平多条件 and holding>0,手数,marketr,X周期低点);
开空:buyshort(开空平多条件 and holding=0,手数,marketr,X周期低点);
开多:buy(开多平空条件 and holding=0, 手数,marketr,X周期高点);

回复

使用道具 举报

21

主题

1万

帖子

1万

积分

Rank: 8Rank: 8

等级: 超级版主

注册:
2021-5-18
曾用名:
FireScript
发表于 2023-9-5 09:08 | 显示全部楼层
这个是因为之前 逻辑是反手。也就是平仓必然有反向的开仓。所以可以做到你说的那种。

但是现在等于是新增了离场的条件了,就破坏了之前的那个逻辑了。即会在反向仓位之前就离场了。
不过可以增加对上次开仓信号的判断来处理掉这个问题。
[PEL] 复制代码
//中间变量
input:x(20,1,100,1),nmin(10,1,100,1),ss(1,1,10000,1);
//p1 是回撤点数;p2是回撤前最大盈利点数的条件;
input:p1(3,1,300,1),p2(7,1,500,1),3b1(1,0,1,1);
 
x周期高点:=ref(hhv(h,x),1);//x是参数,自行调整
x周期低点:=ref(llv(l,x),1);
手数:=ss;
开仓时间:=time>opentime(1) and time<closetime(0)-nmin*100;
平仓时间:=time>=closetime(0)-nmin*100;
{nmin为参数,closetime(0)-nmin*100表示 收盘时间-提前n分钟 n由nmin控制}
 
//交易条件:
开多平空条件:=high>=x周期高点 and 开仓时间 and holding<=0;
开空平多条件:=low<=x周期低点 and 开仓时间 and holding>=0;
 
 
variable:maxprofit=0;//有仓位时最大获利幅度
//开多
if 开多平空条件 and holding<=0 then
begin
  sellshort(1,holding,market);
  buy(TYPE(1)=0 or TYPE(1)=4,手数,limit,c);
  maxprofit:=0;
end
 
 
//开空
if 开空平多条件 and holding>=0 then
begin
  sell(1,holding,market);
  buyshort(TYPE(1)=0 or TYPE(1)=2,手数,limit,c);
  maxprofit:=0;
end
 
 
//判断当前持仓状态下的最大盈利
win:=0;
win2:=0;
 
if holding > 0 and enterbars > 0 then
begin
  win:=(h-enterprice); //记录最大盈利
  if win>maxprofit then maxprofit:=win; 
  
  win2:=(maxprofit-win); //最大盈利后的回调幅度
end
if holding < 0 and enterbars > 0 then
begin
  win:=(enterprice-l); //记录最大盈利
  if win>maxprofit then maxprofit:=win;
  
win2:=(maxprofit-win); //最大盈利后的回调
end
 
 
//回撤止盈
if win2>=p1*mindiff and maxprofit>=p2*mindiff then
begin
if holding>0 then
begin
多止赢:sell(1,0,limit,c);        
end
         
 
if holding<0 then
begin
空止赢:sellshort(1, 0,limit,c);        
end
          
end
 
//
//赚3保1:最高盈利大于3点,回撤到1点以下平仓
if  3b1  and maxprofit>3*mindiff  then
begin
if  h-enterprice<=1.2*mindiff and holding>0 then
begin
多保:sell(1,0,market);
maxprofit:=0;
end
 
if enterprice-l<=1.2*mindiff and  holding<0 then 
begin
空保:sellshort(1,0,market);                
maxprofit:=0;
end
end
 
 
 
if 平仓时间 and holding<>0 then
begin
收盘平多:sell(平仓时间 and holding>0, 0, thisclose);
收盘平空:sellshort(平仓时间 and holding<0,0,thisclose);
maxprofit:=0;
end


金字塔提供一对一VIP专业技术指导服务,技术团队实时响应您的日常使用问题与策略编写。联系电话:021-20339086
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-30 00:51 , Processed in 0.191983 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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