IF Statements do not work with more than one expression

 

I have an issue with some IF statements which are shown in my code below (I have taken out a lot of code that does not relate to the topic). 

All code highlighted in purple works perfectly, both lastSignal & thisSignal strings are updated when required. My problem is after my // Set SAR swap.  Neither statements will execute but not because the statements are incorrect - they are. they do not execute, as I have found through trail and error, because the lastSignal & thisSignal have not been "hard coded". If I alter these strings to "BUY" or "SELL" then they work without issue. I have also tried nesting the statements but that did not work either.

Thanks, Steve


void OnTick()
  {
// MQL Rates
   MqlRates priceArray[];
   ArraySetAsSeries(priceArray, true);
   int data = CopyRates(cPair, _Period, 0, 3, priceArray);

// SAR data
   double mySARArray[];
   int SARDefinition = iSAR(_Symbol, _Period, 0.02, 0.2);
   ArraySetAsSeries(mySARArray, true);
   CopyBuffer(SARDefinition, 0, 0, 3, mySARArray);

// Set SAR structure
   double lastSARValue = NormalizeDouble(mySARArray[1], 5);
   double thisSARValue = NormalizeDouble(mySARArray[0], 5);

   string lastSignal = "";
   string thisSignal = "";

// Set previous signals
   if(lastSARValue < priceArray[1].high)
     {
      	// This works
	lastSignal = "BUY";
     }

   if(lastSARValue > priceArray[1].low)
     {
      	// This works
	lastSignal = "SELL";
     }

// Set current signals
   if(thisSARValue < priceArray[0].high)
     {
      	// This works
	thisSignal = "BUY";
     }

   if(thisSARValue > priceArray[0].low)
     {
      	// This works
	thisSignal = "SELL";
     }

// Set trade
   if(thisSignal == "BUY")
     {
      	// This works
	BuyTrade();
     }

   if(thisSignal == "SELL")
     {
      	// This works
	SellTrade();
     }

// Set SAR swap
   if(lastSignal == "SELL" && thisSignal == "BUY")
     {
	// This does not work
	Comment("Sell to buy");
     }

   if(lastSignal == "BUY" && thisSignal == "SELL")
     {
      	// This does not work
	Comment("Buy to sell");
     }

 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Steve Silcox:

I have an issue with some IF statements which are shown in my code below (I have taken out a lot of code that does not relate to the topic). 

All code highlighted in purple works perfectly, both lastSignal & thisSignal strings are updated when required. My problem is after my // Set SAR swap.  Neither statements will execute but not because the statements are incorrect - they are. they do not execute, as I have found through trail and error, because the lastSignal & thisSignal have not been "hard coded". If I alter these strings to "BUY" or "SELL" then they work without issue. I have also tried nesting the statements but that did not work either.

Thanks, Steve


You're setting your variables to " " on every tick.

Declare lastSignal and thisSignal up top on the global scope, set them to " " in OnInit(), and then update their values in OnTick() as and where needed.

 
Ryan L Johnson #:

You're setting your variables to " " on every tick.

Declare lastSignal and thisSignal up top on the global scope, set them to " " in OnInit(), and then update their values in OnTick() as and where needed.

Thanks for the input Ryan, but I want the variables to change on every tick.
 
Steve Silcox #:
Thanks for the input Ryan, but I want the variables to change on every tick.

You're welcome. As a best practice, I put as many variables as possible on the global scope. I don't go local unless I have a specific reason for doing so. You can still declare your variables up top, and then continue setting them to " " locally. This might give all of your conditions access to them.