Global Variables of the Terminal --- MQL4

 

I haven't posted in the forum for several years so I hoping you also take posts related to MQL4

I'm having several issues with Global Variables of the Terminal, but I will confine this post to just one of the issues.

The issue:  For some reason, when I query the value of the Global Variable entitled "GV_QP1", it returns the correct value, but when I query the value one minute later, the GlobalVariableGet() function returns an error (can't find the variable), then one minute later it can find the variable and return the correct value.  This alternating, correct value, can't find variable, goes on continuously for fifteen minutes until my minute counter variable stops the script.

Anybody know what's going on?

Here's the script and the resulting printout.



iCurrentMinute = Minute();

if( iCurrentMinute != iPreviousMinute  &&  iMinuteCount <15 )

{//---BEGIN:  Update minute counter

iPreviousMinute = iCurrentMinute;

iMinuteCount++;

GV_QP1=GlobalVariableGet(GV_QP1);

Print("Minute by minute update of GV_QP1 value --- It's currently: ",GV_QP1);

iError=GetLastError();

if( iError > 0 )  Print("GlobalVariable Error, #",iError);

}//---END:  Update minute counter

Log Printout


0 08:35:21.764 21_Algo13019 USDCAD,M15:  Global Variables have been reset; Waiting for Startup Delay Period to Expire

0 08:35:21.764 21_Algo13019 USDCAD,M15: Minute by minute update of GV_QP1 value --- It's currently: 100

0 08:35:59.187 21_Algo13019 USDCAD,M15: Minute by minute update of GV_QP1 value --- It's currently: 0

0 08:35:59.187 21_Algo13019 USDCAD,M15: GlobalVariable Error, #4058

0 08:37:04.579 21_Algo13019 USDCAD,M15: Minute by minute update of GV_QP1 value --- It's currently: 100

0 08:37:58.838 21_Algo13019 USDCAD,M15: Minute by minute update of GV_QP1 value --- It's currently: 0

0 08:37:58.838 21_Algo13019 USDCAD,M15: GlobalVariable Error, #4058

0 08:38:58.836 21_Algo13019 USDCAD,M15: Minute by minute update of GV_QP1 value --- It's currently: 100

0 08:39:59.098 21_Algo13019 USDCAD,M15: Minute by minute update of GV_QP1 value --- It's currently: 0

0 08:39:59.098 21_Algo13019 USDCAD,M15: GlobalVariable Error, #4058

0 08:40:59.126 21_Algo13019 USDCAD,M15: Minute by minute update of GV_QP1 value --- It's currently: 100

0 08:41:59.825 21_Algo13019 USDCAD,M15: Minute by minute update of GV_QP1 value --- It's currently: 0

0 08:41:59.825 21_Algo13019 USDCAD,M15: GlobalVariable Error, #4058

0 08:42:59.123 21_Algo13019 USDCAD,M15: Minute by minute update of GV_QP1 value --- It's currently: 100

0 08:43:59.887 21_Algo13019 USDCAD,M15: Minute by minute update of GV_QP1 value --- It's currently: 0

0 08:43:59.887 21_Algo13019 USDCAD,M15: GlobalVariable Error, #4058

0 08:44:58.930 21_Algo13019 USDCAD,M15: Minute by minute update of GV_QP1 value --- It's currently: 100

0 08:45:59.578 21_Algo13019 USDCAD,M15: Minute by minute update of GV_QP1 value --- It's currently: 0

0 08:45:59.578 21_Algo13019 USDCAD,M15: GlobalVariable Error, #4058

0 08:47:04.099 21_Algo13019 USDCAD,M15: Minute by minute update of GV_QP1 value --- It's currently: 100

0 08:48:00.111 21_Algo13019 USDCAD,M15: Minute by minute update of GV_QP1 value --- It's currently: 0

0 08:48:00.111 21_Algo13019 USDCAD,M15: GlobalVariable Error, #4058

0 08:49:02.093 21_Algo13019 USDCAD,M15: Minute by minute update of GV_QP1 value --- It's currently: 100

Documentation on MQL5: Global Variables of the Terminal / GlobalVariableGet
Documentation on MQL5: Global Variables of the Terminal / GlobalVariableGet
  • www.mql5.com
GlobalVariableGet - Global Variables of the Terminal - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

Please edit your post and use the code button (Alt+S) when pasting code.

EDIT your original post, please do not just post the code correctly in a new post.

GV_QP1=GlobalVariableGet(GV_QP1);

This doesn't make sense.

What is the variable GV_QPI ?

Is it a string or a double?

You are checking the GV with the name  GV_QPI as a string and then assigning the value of the GV to the same variable as a double?

So you are alternating looking at a GV named 0 and 100.

Always use

#property strict
 
Keith Watford:
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

Please edit your post and use the code button (Alt+S) when pasting code.

EDIT your original post, please do not just post the code correctly in a new post.

This doesn't make sense.

What is the variable GV_QPI ?

Is it a string or a double?

You are checking the GV with the name  GV_QPI as a string and then assigning the value of the GV to the same variable as a double?

So you are alternating looking at a GV named 0 and 100.

Always use

Keith, thank you for responding to my post.  I appreciate your input and have given some effort to comply with what you have stated:

1) I have edited my post to comply with your instruction regarding the methodology for posting code

2) With regard to "#property strict" I have implemented that in my EA and compiled it, and as a result, I think I understand your questions regarding my variable name with respect to 'string' versus 'number' because the compiler is now saying something to the same effect.  I'm still working through the compiler feedback, but wanted to update my post to let you know I am not ignoring you.

3)  Update to comment 2)  ---- A few weeks ago when I initially created the GV_QP1 variable name in the terminal, I was unable to compile my EA without also declaring GV_QP1 as a variable in the EA itself so when I declared it, I declared it as a double type variable --- that was a mistake.  I now realize the EA must recognize the name of Global Variable of the Terminal as a string, so when you call the Global Variable value via the special functions for communicating with the terminal, I must place the string value (ie the variable name) in the GlobalVariableGet() function, and it will return a value of the double type.  That was a minor nuance I missed when studying the MQL4 documentation.  I have managed to compile the EA with no warnings, so as soon as the market opens next week I will give the revised code whirl as see what happens.

Thank you for your assistance.

 
raft1:

Keith, thank you for responding to my post.  I appreciate your input and have given some effort to comply with what you have stated:

1)  Post in MQL4 forum:  For whatever reason I am unable to find the MQL4 forum.  My current username apparently is for MQL5 only.  I tried to create a new account for the MQL4 forum but the MetaQuotes webpage for registering a new account implies a new registration is good for both forums yet my current registration doesn't seem to allow me access the MQL4 forum.  I abandoned the new registration effort and would have been counter-productive anyway because I wouldn't have been able to edit my post with a new registration name.   If you could point me to the MQL4 forum I'll glad finish my forum interaction at that location.

2) I have edited my post to comply with your instruction regarding the methodology for posting code

3) With regard to "#property strict" I have implemented that in my EA and compiled it, and as a result, I kind of understand your comment regarding your questions regarding my variable name with respect to 'string' versus 'number' because the compiler is now saying something to the same effect.  I'm still working through the compiler feedback, but wanted to update my post to let you know I am not ignoring you.

I have not posted in any MetaQuotes forum in nearly 10 years, but this Global Variables of the Terminal issue has really been giving me a difficult time and I decided to place a post and seek some feedback.  Obviously, the post had its own issues and is masking the effort to resolve the Global Variable issue.

