Auto Stoploss for pending orders

 

Hi everyone,

I found a script for MT5 which automatically and immediately adds a predefined stoploss to active positions. Is there a way modify it, so also non-triggered stop and limit orders receive a SL? It's mainly a safety issue for me, because I trade on the 1m and I am worried my internet connection fails at the wrong time. And I am a total novice with MQL5 :)


Thanks for your help!
#include <Trade/Trade.mqh>
CTrade Trade;

input int InpStopLossPoints = 50; //Stop loss in points

void OnTrade()
{
   datetime checkTime = TimeCurrent()-30;
   int      cnt       = PositionsTotal();
   for (int i=cnt-1; i>=0; i--) {
      ulong ticket = PositionGetTicket(i);
      if (ticket>0) {
         if (PositionGetInteger(POSITION_MAGIC)==0 && PositionGetDouble(POSITION_SL)==0) {
            if (PositionGetInteger(POSITION_TIME)>checkTime) {
               string symbol = PositionGetString(POSITION_SYMBOL);
               double stopLoss = InpStopLossPoints*SymbolInfoDouble(symbol, SYMBOL_POINT);
               double stopLossPrice = (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) ?
                                       PositionGetDouble(POSITION_PRICE_OPEN)-stopLoss :
                                       PositionGetDouble(POSITION_PRICE_OPEN)+stopLoss;
               stopLossPrice = NormalizeDouble(stopLossPrice, (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS));
               Trade.PositionModify(ticket, stopLossPrice, PositionGetDouble(POSITION_TP));
            }
         
         }
      }
   
   }
   
}
Reason: