dual moving average issue

 

Hi, when im using the code below to return the Print, it doesnt work. when i change the code from "<" to ">" it seems to work however. Not sure why "<" doesnt work though.

Any advice would be appreciated.


#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict



      extern int ma1 = 100;
      extern int ma2 = 200;
      
void OnTick()
  {

   
   double movingAverage1 = iMA(Symbol(),5,ma1,0,0,0,0);
   double movingAverage2 = iMA(Symbol(),5,ma1,0,0,0,0);
   
   
   double candleOneOpen = iOpen(Symbol(), PERIOD_M5,1);
   double candleOneClose = iClose(Symbol(),PERIOD_M5,1);
   

   
   if (candleOneOpen && candleOneClose < (movingAverage1 && movingAverage2))
   
      Print("i would be selling");
      
   
}

Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
if (candleOneOpen && candleOneClose < (movingAverage1 && movingAverage2))

The logic here is wrong.

candleOneOpen is not a boolean type and it likely doesn't contain a boolean value (0 or 1). The same is true for movingAverage1 and movingAverage2.

Try writing out the conditional statements into bool variables first, then decide the conditions to check in an if-statement. Not only is it easier to maintain multiple conditions but, when you look back at your code, it'll help you pick up where you left off.

Example:

bool isLowerOpen1  = candleOneOpen < movingAverage1;
bool isLowerOpen2  = candleOneOpen < movingAverage2;
bool isLowerClose1 = candleOneClose < movingAverage1;
bool isLowerClose2 = candleOneClose < movingAverage2;

bool isValidSellSignal = isLowerOpen1 && isLowerOpen2 && isLowerClose1 && isLowerClose2;

if (isValidSellSignal) {
  // Print
}
 
Alexander Martinez #:

The logic here is wrong.

candleOneOpen is not a boolean type and it likely doesn't contain a boolean value (0 or 1). The same is true for movingAverage1 and movingAverage2.

Try writing out the conditional statements into bool variables first, then decide the conditions to check in an if-statement. Not only is it easier to maintain multiple conditions but, when you look back at your code, it'll help you pick up where you left off.

Example:

much appreciated man!


been stuck on this for a few hours now. really appreciate the help.

 
bool isLowerOpen1  = candleOneOpen < movingAverage1;
bool isLowerOpen2  = candleOneOpen < movingAverage2;
bool isLowerClose1 = candleOneClose < movingAverage1;
bool isLowerClose2 = candleOneClose < movingAverage2;

bool isValidSellSignal = isLowerOpen1 && isLowerOpen2 && isLowerClose1 && isLowerClose2;
or
bool isValidSellSignal = MathMax(candleOneOpen, CandleOneClose) < MathMin(movingAverage1, movingAverage2);
 
algotrading01 #:

much appreciated man!


been stuck on this for a few hours now. really appreciate the help.

Glad it helped, take care. :)