Not sure why my code is breaking the back tester.

 

I am working on a new idea and for some reason I cant see why the back tester is just stopping. Its got to be something simple I'm missing. I have set up many print statements to find
out where the code is stopping and I have highlighted the code where I cant get it to move past. I run other bots I have made on the same tester with the same settings and they work fine but this one is just freezing up my tester. What am I missing? If the program continued I would expect the next print out to be "This is the else statement of the sell breakout function telling us its false." Highlighted
in blue but Its not getting that far.


//+------------------------------------------------------------------+
//|                                          Ride-The-fast-MA_EA.mq4 |
//|                              Copyright 2021, Erick Fenstermaker. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
//Note: A Buy is closed with a Sell at Bid, a Sell is closed with a Buy at Ask
#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 candleRange = 40;
extern int fastMaPeriod = 20;
extern int fastMaShift = 0;
extern int fastMaMethod = 0;
extern int fastMaAppliedTo = 0;

extern int slowMaPeriod = 200;
extern int slowMaShift = 0;
extern int slowMaMethod = 0;
extern int slowMaAppliedTo = 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");

         if(sellBreakout())
           {
            Print("We are past the sell breakout funtion now. this indicates a true sign");
           }
         else
           {
            Print("This is the else statement of the sell breakout function telling us its false.");
           }
         //sell();
         //if(buyBreakout())
        }
      else
        {
         Print("we must have an open order.");
        }       // buy();
     }
   else
     {
      Print("Not a new candle yet.");
     }
  }



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


//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)
     {

      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
      if(ticket>0)
        {
         if(OrderSelect(ticket,SELECT_BY_POS))
           {
            sl = Open[0];
            tp = OrderOpenPrice() + (sl*1.5);
            Print("We were able to place our buy order.");
           }
         else
           {
            if(!OrderModify(ticket,price,sl,tp,0,clrBlue))
              {
               Print("Could not modify order due to error: "+(string)err);
              }
           }
        }
  }


//places a short order
void sell()
  {
   int err = 0;
   double price = Bid, 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)
     {

      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
      if(ticket>0)
        {
         if(OrderSelect(ticket,SELECT_BY_POS))
           {
            sl = High[0];
            tp = Open[0]-(sl*1.5);
            Print("We were able to place our buy order.");
           }
         else
           {
            if(!OrderModify(ticket,price,sl,tp,0,clrBlue))
              {
               Print("Could not modify order due to error: "+(string)err);
              }
           }
        }
  }


//Function to look for a sell fractal settup
double sellFractal()
  {

   int candleNum =0;
   double lowLevel=0;
   bool movingAverageTrend = false;

   double candleSlow = iMA(NULL,0,slowMaPeriod,slowMaShift,slowMaMethod,slowMaAppliedTo,candleNum);
   double candleFast = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,candleNum);

//Loop to look at the moving averages and make sure they have not crossed in the resont history.
   while(candleSlow>candleFast&&candleNum<candleRange-1)
      candleNum = candleNum++;
   if(candleNum==candleRange-1)
      movingAverageTrend=true;

//Once we know the moving averages have not crossed in resont history we look for a fractal pattern
   if(movingAverageTrend)
     {
      Print("The Slow moving average is above the fast moving average so lets check our candles.");
      if(Low[1]>Low[2]&&Low[2]>Low[3]&&Low[3]>Low[4]&&Low[4]>Low[5]&&Low[5]<Low[6]&&Low[6]<Low[7]&&Low[7]<Low[8]&&Low[8]<Low[9])
        {
         lowLevel = Low[5];
         Print("There is a fractal sell pattern the new low is: "+(string)lowLevel);
         return lowLevel;
        }
      else
        {
         Print("There is no sell fractal pattern right now.");
         return 0;
        }
     }
   else
     {
      Print("The Slow moving average is not above the fast moving average so we do not take any sell orders.");
      return 0;
     }
   return 0;
  }

//Function to look for a buy fractal settup
double buyFractal()
  {

   int candleNum =0;
   double highLevel=0;
   bool movingAverageTrend = false;

   double candleSlow = iMA(NULL,0,slowMaPeriod,slowMaShift,slowMaMethod,slowMaAppliedTo,candleNum);
   double candleFast = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,candleNum);

//Loop to look at the moving averages and make sure they have not crossed in the resont history.
   while(candleSlow<candleFast&&candleNum<candleRange-1)
      candleNum = candleNum++;
   if(candleNum==candleRange-1)
      movingAverageTrend=true;

//Once we know the moving averages have not crossed in resont history we look for a fractal pattern
   if(movingAverageTrend)
     {
      Print("The Slow moving average is below the fast moving average so lets check our candles.");
      if(High[1]<High[2]&&High[2]<High[3]&&High[3]<High[4]&&High[4]<High[5]&&High[5]>High[6]&&High[6]>High[7]&&High[7]>High[8]&&High[8]>High[9])
        {
         highLevel = High[5];
         Print("There is a fractal sell pattern the new low is: "+(string)highLevel);
         return highLevel;
        }
      else
        {
         Print("There is no sell fractal pattern right now.");
         return 0;
        }
     }
   else
     {
      Print("The moving averages are not consistant enough to take a buy order at this time.");
      return 0;
     }
   return 0;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//Function to sell based on if we have a breakout under the low of the fractal candle
bool sellBreakout()
  {
   if(sellFractal()>Bid)
     {
      Print("The current price has just surpassed the low of the fractal candle we should sell.");
      return true;
     }

   else
     {
      Print("No sign of a downward breakout at this time. Not entering a sell order.");
      return false;
     }
  }

//Function to buy based on if we have a breakout over the high of the fractal candle
bool buyBreakout()
  {
   if(buyFractal()<Ask)
     {
      Print("The current price has just surpassed the high of the fractal candle we should buy.");
      return true;
     }

   else
     {
      Print("No sign of a upward breakout at this time. Not entering a buy order.");
      return false;
     }
  }

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

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

//+------------------------------------------------------------------+
 
  1.  if(OrderSelect(ticket,SELECT_BY_POS))
    A ticket number is not a position.
  2.    double candleSlow = iMA(NULL,0,slowMaPeriod,slowMaShift,slowMaMethod,slowMaAppliedTo,candleNum);
       double candleFast = iMA(NULL,0,fastMaPeriod,fastMaShift,fastMaMethod,fastMaAppliedTo,candleNum);
    
    //Loop to look at the moving averages and make sure they have not crossed in the resont history.
       while(candleSlow<candleFast&&candleNum<candleRange-1)
          candleNum = candleNum++;
    Your variables are constant in the loop.
 
SirFency: I am working on a new idea and for some reason I cant see why the back tester is just stopping. Its got to be something simple I'm missing. I have set up many print statements to find

out where the code is stopping and I have highlighted the code where I cant get it to move past. I run other bots I have made on the same tester with the same settings and they work fine but this one is just freezing up my tester. What am I missing? If the program continued I would expect the next print out to be "This is the else statement of the sell breakout function telling us its false." Highlighted
in blue but Its not getting that far.

The problem seems to be in "sellFractal()":

//Loop to look at the moving averages and make sure they have not crossed in the resont history.
   while(candleSlow>candleFast&&candleNum<candleRange-1)
      candleNum = candleNum++;

The values of "candleSlow" and "candleFast" will never change or update because they are only set once outside of loop and nothing in the loop cause them to update.

The loop in fact is just incrementing "candleNum", theoretically, because its not actually changing. You are post incrementing it but assigning the previous value, so it will just stay at 0 always.

Use one of the following but not both:

candleNum = candleNum + 1; // Either this ...
candleNum++;               // ... or this
 
The same problem also exists in "buyFractal()"!
 
Thanks guys!. I knew it was something simple I was overlooking.