How to Calculate risk percentage of a closed trade?

 

Calculating a Risk Percentage before opening a new trade is popular in the public domain, but getting a percentage of balance used after the trade has been opened seems difficult for me to calculate.


Here's what I came up with, but got something around 3.7k(3,700+).


//+------------------------------------------------------------------+
double CalculateRiskPercentage(double balance, double entryPrice, double stopLoss, double volume, double leverage)
{
    double grossRiskAmount = (entryPrice - stopLoss) * volume;
    double effectiveBalance = balance / leverage;
    double riskPercentage = (grossRiskAmount / effectiveBalance) * 100;
    return riskPercentage;
}

void OnStart()
{
    double balance = AccountInfoDouble(ACCOUNT_BALANCE);  // Get the account balance
    double entryPrice = 50.0;  // Entry price
    double stopLoss = 48.0;  // Stop loss
    double volume = 100;  // Trade volume
    int leverage = AccountInfoInteger(ACCOUNT_LEVERAGE);  // Get the account leverage

    double riskPercentage = CalculateRiskPercentage(balance, entryPrice, stopLoss, volume, leverage);
    Print("Risk Percentage: ", riskPercentage);
}

Has anyone ever tried to calculate risk percentage of a closed trade before or can help set my head at the right edge?


I'm thankful in advance.

 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 

Forum on trading, automated trading systems and testing trading strategies

SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE) sometimes zero

Fernando Carreiro, 2022.08.23 17:41

You can! These are the steps I take. I supply the function with a lot size equal to the “Max Lot Size” allowed for the symbol in question, then calculate the ratio needed to achieve the fractional risk that I wish to apply, to get the correct volume for the order. I then align that with the “Lot Step” and finally check it against both the maximum and minimum allowed lots for the symbol.

The reason I use the “maximum” lots instead of just “1.0” lots as a reference value is because there is no guarantee that the value of 1.0 is within the minimum and maximum values allowed. Given that using 1.0, or the maximum, gives equivalent results anyway (by using the ratio method), I choose to use the “max lots” as the reference point which also offers the most precision for the calculation.

Something like this ...

// This code will not compile. It is only a example reference

if( OrderCalcProfit( eOrderType, _Symbol, dbLotsMax, dbPriceOpen, dbPriceStopLoss, dbProfit ) )
{
   dbOrderLots = fmin( fmax( round( dbRiskMax * dbLotsMax / ( -dbProfit * dbLotsStep ) )
               * dbLotsStep, dbLotsMin ), dbLotsMax ); 
      
   // the rest of the code ...
};
 
Thanks, your answer gave me an idea that helped solve my headache.