Function to calculate lot size based on risk per deposit - MetaTrader 5 Expert - MT4/MT5 Resources
Price_open - opening price;
Price_stoploss - price of stop loss level;
risk_percent_equity - risk per trade (expressed as a percentage of deposit);
Double the risk-based trading volume ( double the opening price, double the price_stop loss, double the risk_percentage_equity) { Double volume = {}; Double margin risk = double account information ( account assets ) *risk_percent_equity / 100 ; double tick_size = symbolinfo double ( symbol (), SYMBOL_TRADE_TICK_SIZE ); doubletick_value = symbolinfodouble ( symbol (), SYMBOL_TRADE_TICK_VALUE ); double minimum lot size = symbolinfo double ( symbol (), SYMBOL_VOLUME_MIN ); Double maximum lot size = SymbolInfoDouble ( symbol (), SYMBOL_VOLUME_MAX ); Double Lot Step = SymbolInfoDouble ( symbol (), SYMBOL_VOLUME_STEP ); Double delta_stoploss = mathematical antibody (opening price - stop loss price); Double margin_risk_1lot = delta_stoploss / scale size * scale value; Trading volume = margin risk/margin risk 1 lot; Volume = mathematical floor (trading volume/lot size) * min_lot; if (volume == 0 ) Trading volume = minimum lot size; if (trading volume > max lot size) Trading volume = maximum lot size; return volume; }
EA trading example for random trading through this function
Enter the integer stop loss point = 100 ; //The number of stop loss points Enter double the risk percentage equity = 1 ; //Risk as a percentage of deposit //+------------------------------------------------------------------+ ENUM_ORDER_TYPE Order Type = Order Type Buy ; Double Equity Open = {}; Double Expected Profit = {}; Boolean Open Position = False ; //+----------------------------------------------------------------------------------+ Blank check () { if (!pos_open) { Mql transaction request requirements; MqlTradeCheck result check; Mql trading results result_trade; zero memory (required); zero_memory (result_check); nilmem (result_transaction); double tick_size = symbolinfo double ( symbol (), SYMBOL_TRADE_TICK_SIZE ); if (order type == order type buy ) Order type = order type_sell ; Other order type = order type purchase ; if (order type == order type buy ) { RequestPrice = SymbolInfoDouble ( symbol (), SYMBOL_ASK ); Request Type = Order Type Purchase ; request.sl = request.price - tick_size * stoploss_points; request.tp = request.price + tick_size * stoploss_points; } other { RequestPrice = SymbolInfoDouble ( symbol (), SYMBOL_BID ); Request Type = Order Type_Sell ; request.sl = request.price + tick_size * stoploss_points; request.tp = request.price - tick_size * stoploss_points; } Double trading volume = GetVolByRisk(request.price, request.sl,risk_percent_equity); request.action = TRADE_ACTION_DEAL ; request.symbol = symbol (); Request volume = volume; request.bias = 5 ; Expected profit = account information double ( account assets ) *risk_percent_equity / 100 ; Equity_opening= account information double ( account assets ); if ( ordercheck (request,resultcheck)) { orderSend (request,result_transaction); openposition= true ; } } } //+------------------------------------------------------------------+ Blank OnlineTrade ( const MqlTradeTransaction &trans, Constant Mql Transaction Request & Requirement, Constant Mql trading results & results) { if (trans.type == TRADE_TRANSACTION_DEAL_ADD ) { Historical Transaction Selection (Transaction); ENUM_DEAL_ENTRY Transaction Entry = ( ENUM_DEAL_ENTRY ) historical transaction acquisition integer (cross-transaction, DEAL_ENTRY ); if (transaction entry == DEAL_ENTRY_OUT ) { print ( "==========" ); print ( "volume=" , trans volume); print ( "equity open=" , equity open); print ( "equity closed=" , account information double ( account assets )); print ( "Expected profit=+/-" , expected profit); > print( "actual profit=" , historical trades get double (cross trade, trade profit )); print ( "==========" ); open position = wrong ; } } } //+------------------------------------------------------------------+ Double get volume by risk ( double opening price, double price_stop, double risk_percentage_equity) { double volume={}; double margin risk=account information ( account assets ) *risk_percent_equity / 100 ; Double tick size = symbol info double (symbol(), SYMBOL_TRADE_TICK_SIZE ); Double tick value = symbol info double ( symbol (), SYMBOL_TRADE_TICK_VALUE ); Double minimum lot size = symbol info double ( symbol (), SYMBOL_VOLUME_MIN ); double the maximum lot size = symbol information double ( symbol (), SYMBOL_VOLUME_MAX ); double the lot step = symbol information double ( symbol (), SYMBOL_VOLUME_STEP ); double the delta_stoploss = mathematical antibody (opening price - stop loss price); double the margin_risk_1lot = delta_stoploss / tick size * tick value; trading volume = margin risk / Margin risk 1 lot; volume = mathematical floor (trading volume/lot step size) * min_lot; if (volume == 0 ) trading volume = minimum lot; if (trading volume > maximum lot) trading volume = maximum lot; return volume; } //+--------------------------------------------------------------------------------+
The indicator uses the "quarter theory" - a technical analysis concept that divides price movements into four quarters around a central base level. It is designed to handle multiple asset types (FX, stocks, commodities, etc.) and provides visualization of quarterly levels on charts.
Example of adding a button for an advisor. In this example, a button has been implemented to close all active positions for all instruments. In addition to the button event handling function, a method for closing positions relative to the symbol name and a method for calculating the position quantity relative to the symbol name are also implemented.
Professional closing panel with 6 smart filters. Close all positions by type, by symbol or by P&L. Real-time profit and loss display. Ideal for emergency exits and risk management. Includes security confirmation.
Record quotes in real quote mode and read them in math mode and call your strategy on each quote.
Attachment download
📎 exp_get_vol.mq5 (8.38 KB)
Source: MQL5 #48222
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •