How do i limit the number of trades in a trading range even when the siginal criteria is still met mql5

 

im tring to write a mql5 code based of rsi... i want the EA to only take two trades max a day even when more siginal criteria still pop up... further more, a max of one sell order and one buy order. How do i go about that... my code apparanently takes as many trades as possible as long as the criteria is met 

 
moud0: only take two trades max a day

The easiest is: at the start of a day, set your counts to zero. After opening an order, increment the count. Don't open if it exceeds your maximum.
          Please help with one trade per day code - Day Trading - Expert Advisors and Automated Trading - MQL5 programming forum #3.2 (2020)

 
moud0:

im tring to write a mql5 code based of rsi... i want the EA to only take two trades max a day even when more siginal criteria still pop up... further more, a max of one sell order and one buy order. How do i go about that... my code apparanently takes as many trades as possible as long as the criteria is met 

Hello Moud,

Here is a detailed solution to your questions. To set the maximum trade, you need to know how to get the number of trades taken by the EA within the specified time.

Here is a code example demonstrating that:

#include <Trade/Trade.mqh>

// Create an instance of the CTrade class
CTrade trade;

// Define the start and end times for the history selection
string start = "00:00";
string end = "23:59";

// Define the magic number for the EA
int MagicNumber = 10;

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
// Set the magic number for the EA
   trade.SetExpertMagicNumber(MagicNumber);

// Convert start and end times to datetime objects
   datetime start_time = StringToTime(start);
   datetime end_time = StringToTime(end);

// Print the start and end times
   Print(start_time);
   Print(end_time);

// Select the history for the specified time range
   bool success = HistorySelect(start_time, end_time);
   int totalorder = 0;

// If history selection is successful, loop through the orders
   if(success)
     {
      int total = HistoryOrdersTotal();

      for(int i = 0; i < HistoryOrdersTotal(); i++)
        {
         // Get the ticket number of the order at index i
         ulong ticket = HistoryOrderGetTicket(i);

         // Check if the order belongs to the EA based on magic number
         if(HistoryOrderGetInteger(ticket, ORDER_MAGIC) == MagicNumber)
           {
            totalorder++;
           }
        }
     }

// Print the total number of orders opened by the EA
   Print("Total number of orders opened by the EA: ", totalorder);
  }

Explanation:

#include <Trade/Trade.mqh>

  • This line includes the " Trade.mqh" file, which contains the declaration of the " CTrade" class. This class is used for trading operations in MQL5.

CTrade trade;

  • This line declares an object named trade of the CTrade class. This object will be used to perform trading operations in the script.

string start = "00:00"; and string end = "23:59";

  • These lines define two string variables named start and end , which represent the start and end times for selecting the trading history. The times are in the format HH:MM.

int MagicNumber = 10;

  • This line defines an integer variable named MagicNumber and assigns it a value of 10. This variable represents the magic number of the EA, which is used to identify its orders.

void OnStart()

  • This line declares the start function of the script, which is automatically called when the script is executed.

trade.SetExpertMagicNumber(MagicNumber);

  • This line sets the magic number for the EA using the SetExpertMagicNumber method of the CTrade class.

datetime start_time = StringToTime(start); and datetime end_time = StringToTime(end);

  • These lines convert the start and end time strings into datetime objects using the StringToTime function. This function converts a string representation of a time into a datetime value.

Print(start_time); and Print(end_time);

  • These lines print the start and end times to the console for debugging purposes. They display the values of the start_time and end_time variables.

bool success = HistorySelect(start_time, end_time);

  • This line selects the trading history for the specified time range using the HistorySelect function. It returns true if the history selection is successful, and false otherwise.

int totalorder = 0;

  • This line initializes an integer variable named totalorder to store the total number of orders opened by the EA.

if (success) { ... }

  • This block of code is executed if the history selection is successful. It loops through all the orders in the trading history using a for loop.

int total = HistoryOrdersTotal();

  • This line calculates the total number of orders in the trading history using the HistoryOrdersTotal function.

ulong ticket = HistoryOrderGetTicket(i);

  • This line retrieves the ticket number of the order at index i using the HistoryOrderGetTicket function.

if (HistoryOrderGetInteger(ticket, ORDER_MAGIC) == MagicNumber) { ... }

  • This line checks if the order belongs to the EA based on its magic number. It uses the HistoryOrderGetInteger function to retrieve the magic number of the order.

totalorder++;

  • This line increments the totalorder variable if the order belongs to the EA.

Print("Total number of orders opened by the EA: ", totalorder);

  • This line prints the total number of orders opened by the EA to the console for debugging purposes.

this code will help you get the total trades taken by the EA, with that you can set condition like:

if(totalorder < 2)
{
  // Buy
}


note:  a magic number in trading refers to a unique identifier assigned to orders placed by an Expert Advisor (EA) or script.


If you have more questions pertaining to the explanation, please ask.

i hope you find this helpful.



 
Israel Pelumi Abioye #:

Hello Moud,

Here is a detailed solution to your questions. To set the maximum trade, you need to know how to get the number of trades taken by the EA within the specified time.

Here is a code example demonstrating that:

Explanation:

#include <Trade/Trade.mqh>

  • This line includes the " Trade.mqh" file, which contains the declaration of the " CTrade" class. This class is used for trading operations in MQL5.

CTrade trade;

  • This line declares an object named trade of the CTrade class. This object will be used to perform trading operations in the script.

string start = "00:00"; and string end = "23:59";

  • These lines define two string variables named start and end , which represent the start and end times for selecting the trading history. The times are in the format HH:MM.

int MagicNumber = 10;

  • This line defines an integer variable named MagicNumber and assigns it a value of 10. This variable represents the magic number of the EA, which is used to identify its orders.

void OnStart()

  • This line declares the start function of the script, which is automatically called when the script is executed.

trade.SetExpertMagicNumber(MagicNumber);

  • This line sets the magic number for the EA using the SetExpertMagicNumber method of the CTrade class.

datetime start_time = StringToTime(start); and datetime end_time = StringToTime(end);

  • These lines convert the start and end time strings into datetime objects using the StringToTime function. This function converts a string representation of a time into a datetime value.

Print(start_time); and Print(end_time);

  • These lines print the start and end times to the console for debugging purposes. They display the values of the start_time and end_time variables.

bool success = HistorySelect(start_time, end_time);

  • This line selects the trading history for the specified time range using the HistorySelect function. It returns true if the history selection is successful, and false otherwise.

int totalorder = 0;

  • This line initializes an integer variable named totalorder to store the total number of orders opened by the EA.

if (success) { ... }

  • This block of code is executed if the history selection is successful. It loops through all the orders in the trading history using a for loop.

int total = HistoryOrdersTotal();

  • This line calculates the total number of orders in the trading history using the HistoryOrdersTotal function.

ulong ticket = HistoryOrderGetTicket(i);

  • This line retrieves the ticket number of the order at index i using the HistoryOrderGetTicket function.

if (HistoryOrderGetInteger(ticket, ORDER_MAGIC) == MagicNumber) { ... }

  • This line checks if the order belongs to the EA based on its magic number. It uses the HistoryOrderGetInteger function to retrieve the magic number of the order.

totalorder++;

  • This line increments the totalorder variable if the order belongs to the EA.

Print("Total number of orders opened by the EA: ", totalorder);

  • This line prints the total number of orders opened by the EA to the console for debugging purposes.

this code will help you get the total trades taken by the EA, with that you can set condition like:


note:  a magic number in trading refers to a unique identifier assigned to orders placed by an Expert Advisor (EA) or script.


If you have more questions pertaining to the explanation, please ask.

i hope you find this helpful.



there is a slight mistake here. the code returns all the orders placed, including the Entry, SL, and TP. 

but if you want to work with only the entries made during the specified period. here is the right code:

#include <Trade/Trade.mqh>

// Create an instance of the CTrade class
CTrade trade;

// Define the start and end times for the history selection
string start = "00:00";
string end = "23:59";

// Define the magic number for the EA
int MagicNumber = 10;

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
// Set the magic number for the EA
   trade.SetExpertMagicNumber(MagicNumber);

// Convert start and end times to datetime objects
   datetime start_time = StringToTime(start);
   datetime end_time = StringToTime(end);

// Print the start and end times
   Print(start_time);
   Print(end_time);

// Select the history for the specified time range
   bool success = HistorySelect(start_time, end_time);
   int totalDeal = 0;

// If history selection is successful, loop through the orders
   if(success)
     {

      for(int i = 0; i < HistoryDealsTotal(); i++)
        {
         ulong ticket = HistoryDealGetTicket(i);

         if(HistoryDealGetInteger(ticket, DEAL_MAGIC) == MagicNumber)
           {
            if(HistoryDealGetInteger(ticket, DEAL_ENTRY) == DEAL_ENTRY_IN)
              {
               totalDeal++;
              }
           }
        }

     }

// Print the total number of orders opened by the EA
   Print("Total number of orders opened by the EA: ", totalDeal);
  }
Reason: