Changing TP or SL effects the number of trades executed

 

Hello,

My EA seems to change the amount of orders executed depending on the SL or TP (holding all other variables constant) which doesn't make sense to me. It seems that the larger the SL and TP, the less trades that are being taken. Would anyone know the reason for this? I have attached the relevant parts of my code that contain the variables sl_mult and tp_mult which are optimized and cause the number of trades to vary. I have double checked and the multipliers do not appear anywhere else in my code.


Thank you. 

void PlaceBuyOrder()
  {
   double lotSize = 1; // Example lot size
   double atr = GetATRValue(1);
   double minStopLevel = GetMinStopLevel();
   double price = SymbolInfoDouble(_Symbol, SYMBOL_ASK); // Get the current ask price
   double stopLoss = price - atr * sl_mult;
   double takeProfit = price + atr * tp_mult;

// Ensure stop loss and take profit are valid
   if((price - stopLoss) < minStopLevel)
     {
      stopLoss = price - minStopLevel;
     }

   if((takeProfit - price) < minStopLevel)
     {
      takeProfit = price + minStopLevel;
     }

   if(trade.Buy(lotSize, _Symbol, price, stopLoss, takeProfit))
     {
      Print("Buy order placed successfully.");
     }
   else
     {
      ExtCheckPassed = false;
      Print("Failed to place buy order. Error code: ", GetLastError());
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void PlaceSellOrder()
  {
   double lotSize = 1; // Example lot size
   double atr = GetATRValue(1);
   double minStopLevel = GetMinStopLevel();
   double price = SymbolInfoDouble(_Symbol, SYMBOL_BID); // Get the current ask price
   double stopLoss = price + atr * sl_mult;
   double takeProfit = price - atr * tp_mult;

// Ensure stop loss and take profit are valid
   if((stopLoss - price) < minStopLevel)
     {
      stopLoss = price + minStopLevel;
     }

   if((price - takeProfit) < minStopLevel)
     {
      takeProfit = price - minStopLevel;
     }

   if(trade.Sell(lotSize, _Symbol, price, stopLoss, takeProfit))
     {
      Print("Buy order placed successfully.");
     }
   else
     {
      ExtCheckPassed = false;
      Print("Failed to place buy order. Error code: ", GetLastError());
     }
  }


//////// TRADE LOGIC /////////////


   // Check if enough time has passed since the last trade
   if(currentTime - lastTradeTime < PeriodSeconds())
   {
      return; // Exit the function if not enough time has passed
   }

   if(signal == 1)
   {
      PlaceBuyOrder();
      signal = 0;
      lastTradeTime = currentTime; // Update the last trade time
   }

   if(signal == -1)
   {
      PlaceSellOrder();
      signal = 0;
      lastTradeTime = currentTime; // Update the last trade time
   }
}
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Requests to execute trade operations are formalized as orders. Each order has a variety of properties for reading. Information on them can be...
 
Rameen SharifHello, My EA seems to change the amount of orders executed depending on the SL or TP (holding all other variables constant) which doesn't make sense to me. It seems that the larger the SL and TP, the less trades that are being taken. Would anyone know the reason for this? I have attached the relevant parts of my code that contain the variables sl_mult and tp_mult which are optimized and cause the number of trades to vary. I have double checked and the multipliers do not appear anywhere else in my code. Thank you. 

Hello!! It's not possible to know from the code you posted, but considering that your EA only keeps 1 position open, it makes perfect sense that, when increasing/decreasing the SL and/or TP, this would reflect on the number of open positions: positions with levels of stops that are further away take longer to close, consequently, fewer positions will be opened, right? 😊

 
Rameen Sharif:
   double stopLoss   = price - atr * sl_mult;
   double takeProfit = price + atr * tp_mult;

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
              MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

  3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

    Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
    My GBPJPY shows average spread = 26 points, average maximum spread = 134.
    My EURCHF shows average spread = 18 points, average maximum spread = 106.
    (your broker will be similar).
              Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

 
Vinicius Pereira De Oliveira #:

Hello!! It's not possible to know from the code you posted, but considering that your EA only keeps 1 position open, it makes perfect sense that, when increasing/decreasing the SL and/or TP, this would reflect on the number of open positions: positions with levels of stops that are further away take longer to close, consequently, fewer positions will be opened, right? 😊

Hello!

The EA doesn't just keep 1 position open. As soon as the signal is detected (-1 or 1), it gets passed onto the if condition that places a buy or sell order. After execution the signal is set back to 0, ready to open a new position. 

 
Rameen Sharif #Hello! The EA doesn't just keep 1 position open. As soon as the signal is detected (-1 or 1), it gets passed onto the if condition that places a buy or sell order. After execution the signal is set back to 0, ready to open a new position. 

Well, in this case, another possibility that occurred to me would be that sl_mult and/or tp_mult are being used, in some way, in their position opening condition/verification (signal)... This is what can be assumed, without see the code.