4)  A few weeks ago when I initially created the GV_QP1 variable name in the terminal, I was unable to compile my EA without also declaring GV_QP1 as a variable in the EA itself so when I declared it, I declared it as a double variable, because Global Variables of the Terminal are variables of the double type.  You obviously seen code line GV_QP1=GlobalVariableGet(GV_QP1) as violation of converting a double variable to a string variable.  I did not think I had made that kind of blunder, but now that I have implement the #property strict requirement, the compiler is giving me a warning that says "implicit conversion from 'number' to 'string' "  Thus, I am clearly violating some basic rule that I am unaware of.

Global variables exist in the client terminal during 4 weeks since their last use, then they are automatically deleted. 

To do this, you need to add variables to an index every 15 days, delete global variables, and set them again.

 
Mehmet Bastem:

Global variables exist in the client terminal during 4 weeks since their last use, then they are automatically deleted. 

To do this, you need to add variables to an index every 15 days, delete global variables, and set them again.

Mehmet, thanks for the reply, but I'm pretty sure the problem with alternating values every minute is not caused by the terminal losing the variable value after 4 weeks.  It appears the problem is caused by me mistakenly using a variable that I declared as a double type when I should have declared it as a string type.

The EA must use a function to communicate with the terminal, and the name of the Global Variable of the Terminal which is originally created in the terminal via the Tools > Global Variable menu selections, then must be declared in the EA to get the EA to compile.  As us you can see in the code I posted, I'm using the GlobalVariableGet() function to call the value.  Inside the parentheses the name of the variable is GV_QP1.  I should have declared that variable name as a string, and I mistakenly declared it as a double because the value the terminal saves is a double type and that is what threw me off course.  Thus, if the function GlobalVariableGet(GV_QP1) is properly formatted, it retrieves a variable of the double type but the variable name is a string when its inserted into the function.

Thanks for responding.

 
Keith Watford:
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

Please edit your post and use the code button (Alt+S) when pasting code.

EDIT your original post, please do not just post the code correctly in a new post.

This doesn't make sense.

What is the variable GV_QPI ?

Is it a string or a double?

You are checking the GV with the name  GV_QPI as a string and then assigning the value of the GV to the same variable as a double?

So you are alternating looking at a GV named 0 and 100.

Always use

Thank you for responding to my post seeking assistance.

Your question regarding whether my variable entitled GV_QP1 was a string or a double was key to me discovering what was causing the alternating variable value every minute.

I now have a working script and thought I would post it to provide clarity on this issue were confusion formerly prevailed.

//--- Declare Global Variables of EA

int iCurrentMinute;
int iPreviousMinute;
int iMinuteCount;
int iError;

string QP1="GV_QP1";  // Value of this string variable must match name of a Global Variable of the Terminal
double dTempValue;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
GlobalVariableSet(QP1,100.0);
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----


//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{

iCurrentMinute = Minute();
if( iCurrentMinute != iPreviousMinute  &&  iMinuteCount <15 )
{//---BEGIN:  Update minute counter
iPreviousMinute = iCurrentMinute;
iMinuteCount++;
dTempValue=GlobalVariableGet(QP1);
Print("Minute by minute update of GV_QP1 value:   Min ",iMinuteCount,"  GV_QP1=",dTempValue);
iError=GetLastError();
if( iError > 0 )  Print("GlobalVariable Error, #",iError);
}//---END:  Update minute counter

return(0);
}

The MQL4 documentation implies it is a matter of simply inserting the "name" of a string variable into the GlobalVariableGet() function that matches the name of a Global Variable in the Terminal, however, that is insufficient and returns Error #4051 (Invalid function parameter value).  It's not enough to have a string variable with a name that matches the name of a Global Variable in the Terminal, you must declare a value for that string variable when the variable is first declared, and the value of that string variable must match the name of the Global Variable in the Terminal.

Stated differently, The “name” of the string variable inserted in the GlobalVariableGet() function can be named any name you choose, however, the “value” assigned to that string variable name must be the name of the Global Variable in the Terminal you are calling.  

Thanks again for your assistance.

Reason: