Download MetaTrader 5

Struggling with GV code

To add comments, please log in or register
Keith
368
Keith  

Hi all

I am trying to get a variable form one chart, into an ea on a different chart, really confused by usage of Global Variables(platform variables).

Here is what I have tried.(deleted init etc for clarity)


string haLow = "GV_haLow";


int start()
  {
    double testhaLow =  GlobalVariableGet(haLow);
     GetLastError();
   Print ("test of halow = ",testhaLow);
  //----
   return(0);
  }
//+------------------------------------------------------------------+

prints 0, no error message.

the chart with haLow, is also open and running. (Should the GV decalration be on that chart? I tried didn't--- make any difference as far as I could tell).

when I hit F3, there is no GV listed.

Have tried "haLow", and "GV-haLow" as parameters still nada.

What I would really like is a clear, concise explanation of how to declare, assign and use these variables.

I have searched this site and the web and everything remains clear as mud.

cheers

Keith

Simon Gniadkowski
Moderator
18035
Simon Gniadkowski  
kminler:

Hi all

I am trying to get a variable form one chart, into an ea on a different chart, really confused by usage of Global Variables(platform variables).

Here is what I have tried.(deleted init etc for clarity)

prints 0, no error message.

the chart with haLow, is also open and running. (Should the GV decalration be on that chart? I tried didn't--- make any difference as far as I could tell).

when I hit F3, there is no GV listed.

Have tried "haLow", and "GV-haLow" as parameters still nada.

What I would really like is a clear, concise explanation of how to declare, assign and use these variables.

It's all spelled out in the documentation . . .

GlobalVariableSet() to write the value to the GV, GlobalVariableGet() to read it . . . if your code hasn't done the Set there will be no GV to read from.

What is the point of the GetLastError() ? you are doing nothing with it . . . read this: What are Function return values ? How do I use them ?

GreenMoney
379
GreenMoney  
kminler:

What I would really like is a clear, concise explanation of how to declare, assign and use these variables.

I have searched this site and the web and everything remains clear as mud.

Here is some sample code for you to play with:

int init() {
   if (!GlobalVariableCheck("GV_Hello"))   // if there isn't a global variable named "GV_Hello"
      GlobalVariableSet("GV_Hello", 0.0);  // create it and set it to 0.0

   return(0);
}

int start() {
   double cv = 0;
   cv = GlobalVariableGet("GV_Hello");
   if (!GlobalVariableSetOnCondition("GV_Hello", cv+1, cv))  // could also use GlobalVariableSet() and then wouldn't need cv
      Print ("Could not update global variable. Error Code # ", GetLastError());
   else
      Print ("Successfully updated global variable.  Value is ", DoubleToStr(GlobalVariableGet("GV_Hello"), 2));

   return(0);
}
Keith
368
Keith  
RaptorUK:

It's all spelled out in the documentation . . .

GlobalVariableSet() to write the value to the GV, GlobalVariableGet() to read it . . . if your code hasn't done the Set there will be no GV to read from.

What is the point of the GetLastError() ? you are doing nothing with it . . . read this: What are Function return values ? How do I use them ?

Thanks for the quick reply and allow me to add my congratualtions for hitting 10,000 plus..quite an achievement.

ok been through all that documtnation and forum stuff a hundred times, still don't get it.

I thougt that by declaring a global variable it would automatically have the local variable value at all times.

So I need to use GlobalVariableSet(), what would the second parameter be? I need to get the value of haLow, I have already delared that

as a GV, I tried to use it like this: GlobalVariableSet(halow,halow). Not surprisingly that doesn't work.

Clearly I am missing something.

Simon Gniadkowski
Moderator
18035
Simon Gniadkowski  
kminler:

Thanks for the quick reply and allow me to add my congratualtions for hitting 10,000 plus..quite an achievement.

ok been through all that documtnation and forum stuff a hundred times, still don't get it.

I thougt that by declaring a global variable it would automatically have the local variable value at all times.

So I need to use GlobalVariableSet(), what would the second parameter be? I need to get the value of haLow, I have already delared that

as a GV, I tried to use it like this: GlobalVariableSet(halow,halow). Not surprisingly that doesn't work.

Clearly I am missing something.

GlobalVariableSet() takes 2 parameters, name which is the name of the GV and value which is the value/variable you want to save to the GV. Once you have saved the value/variable it can be retrieved using GlbalVariableGet() . . .

double ValueToSave = 32.6;
double ValueToRetreive;

string GV_Name = "MyGV";

GlobalVariableSet(GV_Name, ValueToSave);

ValueToRetreive = GlobalVariableGet(GV_Name);
Keith
368
Keith  
RaptorUK:

GlobalVariableSet() takes 2 parameters, name which is the name of the GV and value which is the value/variable you want to save to the GV. Once you have saved the value/variable it can be retrieved using GlbalVariableGet() . . .

Great that is exactly what I needed, many thanks and thanks to Thirteen for the sample code.
Keith
368
Keith  
kminler:
Great that is exactly what I needed, many thanks and thanks to Thirteen for the sample code.

Well I spoke way too soon, still cannot get it to work with variables.

When I use this code

GlobalVariableSet("TGV_gethaLow", haLow);
  double currenthaLow = GlobalVariableGet("TGV_gethaLow"); 
  Print ("test of gvtester haLow = ",currenthaLow);

The print out is 0. (Tested to determine that haLow does have a real value)

The Global Variuable "TGV_gethaLow", is created and if I set it to a numerical value say 16, it is assigned to the Global VAriable.

I have not been able to get the value of haLow from the Heiken Ashi Smoothed indicator.

I have tried declarng the variable different ways in both the test program and the target program nothing works.

I suspect I will feel really dumb when this is sorted out but please lay it on me I'd really like to solve this.

thanks

Keith

Simon Gniadkowski
Moderator
18035
Simon Gniadkowski  
kminler:

Well I spoke way too soon, still cannot get it to work with variables.

When I use this code

The print out is 0. (Tested to determine that haLow does have a real value)

The Global Variuable "TGV_gethaLow", is created and if I set it to a numerical value say 16, it is assigned to the Global VAriable.

I have not been able to get the value of haLow from the Heiken Ashi Smoothed indicator.

I have tried declarng the variable different ways in both the test program and the target program nothing works.

I suspect I will feel really dumb when this is sorted out but please lay it on me I'd really like to solve this.

Perhaps you are checking the GV before the Indicator has written to it, have you checked the GV using F3 to confirm it has been written to ? perhaps the Indicator is writing to it and them immediately setting it back to 0 . . . why not show your code, a snippet here and a snippet there makes it hard to help, what you have posted above looks OK.
Keith
368
Keith  
RaptorUK:
Perhaps you are checking the GV before the Indicator has written to it, have you checked the GV using F3 to confirm it has been written to ? perhaps the Indicator is writing to it and them immediately setting it back to 0 . . . why not show your code, a snippet here and a snippet there makes it hard to help, what you have posted above looks OK.

I have checked using F3, and the value is always 0, I have a print statement in Hiekan Ashi which shows the value of haLow updatring with each incoming tick.

The interesting thing is that if insert these three lines of code directly into the HA indicator the GV does work, i can watch it update in the experts log,

when i check F3 it is updating, unfortunately it is just acting as a local variable.

I have attached the HA indicator with these lines of code commented out in the start segment.

I will also insert GVtester which is what I wrote to eliminate all other possible sources of error.

In the back of my mind I have the niggling notion that it is necessary to somehow point the Mt4 operating system to the location of haLow.

I have also tried to get other variables form other indicatros in case this is a uniq

/+------------------------------------------------------------------+
//|                                                     GVTESTER.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
double haLow; 
int init()
  {
//---- indicators
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();  
   
  
   
   GlobalVariableSet("TGV_gethaLow", haLow);
  double currenthaLow = GlobalVariableGet("TGV_gethaLow"); 
  Print ("test of gvtester haLow = ",currenthaLow);
   

     // int  err = GetLastError();  
     //  Print("total","   ",GlobalVariablesTotal());
     // Print ("CHECK ",GlobalVariableCheck("GV_gethalow"));
    //  Print("Last error", err);
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

ue problem with HA.

Simon Gniadkowski
Moderator
18035
Simon Gniadkowski  
kminler:

I have checked using F3, and the value is always 0, I have a print statement in Hiekan Ashi which shows the value of haLow updatring with each incoming tick.

Not in the Indicator you attached there isn't .. . and it doesn't write to a Global Variable either . . .

Is your GVTESTER indicator running on a chart somewhere ? it will always set the Global Variable to 0 as that is the initial value of haLow and you never change it to anything else . . .
Keith
368
Keith  
RaptorUK:
Not in the Indicator you attached there isn't .. . and it doesn't write to a Global Variable either . . .

Is your GVTESTER indicator running on a chart somewhere ? it will always set the Global Variable to 0 as that is the initial value of haLow and you never change it to anything else . . .

I must have taken the print statement out when I cleaned up the Ha, I had a lot of other stuff in there I was testing.

I'm afraid that I don't understand "doesn't write to Global Variable either" I thought GlobalVariableGet saved the value into the GV and which would then be wrote to "currenthaLow"?

Not too sure that I follow your last line, I have the HA indicator runing on the Aussie 15 min, I attach GVTESTER to the AUDJPY, it is my expectation that

with each tick the value of haLow is recalculated by Ha and GVTESTER will get the new value.

This is indeed what happens when I run HA with the three lines inserted, however; for the life of me I cannot get the value to GVTESTER on the AUDJPY chart.

12
To add comments, please log in or register