Please Help me making a simple EA work

 

Hi all,

I am trying to teach myself how to code EAs, and I have decided to code a simple Ema cross over strategy with an ATR stoploss to start with. It should be pretty straight forward but at this stage I can't see where I am wrong, the EA won't pass an order

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


double SlowMovingAverage = iMA(NULL,0,14,0,MODE_EMA,PRICE_CLOSE,0);
double LastSlowMovingAverage = iMA(NULL,0,14,0,MODE_EMA,PRICE_CLOSE,2);
double FastMovingAverage = iMA(NULL,0,9,0,MODE_EMA,PRICE_CLOSE,0);
double LastFastMovingAverage = iMA(NULL,0,9,0,MODE_EMA,PRICE_CLOSE,2);
double atr = iATR(NULL,0,14,0);


extern int    MagicNumber= 190418;
double lots = 1;
int StopLoss = (int)(atr / Point);

int takeProfit =StopLoss*3;
int Slippage=3;
int Ticket,;
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }


//+------------------------------------------------------------------+
//| Expert Start function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {


//+------------------------------------------------------------------+
//Only 1 order at a time
//+------------------------------------------------------------------+

 if (OrdersTotal() > 0) {
   return;}
//+------------------------------------------------------------------+
//Signal Buy
//+------------------------------------------------------------------+
   if((FastMovingAverage > SlowMovingAverage) && (LastFastMovingAverage < LastSlowMovingAverage)){
                
                
//+------------------------------------------------------------------+
//Buy Order
 //--------------------------------------------------------------+      
                
                OrderSend(Symbol(), OP_BUY,lots, Ask, Slippage, Ask - StopLoss*Point, Ask + takeProfit*Point,NULL, MagicNumber, 0, clrBlue);
  
   }
//+------------------------------------------------------------------+
//Signal Sell
//+------------------------------------------------------------------+


    if ((FastMovingAverage < SlowMovingAverage) && (LastFastMovingAverage > LastSlowMovingAverage)) {
 
   //+------------------------------------------------------------------+
//Sell Order
 //--------------------------------------------------------------+
                 OrderSend(Symbol(), OP_SELL, lots, Bid, Slippage, Bid +StopLoss*Point, Bid - takeProfit*Point,NULL, MagicNumber, 0, clrBlue);

   }
  
   

//+------------------------------------------------------------------+
//Signal Exit Buy
 //--------------------------------------------------------------+
/*if((OrderType() == OP_BUY)) {
   if ((FastMovingAverage < SlowMovingAverage) && (LastFastMovingAverage > LastSlowMovingAverage)) {
                         
         OrderClose(Ticket,lots,Symbol(),PRICE_CLOSE);}

                  
}
//+------------------------------------------------------------------+
//Signal Exit Sell
//+------------------------------------------------------------------+
if(OrderType() == OP_SELL) {
      if ((FastMovingAverage > SlowMovingAverage) && (LastFastMovingAverage < LastSlowMovingAverage)){
      
      OrderClose(Ticket,lots,Symbol(),PRICE_CLOSE);}
   
      }
            */
                  
       
       
       return;

}


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

Please help me understand what I am doing wrong !


Thank you

 
Don't calculate the MA values in OnInit(), they should be in OnTick()
 
Maxime Mounier: can't see where I am wrong,
double SlowMovingAverage = iMA(NULL,0,14,0,MODE_EMA,PRICE_CLOSE,0);
double LastSlowMovingAverage = iMA(NULL,0,14,0,MODE_EMA,PRICE_CLOSE,2);
double FastMovingAverage = iMA(NULL,0,9,0,MODE_EMA,PRICE_CLOSE,0);
double LastFastMovingAverage = iMA(NULL,0,9,0,MODE_EMA,PRICE_CLOSE,2);
double atr = iATR(NULL,0,14,0);
Those are not assignments; it's 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
Reason: