Download MetaTrader 5
To add comments, please log in or register
Register as a seller and start offering your signals!
raven.chrono
324
raven.chrono 2014.10.01 06:54 

Hi guys,


I would like to ask if i had a proper coding when dealing with GlobalVariables.

int OnInit()
{
   //check if GlobalVariable is present
   if(!GlobalVariableCheck("TradeAllowed"))
   {
      GlobalVariableSet("TradeAllowed",1);
   }
   else
   {
      GlobalVariableSet("TradeAllowed",1);
   }
}


I have this code so that the other EA's(slave) will not trade not until one EA(master) have made the first move(or the first trade) which has this code.

void CPanelDialog::OnClickBtn1(void)
{
   int type;
   if(rd1.State()==false && rd2.State()==false){}
   else
   {
      if(rd1.State()==true){ type=OP_BUY; }else if(rd2.State()==true){ type=OP_SELL; }
      if(OpenOrder(type,StringToDouble(edit2.Text())))
      {
        if(GlobalVariableCheck("TradeAllowed"))
        {
            GlobalVariableSet("TradeAllowed",0);
        }
      }
   }
}

 The problem is that, everytime i load the EA or start the EA, the GlobalVariable "TradeAllowed" always have a value of 0 instead of 1 which i declared above.


Can anyone help me with this problem.


Thanks.

SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.10.01 06:57  
Try move it to start .. not oninit ...
raven.chrono
324
raven.chrono 2014.10.01 07:07  
deysmacro:
Try move it to start .. not oninit ...
I put it on OnInit() is that i want to initialize the value to 1 which it will not allow the Slave EA to trade and after the Master EA begin trading, the  value will be changed to 0 to be translated to the Slave EA to start trading.
SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.10.01 07:15  
void CPanelDialog::OnClickBtn1(void)
{
   int type;
   if(rd1.State()==false && rd2.State()==false){}
   else
   {
      if(rd1.State()==true){ type=OP_BUY; }else if(rd2.State()==true){ type=OP_SELL; }
      if(OpenOrder(type,StringToDouble(edit2.Text())))
      {
        if(GlobalVariableGet("TradeAllowed")==1)
        {
            GlobalVariableSet("TradeAllowed",0);
        }
      }
   }
}


GlobalVariableCheck = check if TradeAllowed existed or not without checking its value.

GlobalVariableGet = check if the TradeAllowed value matched or not or whatever value we want is there.

raven.chrono
324
raven.chrono 2014.10.01 07:30  
deysmacro:


GlobalVariableCheck = check if TradeAllowed existed or not without checking its value.

GlobalVariableGet = check if the TradeAllowed value matched or not or whatever value we want is there.

Thanks i got that one but my main problem is that i want to initialize the value of "TradeAllowed" to 1 thats why i used this code.
int OnInit()
{
   //check if GlobalVariable is present
   if(!GlobalVariableCheck("TradeAllowed"))//check if the GlobalVariable "TradeAllowed" is present.
   {
      GlobalVariableSet("TradeAllowed",1);//if not present, create the GlobalVariable "TradeAllowed" with a value of 1.
   }
   else
   {
      GlobalVariableSet("TradeAllowed",1);//if present, set the GlobalVariable "TradeAllowed" value with 1.
   }
}
but the thing is, after the EA has been initialized, the value of the GlobalVariable "TradeAllowed" is always 0, sometimes it will have a value of 1 but after i put the Slave EA it will change its value to 0 to which the Slave EA has no code to change the value of the GlobalVariable "TradeAllowed".
SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.10.01 07:37  

Last try ...

int OnInit()
{
   //check if GlobalVariable is present
   if(GlobalVariableGet("TradeAllowed")!=1)GlobalVariableSet("TradeAllowed",1);
   GlobalVariablesFlush();
}


By the way, what you put on deinit() ?

raven.chrono
324
raven.chrono 2014.10.01 07:43  
deysmacro:

Last try ...

Thanks but it is still not working.
SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.10.01 07:53  
Use Print() after each function to see which one is the culprit.
raven.chrono
324
raven.chrono 2014.10.01 08:23  
deysmacro:
Use Print() after each function to see which one is the culprit.
I use that one for debugging but i dont see any errors. It always return the value 0.
SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.10.01 08:35  
      if(OpenOrder(type,StringToDouble(edit2.Text())))
      {
        if(GlobalVariableCheck("TradeAllowed"))
        {
            GlobalVariableSet("TradeAllowed",0);
        }
      }


That code snippet must be always true which leads to

GlobalVariableSet("TradeAllowed",0);
Keith Watford
Moderator
7950
Keith Watford 2014.10.01 08:38  
It may not be relevant in your case, but if you use strategy tester to test any EA that uses GV's, you MUST make sure that it doesn't change any GV's value that might be accessed by an EA on a live chart.
/
To add comments, please log in or register