Restricting number of trades EA opens

 

Hello guys here's my mql4 code, it is suppossed to restrict the number of trades in a day to three but now its not even opening a single trade what could be the problem

#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#include <Indicators/Indicators.mqh>
// define global variables
int numTradesToday = 0; // to store the number of trades made today
bool canOpenPosition = true; // to store whether a new position can be opened or not
int MagicNumber = 16384;
// define custom function to count the number of trades made today
int CountTradesToday()
{
    datetime todayStart = StrToTime(TimeToStr(TimeCurrent(), TIME_DATE)); // get the start time of the current day
    int count = 0;
    for (int i = OrdersHistoryTotal() - 1; i >= 0; i--) // loop through all the closed orders
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) && OrderCloseTime() >= todayStart) // check if the order was closed today
        {
            count++; // increment the trade count
        }
    }
    return count;
}
  double maslowcurr = iCustom(NULL, PERIOD_CURRENT, "ZigZag", 12, 5, 3, 0, 0);
  double maslowprev = iCustom(NULL, PERIOD_CURRENT, "ZigZag", 12, 5, 3, 0, 1);
  double mafastcurr = iCustom(NULL, PERIOD_CURRENT, "VininI_LRMA_color", 34, 0, 0, 0, 0);
  double mafastprev = iCustom(NULL, PERIOD_CURRENT, "VininI_LRMA_color", 34, 0, 0, 0, 1);
  double volatility = iATR(NULL, 0, 14, PRICE_CLOSE);
  double LotSize = 0.01;
// the main function
void OnTick()
{
    if (canOpenPosition && numTradesToday < 3) // check if a new position can be opened
    {
        // opening position code here
     if (//condition for opening trades && OrdersTotal()<1)
     {
      int ticket2;
       ticket2=OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,0,NormalizeDouble(Bid+100*Point,Digits),"RUBBERBANDS",MagicNumber, 0, clrAqua);
        if(ticket2>0)
        {
         if(OrderSelect(ticket2,SELECT_BY_TICKET,MODE_TRADES))
         {
           Print("Buy order opened : ",OrderOpenPrice());
           numTradesToday++;
          }
  }
  else
  {
    Print("Error opening buy order : ",GetLastError()); 
  }   
        // if a new position is opened successfully, increment the trade count
        
        // update the canOpenPosition flag
        if (numTradesToday >= 3)
        {
            canOpenPosition = false; // can't open new positions if the number of trades today is equal to 3
        }
    }
    
    // check if there are any open positions, and update the canOpenPosition flag if needed
    if (OrdersTotal() > 0 && canOpenPosition == false)
    {
        bool allPositionsClosed = true;
        for (int i = OrdersTotal() - 1; i >= 0; i--) // loop through all the open orders
        {
            if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == Symbol()) // check if the order is for the current symbol
            {
                allPositionsClosed = false; // set the flag to false if there is at least one open position
                break; // exit the loop as soon as an open position is found
            }
        }
        if (allPositionsClosed) // if all the positions are closed, reset the canOpenPosition flag
        {
            canOpenPosition = true;
            numTradesToday = CountTradesToday(); // count the number of trades made today again
        }
    }
}
}
Открой новые возможности в MetaTrader 5 с сообществом и сервисами MQL5
Открой новые возможности в MetaTrader 5 с сообществом и сервисами MQL5
  • 2023.02.22
  • www.mql5.com
MQL5: язык торговых стратегий для MetaTrader 5, позволяет писать собственные торговые роботы, технические индикаторы, скрипты и библиотеки функций
 
Brian Mwago Wainaina -:

Hello guys here's my mql4 code, it is suppossed to restrict the number of trades in a day to three but now its not even opening a single trade what could be the problem

Maybe you are counting all orders and positions in your function. While you just want the positions to be counted.

Reason: