Trailing Stop Management Function – script MetaTrader 5

//+------------------------------------------------------------------+
//|                                       TrailingStopManagement.mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                              |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      ""
#property version   "1.00"
#define _MagicNum  NULL//set magic number

input int    Stoplose      =992;// set stoplose value
input int    TrailingStop  =986;// set TrailingStop value

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   Management();
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Management for open Position conditions                             |
//+------------------------------------------------------------------+
void Management()
  {

   double sellg=0.0;
   double buyg=0.0;
   MqlTick last_tick;
   SymbolInfoTick(_Symbol,last_tick);
   double Ask=last_tick.ask;
   double Bid=last_tick.bid;

   int total = PositionsTotal();
   for(int i = 0; i < total; i++)
     {

      if(PositionGetSymbol(i)!="")
        {
         if(PositionGetInteger(POSITION_MAGIC)!=_MagicNum)
            continue;
         else
           {

            if(PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL)
              {

               if((PositionGetDouble(POSITION_PRICE_OPEN)-Ask)>(Point()*TrailingStop))
                 {
                  if((PositionGetDouble(POSITION_SL)>(Ask+Point()*TrailingStop)) || (PositionGetDouble(POSITION_SL)==0))
                    {
                     if(NormalizeDouble(PositionGetDouble(POSITION_SL),Digits())!=NormalizeDouble(Ask+Point()*TrailingStop,Digits()))
                       {
                        //--- take profit
                        sellg=PositionGetDouble(POSITION_TP);
                        //--- request
                        MqlTradeRequest request;
                        ZeroMemory(request);
                        //--- Change the Stop Loss and Take Profit values of an open position
                        request.action=TRADE_ACTION_SLTP;
                        //--- trade's symbol
                        request.symbol=Symbol();
                        //--- stop lose
                        request.sl=NormalizeDouble(Ask+Point()*TrailingStop,Digits());
                        //--- set take profit
                        request.tp=sellg;
                        //--- TRADE_ACTION_SLTP must set Position
                        request.position=PositionGetTicket(i);
                        //trader mode
                        int filling=(int)SymbolInfoInteger(Symbol(),SYMBOL_FILLING_MODE);
                        if(filling == 1)
                          {
                           request.type_filling = ORDER_FILLING_FOK; //all deal mode
                          }
                        else
                           if(filling == 2)
                             {
                              request.type_filling = ORDER_FILLING_IOC; //part deal mode
                             }
                           else
                              if(filling == 3)
                                {
                                 request.type_filling = ORDER_FILLING_RETURN;  //limit execut mode
                                }


                        MqlTradeResult result;
                        ZeroMemory(result);
                        MqlTradeCheckResult CheckResult;
                        //--- send order
                        if(OrderCheck(request,CheckResult))
                          {
                           OrderSend(request,result);
                          }
                        else
                          {
                           //--- save server request in log
                           Print(CheckResult.retcode,"  -error ");
                           Print(request.sl,"  ");
                           Print(__FUNCTION__,":",CheckResult.comment);
                          }
                       }

                    }

                 }
              }

            if(PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_BUY)
              {

               if(Bid-PositionGetDouble(POSITION_PRICE_OPEN)>Point()*TrailingStop)
                 {
                  if((PositionGetDouble(POSITION_SL)<(Bid-Point()*TrailingStop)) || (PositionGetDouble(POSITION_SL)==0))
                    {
                     if(NormalizeDouble(PositionGetDouble(POSITION_SL),Digits())!=NormalizeDouble(Bid-Point()*TrailingStop,Digits()))
                       {

                        //--- get tp
                        buyg=PositionGetDouble(POSITION_TP);
                        //--- request
                        MqlTradeRequest request;
                        ZeroMemory(request);
                        //--- Change the Stop Loss and Take Profit values of an open position
                        request.action=TRADE_ACTION_SLTP;
                        //--- set trade's symbol
                        request.symbol=Symbol();
                        //--- set stop lose
                        request.sl=NormalizeDouble(Bid-Point()*TrailingStop,Digits());
                        //--- set tp
                        request.tp=buyg;
                        //--- TRADE_ACTION_SLTP must set Position
                        request.position=PositionGetTicket(i);
                        //trade's mode
                        int filling=(int)SymbolInfoInteger(Symbol(),SYMBOL_FILLING_MODE);
                        if(filling == 1)
                          {
                           request.type_filling = ORDER_FILLING_FOK; //all deal mode
                          }
                        else
                           if(filling == 2)
                             {
                              request.type_filling = ORDER_FILLING_IOC; //part deal mode
                             }
                           else
                              if(filling == 3)
                                {
                                 request.type_filling = ORDER_FILLING_RETURN;  //limit execut mode
                                }


                        MqlTradeResult result;
                        ZeroMemory(result);
                        MqlTradeCheckResult CheckResult;
                        //--- send order
                        if(OrderCheck(request,CheckResult))
                          {
                           OrderSend(request,result);
                          }
                        else
                          {
                           //--- save server request in log
                           Print(CheckResult.retcode,"  -error ");
                           Print(request.sl,"  ");
                           Print(__FUNCTION__,":",CheckResult.comment);
                          }
                       }
                    }
                 }
              }



           }




        }

      else
         Print("OrderSelect error code is :",GetLastError());

     }

  }
//+------------------------------------------------------------------+

📈 ROBOTFX MetaTrader Expert Advisors and Indicators to maximize profits and minimize the risks