While in execution, is it useful for an EA to ask for current tick more than once?

 

Hi,

I've a question which I guess I know the answer to it, but just in case...

Suppose I have an EA with the OnTick function. When that function is called, it means a new tick was created with a certain price value. Then the code inside of the function starts to run. Now suppose it takes some time for that code to be executed, time in which the market has already formed a new tick with a different price value. If, at this moment, I ask inside the OnTick the current asset's price (with CopyRates or CopyClose or SymbolInfoDouble(_Symbol,SYMBOL_LAST,result)), will it give me this new, updated price value or will it give me the price the asset had when OnTick was called?

Or putting it in a different perspective: if, right at the start of my OnTick function, I save the current price in a global variable and use it throught the code, both within OnTick as well as OnTimer and OnChartEvent, will the use of my global variable always imply the use of the latest price available to my EA or I may end up with "not the last price available"?

Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Positioning Constants
Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Positioning Constants
  • www.mql5.com
Positioning Constants - Chart Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Martin Bittencourt: I ask inside the OnTick the current asset's price (with CopyRates or CopyClose or SymbolInfoDouble(_Symbol,SYMBOL_LAST,result)), will it give me this new, updated price value or will it give me the price the asset had when OnTick was called?

Yes, you will get the most recent updated quotes at the moment you run the function call.

However, please note that for most symbols you would need to the read the Bid and Ask quotes and not the Last quote (which is only supported for certain symbols).

Martin Bittencourt: if, right at the start of my OnTick function, I save the current price in a global variable and use it throught the code, both within OnTick as well as OnTimer and OnChartEvent, will the use of my global variable always imply the use of the latest price available to my EA or I may end up with "not the last price available"?

No, if you save it to a variable then reuse that variable's value at a later time, then obviously it is no longer the latest value. It will become stale as it holds the value at the time it was read. It is not updated automatically.

To get the latest value you would need to call the functions again.

 
Fernando Carreiro #:

Yes, you will get the most recent updated quotes at the moment you run the function call.

However, please note that for most symbols you would need to the read the Bid and Ask quotes and not the Last quote (which is only supported for certain symbols).

No, if you save it to a variable then reuse that variable's value at a later time, then obviously it is no longer the latest value. It will become stale as it it holds the value at the time it was read. It is not updated automatically.

To get the latest value you would need to call the functions again.

As the OP is from Brazil, most often they are trading centralised market, with charts based on "last" prices.
 
Fernando Carreiro #:

It is not updated automatically.

Well, naturally I'm thinking about updating the global variable each time OnTick is called ^.^

Fernando Carreiro #:

However, please note that for most symbols you would need to the read the Bid and Ask quotes and not the Last quote (which is only supported for certain symbols).

Alain Verleyen #:
As the OP is from Brazil, most often they are trading centralised market, with charts based on "last" prices.

Hmm that's something interesting I wasn't aware of! So if I decide to use my EA in Forex or even some other stock exchange rather then Brazil's B3, I'll have to update that part of the code? I wonder what else will have I have to update :3

Fernando Carreiro #:

Yes, you will get the most recent updated quotes at the moment you run the function call.

OK than. I was hoping I could save some processing by not getting the last asset's price inside each function that needed it, but it seems I won't be able to do that :T Well thanks guys for your replies!

Reason: