New free code from MQL5: indicators, EAs, and scripts for traders.

Parameters:
-
riskPercent - percent of the current balance you want to risk
-
sl - the value of the StopLoss for the given order
- price - the open price of the order
-
error - refereced parameter. an error code, that is returned by the function
Note: I can't say that it works correctly. I've checked it approximately. So your comments are appreciated.
Here is an example:
Trade Renko charts automatically using the advanced Renko Expert Advisors. Clean trends, better entries. Discover them here.
int riskPercentage = 3, error; double sl = Ask - 40 * Point, openPrice = Ask; double lots = GetNeccessaryLotsWithRisk(riskPercentage, sl, openPrice, error); if (CompareDoubles(lots, 0)) { switch (error) { case LOTS_TOO_BIG: // lots are bigger than max lots Alert("Risk percentage is too big"); break; case LOTS_TOO_SMALL: // lots are smaller than min lots Alert("Risk percentage is too small"); break; default: // formula's result =) Alert("It seems like you've got $0 or stoploss is too far"); break; } }
Level up your trading with professional RobotFX expert advisors and indicators. Visit robotfx.org for proven MT4/MT5 tools.
8568