HistorySelect() slow down EA

 

Hi,


I'm coding an EA that need to read the past trades, I'm using the function HistorySelect() to read them.

But as I could see from other users on the forum, the HistorySelect() feature slows down the backtest and execution of the EA a lot.

I would like to select a historical period from HistorySelect(X,TimeCurrent()) where X is the Time where I had the last negative closed Deal. 

The problem is that to get the closing moment of my last negative Deal, I have to use HistorySelect() first. 

How can I speed up the use of HistorySelect()?

Documentation on MQL5: Trade Functions / HistorySelect
Documentation on MQL5: Trade Functions / HistorySelect
  • www.mql5.com
Retrieves the history of deals and orders for the specified period of server time. Parameters from_date [in]  Start date of the request...
 

You may use OnTradeTransaction to save the latest loss time whenever a trade closes in loss.

I did not test this:

#include <Trade\DealInfo.mqh>
datetime latest_loss_time=NULL;
void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
  {
   CDealInfo      m_deal;                    
   if(trans.type==TRADE_TRANSACTION_DEAL_ADD)
     {
      if(HistoryDealSelect(trans.deal))
         m_deal.Ticket(trans.deal);

      long deal_type=-1;
      m_deal.InfoInteger(DEAL_ENTRY,deal_type);

      double deal_profit=-1;
      m_deal.InfoDouble(DEAL_PROFIT,deal_profit);

      long deal_time=-1;
      m_deal.InfoInteger(DEAL_TIME,deal_time);

      if((ENUM_DEAL_ENTRY)deal_type == DEAL_ENTRY_OUT && deal_profit<0)
         latest_loss_time=deal_time;
         
     }
  }
 
Luca De Andrea:

I'm coding an EA that need to read the past trades, I'm using the function HistorySelect() to read them.

But as I could see from other users on the forum, the HistorySelect() feature slows down the backtest and execution of the EA a lot.

I would like to select a historical period from HistorySelect(X,TimeCurrent()) where X is the Time where I had the last negative closed Deal. 

The problem is that to get the closing moment of my last negative Deal, I have to use HistorySelect() first. 

How can I speed up the use of HistorySelect()?

Yes, or you can do it like this...

// Define global variables to store the last negative deal time and its closing price
datetime lastNegativeDealTime = 0;
double lastNegativeDealPrice = 0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    // Initialize the last negative deal time and price
    InitializeLastNegativeDeal();
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Function to initialize the last negative deal information       |
//+------------------------------------------------------------------+
void InitializeLastNegativeDeal()
{
    int dealsTotal = HistoryDealsTotal();
    for (int i = dealsTotal - 1; i >= 0; i--)
    {
        // Fetch deal information
        if (HistoryDealSelect(i))
        {
            double dealProfit = HistoryDealGetDouble(DEAL_PROFIT);
            if (dealProfit < 0)
            {
                lastNegativeDealTime = HistoryDealGetInteger(DEAL_TIME);
                lastNegativeDealPrice = HistoryDealGetDouble(DEAL_PRICE);
                break; // Exit loop after finding the last negative deal
            }
        }
    }
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    // Your trading logic goes here...
    // Use lastNegativeDealTime and lastNegativeDealPrice as needed...
}
The OnInit() function initializes the last negative deal time and price by calling the InitializeLastNegativeDeal()function.
The InitializeLastNegativeDeal()function iterates through the historical deals using HistoryDealSelect() and HistoryDealGetXXX() functions to find the last negative deal. Once found, it stores the deal time and price in global variables.
In the OnTick()function (where your trading logic would typically reside), you can use the lastNegativeDealTime and lastNegativeDealPrice variables as needed without repeatedly calling HistorySelect(). This helps in speeding up the execution of your EA.

By initializing the last negative deal information only once during initialization and then using the cached values in subsequent ticks, you can significantly reduce the processing time and optimize the performance of your EA.