Money Management [Code Help!]

 

Hello all! 

I'm working on a EA that uses Money Management, I found this code very useful:

//+--------------------------------------+
//|Manejo de capital                     |
//+--------------------------------------+
double Lotaje()
{
 
 if (Activate_Money_Management == Use_Money_Management) //Si el manejo de riesgo esta activado
   {
     double Min_Lots = MarketInfo(Symbol(), MODE_MINLOT);//Buscar el lotaje minimo permitido por el broker
     double Max_Lots = MarketInfo(Symbol(),MODE_MAXLOT);// Buscar el lotaje maximo permitido por el broker.
     double Lot_Size = MarketInfo(Symbol(), MODE_LOTSIZE);//Buscar el tamaño del lote en la moneda base de la cuenta.
     double Stop_Level = MarketInfo(Symbol(), MODE_STOPLEVEL); //Buscar los niveles stop de la cuenta
     double Leverage = AccountLeverage();//Buscar el apalancamiento de la cuenta 
  
      double Local_Lots = NormalizeDouble(AccountFreeMargin()*Riesgo/100/Risk_Profile(),Digitos_Lotes); //Calculo del lotaje
      
//Que hacer si el lotaje calculado es menor o mayor al permitido?
     
      if(Local_Lots < Min_Lots) //Si nuestro lotaje calculado es menor al minimo permitido
         {
           if(Minimum_Lots >= Min_Lots) //Y si nuestro minimo establecido es mayor o igual al permitido
            {
             Local_Lots = Minimum_Lots; //Usaremos el minimo establecido
             }
             else
             {
            Local_Lots = Min_Lots; //De lo contrario, usaremos el minimo permitido.
            }
           }
         else if(Local_Lots > Max_Lots)//Si nuestro lotaje calculado es mayor al maximo permitido
           {
            if(Maximum_Lots <= Max_Lots) //Y si nuestro lotaje maximo establecido es menor o igual al permitido
             {
              Local_Lots = Maximum_Lots; //Usaremos el maximo establecido
              }
             else
             {
              Local_Lots = Max_Lots; //De lo contrario, usaremos el maximo permitido
             }
            }
            
//Que hacer si el lotaje calculado es menor o mayor al establecido?
            
     if(Local_Lots < Minimum_Lots) //Si el lotaje calculado es menor al minimo establecido
      {
       Local_Lots = Minimum_Lots; //Usaremos el minimo establecido
       } 
       else if(Local_Lots > Maximum_Lots) //Si el lotaje calculado es mayor al maximo establecido
       {
       Local_Lots = Maximum_Lots; //Usaremos el maximo establecido
      }
//Que hacer si no tenemos suficiente margen en la cuenta
      
       if (AccountFreeMargin() < Ask * Local_Lots * Lot_Size / Leverage) //Si nuestro margen es menor al requerido
         {
            Alert("ALERTA, NO HAY MARGEN SUFICIENTE");
            Alert("EL CAPIITAL ACTUAL ES: ", AccountFreeMargin());
            Alert("LA CANTIDAD ACTUAL DE LOTES POR OPERACION ES: ",Local_Lots);
            Alert("DETENIENDO EA...");
            ExpertRemove(); //Alertar y detener el bot
          } 
           
        return(Local_Lots); //Luego de las verificaciones, retornar el lotaje calculado
     }  
   return(Lotes); //Si el manejo esta desactivado, usar el lotaje establecido por nosotros
 }  
//+--------------------------------------+
//|Perfil de riesgo                      |
//+--------------------------------------+
double Risk_Profile()
   {
     if(Perfil_De_Riesgo == Bajo) //Si el perfil es bajo
      {
        return(1000.00); //Operar 0.01 por cada $1,000 en la cuenta
    }
     else if(Perfil_De_Riesgo == Intermedio) //Si el perfil es intermedio
      {
         return(500.00); //Operar 0.01 por cada $500 en la cuenta
       }
     return (200.00); //De lo contrario si ninguna es verdadera, el perfil es alto y debemos operar 0.01 por cada $200 en la cuenta.
    } 

As it does what I just needed to, the problem I'm facing is pretty specific:

       if (AccountFreeMargin() < Ask * Local_Lots * Lot_Size / Leverage) //Si nuestro margen es menor al requerido
         {
            Alert("ALERTA, NO HAY MARGEN SUFICIENTE");
            Alert("EL CAPIITAL ACTUAL ES: ", AccountFreeMargin());
            Alert("LA CANTIDAD ACTUAL DE LOTES POR OPERACION ES: ",Local_Lots);
            Alert("DETENIENDO EA...");
            ExpertRemove(); //Alertar y detener el bot
          } 
           

This part of the code is useless when I put the EA on a JPY pair, as the Ask is ussualy over 100, then if I do the math for a 0.05 lots order: (112.00 * 0.05 * 100000)/200 it returns $2,800 margin required for a 0.05 trade (Which is obviously not true) but if I set a part with something like

       if(Digits() ==3)
        {       
        if (AccountFreeMargin() < Ask * Local_Lots * Lot_Size / Leverage*100) //Si nuestro margen es menor al requerido
         {
            Alert("ALERTA, NO HAY MARGEN SUFICIENTE");
            Alert("EL CAPIITAL ACTUAL ES: ", AccountFreeMargin());
            Alert("LA CANTIDAD ACTUAL DE LOTES POR OPERACION ES: ",Local_Lots);
            Alert("DETENIENDO EA...");
            ExpertRemove(); //Alertar y detener el bot
          } 
        }
        else
        {
               if (AccountFreeMargin() < Ask * Local_Lots * Lot_Size / Leverage) //Si nuestro margen es menor al requerido
         {
            Alert("ALERTA, NO HAY MARGEN SUFICIENTE");
            Alert("EL CAPIITAL ACTUAL ES: ", AccountFreeMargin());
            Alert("LA CANTIDAD ACTUAL DE LOTES POR OPERACION ES: ",Local_Lots);
            Alert("DETENIENDO EA...");
            ExpertRemove(); //Alertar y detener el bot
          } 
           }
           

It would be just lik a hot fix, becuase if I put the EA on XAUUSD for example, it has 3 digits too so the formula would be useless, any ideas on how to fix this?


I took the code from here: https://wetalktrade.com/money-management-lot-sizing-mql-tutorial/

Reason: