HELP: Calculate risk per trade based on fixed amount & risk percent of account balance

 
Hello traders. My code down here doesn't place trades when I select risk type as fixed amount or as a risk percent of the account balance. It only places trades when fixed lot size is selected in the user inputs. The CalculateLotSize function should calculate lot sizes based on stop loss distance given by ema distance from entry, because my stop loss is based on ema. More so, the ea doesn't include a take profit which i have tried to set it to 3 times the stop loss. Where do you think I am wrong. Thanks in advance.
#include <Trade/Trade.mqh>

sinput group "===EA SETTINGS==="
input long imagic = 001;//Expert Magic
input string iexpirydate = "2024.12.31 23:00"; //Expiry Date

enum LOT_MODE_ENUM{
    LOT_MODE_FIXED,  //Fixed LotSize
    LOT_MODE_MONEY,   //Risk Amount
    LOT_MODE_PERCENT_ACCOUNT //Risk Percent
};
input LOT_MODE_ENUM ilotmode = LOT_MODE_FIXED;//Risk Type
input double irisk = 0.01; //LotSize/Amount/Percent of Account

int handleEMAm5;

double EMA5[];


MqlTick ptick, tick;
MqlRates Bar[];

CTrade trade;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
  
   if(ilotmode==LOT_MODE_FIXED && (irisk<=0 || irisk>=2)){
     Alert("Lot Size input is zero or above 2");
     return false;
   }
  
   if(ilotmode==LOT_MODE_MONEY && (irisk<=0 || irisk>=250)){
     Alert("Risk amount  input is zero or above 250");
     return false;
   }
   
   if(ilotmode==LOT_MODE_PERCENT_ACCOUNT && (irisk<=0 || irisk>=3)){
     Alert("Risk percent  input is zero or above 3");
     return false;
   }
  
   trade.SetExpertMagicNumber(imagic);
   trade.SetAsyncMode(true);
   
   handleEMAm5 = iMA(_Symbol,PERIOD_M5,21,0,MODE_EMA,PRICE_CLOSE);
     if(handleEMAm5==INVALID_HANDLE){return INIT_FAILED;}
  
//---
   if(TimeCurrent()<StringToTime(iexpirydate)){
     Print(__FUNCTION__,">License is active");
   }else {
     Alert(__FUNCTION__,">License is expired. Contact WhatsApp: +254794 159905","\nEmail: douglasarcade24@gmail.com");
     ExpertRemove();
     return INIT_FAILED;
   }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

 IndicatorRelease(handleEMAm5);
 
//+---------------
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   ptick = tick;
   SymbolInfoTick(_Symbol,tick);
   
   CopyRates(_Symbol,PERIOD_M1,0,3,Bar);
   ArraySetAsSeries(Bar,true);
   
   CopyBuffer(handleEMAm5,0,0,3,EMA5);

   ArraySetAsSeries(EMA5,true);

   long spread = SymbolInfoInteger(_Symbol,SYMBOL_SPREAD);
   int positioncount = CountOpenPositions();
   
   //BUY

    if(Bar[2].close<=EMA5[2] && Bar[1].close>EMA5[1]){
      if(positioncount<=0){
      
      //calculate lots
      double lots;
       if(!CalculateLots((tick.ask-NormalizeDouble(EMA5[2],2))*100,lots)){return;}
         
      //tp is three times the risk 
       double tp_bpoints = NormalizeDouble(((tick.ask-NormalizeDouble(EMA5[2],2))*100)*3,_Digits);
       
       trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,lots,tick.ask,EMA5[2]- (spread*2)*_Point,tick.ask+tp_bpoints);


      }
    }
 
   //SELL
    if(Bar[2].close>=EMA5[2] && Bar[1].close<EMA5[1]){
      if(positioncount<=0){
      
      //calculate lots
      double lots;
       if(!CalculateLots((NormalizeDouble(EMA5[2],2)-tick.bid)*100,lots)){return;}
      
      //tp is three times the risk
       double tp_spoints = NormalizeDouble(((NormalizeDouble(EMA5[2],2)-tick.bid)*100)*3,_Digits);
       
       trade.PositionOpen(_Symbol,ORDER_TYPE_SELL,lots,tick.bid,EMA5[2]+(spread*2)*_Point,tick.bid-tp_spoints);

      }
    }
  }
//+------------------------------------------------------------------+

// Count no. of open positions
 int CountOpenPositions(){
  
  int count = 0 ;
  
  int total =PositionsTotal();
  for(int i=total-1; i>=0; i--){
    ulong positionTicket = PositionGetTicket(i);
    if(positionTicket<=0){Print("Failed to get ticket");return false;}
    if(!PositionSelectByTicket(positionTicket)){Print("Failed to select position"); return false;}
    long magic;
    if(!PositionGetInteger(POSITION_MAGIC,magic)){Print("Failed to get magic");return false;}
    string symbol;
    if(!PositionGetString(POSITION_SYMBOL,symbol)){Print("Failed to get symbol");return false;}
    if(symbol == _Symbol){
    if(magic == imagic){
      count++;
      }
     } 
    }
    return count;
}

//Calculate LotSize based on risk type selected
bool CalculateLots(double slDistance, double &lots){
    
    lots = 0.0;
    
    if(ilotmode==LOT_MODE_FIXED){
     lots= irisk;
    }
   else{
     double tickSize = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
     double tickValue = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);
     double VolumeStep = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);
     
     double riskAmount = ilotmode==LOT_MODE_MONEY ? irisk : AccountInfoDouble(ACCOUNT_BALANCE)*irisk/100;
     double moneyVolumeStep = (slDistance/tickSize)*tickValue*VolumeStep;
     
     lots = MathFloor(riskAmount/moneyVolumeStep)*VolumeStep;
   }
   //Check calculated lotsize
   if(!CheckLots(lots)){return false;}
   
   return true;
}

//Check Lots for Min, Max & Step
bool CheckLots(double &lots){

  double min = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN);
  double max = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX);
  double step = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);
  
  if(lots<min){
   Print("Lot size will be set to the minimum allowed vol");
   lots = min;
   return true; 
  }
  
  if(lots>max){
   Print("Lot size is greater than the max allowed vol. Lots: ",lots," max vol: ", max);
   return false; 
  }
  
  lots = (int)MathFloor(lots/step)*step;
 return true;
}