Tester different Bar Open price in different testing modes

 

Hi all, can anyone explain:

why this code returns different Ask and Spread values for the same bar OPEN price from different test modes (tested on Open prices only mode and Every tick based on real ticks).


Is there an error in my code or they shouldn't match at all? Regarding to this https://www.mql5.com/en/articles/239 they should:




#define _Ask    SymbolInfoDouble(Symbol(),SYMBOL_ASK)
#define _Bid    SymbolInfoDouble(Symbol(),SYMBOL_BID)
#define _Spread         (int)SymbolInfoInteger(Symbol(),SYMBOL_SPREAD)

#define DD(d) DoubleToString(d,_Digits)  // - Double to String by Digits

int max=10;
int count=0;
int testBar=1;
//+------------------------------------------------------------------+


void OnTick()
  {
   if(count>max){ExpertRemove();} // - just first ten ticks
   
   if(!IsNewBar()){return;} // - if not a new bar tick - bypass
   
   double price=iOpen(NULL,0,testBar); // - get some info about tick
   datetime time=iTime(NULL,0,testBar);
   
   Print(TimeToString(time,TIME_SECONDS)," open:",DD(price)," Ask:",_Ask," Bid:",_Bid," Spread:",_Spread);  

   count++;
   
  }
//+------------------------------------------------------------------+


datetime lastBarTime=0;
 
bool IsNewBar(){

   datetime newBarTime=0;
    
   newBarTime=iTime(NULL,0,1);
   
   //newBarTime=(datetime)SeriesInfoInteger(_Symbol,PERIOD_CURRENT,SERIES_LASTBAR_DATE);
   
/* 
   datetime lastTime[1];
   if (CopyTime(_Symbol,_Period,0,1,lastTime)==1){
      newBarTime=lastTime[0];
   }else{return false;}
*/
   if(newBarTime!=lastBarTime){ //-we have a new bar
      lastBarTime=newBarTime;
      return true;
   }
   
   return false;
}
//+------------------------------------------------------------------+


Open Prices only output:

2018.10.24 19:45:26.947 Core 1  2018.01.03 00:00:00   23:59:00 open:1.20583 Ask:1.20588 Bid:1.2058 Spread:8
2018.10.24 19:45:26.947 Core 1  2018.01.03 00:01:00   00:00:00 open:1.20580 Ask:1.20589 Bid:1.20581 Spread:8
2018.10.24 19:45:26.947 Core 1  2018.01.03 00:02:00   00:01:00 open:1.20581 Ask:1.20588 Bid:1.2058 Spread:8
2018.10.24 19:45:26.947 Core 1  2018.01.03 00:03:00   00:02:00 open:1.20580 Ask:1.20585 Bid:1.2058 Spread:5
2018.10.24 19:45:26.947 Core 1  2018.01.03 00:04:00   00:03:00 open:1.20580 Ask:1.20572 Bid:1.20562 Spread:10
2018.10.24 19:45:26.947 Core 1  2018.01.03 00:05:00   00:04:00 open:1.20562 Ask:1.20569 Bid:1.20561 Spread:8
2018.10.24 19:45:26.947 Core 1  2018.01.03 00:06:30   00:05:00 open:1.20561 Ask:1.2057 Bid:1.2056 Spread:10
2018.10.24 19:45:26.947 Core 1  2018.01.03 00:07:00   00:06:00 open:1.20560 Ask:1.20567 Bid:1.20559 Spread:8
2018.10.24 19:45:26.947 Core 1  2018.01.03 00:08:00   00:07:00 open:1.20559 Ask:1.20567 Bid:1.20561 Spread:6
2018.10.24 19:45:26.947 Core 1  2018.01.03 00:09:30   00:08:00 open:1.20561 Ask:1.20569 Bid:1.2056 Spread:9
2018.10.24 19:45:26.947 Core 1  2018.01.03 00:10:00   00:09:00 open:1.20560 Ask:1.20565 Bid:1.20561 Spread:4

