Download MetaTrader 5

Setting multiple booleans from one function

To add comments, please log in or register
David
26
David  

I'm experimenting with using what I think of as 'triggers'. They are booleans that start at false and then are set to true when certain parameters happen (X line cross Y line etc).  I can then get the EA to place an order when a number of the booleans are all true. The advantage is that it allows for those 'checks' to happen in different orders - which reduces the amount of coding needed in checking whether they have all occured.

Other checks (such as 'order has been placed', or 'time X has passed') can then set one or more of the booleans back to false.

Given that some checks could trigger a change in more than one boolean (i.e. a particular signal might change a buy order boolean to true and a sell order boolean to false), I'm wanting to be able to adjust multiple booleans within a function.

I've searched around both on here and on other forums - but I've not been able to find a way to do this (and I've tried various things in the code).

Does anybody know a way to do this?

A sample of the code (which checks for the highe and close pice of candles 2 and 1 being either side of an upper bollinger band) is


void SellPullBand1Trigger()

{

double UpperSD2OneMin = iBands(Symbol(),0,20,2,0,0,1,1);
double UpperSD2TwoMin = iBands(Symbol(),0,20,2,0,0,1,2);

double CloseOne = iClose(NULL,0,1);
double HighTwo = iHigh(NULL,0,2);

if(HighTwo>UpperSD2TwoMin)
if(CloseOne<UpperSD2OneMin)
SellBand1Trigger = true;

}


And an example of what I want to do as well might be to set 'buyband3trigger = false' at the same time.

Any help much appreciated

Keith Watford
Moderator
10561
Keith Watford  

You could either declare your variables globally so that they can be modified in a function or

pass the vaiables to the function by reference.

Osama Shaban
1522
Osama Shaban  
Keith Watford:

You could either declare your variables globally so that they can be modified in a function or

pass the vaiables to the function by reference.

Exactly. This is what I'm doing in my codes.

As Watford said. Just declare the Boolian variables as global then at the first lines of your start function assign them to false, then let your void type functions

work as switches of false and true according to the logic you are implementing. 

David
26
David  

Hi - Keith, Osama - thanks for the replies

The booleans are declared globaly (and set to false).

What I want to do is after the if statement change two booleans rather than one - i.e. I want to flick two switches with one void type function.

But I'm now wondering if I'm being dumb....

If I call a second boolena after the ; will that work - i.e. something like


if(HighTwo>UpperSD2TwoMin)
if(CloseOne<UpperSD2OneMin)
SellBand1Trigger = true;
SellBand2Trigger = true;
will that set both booleans to true?
honest_knave
Moderator
2332
honest_knave  
David:

If I call a second boolena after the ; will that work - i.e. something like 

Just use parentheses.

If you use global variables 

bool SellBand1Trigger=false;
bool BuyBand3Trigger =false;

void SellPullBand1Trigger()
  {
   double UpperSD2OneMin = iBands(Symbol(),0,20,2,0,0,1,1),
          UpperSD2TwoMin = iBands(Symbol(),0,20,2,0,0,1,2);
  
   double CloseOne = iClose(NULL,0,1),
          HighTwo  = iHigh(NULL,0,2);

   if(HighTwo>UpperSD2TwoMin && CloseOne<UpperSD2OneMin)
     {
      SellBand1Trigger = true;
      BuyBand3Trigger = false;
     }
  }

or if you pass by reference

void SellPullBand1Trigger(bool &Trigger1, bool &Trigger2)
  {
   double UpperSD2OneMin = iBands(Symbol(),0,20,2,0,0,1,1),
          UpperSD2TwoMin = iBands(Symbol(),0,20,2,0,0,1,2);
  
   double CloseOne = iClose(NULL,0,1),
          HighTwo  = iHigh(NULL,0,2);

   if(HighTwo>UpperSD2TwoMin && CloseOne<UpperSD2OneMin)
     {
      Trigger1 = true;
      Trigger2 = false;
     }
  }
whroeder1
15551
whroeder1  
honest_knave: Just use parentheses.
They are not parentheses. Choose your words better.
honest_knave
Moderator
2332
honest_knave  
whroeder1:
They are not parentheses. Choose your words better.

Quite right. Sometimes native speakers are the sloppiest when it comes to grammar.

David
26
David  
Well I'm not going to enter the naming wars... but that you honest knave - I though it might be easy, I just couldn't find it anywhere
To add comments, please log in or register