My EA doesnot take trade as per the parameters.

 
Can anyone help me with my code. My EA only takes 2 trades but it must have taken more trades in strategy test.
#include <Trade/Trade.mqh>
input ENUM_TIMEFRAMES TimeFrames = PERIOD_CURRENT;
input int MaPeriod = 200;
input double Lots = 0.1;
input double slValue = 0.0050;
input double tpValue = 0.0050;




int handleMa;
int handleMacd;
int barsTotal;
CTrade trade ;


double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);


ulong posticket;


int OnInit()
  {
  
  

   handleMa = iMA(_Symbol,TimeFrames,MaPeriod,0,MODE_SMA,PRICE_CLOSE);
   handleMacd = iMACD(_Symbol,TimeFrames,12,26,9,PRICE_CLOSE);
   
   
   return(INIT_SUCCEEDED);
  }
void OnDeinit(const int reason)
  {
   
  }
void OnTick()
  {
 

  
   
   double Ma [];
   CopyBuffer(handleMa,BASE_LINE,0,1,Ma);
   
   double MacdMain[], MacdSignal[];
   CopyBuffer(handleMacd,MAIN_LINE,0,1,MacdMain);
   CopyBuffer(handleMacd,SIGNAL_LINE,0,1,MacdSignal);
   
   
  
   bid = NormalizeDouble(bid,_Digits);
   ask = NormalizeDouble(ask,_Digits);      
   

    if(bid < Ma[0]){
    
    if( MacdMain[0] > 0){
    
    if( MacdMain[0] < MacdSignal[0]){
    
      Print("Sell Entry is triggering...");
      
          
            if( posticket <= 0){  
      
            double sl= bid+ slValue;
            sl = NormalizeDouble(sl,_Digits);
            
            double tp= bid- tpValue;
            tp = NormalizeDouble(tp,_Digits);
            
          
             
            trade.Sell(Lots,_Symbol,bid,sl,tp);
            posticket = trade.ResultOrder();
            
          }  
          }
          }
            
     }
   
   

   else if(ask > Ma[0]){
   
   if( MacdMain[0] < 0){
   
   if( MacdMain[0] > MacdSignal[0]){
   
      Print("Buy Entry is triggering...");
      
           if( posticket <= 0){  
  
      
            double sl= ask- slValue;
            sl = NormalizeDouble(sl,_Digits);
            
            double tp= ask+ tpValue;
            tp = NormalizeDouble(tp,_Digits);
            
            
             
            trade.Buy(Lots,_Symbol,ask,sl,tp);
            posticket = trade.ResultOrder();
            
            
    }
    }
   }
   
   }
   
   }
  
 
double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);

ulong posticket;

int OnInit() …

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)

Reason: