Can we modify extern variables during execution ? - page 2

 
Arkenis:


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 ?

There are functions to work with GlobalVariables <--- click
 

I'm not so sure what you trying to achieve. As far as I know we can not change the value of extern variable. However with a little trick, we actually can by assign them to global variable. Global variable does not lose it's value when changing TF, Symbol or open up property window.

Please study this EA, and attach to a chart and open its property (press F7) or change TF and Symbol over and over, but don't change EA extern variable.

#include <WinUser32.mqh>

extern int extern_input=1;
//--- global variable
int previous_extern_input, input_calculated;
int Again;
string text;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
  Again = IDOK;
  if (previous_extern_input != extern_input) // --- new input
    {
    text = "New Input";
    previous_extern_input = extern_input;
    input_calculated = extern_input;
    }
    else // no new input 
    {
    text = "No new Input";
    }
    
    MessageBox (""+text+"\nExtern Input = "+extern_input+"\nPrevious extern input = "+previous_extern_input+
                "\nInput calculated = "+input_calculated,text, MB_OK|MB_ICONEXCLAMATION);
                
   start();
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit(){return(0);}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  if (Again == IDNO) return(0);
  while (true)
     {
     input_calculated += 2;
     Again = MessageBox ("Value of input calculated plus 2 is "+input_calculated+
             "\nDo you want to add by 2 again ?\n\n"+input_calculated,"Added by Two", 
             MB_YESNO|MB_ICONEXCLAMATION);
     if (Again == IDNO) break;
     }

   return(0);
  }
//+------------------------------------------------------------------+

:D

 
Arkenis:

... (my english isn't very good) ...

My first language is English and I have trouble understanding the documentation. I think most of the documentation was translated from Russian. And the translation is not so good in places.
 
MisterDog:
My first language is English and I have trouble understanding the documentation. I think most of the documentation was translated from Russian. And the translation is not so good in places.

Hi MisterDog,

Here https://www.mql5.com/en/forum/139870

:D

 

This works perfectly.

extern int var = 1;

int init()
{
    if(var < 1) var = 1;    // dont allow var to be less than 1
}
 
onewithzachy:

I'm not so sure what you trying to achieve. As far as I know we can not change the value of extern variable. However with a little trick, we actually can by assign them to global variable. Global variable does not lose it's value when changing TF, Symbol or open up property window.

Please study this EA, and attach to a chart and open its property (press F7) or change TF and Symbol over and over, but don't change EA extern variable.

:D


Okay, I didn't thought about this solution, works well :) But when you told me to use GV, I thought about GV with the GUI, which keep its value even if you restart your EA or MT4, so I coded a new init function:

#define  NPARAMETRES 5

string liste_parametres[NPARAMETRES] = {"ordre_si_depassement_trendline", "rebond_trendline", "perteMax", "bollinger_close", "bollinger_depassement"};

int init()
  {
//----
   int i;   
   
   for(i = 0; i < NPARAMETRES; i++){
      if(!GlobalVariableCheck(Symbol() + "_" + liste_parametres[i])){
         if(liste_parametres[i] == "perteMax"){
            GlobalVariableSet(Symbol() + "_" + liste_parametres[i], 4.0);
         }
         else{
            GlobalVariableSet(Symbol() + "_" + liste_parametres[i], 0);
         }
      }
   }
            
   
//----
   return(0);
  }

Then I use GlobalVariableGet() and GlobalVariableSet() to work with these variables, it works well too ;)

flaab:

This works perfectly.


I'm not sure about what you're meaning.. if I change var value from the EA, var will keep its default value with the GUI, 1 here, while the EA will actually says that var has been changed, so it doesn't really work :/

 

Well, Like I've said, I'm not so sure what you've trying to achieve, However, GlobalVariable() and File keeps it's value when EA, MT4 or even Computer is switched off. Just don't forget to make the name of GlobalVariable() different for each EA, Symbol, TF, and Chart. So it won't be a mess.

:D

 
onewithzachy:

Well, Like I've said, I'm not so sure what you've trying to achieve, However, GlobalVariable() and File keeps it's value when EA, MT4 or even Computer is switched off. Just don't forget to make the name of GlobalVariable() different for each EA, Symbol, TF, and Chart. So it won't be a mess.

:D


Yep, I already make a different name for each Symbol, don't need other details for the moment :)

Thanks for help !

Reason: