how can I use a variable fron ontick in a function? - page 2

 
Paul Anscombe:

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



yes i need the close bar value not current one, sorry if i didn't mention it clearly. either way even when  a new bar is formed it will not update the value of the new previous bar


remcous:

It's normal, you must no redefine your ema10_global as double in your global AND in you ontick...


then :

sorry but ema10_global was defined only once, in the global. the on tick variable is called ema10.

thanks for your edit, but even with your code it still doesn't fix the issue of global variables not being updated...

 
zelda_lee:

yes i need the close bar value not current one, sorry if i didn't mention it clearly. either way even when  a new bar is formed it will not update the value of the new previous bar


sorry but ema10_global was defined only once, in the global. the on tick variable is called ema10.

thanks for your edit, but even with your code it still doesn't fix the issue of global variables not being updated...

I'm sorry but when bar is closed, value don't change.

If you want your ema10_global updated at every closed bar, you need to update it  like this:

//+------------------------------------------------------------------+
//|                                                         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 = 0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   ema10_global = iMA(NULL, 0, 10, 0, MODE_EMA, PRICE_CLOSE, 1);
   double ema10 = iMA(NULL, 0, 10, 0, MODE_EMA, PRICE_CLOSE, 0);
   Comment("global : ", ema10_global, "\n ontick : ", ema10);  
  }
//+------------------------------------------------------------------+
 
remcous:
I'm sorry but when bar is closed, value don't change.

it wont change for that particular bar but the value is aiming for the previous bar, which changes with every new bar

 
zelda_lee:

it wont change for that particular bar but the value is aiming for the previous bar, which changes with every new bar

yeah, see previous reply, you forget to update your ema10_global...
 
zelda_lee:

it wont change for that particular bar but the value is aiming for the previous bar, which changes with every new bar

it works just fine for me...

#property strict

double ema10;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   ema10 = iMA(NULL, 0, 10, 0, MODE_EMA, PRICE_CLOSE, 1);
   Comment("\n ema10: ", ema10);   
}
//+------------------------------------------------------------------+
 
remcous:
yeah, see previous reply, you forget to update your ema10_global...

wow ok that does the trick! so thankful for your help

had no idea you can use such a neat way to update, much appreciated!!



Paul Anscombe:

it works just fine for me...

yes now its working, wasn't aware you can update global from functions

thanks guys for the lesson

 
zelda_lee:

wow ok that does the trick! so thankful for your help

had no idea you can use such a neat way to update, much appreciated!!



yes now its working, wasn't aware you can update global from functions

thanks guys for the lesson

You're welcome :-)

Reason: