support 发表于 2022-12-5 12:49:32

永远保持订单距离最大盈利处10个点(移动止损)

永远保持订单距离最大盈利 10 个点,也就是多单随着行情上涨,止损上移,行情下跌,
止损不变,空单相反。
这种操作就是标准的移动止损,移动止损和平保不同,平保只修改一次止损,移动止损会不断将多单的止损往上移动,空单的止损往下移动。移动止损不仅仅只是保护了账户不亏损,而且还能随着行情变化,将利润扩大。
在MT4平台上,实现这样一个EA非常简单,只需要短短几十行代码,下面提供完整全部代码,供初学者学习参考。
//+------------------------------------------------------------------+
//|                           永远保持订单距离最大盈利处10个点.mq4 |
//|                                                         漆学军 |
//|                                           http://www.forexmt4.cn |
//+------------------------------------------------------------------+
#property copyright "漆学军"
#property link      "http://www.forexmt4.cn"
#property version   "1.00"
#property strict

input int tp=100;//永远保持订单距离最大盈利处多少个点
//+------------------------------------------------------------------+
//| 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()==0 || OrderStopLoss()<OrderClosePrice()-tp*Point)
               {
                  bool res=OrderModify(OrderTicket(), OrderOpenPrice(), OrderClosePrice()-tp*Point, OrderTakeProfit(), 0);
               }
            }

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

木兮 发表于 2023-11-7 11:09:05

:$

yitang 发表于 2024-3-3 11:59:03

谢谢楼主分享

anna 发表于 2024-3-9 11:44:43

支持一下
页: [1]
查看完整版本: 永远保持订单距离最大盈利处10个点(移动止损)