//+------------------------------------------------------------------+
//|                                           OrderSelect_Sample.mq4 |
//|                                          Copyright 2022 mql5.com |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022 mql5.com"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

input int                 TakeProfitPoints          = 500;
input int                 StopLossPoints            = 300;
      
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
     if(StopLossPoints>0 || TakeProfitPoints>0)
      {
     OrderSelectFunctionOpenOrders(StopLossPoints,TakeProfitPoints);
      }
  }
//+------------------------------------------------------------------+
void OrderSelectFunctionOpenOrders(int SLPts, int TPPts)
 {
 int     itotal       = OrdersTotal();
 double  actualSLPts  = 0;
 double  actualTPPts  = 0;
 double  newSLPrice   = 0;
 double  newTPPrice   = 0;
 bool    modifyResult = false;

   for(int icnt=0;icnt<itotal;icnt++) // for loop
     {
      if(OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES)) // Selecting open orders
       {
       //---- These are all the data we can get from open orders
       //---- You may not need them all for your specific purpose  
       string     orderSymbol     = OrderSymbol();
       int        orderTicket     = OrderTicket();
       int        orderType       = OrderType();
       double     orderPrice      = OrderOpenPrice();
       datetime   orderTime       = OrderOpenTime();
       string     orderComment    = OrderComment();
       int        orderMagic      = OrderMagicNumber();
       double     orderLots       = OrderLots();
       double     orderSL         = OrderStopLoss();
       double     orderTP         = OrderTakeProfit();
       double     orderCommission = OrderCommission();
       double     orderSwap       = OrderSwap();
       double     orderProfit     = OrderProfit();
       
       actualSLPts = MathAbs(orderPrice-orderSL) / _Point;
       actualTPPts = MathAbs(orderPrice-orderTP) / _Point;
       
       if(orderType == OP_BUY)
        {
        newSLPrice  = NormalizeDouble(orderPrice - SLPts * _Point, _Digits);
        newTPPrice  = NormalizeDouble(orderPrice + TPPts * _Point, _Digits);
        if((orderSL != newSLPrice && Bid > newSLPrice) || (orderTP != newTPPrice && Bid < newTPPrice))
         {
         modifyResult = OrderModify(orderTicket,orderPrice,newSLPrice,newTPPrice,0,clrNONE);
         }
        
        if(!modifyResult)
         {
         Print("Buy OrderModify error - code: ", GetLastError()); 
         }
         else 
         {  
         Print("Buy OrderModify success!"); 
         PlaySound("ok.wav");
         }
        }
        
       if(orderType == OP_SELL)
        {
        newSLPrice  = NormalizeDouble(orderPrice + SLPts * _Point, _Digits);
        newTPPrice  = NormalizeDouble(orderPrice - TPPts * _Point, _Digits);
        if((orderSL != newSLPrice && Ask < newSLPrice) || (orderTP != newTPPrice && Ask > newTPPrice))
         {
         modifyResult = OrderModify(orderTicket,orderPrice,newSLPrice,newTPPrice,0,clrNONE);
         }
        
        if(!modifyResult)
         {
         Print("Sell OrderModify error - code: ", GetLastError()); 
         }
         else 
         {  
         Print("Sell OrderModify success!"); 
         PlaySound("ok.wav");
         }
        }
       }
     }
 }
//+------------------------------------------------------------------+
