GlobalVariableSet().....baffling!!!

 
int OnInit()
{
 
   Print(GlobalVariableCheck(price));
   if(!GlobalVariableCheck(price))GlobalVariableSet(price,0);
   Print(GlobalVariableCheck(price));
return(INIT_SUCCEEDED);
}

Print statement output.

Has anybody seen this before?

OK..please let me know what I am doing wrong?

thanks.

 
sd59:
price

You code is incomplete and won't compile. What is the "price"?

 
Stanislav Korotky #:

You code is incomplete and won't compile. What is the "price"?

string price;

int OnInit()
{
 
   Print(GlobalVariableCheck(price));
   if(!GlobalVariableCheck(price))GlobalVariableSet(price,0);
   Print(GlobalVariableCheck(price));
return(INIT_SUCCEEDED);
}

sorry missed out the string declaration

 
sd59 #GlobalVariableSet

You are not checking the return value for the function GlobalVariableSet to see if it succeeds or fails, nor are you checking the error status if it fails.

Return Value

If successful, the function returns the last modification time, otherwise 0. For more details about the error, call GetLastError()

Your sample code also does not assign any name to the "price" string variable, so it will always fail due it being an empty string.

string price = "variable name"; // Give the Global Terminal Variable a name
GlobalVariableSet - Global Variables of the Terminal - MQL4 Reference
GlobalVariableSet - Global Variables of the Terminal - MQL4 Reference
  • docs.mql4.com
GlobalVariableSet - Global Variables of the Terminal - MQL4 Reference
 
Fernando Carreiro #:

You are not checking the return value for the function GlobalVariableSet to see if it succeeds or fails, nor are you checking the error status if it fails.

Your sample code also does not assign any name to the "price" string variable, so it will always fail due it being an empty string.

it doesn't matter if you assign string price = 0; no difference.

i am checking return value - please see the 'false' print statements!

name = price (a string)

value = 0

 
sd59 #:

it doesn't matter if you assign string price = 0; no difference.

i am checking return value - please see the 'false' print statements!

name = price (a string)

value = 0


No, you are NOT doing any of the things I outlined in my post. Read it again carefully and correct your code accordingly.

After the changes, post your new code if it is still not working for you.

 

I have no idea what you are talking about - please stop wasting my time.

if you have a better solution ( for 4 lines of code) just say it stop trying to be clever!

 
sd59 #:

I have no idea what you are talking about - please stop wasting my time.

if you have a better solution ( for 4 lines of code) just say it stop trying to be clever!

See Checking the existence of a variable at:

Articles

MQL5 Programming Basics: Global Variables of the MetaTrader 5 Terminal

Dmitry Fedoseev, 2016.11.25 13:11

Global variables of the terminal provide an indispensable tool for developing sophisticated and reliable Expert Advisors. If you master the global variables, you will no more be able to imagine developing EAs on MQL5 without them.

Pay attention to MathMin().

 
sd59 #:

sorry missed out the string declaration

Your variable string price is empty, hence you got false results for every function call - nothing works without a name - assign it before the calls and everything will work.