# Trying to sometimes look at only closed bars, sometimes calculate every tick.

4

The expert advisor I wrote is very slow in backtesting. Therefore, I attempted to rewrite it to only look at the recent closed bar, unless one of the preconditions for trading was close to being met, in which case it would calculate every tick, as before.

Unfortunately, now the two versions produce different results. Usually the first version enters a trade slightly before the second version, though sometimes the second version enters a trade slightly sooner. This is a huge problem mostly because it means I am misunderstanding how my code runs!

Here is the main function in both versions, abbreviated to only show the important stuff:

```//+------------------------------------------------------------------+
//|                                                    Two Loops.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+

// Declare variables

//--Main Function
void Calculate(int index) {

if (TimeHour(Time[index]) == 22 && TimeMinute(Time[index]) == 0) {                              // New Day

if (Time[index] != NewDayTime) {                                                             // First Bar of the day

DayOpen = iOpen(Symbol(),0,0);                                                            // Daily Open

//Set some things to zero

} else return;

} else {

DailyHigh = MathMax(High[index],DailyHighBuffer[index+1]);
DailyLow  = MathMin(Low[index],DailyLowBuffer[index+1]);

DailyHighBuffer[index] = DailyHigh;
DailyLowBuffer[index]  = DailyLow;

//--Find Spikes
if (High[index] > DailyHighBuffer[index+1]) SpikeDown = DailyHigh;
if (Low[index] < DailyLowBuffer[index+1]) SpikeUp = DailyLow;

if ((iLow(NULL,0,index+1) < iLow(NULL,0,index+2)) && (iLow(NULL,0,index+1) < Low[index]) &&
(iLow(NULL,0,index+1) < SpikeDown))
SpikeDown = iLow(NULL,0,index+1);

if ((iHigh(NULL,0,index+1) > iHigh(NULL,0,index+2)) && (iHigh(NULL,0,index+1) > High[index]) &&
(iHigh(NULL,0,index+1) > SpikeUp))
SpikeUp = iHigh(NULL,0,index+1);

// If the distance between the DailyOpen and the DailyHigh or DailyLow is big, that is a precondition to being ready to trade.
}

if (Bid > ShortBuffer[index]) {
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"",MAGICMA,0,Red);
GoShort = 1;
}
}

GoLong = 1;
}
}
// Calculate difference between DayOpen and both DailyHigh and DailyLow
}```

Here is the Ontick function in the first version:

```//+------------------------------------------------------------------+
//| Expert tick function version #1                                  |
//+------------------------------------------------------------------+
void OnTick()
{
//---
int i=Bars-Counted_bars-1;           // Index of the first uncounted

if (allLoaded == true || (TimeHour(Time[0]) == 22 && TimeMinute(Time[0]) == 0)) {

// Initialize arrays
}
while(i >= 0) {                                                                                 // Loop for uncounted bars
if (i == 0) {
if (Time[0] != ArrayResizeTime && allLoaded) {
ArrayResizeTime = Time[0];
}
}
if (i < 10000) Calculate(i);
if (i > 0) Counted_bars++;
i--;
}
}
}```

Here is the second version, which triggers calculating every tick long before either version would enter a trade:

```//+------------------------------------------------------------------+
//| Expert tick function version #2                                  |
//+------------------------------------------------------------------+
void OnTick() {
//---
int i=Bars-Counted_bars-1;                                                                     // Index of the first uncounted

while(i > 0) {                                                                                 // Loop for uncounted bars
if (i < 10000) Calculate(i);                                                                // ONLY CALCULATE ON CLOSED BARS IF THE RARE
Counted_bars++;                                                                             // PRECONDITION TO TRADE IS NOT MET
i--;
}
if (((FarUpfromOpen != 0) || (FarDownfromOpen != 0)) &&                                        // ONLY CALCULATE ON EVERY TICK OF OPEN BAR IF ONE
(i == 0)) {                                                                                 // OF THE MANY PRECONDITIONS TO TRADING IS MET.
Calculate(i);
Counted_bars++;
}
}```