GlobalVariableSet not working

 

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.

 
Try move it to start .. not oninit ...
 
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.
 
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.

 
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".
 

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() ?

 
deysmacro:

Last try ...

Thanks but it is still not working.
 
Use Print() after each function to see which one is the culprit.
 
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.
 
      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);
 
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.
Reason: