Test is really slow

 

Hello,

After I added this code my backtest got REALLY slow to the point where it crashes after a few months...

Any ideas on how to fix this.

void CloseAllTrades()
  {
   for(int i=PositionsTotal()-1; i>=0; i++)
     {
      int ticket = PositionGetTicket(i);
      trade.PositionClose(ticket);
     }
  }

void NewCandle()
  {
   if(AccountInfoDouble(ACCOUNT_EQUITY)>AccountInfoDouble(ACCOUNT_BALANCE))
     {
      CloseAllTrades();
     }
  }
//This code is getting called in OnTick()

if(Latest_Close_Price != BarData[1].close)
     {
      Latest_Close_Price = BarData[1].close;
      NewCandle();
     }

Thanks to everyone

 
Luvaha:

Hello,

After I added this code my backtest got REALLY slow to the point where it crashes after a few months...

Any ideas on how to fix this.


Thanks to everyone

I'm not a coding master yet but I can say that the ticket value must be of "uint" type rather than "int";

also if "Latest_Close_Price" takes the value of candle close 0, on an every tick modeling, it will be different from candle close 1 almost every tick, and then execute the functions every time. You can use this condition instead :

static int LastBarsCount=0;

if(iBars(_Symbol,PERIOD_CURRENT)>LastBarsCount)
 {
 LastBarsCount=iBars(_Symbol,PERIOD_CURRENT);
 NewCandle();
 }
 
Sebastien Nicolas Paul Boulenc #:

I'm not a coding master yet but I can say that the ticket value must be of "uint" type rather than "int";

also if "Latest_Close_Price" takes the value of candle close 0, on an every tick modeling, it will be different from candle close 1 almost every tick, and then execute the functions every time. You can use this condition instead :

I changed it but it didn't work...

But still thanks

 
In the editor start the profiler: Debug => Profiling to find out where your code hangs..
 
I noticed that in your first function, you are incrementing with +1 while the continuing condition is i>=0, starting from a positive index, it's going to be a problem. You need decrementing by 1.
 
Carl Schreiber #:
In the editor start the profiler: Debug => Profiling to find out where your code hangs..

I tried that but it won't debug...

When I press Start profiling on history data it will always run a random indicator and not my ea...

Why is that?

 
How to quickly develop and debug a trading strategy in MetaTrader 5
How to quickly develop and debug a trading strategy in MetaTrader 5
  • www.mql5.com
Scalping automatic systems are rightfully regarded the pinnacle of algorithmic trading, but at the same time their code is the most difficult to write. In this article we will show how to build strategies based on analysis of incoming ticks using the built-in debugging tools and visual testing. Developing rules for entry and exit often require years of manual trading. But with the help of MetaTrader 5, you can quickly test any such strategy on real history.
Reason: