#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      ""
#property version  "1.00"
#define _MagicNum  NULL
input int    Stoplose      =992;
input int    TrailingStop  =986;
void OnStart()
  {
  Management();
  }
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()))
                      {
                       Â
                        sellg=PositionGetDouble(POSITION_TP);
                       Â
                        MqlTradeRequest request;
                        ZeroMemory(request);
                       Â
                        request.action=TRADE_ACTION_SLTP;
                       Â
                        request.symbol=Symbol();
                       Â
                        request.sl=NormalizeDouble(Ask+Point()*TrailingStop,Digits());
                       Â
                        request.tp=sellg;
                       Â
                        request.position=PositionGetTicket(i);
                       Â
                        int filling=(int)SymbolInfoInteger(Symbol(),SYMBOL_FILLING_MODE);
                        if(filling == 1)
                          {
                          request.type_filling = ORDER_FILLING_FOK;
                          }
                        else
                          if(filling == 2)
                            {
                              request.type_filling = ORDER_FILLING_IOC;
                            }
                          else
                              if(filling == 3)
                                {
                                request.type_filling = ORDER_FILLING_RETURN; Â
                                }
                        MqlTradeResult result;
                        ZeroMemory(result);
                        MqlTradeCheckResult CheckResult;
                       Â
                        if(OrderCheck(request,CheckResult))
                          {
                          OrderSend(request,result);
                          }
                        else
                          {
                         Â
                          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()))
                      {
                       Â
                        buyg=PositionGetDouble(POSITION_TP);
                       Â
                        MqlTradeRequest request;
                        ZeroMemory(request);
                       Â
                        request.action=TRADE_ACTION_SLTP;
                       Â
                        request.symbol=Symbol();
                       Â
                        request.sl=NormalizeDouble(Bid-Point()*TrailingStop,Digits());
                       Â
                        request.tp=buyg;
                       Â
                        request.position=PositionGetTicket(i);
                       Â
                        int filling=(int)SymbolInfoInteger(Symbol(),SYMBOL_FILLING_MODE);
                        if(filling == 1)
                          {
                          request.type_filling = ORDER_FILLING_FOK;
                          }
                        else
                          if(filling == 2)
                            {
                              request.type_filling = ORDER_FILLING_IOC;
                            }
                          else
                              if(filling == 3)
                                {
                                request.type_filling = ORDER_FILLING_RETURN; Â
                                }
                        MqlTradeResult result;
                        ZeroMemory(result);
                        MqlTradeCheckResult CheckResult;
                       Â
                        if(OrderCheck(request,CheckResult))
                          {
                          OrderSend(request,result);
                          }
                        else
                          {
                         Â
                          Print(CheckResult.retcode,"  -error ");
                          Print(request.sl,"  ");
                          Print(__FUNCTION__,":",CheckResult.comment);
                          }
                      }
                    }
                }
              }
          }
        }
      else
        Print("OrderSelect error code is :",GetLastError());
    }
  }