Buffer[0] to variable copy

To add comments, please log in or register
AdnanSyed
69
AdnanSyed  

I have current EA logic portion using actual buffers [0] and [1]. buffer zero values are constantly changing. here is my question


if I copy the buffer[0] to a variable how often will that value update in the variable ?

William Roeder
19155
William Roeder  
as often as you "copy the buffer[0] to a variable."
AdnanSyed
69
AdnanSyed  
William Roeder:
as often as you "copy the buffer[0] to a variable."

thank you!


just to clarify, 


copy buffer xxxxx[0]  to variable 

variable will not change value until next candle ?

Seng Joo Thio
746
Seng Joo Thio  
AdnanSyed:

I have current EA logic portion using actual buffers [0] and [1]. buffer zero values are constantly changing. here is my question

if I copy the buffer[0] to a variable how often will that value update in the variable ?

buffer[0] will change if re-computed because candle 0 is constantly changing (except for open price, which will stay the same throughout).

but when you copy to a variable, that variable takes the value of buffer[0] at the instant that you do the copy, and will stay the same until you repeat everything (i.e. re-compute buffer[0], and copy it to variable) again.

William Roeder
19155
William Roeder  
AdnanSyed: variable will not change value until next candle ?

Next candle is irrelevant. It will not change until you assign it another value.

Global and static variables work exactly the same way in MT4/MT5/C/C++.
  1. They are initialized once on program load.
  2. They don't update unless you assign to them.
  3. In C/C++ you can only initialize them with constants, and they default to zero.
  4. In MTx you should only initialize them with constants. There is no default in MT5 (or MT4 with strict which you should always use.)

    MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
  5. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
              external static variable - Inflation - MQL4 programming forum
AdnanSyed
69
AdnanSyed  

Thank you everyone. 


I am going to copy buffer 0 and buffer one for my indicator triggers, seem like its better open and do some testing.  

To add comments, please log in or register