Every tick based on real ticks:

2018.10.24 19:45:32.263 Core 1  2018.01.03 00:00:09   23:59:00 open:1.20583 Ask:1.20591 Bid:1.2058 Spread:11
2018.10.24 19:45:32.263 Core 1  2018.01.03 00:01:03   00:00:00 open:1.20580 Ask:1.20591 Bid:1.20581 Spread:10
2018.10.24 19:45:32.263 Core 1  2018.01.03 00:02:36   00:01:00 open:1.20581 Ask:1.2059 Bid:1.2058 Spread:10
2018.10.24 19:45:32.263 Core 1  2018.01.03 00:03:08   00:02:00 open:1.20580 Ask:1.2059 Bid:1.2058 Spread:10
2018.10.24 19:45:32.263 Core 1  2018.01.03 00:04:00   00:03:00 open:1.20580 Ask:1.20572 Bid:1.20562 Spread:10
2018.10.24 19:45:32.263 Core 1  2018.01.03 00:05:02   00:04:00 open:1.20562 Ask:1.20571 Bid:1.20561 Spread:10
2018.10.24 19:45:32.263 Core 1  2018.01.03 00:06:43   00:05:00 open:1.20561 Ask:1.2057 Bid:1.2056 Spread:10
2018.10.24 19:45:32.263 Core 1  2018.01.03 00:07:21   00:06:00 open:1.20560 Ask:1.20569 Bid:1.20559 Spread:10
2018.10.24 19:45:32.263 Core 1  2018.01.03 00:08:29   00:07:00 open:1.20559 Ask:1.20569 Bid:1.20561 Spread:8
2018.10.24 19:45:32.263 Core 1  2018.01.03 00:09:55   00:08:00 open:1.20561 Ask:1.2057 Bid:1.2056 Spread:10
2018.10.24 19:45:32.263 Core 1  2018.01.03 00:10:03   00:09:00 open:1.20560 Ask:1.2057 Bid:1.20561 Spread:9

As you can notice corresponding Ask and Spread values are different. Why?


Thanks!

The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
The idea of ​​automated trading is appealing by the fact that the trading robot can work non-stop for 24 hours a day, seven days a week. The robot does not get tired, doubtful or scared, it's is totally free from any psychological problems. It is sufficient enough to clearly formalize the trading rules and implement them in the algorithms, and...
 

Because the resolution of price points chosen does not allow for finer calculation.

Take spread with a grain of salt if your test mode is not based on every tick.

 
Enrique Dangeroux:

Because the resolution of price points chosen does not allow for finer calculation.

Take spread with a grain of salt if your test mode is not based on every tick.

Thank's in advance, but I think you are loosing the whole picture.

In Bar Open Pprice mode where the open bar price came from? From server i guess. What exactly is "Bar Open Price"? Is it just Ask? Or Bid? Or Bid+Spread? Looks like just Bid price.

In Every tick mode we have the first tick in each bar which means exactly the same - bar opening price. We don't look at other ticks just this one(look at he code - we are tracing just new bar event). And this one from the server too.


Now, should OPEN price of ONE particular BAR be equal for both modes? Definitely yes, but they don't! ///EDITED


It seems to me if Bids of both modes are equal, the error goes with Spread value and Ask is calculated from Bid+Spread, If so why spread of Bar opening is not equal to the same spread from tick mode? Where does it came from?

It would be nice to see some explanation from developers (thank you guys - platfom is great - big job)

 
terminalstat:

Thank's in advance, but I think you are loosing the whole picture.

In Open price mode where the open bar price comes from? From server i guess. What exactly is it? is it just Ask? or Bid? or Bid+Spread?

In Every tick mode we have the first tick in each bar which means exactly the same - bar opening price. We don't look at other ticks just first one. And this one from the server too.


Now should this prices be equeal to each other? Defenitely yes, but they don't!

