Calculating lot size for particular risk percentage based on deposit currency

 

Hey all!

I am trying to create a function that returns the appropriate lot size according to a required risk % of the total balance (in deposit currency) available.  

So far I managed to code these 2  functions by looking at different sources available online (forums + source base in meta editor) by getting a rough understanding of whats going on. I noticed an issue though. Both of these functions have these issues. I was testing this on GBPUSD currency on strategy tester however my deposit currency is AUD. 

Using 3000 AUD as starting balance, my functions are both actually returning the lot size assuming the balance is 3000 USD, not AUD which is the deposit currency. I could verify this by using the myfxbook lot size calculator which is a great website to help me size my trades.


Can anyone help me fix this issue to get the code to actually get the lot size based on risk of deposit currency not USD as it currently does? Thank you!!

double calculateLotSize(const string symbl,const double cptl,const double sl_pts,const double riskPerc)
{

    double capital= AccountInfoDouble(ACCOUNT_BALANCE);//cptl ;
    string currency = AccountInfoString(ACCOUNT_CURRENCY);
    Comment("Account Currency: "+currency);

   
    double PipValue = (((SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_VALUE))*point)/(SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE)));
  
    double lots = (riskPerc/100) * capital / (PipValue * (sl_pts/10));
  
  
    // Truncate lot quantity to 2 decimal digits without rounding it
    lots = floor(lots * 100) / 100;
  
    Print("Lot size = " + DoubleToString(lots,2));
  
   
   return (lots);  
   
}

double calculateLotSize2(const string symbl,const double cptl,const double sl_pts,const double riskPerc)
{

    double capital= AccountInfoDouble(ACCOUNT_BALANCE); //cptl parameter not being used atm ;
    string currency = AccountInfoString(ACCOUNT_CURRENCY);
    Comment("Account Currency: "+currency);

    string symbol=symbl;
   double sl_points=sl_pts;
   double risk=riskPerc;

//---, 
  
  double TickSize  = SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE);
    double TickValue = SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE);
   double PointSize = SymbolInfoDouble(Symbol(),SYMBOL_POINT);
   double PointValue= TickValue * PointSize / TickSize;

//---
  
   double risk_money = risk/100.0*capital;
   double lots = risk_money/sl_points/PointValue;
   double lotstep = SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);
   lots = NormalizeDouble(lots/lotstep,0)*lotstep;

//--- Print the calculated lots and risk used
   Print("Lot size = " + DoubleToString(lots,2));
   Print("Risked amount = " +DoubleToString(risk_money,2));
   
   
   return (lots);  
   
}


PS.

Also if anyone could point me to good resources to understand the below, (I only have rough understanding not complete understanding)

SYMBOL_POINT
SYMBOL_VOLUME_STEP
SYMBOL_TRADE_TICK_VALUE
SYMBOL_TRADE_TICK_SIZE

that would be great. The MetaEditor F1 help reference does not explain it properly / adequately... atleast I felt that way. So it was very difficult for me to understand all that. Also I am sort of new to MQL5 , so if you have any tips to improve the above code in other ways that comes with experience only, really appreciate it.

 
TLDR - Issue- Current code calculates lot size in USD instead of deposit currency based on risk %.
 
anyone?
 

Why do you try yourself what already exists?

Search for lot size: https://www.mql5.com/en/search#!keyword=lot%20size&module=mql5_module_codebase

400 examples only in the code base - pick one and copy it, they are made even for you :)

Reason: