Calculating lot size based on risk taking into account stoploss

 
Anyone have a good formula to calculate lot size based on risk taking into account the stoploss?
 
Example in this thread -> https://www.mql5.com/en/forum/123914.
 
dwmcqueen:
Anyone have a good formula to calculate lot size based on risk taking into account the stoploss?

Not sure if this is exactly what you want but I calculate lot sized based on a risk parameter using ATR. You could use any other indicator/calculation based on the same idea. Then this can be used to calculate the SL TP also.

Here is the pseudo code:

External Parameters:
Equity Risk Per Trade = 0.02                                      // 2% Risk
TP % Per Trade = 2.50                                             // 250% of the Risk

Risk In Pips = 3.0 * ATR(14)                                      // 3 times the 14 period ATR
e = Equity                                                        // Get the Account Balance
Contract Size = 100,000                                           // Get the Lot Size of the Symbol we are trading

Lots = Equity Risk Per Trade * e / (Contract Size * Risk In Pips) // Example:   2.0  = 0.02 * 50,000 / (100,000 * 0.0050) 
                                                                  // Lots may need to be normalised to a specific decimal place.
SL = Risk In Pips                                                 // StopLoss based on the ATR
TP = Risk In Pips * TP % Per Trade                                // StopLoss as a multiple of the ATR

Hope it helps.

Pete

Reason: