Maximum Percentage of Stock Risk - MetaTrader 5 Library | MT5 EA Download - MetaTrader 5 Resources
This code allows you to set the maximum percentage of equity risk.
It checks whether the requested buy/sell lot is suitable for the risk. If not, the batch is automatically adjusted to suit the set risk.
boolean use maxrisk% = true ; double maxrisk% = 25 ; #include <Trading\SymbolInfo.mqh> //+----------------------------------------------------------------------------------+ //| GetLotSize RPTrade | //+------------------------------------------------------------------+ Double the acquisition lot size ( double the number of lots) { //--- Get symbol information about the specification CSymbolInfo; Integer digitals_bn = symInfo.digits (); double point_bn = symInfo. View (); string symbol _bn= _symbol ; //--- adjust lot integer tmp decimal = 1 ; double old_lot = lot; //--- if (( Normalized Double ( Account_FREEMARKIN ) * (Maximum Risk Percent / 100 ) / 1000.0 , tmpdecimal) < Lot) && UseMaximumPercentageRisk) // Is batch fitting risky? { Lot Size = Normalized Double ( Account Info Double ( ACCOUNT_FREEMARKIN ) * (Maximum Risk Percent / 100 ) / 1000.0 , tmp Decimal); // Calculate New Lot Size if (Lot Size < Symbol Info Double (Symbol_bn, SYMBOL_VOLUME_MIN )) // Does LotSize fit the broker's minimum LotSize? { lot size = symbolinfodouble (symbol_bn, SYMBOL_VOLUME_MIN ); //No! Set LotSize to the minimum value of broker ls print ( _symbol , "lots adjusted from" , old lot, "minimum size allowed for the server" , lot size); } other { print ( _symbol , "lots adjusted from" , old lots, "to" ,lots, "Adhere to maximum risk conditions. Only risk can be taken on each trade" ,max% risk, "Percentage of available margin." ); //Yes! if ( mathAb (LotSize/ SymbolInfoDouble (Symbol_bn, SYMBOL_VOLUME_STEP )) - MathWheel (LotSize/ SymbolInfoDouble (Symbol_bn, SYMBOL_VOLUME_STEP )))> 1.0 electron - 10 ) // Does LotSize comply with the broker's allowed steps? { Lot = Symbol_bn , SYMBOL_VOLUME_STEP ) * Normalized Double (Lot/ Symbol_bn (Symbol_bn, SYMBOL_VOLUME_STEP ), 0 ); // No! Recalculate LotSize. print ( "M——" , _symbol , "Warning: Your calculated risk lot percentage is not a multiple of the minimum step" , symbolinfo double (symbol_bn, SYMBOL_VOLUME_STEP ), ".Batch size changed to" , lot size); } } } return (lot size); }
Attachment download
📎 rptrade.mqh (2.63 KB)
Source: MQL5 #2142
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •