Please do not have the MQL4 version?
bool UseMaximumPercentageatRisk = true; double MaximumPercentageatRisk =1;
bool IsMicroAccount = true ; //for accounts allowing 0.01 lots
double GetLots(double lotsize_bn) { int tmpdecimal=1; if(IsMicroAccount) { tmpdecimal=2; } double old_lot=lotsize_bn; if((NormalizeDouble(AccountFreeMargin()*(MaximumPercentageatRisk/100)/1000.0,tmpdecimal)<lotsize_bn) && UseMaximumPercentageatRisk) { lotsize_bn=NormalizeDouble(AccountFreeMargin()*(MaximumPercentageatRisk/100)/1000.0,tmpdecimal); if(lotsize_bn<MarketInfo(Symbol(),MODE_MINLOT)) { lotsize_bn=MarketInfo(Symbol(),MODE_MINLOT); Print("RPTrade-Lot adjusted from ",old_lot," to minimum size allowed by the server of ",lotsize_bn," but it DOES NOT comply with Maximum Risk condition. User interaction is required!"); } else { Print("RPTrade-Lot adjusted from ",old_lot," to ",lotsize_bn," to comply with Maximum Risk condition. Each trade can risk only ",MaximumPercentageatRisk,"% of free margin."); } } return (lotsize_bn); }
Should be something like this ;-)
R.
Maximum Percentage of Equity Risk:
This code allows you to set a maximum percentage of equity risk.
It checks if asked buy/sell lots are fitting the risk. If not, lots are automatically adjusted to fit the risk that was set.
Author: Remi PASSANELLO