Can we modify extern variables during execution ?

 

Hi,

I'm trying to modify extern variables in the init function but it doesn't seem to be working.. The EA says that variables are modified, but when I check in MT4, extern variables still have its default values :/

Here is my code:

double parametres[NPARAMETRES];
int handle;

int init()
  {
//----
   int i;
   handle = FileOpen(Symbol() + ".dat",FILE_BIN|FILE_READ);
   if(handle != -1){ // Si il y a des données à récupérer sur les parametres de l'EA
      if(FileReadArray(handle, parametres, 0, NPARAMETRES) != NPARAMETRES){
         Print(Symbol() + "Erreur: Parametres manquants");
      }
      
      FileClose(handle);
      
      for(i = 0; i < NPARAMETRES; i++){
         Print(Symbol() + ": parametres[" + i + "] = " + parametres[i]);
      }
      
      if(parametres[0] == 1){
         ordre_si_depassement_trendline = true;
         Print(Symbol() + ": ordre_si_depassement_trendline set to true ? " + ordre_si_depassement_trendline);
      }
      
      if(parametres[1] != perteMax) perteMax = parametres[1];
      if(parametres[2] == 1)  bollinger_close = true;
      if(parametres[3] == 1)  bollinger_depassement = true;
   }
   
//----
   return(0);
  }

int deinit()
  {
//----
   handle = FileOpen(Symbol() + ".dat",FILE_BIN|FILE_WRITE);
   if(handle != -1){
      if(ordre_si_depassement_trendline)  parametres[0] = 1;
      else                                parametres[0] = 0;
      
      parametres[1] = perteMax;
      
      if(bollinger_close)  parametres[2] = 1;
      else                 parametres[2] = 0;
      
      if(bollinger_depassement)  parametres[3] = 1;
      else                       parametres[3] = 0;
   
      if(FileWriteArray(handle, parametres, 0, NPARAMETRES) != NPARAMETRES){
         Print(Symbol() + "Erreur durant l\'ecriture des parametres");
      }
      
      FileClose(handle);
   }
   
//----
   return(0);
  }
 
Arkenis:

Hi,

I'm trying to modify extern variables in the init function but it doesn't seem to be working.. The EA says that variables are modified, but when I check in MT4, extern variables still have its default values :/

Here is my code:

which variables are you trying modify? The init() function only runs once when you first load the EA or change timeframe.
 
sd59:
which variables are you trying modify? The init() function only runs once when you first load the EA or change timeframe.

Yes I know that, but sometimes I modify extern variables manually, and if I close MT4 or compile the EA, extern variables will be reseted to its default values. So I want the EA to get the parameters used before closing MT4 (in the init() function)
 

You could store your changed externs in Global variables . . it's nasty but I've done it before.

 
Arkenis:
I'm trying to modify extern variables in the init function but it doesn't seem to be working.. The EA says that variables are modified, but when I check in MT4, extern variables still have its default values :/
Are you looking at them with the GUI? MT4 has no idea that you changed them and shows the values set on load. Print them out at the end of init to verify.
 
The short answer is "yes, you can". But I don't see any extern variable in your code.
 
RaptorUK:

You could store your changed externs in Global variables . . it's nasty but I've done it before.


Yes, it could be a solution, but if i use it for several charts, I will have to create variables for each chart :/

WHRoeder:
Are you looking at them with the GUI? MT4 has no idea that you changed them and shows the values set on load. Print them out at the end of init to verify.

I've already checked it, the EA correctly get my old parameters for the bool variables (it doesn't work with perteMax which is a double, it always keep its default value, i don't know why) but it doesn't modify extern variables in the GUI, so my question is : is it possible to change them from the EA ?

What I don't understand is that I correctly write parameters value in deinit() function, so I can read it from EA, but I can't rewrite it..

flaab:
The short answer is "yes, you can". But I don't see any extern variable in your code.

You're right, I missed it:

extern bool   ordre_si_depassement_trendline = false;
extern double perteMax = 4;
extern bool   bollinger_close = false;
extern bool   bollinger_depassement = false;
 

You can use WindowHandle() as part of Global Variable name, but GV don't accept string as value.

:D

 
Arkenis:


Yes, it could be a solution, but if i use it for several charts, I will have to create variables for each chart :/

Yes, just prepend the symbol to the name of the Global Variables then you have unique versions for each chart . . add the period too if you need to.

I don't really understand the point of what you are trying to do so I can't suggest any better solutions . . if you explained why you are trying to do what you are trying to do there might be better solutions.

For example, I used the Global Variable technique to allow a few acquaintances of mine to use one of my "Indicators" and to be able to change the defaults without me giving them the source code . . .

 
RaptorUK:

Yes, just prepend the symbol to the name of the Global Variables then you have unique versions for each chart . . add the period too if you need to.

I don't really understand the point of what you are trying to do so I can't suggest any better solutions . . if you explained why you are trying to do what you are trying to do there might be better solutions.

For example, I used the Global Variable technique to allow a few acquaintances of mine to use one of my "Indicators" and to be able to change the defaults without me giving them the source code . . .


Well, my code must be complicated to understand since I use french names for my variables :/ So what I'm trying to do is to draw manually a resistance and/or a support, then if the price goes over one of it, over the resistance for example, it will close all my sell positions for the chart, and if the variable 'ordre_si_depassement' is set to true, it will also send a buy order. perteMax is used to determine my maximal lose for one trade, and bollinger_close and bollinger_depassement tell if the EA must use bollinger bands to work or not.

I admit that I don't know how to access to Global variables from an EA, can I access to it like if I declared it in the EA or do I have to use some function ?

onewithzachy:

You can use WindowHandle() as part of Global Variable name, but GV don't accept string as value.

:D


I just read its documentation, and I didn't understand what is was for (my english isn't very good) .. If you can explain me its utility, it would be great :)

 

Use Google Translate..

Here's GlobalVariable().

For the sake of simple explanation, WindowsHandle() basically gives unique identification number to a chart. For example if we open 2 or more charts, all EURUSD M1 chart, we can programmaticaly tell which one is the one - just like magic number in EA or serial number in paper money.

:D

Reason: