漆学军:盈利30点将止损提高到盈利15点处(平保)

《MT4EA编程速成教程》中第23页习题1、盈利30点将止损提高到盈利15点处。

这种操作,有时候也叫平保,为了避免将盈利的单子变亏损了所做的一个保护。这种操作非常有用,很多人都会用到,通过编写ea来实现这种功能,非常简单,短短几十行代码就能实现。

下面是这个程序的全部代码,感兴趣可以复制下去保存为EA就可以运行。

//+------------------------------------------------------------------+
//|                               盈利30点将止损提高到盈利15点处.mq4 |
//|                                                             云开 |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "云开"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

input int tratp=300;//盈利多少点
input int tp=150;//止损调高到盈利多少点
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   int total=OrdersTotal();
   for(int i=0; i<total; i++)
     {
      if(OrderSelect(i, SELECT_BY_POS))
        {
         if(OrderSymbol()==Symbol())
           {
            if(OrderType()==OP_BUY)
              {
               if(OrderStopLoss()<OrderOpenPrice()+tp*Point && OrderClosePrice()-OrderOpenPrice()>tratp*Point)
                 {
                  bool res=OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice()+tp*Point, OrderTakeProfit(), 0);
                 }
              }

            if(OrderType()==OP_SELL)
              {
               if((OrderStopLoss()==0 || OrderStopLoss()>OrderOpenPrice()-tp*Point) && OrderOpenPrice()-OrderClosePrice()>tratp*Point)
                 {
                  bool res=OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice()-tp*Point, OrderTakeProfit(), 0);
                 }
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漆学军

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值