//+------------------------------------------------------------------+
//|                                     Modify_SELL_SL_TP_Script.mq5 |
//|                                          Copyright@ BestTraderEv |
//|                         https://www.forexfactory.com/besttraderev|
//+------------------------------------------------------------------+
#property copyright "Copyright@ BestTraderEv"
#property link      "https://www.forexfactory.com/besttraderev"
#property description "This script modifies StopLoss and TakeProfit for SELL positions"
#property script_show_inputs
//---
#include <Trade\Trade.mqh> //Instatiate Trades Execution Library
#include <Trade\PositionInfo.mqh> //Instatiate Library for Positions Information
//---
CTrade         m_trade; // Trades Info and Executions library
CPositionInfo  m_position; // Library for all position features and information

input group  "*** Modify SELL orders ***";
input double SLpip   = 0;   // Stop Loss Points
input double TPpip   = 0;   // Take Profit Points
input double SLprice = 0;   // Stop Loss Price (Priority if > 0)
input double TPprice = 0;   // Taker Profit Price (Priority if > 0)

double minDistance,slNew,tpNew,Bid,Ask,orderSL,orderTP,slPoints,tpPoints;
int    itotal;
string orderSymbol;
// script modifies stop loss and take profit
int OnStart()
 {
   minDistance=SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL)*_Point;
   Ask=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   Bid=SymbolInfoDouble(_Symbol,SYMBOL_BID);
   
   slPoints=SLpip*_Point;
   tpPoints=TPpip*_Point;
     
   itotal=PositionsTotal();
      for(int i = itotal - 1; i >= 0; i--) // loop all Open Positions
         if(m_position.SelectByIndex(i))  // select a position
           {
          slNew=0; tpNew=0;
         if(slPoints>0)
            slNew=NormalizeDouble(Ask+slPoints,_Digits);
         else if(SLprice>0)
            slNew=NormalizeDouble(SLprice,_Digits);
         else
            slNew=PositionGetDouble(POSITION_SL);
          if(slNew-Ask<=minDistance && slNew>0)          // check broker stop levels
           {
            Alert("Stop Loss is too close to market price or on wrong side!!!");
            return(0);
           } 
            
         if(tpPoints>0)
            tpNew=NormalizeDouble(Ask-tpPoints,_Digits);
         else if(TPprice>0)
            tpNew=NormalizeDouble(TPprice,_Digits);
         else
            tpNew=PositionGetDouble(POSITION_TP);
          if(Ask-tpNew<=minDistance && tpNew>0)
           {
            Alert("Take Profit is too close to market price or on wrong side!!!");
            return(0);
           } 
           
         orderSL=PositionGetDouble(POSITION_SL);
         orderTP=PositionGetDouble(POSITION_TP);
         orderSymbol=PositionGetString(POSITION_SYMBOL);
         
         if(_Symbol==orderSymbol&&(orderTP!=tpNew || orderSL!=slNew))
           {
            m_trade.PositionModify(m_position.Ticket(),slNew,tpNew); // then modify it --period
            PlaySound("ok.wav");
            Sleep(100); // Relax for 100 ms
           }
       }
         
   return(0);
}
//+------------------------------------------------------------------+

