My EA is not trading anything.

 
Hi. I'm trying to learn to code. I'm attempting to code a moving average crossover, but when I ran it through Strategy Tester, no trade was open. I followed a few YouTube videos when writing this code. I know there are a lot of master coders here. Can you please tell me my errors?


//--------------------------------------------------------------------------------------------------------------

int FastEMAperiod = 20 ;          //EMA period

int SlowEMAperiod = 50;          //EMA period



double FastEMA = iMA(NULL,0,FastEMAperiod,0,MODE_EMA,PRICE_CLOSE,0);     //EMA value

double SlowEMA = iMA(NULL,0,SlowEMAperiod,0,MODE_EMA,PRICE_CLOSE,0);     //EMA value



double FastEMAbefore = iMA(NULL,0,FastEMAperiod,0,MODE_EMA,PRICE_CLOSE,1);     //EMA value

double SlowEMAbefore = iMA(NULL,0,SlowEMAperiod,0,MODE_EMA,PRICE_CLOSE,1);     //EMA value



bool CrossBuy = FastEMA > SlowEMA && FastEMAbefore < SlowEMAbefore ;

bool CrossSell = FastEMA < SlowEMA && FastEMAbefore > SlowEMAbefore ;



   double accountsize = AccountEquity();

   double RiskPercentageTrade = .02;

   double StopLossInPips = .002;

   double TakeProfit = .004;

   double PositionSize = 0.1;

   int MagicNB = 1;

   int Ticket;



int OnInit() //-----------------------------------------------------------------------------------------------------------------------

  { 

  return(INIT_SUCCEEDED);

  }



void OnDeinit(const int reason) //----------------------------------------------------------------------------------------------------------------

  {

  }



void OnTick() //-----------------------------------------------------------------------------------------------------------------------

  { 

   double LastHigh = iHigh(NULL,0,1); 

   double LastLow = iLow(NULL,0,1);

  

   if(CrossBuy == TRUE) {      

      if (OrderSelect(Ticket,SELECT_BY_TICKET) && OrderType() == (OP_SELLSTOP||OP_SELL)) 

         { if (OrderClose(OrderTicket(),OrderLots(),LastHigh+TakeProfit,500)) 

            { Ticket = 0;  }  }                     

      if (Ticket <= 0) 

         {  Ticket = OrderSend(NULL,OP_BUYSTOP,PositionSize,LastHigh,500,LastHigh-StopLossInPips,LastHigh+TakeProfit,NULL,1); } 

                        }

      

   else if(CrossSell == TRUE) {  

      if (OrderSelect(Ticket,SELECT_BY_TICKET) && OrderType() == (OP_BUYSTOP || OP_BUY)) 

         { if (OrderClose(OrderTicket(),OrderLots(),LastLow-TakeProfit, 500)) 

            { Ticket = 0;}}            

      if(Ticket <= 0) 

         { Ticket = OrderSend(NULL,OP_SELLSTOP,PositionSize,LastLow,500,LastLow+StopLossInPips,LastLow-TakeProfit,NULL,1);}

                              }   
The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
What are the differences between the three modes of testing in MetaTrader 5, and what should be particularly looked for? How does the testing of an EA, trading simultaneously on multiple instruments, take place? When and how are the indicator values calculated during testing, and how are the events handled? How to synchronize the bars from different instruments during testing in an "open prices only" mode? This article aims to provide answers to these and many other questions.
 
double FastEMA = iMA(NULL,0,FastEMAperiod,0,MODE_EMA,PRICE_CLOSE,0);     //EMA value
double SlowEMA = iMA(NULL,0,SlowEMAperiod,0,MODE_EMA,PRICE_CLOSE,0);     //EMA value

double FastEMAbefore = iMA(NULL,0,FastEMAperiod,0,MODE_EMA,PRICE_CLOSE,1);     //EMA value
double SlowEMAbefore = iMA(NULL,0,SlowEMAperiod,0,MODE_EMA,PRICE_CLOSE,1);     //EMA value

bool CrossBuy = FastEMA > SlowEMA && FastEMAbefore < SlowEMAbefore ;
bool CrossSell = FastEMA < SlowEMA && FastEMAbefore > SlowEMAbefore ;

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 indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), 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. A 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)

 
William Roeder #:

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)

I transferred those lines to OnTick, finally coded my first EA. Thank you, sir!!