It seems to me if Bids of both modes are equal, the error goes with Spread value and Ask is calculated from Bid+Spread, If so why spread of Bar opening is not equal to the same spread from tick mode? Where does it came from?

It would be nice to see some explanation from developers (thank you guys - platfom is great - big job)

charts (candles or whatever) are based on BID price. do is open prices of candles in tester or non tester. no difference

also, if no gap happens between two consecutive bars, of course the open price of bar N is equal to close price of bar N-1. I hope you asked that there.
otherwise what do you mean they should be equal ?
 
Code2219 or probably 2319:
charts (candles or whatever) are based on BID price. do is open prices of candles in tester or non tester. no difference

also, if no gap happens between two consecutive bars, of course the open price of bar N is equal to close price of bar N-1. I hope you asked that there.
otherwise what do you mean they should be equal ?

Thanks to point me on that, I was afraid it's not clear)

No, we are talking about the same bar. Lets say bar of 1 january 12.00.00. Information about this bar is different depending on mode. In Open Price mode it's one price, and when we are accesing the SAME bar through Tick mode and getting it's OPEN price - it's kinda different) That's what I'm talking about, hope it's more clear.

 
terminalstat:
Thank's, can you run my code and tell me: really there is no difference? May be my terminal is broken, ..mm?
looking at your print log, it's clear that bids are how open prices are reliable/same in both modes.

i'm not sure how other two are simulated in OpenPricesOnly mode. (has bid & spread and calculates ask? has bid & ask and calculates spread ? has three of them ?)
maybe it's covered in docs. (tester part)

anyway, I stick to RealTicks mode, always. and can  only suggest that
 
terminalstat: Thank's, can you run my code and tell me: really there is no difference? May be my terminal is broken, ..mm?

How can you question the results when your own code is in fact forcing it to only react to a new bar tick (open price)?

if(!IsNewBar()){return;} // - if not a new bar tick - bypass

Obviously, if your code is causing the OnTick() event handler to return, and do nothing further unless it is on a new bar (opening price), then testing it on Ticks or Real Ticks has no impact and no different than testing on only Open Prices.

Your code is only printing the Ask and Bid and Counting only on the opening of a bar and therefore equivalent to an Open Prices only irrespective of using Tick Data or not for your test.

Is that not obvious? What did you expect?

If you want it to run on every tick, then remove the above condition from your code!

 
Fernando Carreiro:

How can you question the results when your own code, is in fact forcing it to only react to a new bar tick (open price)?

Obviously, if your code is causing the OnTick() event handler to return, and do nothing further unless it is on a new bar (opening price), then testing it on Ticks or Real Ticks has no impact and no different than testing on only Open Prices.

Your code is only printing the Ask and Bid and Counting only on the opening of a bar and therefore equivalent to an Open Prices only irrespective of using Tick Data for your test.

Is that not obvious? What did you expect?

he's asking why ask and spread prices are different in different tester modes (OPonly, RealTicks).

 
Code2219 or probably 2319:

he's asking why ask and spread prices are different in different tester modes (OPonly, RealTicks).

Thank's man, exactly!
 
Code2219 or probably 2319:he's asking why ask and spread prices are different in different tester modes (OPonly, RealTicks).

Oh! OK, misunderstood the problem!

@terminalstat:

In that case, I can say the bid prices are not different and are exactly the same. Only the ask prices are different because the system is using the average spread of the bar for "Open Prices" (the spread stored on the bar data), instead of the individual opening tick spread. That is to be expected and is not a bug!

 
Fernando Carreiro:

Oh! OK, misunderstood the problem!

In that case, I can say the bid prices are not different and are exactly the same.

Only the ask prices are different because the system is using he average spread of the bar for "Open Prices" (the spread stored on the bar data), instead of the individual opening tick spread. That is to be expected and is not a bug!

Thank, info is 100%? where did you get it?
Reason: