//+------------------------------------------------------------------+
//|                                                   JB-Auto-SL.mq5 |
//|                                          Copyright 2024,JBlanked |
//|                                        https://www.jblanked.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024,JBlanked"
#property link      "https://www.jblanked.com/"
#property version   "1.00"
#property description "I am a simple program that will apply stop loss and take profit to each new position automatically."
#ifdef __MQL5__
#include <Trade\Trade.mqh>
CPositionInfo posi;
CTrade trade;
#endif
//---
input double inpStopLossPoints   = 20; // Stop Loss (Points)
input double inpTakeProfitPoints = 40; // Take Profit (Points)
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
#ifdef __MQL5__
   for(int i = PositionsTotal() - 1; i >= 0; i--)
     {
      if(!posi.SelectByIndex(i))
        {
         continue;
        }

      switch(posi.PositionType())
        {
         case POSITION_TYPE_BUY:
            Modify(
               posi.Ticket(), 
               posi.PriceOpen(), 
               inpStopLossPoints == 0 ? posi.StopLoss() : posi.PriceOpen() - (inpStopLossPoints * Point()), 
               inpTakeProfitPoints == 0 ? posi.TakeProfit() : posi.PriceOpen() + (inpTakeProfitPoints * Point())
            );
            break;

         case POSITION_TYPE_SELL:
            Modify(
               posi.Ticket(), 
               posi.PriceOpen(), 
               inpStopLossPoints == 0 ? posi.StopLoss() : posi.PriceOpen() + (inpStopLossPoints * Point()), 
               inpTakeProfitPoints == 0 ? posi.TakeProfit() : posi.PriceOpen() - (inpTakeProfitPoints * Point())
            );
            break;
        };
     }
#else
   for(int i = OrdersTotal() - 1; i >= 0; i--) //count backwards
     {
      if(!OrderSelect(i, SELECT_BY_POS)) // select the order
        {
        continue;
        }
        
      switch(OrderType())
        {
         case OP_BUY:
         case OP_BUYLIMIT:
         case OP_BUYSTOP:
            Modify(
               (long)OrderTicket(), 
               OrderOpenPrice(), 
               inpStopLossPoints == 0 ? OrderStopLoss() : OrderOpenPrice() - (inpStopLossPoints * Point()), 
               inpTakeProfitPoints == 0 ? OrderTakeProfit() : OrderOpenPrice() + (inpTakeProfitPoints * Point())
            );
            break;

         case OP_SELL:
         case OP_SELLLIMIT:
         case OP_SELLSTOP:
            Modify(
               (long)OrderTicket(), 
               OrderOpenPrice(), 
               inpStopLossPoints == 0 ? OrderStopLoss() : OrderOpenPrice() + (inpStopLossPoints * Point()), 
               inpTakeProfitPoints == 0 ? OrderTakeProfit() : OrderOpenPrice() - (inpTakeProfitPoints * Point())
            );
            break;
        };
     }
#endif
//---
  }
//+------------------------------------------------------------------+
bool              Modify(const ulong ticket, const double entry, const double stopLoss, const double takeProfit, const datetime expiration = 0)
  {
#ifdef __MQL5__
   return trade.PositionModify(ticket, stopLoss, takeProfit);
#else
   return OrderModify((int)ticket, entry, stopLoss, takeProfit, expiration) > 0;
#endif
  }
//+------------------------------------------------------------------+
