how can I use a variable fron ontick in a function?

 

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

 
zelda_lee:

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...

 
zelda_lee:

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

 
remcous:

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


Paul Anscombe:

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.

 
zelda_lee:

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.

Show us some code...
 
zelda_lee:

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.

Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
One Click Close The script allows users to easily close positions if their profit/loss reaches or exceeds a value specified in pips. Please set slippage value first. Sometimes some positions do not close due to high volatility of the market. Please set larger slippage or restart the script. The free demo version is: ...
 
zelda_lee:

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);



 
remcous:

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 

 
zelda_lee:

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.
 
Paul Anscombe:

he has not redefined they are different variables  ema10_global  and ema10 

yeah, see that after.
Reason: