Sell trades aren't working

 

Hello guys. I'm very new to coding started a few weeks ago. And I have build the following EA. But the EA only opens buy trades. Can you guys help me out? What is wrong with the else if statement. 

Please explain in detail because I wan't to learn as much as possible. 


#include <Trade\Trade.mqh>

// input variable

input double lot        =  10;
input double stopLoss   =  5.00;
input double takeProfit =  25.00;
input int shortSmaPeriod = 21;
input int longSmaPeriod = 34;
input int retouchSmaPeriod = 1;

CTrade trade;
MqlRates priceData[];
double currentAsk, currentBid;
int numberOfPriceDataPoints, numberOfShortSmaData, numberOfLongSmaData, numberOfRetouchSmaData;
double shortSmaData[], longSmaData[], retouchSmaData[];
double shortSma1, shortSma2, longSma1, longSma2, retouchSma, shortSma0;
int shortSmaHandle, longSmaHandle, retouchSmaHandle;


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   ArraySetAsSeries(priceData, true);
   ArraySetAsSeries(shortSmaData,true);
   ArraySetAsSeries(longSmaData,true);
   ArraySetAsSeries(retouchSmaData,true);
   
   shortSmaHandle   = iMA(_Symbol,PERIOD_CURRENT,shortSmaPeriod,0,MODE_EMA,PRICE_CLOSE);
   longSmaHandle    = iMA(_Symbol,PERIOD_CURRENT,longSmaPeriod,0,MODE_EMA,PRICE_CLOSE);
   retouchSmaHandle = iMA(_Symbol,PERIOD_CURRENT,retouchSmaPeriod,0,MODE_EMA,PRICE_CLOSE);
   

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   IndicatorRelease(shortSmaHandle);
   IndicatorRelease(longSmaHandle);
   IndicatorRelease(retouchSmaHandle);
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

   currentAsk =  NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   currentBid =  NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   
   numberOfPriceDataPoints = CopyRates(_Symbol,0,0,10,priceData);
   numberOfShortSmaData   =  CopyBuffer(shortSmaHandle,0,0,10,shortSmaData);
   numberOfLongSmaData    =  CopyBuffer(longSmaHandle,0,0,10,longSmaData);
   numberOfRetouchSmaData =  CopyBuffer(retouchSmaHandle,0,0,10,retouchSmaData);
   
   retouchSma = retouchSmaData[0];
   shortSma0 = shortSmaData[0];
   shortSma1 = shortSmaData[1];
   shortSma2 = shortSmaData[2];
   longSma1 = longSmaData[1];
   longSma2 = longSmaData[2];
   
// entries
   if(PositionSelect(_Symbol) == false)
   {
         if(shortSma2 < longSma2 && shortSma1 > longSma1)
            if(retouchSma <= shortSma0)
      {
      trade.Buy(lot,_Symbol,currentBid,currentBid-stopLoss*_Point,currentBid+takeProfit*_Point,NULL);
      }  
 
   else if(shortSma2 > longSma2 && shortSma1 < longSma1)
            if(retouchSma >= shortSma0)
      {
      trade.Sell(lot,_Symbol,currentAsk,currentAsk+stopLoss*_Point,currentAsk-takeProfit*_Point,NULL);
      }    
     }
    }
//+------------------------------------------------------------------+
 
 
// entries
 
    if(PositionSelect(_Symbol) == true)
    }
    if((shortSma2 < longSma2 && shortSma1 > longSma1) && retouchSma <= shortSma0)
      {
      trade.Buy(lot,_Symbol,currentBid,currentBid-stopLoss*_Point,currentBid+takeProfit*_Point,NULL);
      }  
 
   else if((shortSma2 > longSma2 && shortSma1 < longSma1) && retouchSma >= shortSma0)
      {
      trade.Sell(lot,_Symbol,currentAsk,currentAsk+stopLoss*_Point,currentAsk-takeProfit*_Point,NULL);
      }
    }    
 
gardee005 #:

actually should be 

if(PositionSelect(_Symbol) == true)

also you should normailize the SL and TP before insertion


         sl = SymbolInfoDouble(Symbol(),SYMBOL_BID) - SlPoints*Point();
         sl = NormalizeDouble(sl, Digits());
         tp = SymbolInfoDouble(Symbol(),SYMBOL_BID) + TpPoints*Point();
         tp = NormalizeDouble(tp, Digits());
 
gardee005 #:

actually should be 

also you should normailize the SL and TP before insertion


also you buy at the current ask and sell at the current bid. you have it the other way round