Unable to open BUY trades (MQL4)

 

Hello everyone,

My EA is only opening SELL positions. I have the similar coding layout on my other EAs as well but haven't been able to figure out what's wrong with this particular one, since i don't see any error messages. Strategy Tester shows also no BUY positions being opened. The code is as below:


//Current chart, current period, number of candles, no shift, simple, current close price
   double CurrentMainMACD = iMACD(Symbol(), PERIOD_M5, Fast, Slow, 9, PRICE_CLOSE, MODE_MAIN, 0);

//Current chart, current period, number of candles, no shift, simple, previous close price
   double PreviousMainMACD = iMACD(Symbol(), PERIOD_M5, Fast, Slow, 9, PRICE_CLOSE, MODE_MAIN, 1);

//Current chart, current period, number of candles, no shift, simple, current close price
   double CurrentSignalMACD = iMACD(Symbol(), PERIOD_M5, Fast, Slow, 9, PRICE_CLOSE, MODE_SIGNAL, 0);

//Current chart, current period, number of candles, no shift, simple, previous close price
   double PreviousSignalMACD = iMACD(Symbol(), PERIOD_M5, Fast, Slow, 9, PRICE_CLOSE, MODE_SIGNAL, 1);
   
   double SlowEMA = iMA(NULL, 0, EMA_Period, 0, MODE_EMA, PRICE_CLOSE, 1);
   

//To make sure only one position is opened per bar
   static datetime prevtime = 0;

   if(prevtime == Time[0])
      return;
   prevtime = Time[0];
     {

   //If fast SMA is above slow SMA
      if((PreviousSignalMACD < PreviousMainMACD) && (CurrentSignalMACD > CurrentMainMACD))
        {
          if(SlowEMA > EMA_Period)
            {
         //Chart output for BUY signal
         signal="Buy";   
            }
        }
        
      //If fast SMA is below slow SMA
      else if((PreviousSignalMACD > PreviousMainMACD) && (CurrentSignalMACD < CurrentMainMACD))
        {
          if(SlowEMA < EMA_Period)
            {
         //Chart output for SELL signal
         signal="Sell";   
            }
        }
        
       //Position for BUY    
      if(signal == "Buy")
        {
         //Here we are assuming that the TakeProfit and StopLoss are entered in Pips
         ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 10*10, Ask-SL*Point*10, Ask+TP*Point*10, "Long by MaCD & EMA", 616161, 0, Blue);
         if(ticket < 0)
           {
            Alert("Error Sending Order! Error Code: ", GetLastError());
           }
        }

      //Position for SELL
      else if(signal == "Sell")
        {
         //Here we are assuming that the TakeProfit and StopLoss are entered in Pips
         ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, 10*10, Bid+SL*Point*10, Bid-TP*Point*10, "Short by MaCD & EMA", 616161, 0, Red);
         if(ticket < 0)
           {
            Alert("Error Sending Order! Error Code: ", GetLastError());
           }
        }
     }

//Create a chart output
   Comment("The signal is: ",signal,"\n");


Any help is appreciated.

 
if(SlowEMA > EMA_Period)

Why are you comparing the ema to its period?

 
Keith Watford #:

Why are you comparing the ema to its period?

I've written it as an extern input for 200 EMA, that i sometimes change into 100 or 50. So that's a buy/sell condition for being above/below 200 EMA.
 
Keith Watford #:

Why are you comparing the ema to its period?

if(SlowEMA > EMA_Period)
SeferAmcaFTW #:
I've written it as an extern input for 200 EMA, that i sometimes change into 100 or 50. So that's a buy/sell condition for being above/below 200 EMA.

I didn't ask you what you had done, I asked you why??????

THINK!

 
Keith Watford #:

I didn't ask you what you had done, I asked you why??????

THINK!

Holy hell that was the wake up call i needed!!

Just fixed it as below and it's working fine now:

 if(Close[1] > SlowEMA)

Thanks a bunch!

Reason: