Backtesting takes too long

 

Hello everyone, im struggling with backtesting EA. Normally when I backtest with visualized chart, it will run through smoothly. But with the code below, it doesn't do it quickly any more. Even if I just run the backtest without chart, it will eventually disconnect and stop the test after a few seconds

I tried to debug and found the problem comes form the line (ii>0). However, I don't know an alternative way to solve it. 

1. I need (ii>0) or else, the EA will return the error of "rsi out of array"

2. If I add (ii>0 && ii<20) to limit the array, then it won't execute trades 

Below is the code, i deleted all other unimportant functions. Please suggest me any changes should be applied for it

#include <Trade\Trade.mqh>

CTrade trade;
CAccountInfo acc;

bool orderSent = false;

//Arrays
double rsi[];
int ii, rsiValue;

//=========================================

void OnTick()
{
   rsiValue = iRSI(_Symbol, PERIOD_CURRENT, 14, PRICE_CLOSE);
   ii = CopyBuffer(rsiValue,0, 0, RSI_ARRAY_SIZE, rsi);

      if (ii>0) //this is the line where it slow down my backtesting process
      {
         if (rsi[ii-2] > 70 && orderSent == false)
         {
           orderSent = true;
           trade.PositionOpen(_Symbol,ORDER_TYPE_SELL, 0.01, SymbolInfoDouble(Symbol(),SYMBOL_BID),0.0,0.0,NULL);
           trade.SellLimit(0.01, SymbolInfoDouble(Symbol(),SYMBOL_BID)+0.01, NULL, 0.0, 0.0, ORDER_TIME_GTC, 0,"");
   	 }

   	 if (acc.Profit() >= 10) // if equity is $10 higher than balance => close all position/delete all pending order
         { 
            orderSent=false;
            trade.PositionClose("EURUSD", 100);
            for(int i=OrdersTotal()-1;i>=0;i--)
            {
               ulong orderticket = OrderGetTicket(i);
               trade.OrderDelete(orderticket);
            } 
      	 }
       } 
} // end OnTick { }
 
  1. You are obtaining the indicator handle on every tick. Don't!
    Get it only once, in the OnInit() handler.
  2. You are reading the buffer on every tick. Don't!
    Read it only once per bar — Detecting the start of a new bar or candle
Detecting the start of a new bar or candle
Detecting the start of a new bar or candle
  • www.mql5.com
Detecting the start of a new bar or candle, in the OnTick() event handler of an expert advisor.
 
Fernando Carreiro #:
  1. You are obtaining the indicator handle on every tick. Don't!
    Get it only once, in the OnInit() handler.
  2. You are reading the buffer on every tick. Don't!
    Read it only once per bar — Detecting the start of a new bar or candle

wow, I didn't know that it could be this simple.

I appreciate this, thank you a lot, sir!