Program stops the tester

 

I'm not understanding why the tester is stopping. It should go back to the beginning and start the checks again when all my statements end up being false right? I have done my best to comment everything and print statements to easily follow the path as its running. I have highlighted where it stops. I'm assuming its something in the on tick area but I'm not sure what I'm doing wrong. I am teaching myself to program. I'm not a very good teacher as I dont know how to program lol.



//+------------------------------------------------------------------+
//|                                          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 fastMovingAverage;
double lastFastMovingAverage;
double slowMovingAverage;
double lastSlowMovingAverage;


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()
  {
//Looking for a new candle  
   if(isNewCandle())
     {
      Print("We have a new Candle");
//Looking for any open orders
      if(OrdersTotal()==0)
         Print("No open orders right now. Lets look for an oportunity.");
//looking for an upward trend
      if(maTrendUp())
        {
//looking for a fractal pattern
         if(buyFractal()>0)
           {
//looking for a breakout over the fractal pattern high
            if(buyBreakout())
              {
//entering a long position
               buy();
              }
            else
              {
               Print("the high has not surpassed the fractal signal yet. Its not time to buy.");
              }
           }
         else
           {
            Print("On tick confirmation of no long breakout.");
           }
        }
      else
        {
         Print("On tick confirmation of no trend up.");
        }
//looking for a downward trend
      if(maTrendDown())
        {
//looking for a fractal pattern
         if(sellFractal()>0)
           {
//looking for a breakout below the fractal pattern
            if(sellBreakout())
              {
//entering a short
               sell();
              }
            else
              {
               Print("the low has not surpassed the fractal signal yet. Its not time to sell.");
              }
           }
         else
           {
            Print("On tick confirmation of no short breakout.");
           }
        }
      else
        {
         Print("On tick confirmation of no downtrend.");
        }
     }
  }
//Not sure why it wont go back to the beginning and start again???



//+------------------------------------------------------------------+
//|                           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;

   Print("The Slow moving average has been above the fast moving average for a while 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;
     }
  }

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

   int candleNum = 0;
   double highLevel=0;

   Print("The Slow moving average has been below the fast moving average for a while 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 long pattern the new low is: "+(string)highLevel);
      return highLevel;
     }
   else
     {
      Print("There is no long fractal pattern right now.");
      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;
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool maTrendUp()
  {
   bool retval = false;

   Print("We are begining to look for a trend up.");

//Loop to look at the moving averages and make sure they have not crossed in the resont history.
   for(int candleNum=0; candleNum<=candleRange; candleNum++)
     {

//Variables to define an MA cross over
      fastMovingAverage = iMA(NULL,0,fastMaPeriod,0,MODE_SMA,PRICE_CLOSE,candleNum);
      lastFastMovingAverage = iMA(NULL,0,fastMaPeriod,0,MODE_SMA,PRICE_CLOSE,(candleNum+1));
      slowMovingAverage = iMA(NULL,0,slowMaPeriod,0,MODE_SMA,PRICE_CLOSE,candleNum);
      lastSlowMovingAverage = iMA(NULL,0,slowMaPeriod,0,MODE_SMA,PRICE_CLOSE,(candleNum+1));

      bool  wasUp = lastSlowMovingAverage<lastFastMovingAverage,
            isUp = slowMovingAverage<fastMovingAverage,
            isCross = isUp != wasUp;

      Print("Looking at candle: "+(string)candleNum);
      
//conditions to either continue the loop or exit the loop
      if(isCross||!isUp)
        {
         Print("The slow MA is either over the fast MA or has just crossed above the fast MA. Not a good time to look for a Short Trade.");
         retval=false;
         return retval;
        }
      else
         if(candleNum==candleRange&&isUp==true&&!isCross)
           {
            Print("We apear to have a Trend upward with no resent MA crosses. Lets look for a fractal pattern to enter a Long position.");
            retval=true;
            return retval;
           }
         else
           {
            if(candleNum!=candleRange&&isUp==true&&!isCross)
               Print("Continue Loop to next candle because the MA has not crossed We still show a up trend and we have not gone far enough into history.");
           }
     }
   return false;
  }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool maTrendDown()
  {
   bool retval = false;

   Print("We are begining to look for a down trend.");

//Loop to look at the moving averages and make sure they have not crossed in the resont history.
   for(int candleNum=0; candleNum<=candleRange; candleNum++)
     {
     
//Variables to define an MA cross over
      fastMovingAverage = iMA(NULL,0,fastMaPeriod,0,MODE_SMA,PRICE_CLOSE,candleNum);
      lastFastMovingAverage = iMA(NULL,0,fastMaPeriod,0,MODE_SMA,PRICE_CLOSE,(candleNum+1));
      slowMovingAverage = iMA(NULL,0,slowMaPeriod,0,MODE_SMA,PRICE_CLOSE,candleNum);
      lastSlowMovingAverage = iMA(NULL,0,slowMaPeriod,0,MODE_SMA,PRICE_CLOSE,(candleNum+1));


      bool  wasUp = lastFastMovingAverage>lastSlowMovingAverage,
            isUp = fastMovingAverage>slowMovingAverage,
            isCross = isUp != wasUp;

      Print("Looking at candle: "+(string)candleNum);
      
//conditions to either continue the loop or exit the loop
      if(isCross||isUp)
        {
         Print("The slow MA either is above or has crossed above the fast MA. Not a good time to look for a Short Trade.");
         retval=false;
         return retval;
        }
      else
         if(candleNum==candleRange&&!isCross&&!isUp)
           {
            Print("We apear to have a Trend down. Lets look for a fractal pattern to enter a short position.");
            retval=true;
            return retval;
           }
         else
           {
            if(candleNum!=candleRange&&!isUp&&!isCross)
               Print("Continue Loop to next candle because the MA has not crossed We still show a down trend and we have not gone far enough into history.");
           }
     }
   return false;
  }
//+------------------------------------------------------------------+
 
SirFency:  I have highlighted where it stops.

Place a print statement to follow the start.

 
William Roeder:

Place a print statement to follow the start.

I must not understand where the start is. I put a  print statement following the very first function that checks for a new candle. When you say "Place a print statement to follow the start." Where do you mean? The start of?

 
SirFency: I put a  print statement following the very first function that checks for a new candle. When you say "Place a print statement to follow the start." Where do you mean? The start of?

Start of the function. Before your first check.

 
William Roeder:

Start of the function. Before your first check.

I tried to look for the clue you handed me but I'm just not understanding. I added in some print statements into the new candle function but I'm not getting clued in. I rechecked the  maTrendDown() for any reason it could be breaking the tester but as far as I can see it should just exit the if statement and wait for the next candle. I'm very confused. I have added the code so you can see the new print statements I added. Is it the way I structured the "On Tick" code or is it a function that is broken? I did remove a bunch of "else" lines for now just to see if that was causing any issues. I'm still getting a dead stop as soon as my maTrendDown() shows false. I use this same candle check in all of my EA's and it seems to cause no issues in any of them so I am lead to believe the issue is somewhere else.


thank you for your time BTW. 



//+------------------------------------------------------------------+
//|                                          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 fastMovingAverage;
double lastFastMovingAverage;
double slowMovingAverage;
double lastSlowMovingAverage;


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()
  {
   Print("Very Start.");
//Looking for a new candle
   if(isNewCandle())
     {
      Print("On tick confirmation of New Candel.");
      //Looking for any open orders
      if(OrdersTotal()==0)
         Print("No open orders right now. Lets look for an oportunity.");
      //looking for an upward trend
      if(maTrendUp())
        {
         //looking for a fractal pattern
         if(buyFractal()>0)
           {
            //looking for a breakout over the fractal pattern high
            if(buyBreakout())
              {
               //entering a long position
               buy();
              }
           }
        }
      //looking for a downward trend
      if(maTrendDown())
        {
         //looking for a fractal pattern
         if(sellFractal()>0)
           {
            //looking for a breakout below the fractal pattern
            if(sellBreakout())
              {
               //entering a short
               sell();
              }
           }
        }
     }
  }
//Not sure why it wont go back to the beginning and start again???



//+------------------------------------------------------------------+
//|                           Functions                              |
//+------------------------------------------------------------------+
//Checks for a new candle
bool isNewCandle()
  {
   Print("Looking to see if there is a new candle");
   static datetime savedCandleTime;
   if(Time[0]==savedCandleTime)
     {
      Print("No new candle");
      return false;
     }
   else
     {
      Print("there is a new candle");
      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;

   Print("The Slow moving average has been above the fast moving average for a while 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;
     }
  }

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

   int candleNum = 0;
   double highLevel=0;

   Print("The Slow moving average has been below the fast moving average for a while 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 long pattern the new low is: "+(string)highLevel);
      return highLevel;
     }
   else
     {
      Print("There is no long fractal pattern right now.");
      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;
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool maTrendUp()
  {
   bool retval = false;

   Print("We are begining to look for a trend up.");

//Loop to look at the moving averages and make sure they have not crossed in the resont history.
   for(int candleNum=0; candleNum<=candleRange; candleNum++)
     {

      //Variables to define an MA cross over
      fastMovingAverage = iMA(NULL,0,fastMaPeriod,0,MODE_SMA,PRICE_CLOSE,candleNum);
      lastFastMovingAverage = iMA(NULL,0,fastMaPeriod,0,MODE_SMA,PRICE_CLOSE,(candleNum+1));
      slowMovingAverage = iMA(NULL,0,slowMaPeriod,0,MODE_SMA,PRICE_CLOSE,candleNum);
      lastSlowMovingAverage = iMA(NULL,0,slowMaPeriod,0,MODE_SMA,PRICE_CLOSE,(candleNum+1));

      bool  wasUp = lastSlowMovingAverage<lastFastMovingAverage,
            isUp = slowMovingAverage<fastMovingAverage,
            isCross = isUp != wasUp;

      Print("Looking at candle: "+(string)candleNum);

      //conditions to either continue the loop or exit the loop
      if(isCross||!isUp)
        {
         Print("The slow MA is either over the fast MA or has just crossed above the fast MA. Not a good time to look for a Short Trade.");
         retval=false;
         return retval;
        }
      else
         if(candleNum==candleRange&&isUp==true&&!isCross)
           {
            Print("We apear to have a Trend upward with no resent MA crosses. Lets look for a fractal pattern to enter a Long position.");
            retval=true;
            return retval;
           }
         else
           {
            if(candleNum!=candleRange&&isUp==true&&!isCross)
               Print("Continue Loop to next candle because the MA has not crossed We still show a up trend and we have not gone far enough into history.");
           }
     }
   return false;
  }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool maTrendDown()
  {
   bool retval = false;

   Print("We are begining to look for a down trend.");

//Loop to look at the moving averages and make sure they have not crossed in the resont history.
   for(int candleNum=0; candleNum<=candleRange; candleNum++)
     {

      //Variables to define an MA cross over
      fastMovingAverage = iMA(NULL,0,fastMaPeriod,0,MODE_SMA,PRICE_CLOSE,candleNum);
      lastFastMovingAverage = iMA(NULL,0,fastMaPeriod,0,MODE_SMA,PRICE_CLOSE,(candleNum+1));
      slowMovingAverage = iMA(NULL,0,slowMaPeriod,0,MODE_SMA,PRICE_CLOSE,candleNum);
      lastSlowMovingAverage = iMA(NULL,0,slowMaPeriod,0,MODE_SMA,PRICE_CLOSE,(candleNum+1));


      bool  wasUp = lastFastMovingAverage>lastSlowMovingAverage,
            isUp = fastMovingAverage>slowMovingAverage,
            isCross = isUp != wasUp;

      Print("Looking at candle: "+(string)candleNum);

      //conditions to either continue the loop or exit the loop
      if(isCross||isUp)
        {
         Print("The slow MA either is above or has crossed above the fast MA. Not a good time to look for a Short Trade.");
         retval=false;
         return retval;
        }
      else
         if(candleNum==candleRange&&!isCross&&!isUp)
           {
            Print("We apear to have a Trend down. Lets look for a fractal pattern to enter a short position.");
            retval=true;
            return retval;
           }
         else
           {
            if(candleNum!=candleRange&&!isUp&&!isCross)
               Print("Continue Loop to next candle because the MA has not crossed We still show a down trend and we have not gone far enough into history.");
           }
     }
   return false;
  }
//+------------------------------------------------------------------+
 
SirFency:

I tried to look for the clue you handed me but I'm just not understanding. I added in some print statements into the new candle function but I'm not getting clued in. I rechecked the  maTrendDown() for any reason it could be breaking the tester but as far as I can see it should just exit the if statement and wait for the next candle. I'm very confused. I have added the code so you can see the new print statements I added. Is it the way I structured the "On Tick" code or is it a function that is broken? I did remove a bunch of "else" lines for now just to see if that was causing any issues. I'm still getting a dead stop as soon as my maTrendDown() shows false. I use this same candle check in all of my EA's and it seems to cause no issues in any of them so I am lead to believe the issue is somewhere else.


thank you for your time BTW. 



I beat my head against the wall only to find that my back testing data was the issue. Once I shut down MT4 and reopened it the back tester just started working properly. Not sure why that is the case but at least I can move forward on this program. Thank for your time  William Roeder.

Reason: