hi, i have a variable in the ontick which is based on a moving average so it keeps on changing (and thats good)
I know I can't use it in a function since its not global, but if I declare it globally the value will be fixed to that when it was first loaded.
for now i'm reclaring the varible both in the function and ontick, but it's not the best solution since i might update a value of one and not the other
so whats the best workaround for this pls?
thanks
Hello,
Why don't you declare global variable with a value like -1 or something else ? And you test your variable :
if (myglobalvar==-1) do nothing
The most easy i found...
hi, i have a variable in the ontick which is based on a moving average so it keeps on changing (and thats good)
I know I can't use it in a function since its not global, but if I declare it globally the value will be fixed to that when it was first loaded.
for now i'm reclaring the varible both in the function and ontick, but it's not the best solution since i might update a value of one and not the other
so whats the best workaround for this pls?
thanks
if you declare it in global space it is not fixed, you can see it and change it = available to all of the program
Hello,
Why don't you declare global variable with a value like -1 or something else ? And you test your variable :
if (myglobalvar==-1) do nothing
The most easy i found...
sorry am not understanding your suggestion
if you declare it in global it is not fixed, you can see it and change it = available to all of the program
by fixed I mean it is not updating with everytick, it is reading the moving average value of when the EA is loaded and remains with that value.
i can confirm this because i have created 2 variables, one global and one under the ontick function, and then printed them both as comments, and only the ontick one updates.
sorry am not understanding your suggestion
by fixed I mean it is not updating with everytick, it is reading the moving average value of when the EA is loaded and remains with that value.
i can confirm this because i have created 2 variables, one global and one under the ontick function, and then printed them both as comments, and only the ontick one updates.
sorry am not understanding your suggestion
by fixed I mean it is not updating with everytick, it is reading the moving average value of when the EA is loaded and remains with that value.
i can confirm this because i have created 2 variables, one global and one under the ontick function, and then printed them both as comments, and only the ontick one updates.
you need to show your code
#property copyright "Copyright 2020, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property strict double ema10_global = iMA(NULL, 0, 10, 0, MODE_EMA, PRICE_CLOSE, 1); void OnTick() { double ema10 = iMA(NULL, 0, 10, 0, MODE_EMA, PRICE_CLOSE, 1); Comment("global : ", ema10_global, "\n ontick : ", ema10); }
if you run it as it is you see the global ema will get the first value when loaded and wont update, hence i can not use it in function since it will have a wrong value.
- www.mql5.com
if you run it as it is you see the global ema will get the first value when loaded and wont update, hence i can not use it in function since it will have a wrong value.
using index 1 you are asking for the last bar value not the current value, so it will only update when there is a new bar
void OnTick() { double ema10 = iMA(NULL, 0, 10, 0, MODE_EMA, PRICE_CLOSE, 1); Comment("global : ", ema10_global, "\n ontick : ", ema10);
It's normal, you must no redefine your ema10_global as double in your global AND in you ontick...
then :
he has not redefined they are different variables ema10_global and ema10
if you run it as it is you see the global ema will get the first value when loaded and wont update, hence i can not use it in function since it will have a wrong value.
//+------------------------------------------------------------------+ //| test.mq4 | //| Copyright 2020, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2020, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property strict double ema10_global = iMA(NULL, 0, 10, 0, MODE_EMA, PRICE_CLOSE, 1); //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- double ema10 = iMA(NULL, 0, 10, 0, MODE_EMA, PRICE_CLOSE, 0); Comment("global : ", ema10_global, "\n ontick : ", ema10); } //+------------------------------------------------------------------+Don't forget, last digit in iMA call is the index of bar in chart, 0 mean current and 1 last closed bar.
he has not redefined they are different variables ema10_global and ema10
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
hi, i have a variable in the ontick which is based on a moving average so it keeps on changing (and thats good)
I know I can't use it in a function since its not global, but if I declare it globally the value will be fixed to that when it was first loaded.
for now i'm reclaring the varible both in the function and ontick, but it's not the best solution since i might update a value of one and not the other
so whats the best workaround for this pls?
thanks