Why do the prices of the lines not match?

 

Why the Bid price and Ask price indicated by MT5 do not match the Bid price and Ask price that I execute by code? At all times there is a gap.


//+------------------------------------------------------------------+
//|                                                     Prueba01.mq5 |
//|                                Copyright 2021, Simón Del Vecchio |
//|                    https://www.mql5.com/en/users/simondelvecchio |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, Simón Del Vecchio"
#property link      "https://www.mql5.com/en/users/simondelvecchio"
#property version   "1.00"

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double Ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
double Bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   CreateBidAskLine();
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick(void)
  {
   Ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
   Bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);
   ObjectSetDouble(0, "BID", OBJPROP_PRICE, Bid);
   ObjectSetDouble(0, "ASK", OBJPROP_PRICE, Ask);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CreateBidAskLine()
  {
   ObjectCreate(0, "BID", OBJ_HLINE, 0, 0, Bid);
   ObjectSetInteger(0, "BID", OBJPROP_COLOR, clrGreen);
//
   ObjectCreate(0, "ASK", OBJ_HLINE, 0, 0, Ask);
   ObjectSetInteger(0, "ASK", OBJPROP_COLOR, clrRed);
  }
//+------------------------------------------------------------------+
 
Antonio Simon Del Vecchio: Why the Bid price and Ask price indicated by MT5 do not match the Bid price and Ask price that I execute by code? At all times there is a gap.

Because there is a delay in updating graphical objects. Print out the values in a "Comment()" and you will see it in more real-time.

 
Fernando Carreiro #:

Because there is a delay in updating graphical objects. Print out the values in a "Comment()" and you will see it in more real-time.

Thanks Fernando,

I did what you told me and apparently the Bid and Ask lines that MT5 shows are not real. It always shows a smaller spread than the real one.

 

I'm not having that problem and the delay only appears infrequently.


#define MAsk "Ask Line"
#define MBid "Bid Line"

int OnInit(void) {
   ObjectCreate(     0, MAsk, OBJ_HLINE, 0, 0, 0 );
   ObjectCreate(     0, MBid, OBJ_HLINE, 0, 0, 0 );
   ObjectSetInteger( 0, MAsk, OBJPROP_COLOR, clrRed   );
   ObjectSetInteger( 0, MBid, OBJPROP_COLOR, clrGreen );
   return(INIT_SUCCEEDED);
  };
  
void OnDeinit( const int  reason ) {
   ObjectDelete( 0, MAsk );
   ObjectDelete( 0, MBid );
};

void OnTick(void) {
   MqlTick oTick; // Tick data object
   if( SymbolInfoTick( _Symbol, oTick ) ) {
      ObjectSetDouble(0, MAsk, OBJPROP_PRICE, oTick.ask );
      ObjectSetDouble(0, MBid, OBJPROP_PRICE, oTick.bid );
      Comment( "Ask: ", DoubleToString( oTick.ask, _Digits ),
         ", Bid: ", DoubleToString( oTick.bid, _Digits ) );
   };
};
 
Fernando Carreiro #:

I'm not having that problem and the delay only appears infrequently.


I used your code to test and I have the same problem. Will it be the broker? Although I tried in two brokers and I have the same problem. It's strange.

 
Antonio Simon Del Vecchio #:I used your code to test and I have the same problem. Will it be the broker? Although I tried in two brokers and I have the same problem. It's strange.
It could also be a problem with your setup or your PC!
 
Fernando Carreiro #:
It could also be a problem with your setup or your PC!

Yes, it could also be. Thank you very much for your time Fernando. Greetings.

 
Antonio Simon Del Vecchio: Why the Bid price and Ask price indicated by MT5 do not match the Bid price and Ask price that I execute by code? At all times there is a gap.

I have a similar (or same) problem.

With this code my closing and my Bid do not coincide.... which is better for do calculation?

MqlTick tick;
SymbolInfoTick(_Symbol, tick);
MqlRates price[];
   if(!CopyRates(NULL, PERIOD_CURRENT, 0, 1, price))
     {
      GetLastError();
      PrintFormat("Error! Copy rates to array price[] failed, error code: %d", _LastError);
      return;
     }
Print("Ask ",tick.ask);
Print("Bid ",tick.bid);
Print("Low ",price[0].low);
Print("Close ",price[0].close);
 
Milko Vivaldi #:

I have a similar (or same) problem.

With this code my closing and my Bid do not coincide.... which is better for do calculation?

The price can change before the 2 function calls.

What symbol is it ? What values did you get, show the log.

Reason: