My MQL4 expert can't detect 5 EMA and 20 EMA crossover in strategy tester

 

Hello people, I am a newbie in coding bot for meta trader platform, but I have decent experience coding JavaScript for the web, I just build a simple mql4 bot using 5 and 20 ema to place order, but when I try to test it via strategy tester it keep showing both condition not met, please what am I doing wrong.

below are my code


input int fastEma_Period = 5;
input int fastEma_ma_Shift = 0;
input int fastEma_ma_Method = 1;               // EXPONENTIAL
input int fastEma_applied_Price = 0;          //  CLOSED PRICE
input int fastEma_shift_CandleIndex = 0;      //  CURRENT FORMING CANDLESTICK

input int slowEma_Period = 20;
input int slowEma_ma_Shift = 0;
input int slowEma_ma_Method = 1;              // EXPONENTIAL
input int slowEma_applied_Price = 0;          // CLOSED PRICE
input int slowEma_shift_CandleIndex = 0;      // CURRENT FORMING CANDLESTICK

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double fastEmaBar0 = iMA(NULL,0,fastEma_Period,fastEma_ma_Shift,fastEma_ma_Method,fastEma_applied_Price,fastEma_shift_CandleIndex);
double slowEmaBar0 = iMA(NULL,0,slowEma_Period,slowEma_ma_Shift,slowEma_ma_Method,slowEma_applied_Price,slowEma_shift_CandleIndex);
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double fastEmaBar1 = iMA(NULL,0,fastEma_Period,fastEma_ma_Shift,fastEma_ma_Method,fastEma_applied_Price,fastEma_shift_CandleIndex + 1);
double slowEmaBar1 = iMA(NULL,0,slowEma_Period,slowEma_ma_Shift,slowEma_ma_Method,slowEma_applied_Price,slowEma_shift_CandleIndex + 1);

double lotSize = AccountBalance() / 10000;

int maBt;
int maSt;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   enterMarket();
// ExpertRemove();
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string maCrossOverSignal(double fastEmaBar1, double slowEmaBar1, double fastEmaBar0, double slowEmaBar0)
  {
   string signal;
   if((fastEmaBar1 <= slowEmaBar1) && (fastEmaBar0 > slowEmaBar0))
     {
      signal = "Buy";
     }
   else
      if((fastEmaBar1 >= slowEmaBar1) && (fastEmaBar0 < slowEmaBar0))
        {
         signal = "Sell";
        }
   return signal;
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void enterMarket()
  {
   if(/*OrdersTotal() == 0 &&*/ maCrossOverSignal(fastEmaBar1,slowEmaBar1,fastEmaBar0,slowEmaBar0)== "Buy")
     {
      //continue inside this condition, bcos its not getting met
      Print("time to Buy ");
      maBt = OrderSend(NULL,OP_BUY,lotSize,Ask,100,0,0,"maBt");
      if(maBt > 0)
        {
         if(OrderSelect(maBt,SELECT_BY_TICKET))
           {
            modify(maBt,OrderOpenPrice(),OrderOpenPrice() - 50 * Point,OrderOpenPrice() + 200 * Point,0);
           }
        }
     }
   else
      if(/*OrdersTotal() == 0 && */maCrossOverSignal(fastEmaBar1,slowEmaBar1,fastEmaBar0,slowEmaBar0)== "Sell")
        {
         Print("time to Sell ");
         maSt = OrderSend(NULL,OP_SELL,lotSize,Bid,100,0,0,"maSt");
         if(maSt > 0)
           {
            if(OrderSelect(maSt,SELECT_BY_TICKET))
              {
               modify(maSt,OrderOpenPrice(),OrderOpenPrice() + 50 * Point,OrderOpenPrice() - 200 * Point,0);
              }
           }
        }
      else
        {
         Print("both condition not met");
        }
  }
 
double fastEmaBar0 = iMA(NULL,0,fastEma_Period,fastEma_ma_Shift,fastEma_ma_Method,fastEma_applied_Price,fastEma_shift_CandleIndex);
double slowEmaBar0 = iMA(NULL,0,slowEma_Period,slowEma_ma_Shift,slowEma_ma_Method,slowEma_applied_Price,slowEma_shift_CandleIndex);

double fastEmaBar1 = iMA(NULL,0,fastEma_Period,fastEma_ma_Shift,fastEma_ma_Method,fastEma_applied_Price,fastEma_shift_CandleIndex + 1);
double slowEmaBar1 = iMA(NULL,0,slowEma_Period,slowEma_ma_Shift,slowEma_ma_Method,slowEma_applied_Price,slowEma_shift_CandleIndex + 1);

These should not be assigned values globalscope. declare and assign values in your 

maCrossOverSignal()

function.

 
double fastEmaBar0 = iMA();
double slowEmaBar0 = iMA(…);

Those are not assignments; they are initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

  1. They are initialized once on program load.

  2. They don't update unless you assign to them.

  3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

    MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and

    Don't try to use any price or server related functions in OnInit (or on load), as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

  4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
              external static variable - MQL4 programming forum #2 2013.02.10

 

Thank you very much @Keith Watford and @William Roeder.

The code is working properly now after i declare the moving average output inside the maCrossoOverSignal() function.

Reason: