Checking if a function is true

 

I am trying to check if I already have an order open and if I do, make sure that it is either a long or a short. It seems like instead of my "if" statement checking to see if the bool function is true or not the program is just running the function its supposed to be checking.


There are other issues like it not actually placing the orders properly and its not closing the orders properly but these I think I will eventually work out. I'm really stuck on why the functions are running instead of just being checked.


//+------------------------------------------------------------------+
//|                                          Ride-The-fast-MA_EA.mq4 |
//|                              Copyright 2021, Erick Fenstermaker. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, Erick Fenstermaker."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#include <stderror.mqh>
#include <stdlib.mqh>
//+------------------------------------------------------------------+
//|                          Variables                               |
//+------------------------------------------------------------------+
extern int fastMaPeriod = 10;
extern int fastMaShift = 0;
extern int fastMaMethod = 0;
extern int fastMaAppliedTo = 0;


extern double maToClose1 = 0;
extern double maToClose2 = 0;
extern double maToHigh3 = 0;
extern double maToHigh4 = 0;
extern double maToHigh5 = 0;
extern double maToLow3 = 0;
extern double maToLow4 = 0;
extern double maToLow5 = 0;


double pips = 0;
extern double lotSize = 0.01;
extern int slippage = 30;
extern int magicNumber = 1234;
extern double stopLoss = 0;
extern double takeProfit = 0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   pips = Point;
   if(Digits==3||Digits==5)
      pips*=10;


   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(isNewCandle()==true)
     {
      Print("New Candle");
      if(OrdersTotal()==0)
        {
         Print("No Open Orders");
         checkForBuySignal();
         checkForSellSignal();
        }
      else
         if(OrdersTotal()==1)
           {
            Print("We have an open order.");
            //Find out what type the order is dont just chack both long and short. its causing problems
            checkForBuyClose();
            checkForSellClose();
           }
     }

  }




//+------------------------------------------------------------------+
//|                           Functions                              |
//+------------------------------------------------------------------+
//Checks for a new candle
bool isNewCandle()
  {
   static datetime savedCandleTime;
   if(Time[0]==savedCandleTime)
      return false;
   else
      savedCandleTime=Time[0];
   return true;
  }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//Checks conditions to go long
bool checkForBuySignal()
  {
   bool retval = false;
   double fastMA1 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,1);
   double fastMA2 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,2);
   double fastMA3 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,3);
   double fastMA4 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,4);
   double fastMA5 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,5);

   if(Close[2] + maToClose2 < fastMA2 && Close[1] - maToClose1 > fastMA1)
     {
      if(High[3] + maToHigh3 < fastMA3 && High[4] + maToHigh4 < fastMA4 && High[5] + maToHigh5 < fastMA5)
        {
         retval = true;
         Print("We have a buy signal");
         buy();
         return retval;
        }
      else
        {
         Print("No buy signal at this time. The steepness of the MA was not correct.");
         retval = false;
         return retval;
        }

     }
   else
     {
      Print("No buy signal at this time. The candles have not crossed the MA properly.");
      retval = false;
      return retval;
     }

  }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//checks for conditions to go short
bool checkForSellSignal()
  {
   bool retval = false;
   double fastMA1 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,1);
   double fastMA2 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,2);
   double fastMA3 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,3);
   double fastMA4 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,4);
   double fastMA5 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,5);

   if(Close[2] - maToClose2 > fastMA2 && Close[1] + maToClose1 < fastMA1)
     {
      if(Low[3] - maToLow3 > fastMA3 && Low[4] - maToLow4 > fastMA4 && Low[5] - maToLow5 > fastMA5)
        {
         Print("We have a sell signal.");
         retval = true;
         return retval;
         sell();
        }
      else
        {
         Print("No sell signal at this time. The steepness of the MA was not correct.");
         retval = false;
         return retval;
        }
     }
   else
     {
      Print("No sell signal at this time. The candles have not crossed the MA properly.");
      retval = false;
      return retval;
     }

  }



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//places a long order
bool buy()
  {
   bool retval = false;
   int err=0;
   double price = Ask, sl=0, tp=0;

   int ticket = OrderSend(Symbol(),OP_BUY,lotSize,price,slippage,0,0,"Trade Placed By Ride The Fast MA EA. ",magicNumber,0,clrBlue);
   if(ticket <0)
     {
      err = GetLastError();
      Print("Could not place order due to Error: "+(string)err+" "+ErrorDescription(err));
      if(err==ERR_TRADE_NOT_ALLOWED)
         MessageBox("You can not place a trade because \"Allow Live Trade\" is not turned on.");
      return retval;
     }
   else
     {
      Print("We were able to place our buy order.");
      retval = true;
      return retval;
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//places a short order
bool sell()
  {
   bool retval = false;
   int err=0;
   double price = Bid, sl=0, tp=0;

   int ticket = OrderSend(Symbol(),OP_SELL,lotSize,price,slippage,0,0,"Trade Placed By Ride The Fast MA EA. ",magicNumber,0,clrBlue);
   if(ticket <0)
     {
      err = GetLastError();
      Print("Could not place order due to Error: "+(string)err+" "+ErrorDescription(err));
      if(err==ERR_TRADE_NOT_ALLOWED)
         MessageBox("You can not place a trade because \"Allow Live Trade\" is not turned on.");
      return retval;
     }
   else
     {
      Print("We were able to place our sell order.");
      retval = true;
      return retval;
     }
  }
//+------------------------------------------------------------------+
//Suposed to check if there is no short order open then check if there are conditions to close the Long order
bool checkForBuyClose()
  {
   bool retval = false;

   double fastMA1 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,1);
   double fastMA2 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,2);
   double fastMA3 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,3);
   double fastMA4 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,4);
   double fastMA5 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,5);

   if(!sell())
     {
      if(Close[1] + maToClose1 < fastMA1)
        {
         Print("Close value: "+(string)Close[1]+" MA Value: "+(string)fastMA1);
         Print("We need to exit this long trade.");
         retval = true;
         buyClose();
         return retval;
        }
      else
        {
         Print("We need to stay in this Long trade.");
         retval = false;
         return retval;
        }
     }
   else
     {
      Print("We are going short right now. We cannot close a buy order.");
      retval = false;
      return retval;
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

//Suposed to check if there is no Long order open then check if there are conditions to close the Short order
//then calls the Short Close function to actually close the Short Order
bool checkForSellClose()
  {
   bool retval = false;

   double fastMA1 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,1);
   double fastMA2 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,2);
   double fastMA3 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,3);
   double fastMA4 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,4);
   double fastMA5 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,5);

   if(!buy())
     {
      if(Close[1] - maToClose1  > fastMA1)
        {
         Print("We need to exit this short trade.");
         retval = true;
         sellClose();
         return retval;
        }
      else
        {
         Print("We need to stay in this Short trade.");
         retval = false;
         return retval;
        }
     }
   else
     {
      Print("We are currently going Long. We cannot close a Sell Order.");
      retval = false;
      return retval;
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//Closes the Long order
void  buyClose()
  {
   Print("Trying to close our Long trade.");
   int err = 0;
   if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
     {
      if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),30,Red))
        {
         Print("The MA Closed the Order. ");
        }
      else
        {
         err = GetLastError();
         Print("Could not close order due to Error: "+(string)err+" "+ErrorDescription(err));
        }
     }
   else
     {
      err = GetLastError();
      Print("Could not select order due to Error: "+(string)err+" "+ErrorDescription(err));
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//Closes the Short order
void  sellClose()
  {
   Print("trying to close our Short trade.");
   int err = 0;
   if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
     {
      if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),30,Red))
        {
         Print("The MA Closed the Order. ");
        }
      else
        {
         err = GetLastError();
         Print("Could not close order due to Error: "+(string)err+" "+ErrorDescription(err));
        }
     }
   else
     {
      err = GetLastError();
      Print("Could not select order due to Error: "+(string)err+" "+ErrorDescription(err));
     }
  }

 

I have completely changed the code. I think that it had to do with using bool. I am now checking it a different way. It seems to be working now.

I am concerned about why its telling me "Not all control paths return a value"  I highlighted where I think its complaining. I am returning a value. I clearly am missing something so for now I just removed the "Strict" property.

I also need to figure out why its entering trades that dont resemble my rules at all.


//+------------------------------------------------------------------+
//|                                          Ride-The-fast-MA_EA.mq4 |
//|                              Copyright 2021, Erick Fenstermaker. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, Erick Fenstermaker."
#property link      "https://www.mql5.com"
#property version   "1.00"
//#property strict
#include <stderror.mqh>
#include <stdlib.mqh>
//+------------------------------------------------------------------+
//|                          Variables                               |
//+------------------------------------------------------------------+
extern int fastMaPeriod = 10;
extern int fastMaShift = 0;
extern int fastMaMethod = 0;
extern int fastMaAppliedTo = 0;


extern double maToClose1 = 0;
extern double maToClose2 = 0;
extern double maToHigh3 = 0;
extern double maToHigh4 = 0;
extern double maToHigh5 = 0;
extern double maToLow3 = 0;
extern double maToLow4 = 0;
extern double maToLow5 = 0;


double pips = 0;
extern double lotSize = 0.01;
extern int slippage = 30;
extern int magicNumber = 1234;
extern double stopLoss = 0;
extern double takeProfit = 0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   pips = Point;
   if(Digits==3||Digits==5)
      pips*=10;


   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(isNewCandle()==true)
     {
      Print("New Candle");
      if(OrdersTotal()==0)
        {
         Print("No Open Orders");
         checkForBuySignal();
         checkForSellSignal();
        }
      if(OrdersTotal()==1)
        {
         Print("We have an open order.");
         if(findOrderType(4)==1)
           {
            Print("The Open order is a Long so we are checking for a Long Close Signal.");
            checkForBuyClose();
           }
         if(findOrderType(4)==0)
           {
            Print("The Open order is a Short so we are checking for a Short Close Signal.");
            checkForSellClose();
           }
         else
           {
            if(findOrderType(4)==2)
               findOrderType(4);
           }
        }
     }
  }





//+------------------------------------------------------------------+
//|                           Functions                              |
//+------------------------------------------------------------------+
//Checks for a new candle
bool isNewCandle()
  {
   static datetime savedCandleTime;
   if(Time[0]==savedCandleTime)
      return false;
   else
      savedCandleTime=Time[0];
   return true;
  }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//Checks conditions to go long
void checkForBuySignal()
  {
   double fastMA1 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,1);
   double fastMA2 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,2);
   double fastMA3 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,3);
   double fastMA4 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,4);
   double fastMA5 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,5);

   if(Close[2] + maToClose2 < fastMA2 && Close[1] - maToClose1 > fastMA1)
     {
      if(High[3] + maToHigh3 < fastMA3 && High[4] + maToHigh4 < fastMA4 && High[5] + maToHigh5 < fastMA5)
        {
         Print("We have a buy signal");
         buy();
        }
      else
        {
         Print("No buy signal at this time. The steepness of the MA was not correct.");
        }

     }
   else
     {
      Print("No buy signal at this time. The candles have not crossed the MA properly.");
     }

  }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//checks for conditions to go short
void checkForSellSignal()
  {
   double fastMA1 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,1);
   double fastMA2 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,2);
   double fastMA3 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,3);
   double fastMA4 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,4);
   double fastMA5 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,5);

   if(Close[2] - maToClose2 > fastMA2 && Close[1] + maToClose1 < fastMA1)
     {
      if(Low[3] - maToLow3 > fastMA3 && Low[4] - maToLow4 > fastMA4 && Low[5] - maToLow5 > fastMA5)
        {
         Print("We have a sell signal.");
         sell();
        }
      else
        {
         Print("No sell signal at this time. The steepness of the MA was not correct.");
        }
     }
   else
     {
      Print("No sell signal at this time. The candles have not crossed the MA properly.");
     }

  }



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//places a long order
void buy()
  {
   int err=0;
   double price = Ask, sl=0, tp=0;

   int ticket = OrderSend(Symbol(),OP_BUY,lotSize,price,slippage,0,0,"Trade Placed By Ride The Fast MA EA. ",magicNumber,0,clrBlue);
   if(ticket <0)
     {
      err = GetLastError();
      Print("Could not place order due to Error: "+(string)err+" "+ErrorDescription(err));
      if(err==ERR_TRADE_NOT_ALLOWED)
         MessageBox("You can not place a trade because \"Allow Live Trade\" is not turned on.");
     }
   else
     {
      Print("We were able to place our buy order.");
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//places a short order
void sell()
  {
   int err=0;
   double price = Bid, sl=0, tp=0;

   int ticket = OrderSend(Symbol(),OP_SELL,lotSize,price,slippage,0,0,"Trade Placed By Ride The Fast MA EA. ",magicNumber,0,clrBlue);
   if(ticket <0)
     {
      err = GetLastError();
      Print("Could not place order due to Error: "+(string)err+" "+ErrorDescription(err));
      if(err==ERR_TRADE_NOT_ALLOWED)
         MessageBox("You can not place a trade because \"Allow Live Trade\" is not turned on.");
     }
   else
     {
      Print("We were able to place our sell order.");
     }
  }
//+------------------------------------------------------------------+
//Suposed to check if there is no short order open then check if there are conditions to close the Long order
void checkForBuyClose()
  {

   double fastMA1 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,1);
   double fastMA2 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,2);
   double fastMA3 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,3);
   double fastMA4 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,4);
   double fastMA5 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,5);

   if(Close[1] + maToClose1 < fastMA1)
     {
      Print("Close value: "+(string)Close[1]+" MA Value: "+(string)fastMA1);
      Print("We need to exit this long trade.");
      buyClose();
     }
   else
     {
      Print("We need to stay in this Long trade.");
     }
  }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

//Suposed to check if there is no Long order open then check if there are conditions to close the Short order
//then calls the Short Close function to actually close the Short Order
void checkForSellClose()
  {

   double fastMA1 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,1);
   double fastMA2 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,2);
   double fastMA3 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,3);
   double fastMA4 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,4);
   double fastMA5 = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,5);

   if(Close[1] - maToClose1  > fastMA1)
     {
      Print("We need to exit this short trade.");
      sellClose();
     }
   else
     {
      Print("We need to stay in this Short trade.");
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//Closes the Long order
void  buyClose()
  {
   Print("Trying to close our Long trade.");
   int err = 0;
   if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
     {
      if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),30,Red))
        {
         Print("The MA Closed the Order. ");
        }
      else
        {
         err = GetLastError();
         Print("Could not close order due to Error: "+(string)err+" "+ErrorDescription(err));
        }
     }
   else
     {
      err = GetLastError();
      Print("Could not select order due to Error: "+(string)err+" "+ErrorDescription(err));
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//Closes the Short order
void  sellClose()
  {
   Print("trying to close our Short trade.");
   int err = 0;
   if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
     {
      if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),30,Red))
        {
         Print("The MA Closed the Order. ");
        }
      else
        {
         err = GetLastError();
         Print("Could not close order due to Error: "+(string)err+" "+ErrorDescription(err));
        }
     }
   else
     {
      err = GetLastError();
      Print("Could not select order due to Error: "+(string)err+" "+ErrorDescription(err));
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int findOrderType(int x)
  {
//Order type Sell = false, Order Type Buy = True
   if(OrderSelect(0,SELECT_BY_POS))
     {
      int type = OrderType();
      if(type == OP_BUY)
        {
         return (1);
        }
      if(type == OP_SELL)
         return (0);
     }
   else
     {
      int err = GetLastError();
      Print("Could not select order due to Error: "+(string)err+" "+ErrorDescription(err));
      return (2);
     }
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
 

Always use strict, don't disable it just to silence the compiler.

Think about your code and make sure that it will always return a value.

//+------------------------------------------------------------------+
int findOrderType(int x)
{
//Order type Sell = false, Order Type Buy = True
   if(OrderSelect(0,SELECT_BY_POS))
      {
      int type = OrderType();
      if(type == OP_BUY)
         {
         return (1);
         }
      if(type == OP_SELL)
         return (0);
      }
   else
      {
      int err = GetLastError();
      Print("Could not select order due to Error: "+(string)err+" "+ErrorDescription(err));
      return (2);
      }
   return(-1);
}
//+------------------------------------------------------------------+
 
Keith Watford:

Always use strict, don't disable it just to silence the compiler.

Think about your code and make sure that it will always return a value.

Awesome thank you Keith! I didnt even think about outside the if statements.