2026-10-27 18:07:28 -05:00
[← MQLArticles ](../README.md )
2026-10-30 09:44:06 -05:00
# Account/RM
2026-10-27 18:07:28 -05:00
2026-10-30 09:44:06 -05:00
Risk management library: position sizing, SL/TP calculation and account-level loss/profit limits. Part of the `Account` module (`namespace TSN` ).
2026-10-27 18:07:28 -05:00
## Main features
- Lot size and SL/TP calculation based on account risk (`CRiskManagemet` ).
- Personal and prop-firm risk profiles (`CRiskManagemetPersonal` , `CRiskManagemetPropFirm` ).
- Max loss / max profit limits (daily, weekly, monthly, since peak) with automatic position closing — see [LossProfit ](./LossProfit/README.md ) for the full inheritance model.
2026-10-30 09:44:06 -05:00
- Creators that build the right instance from the selected mode: `CRiskManagementCreator` (returns a `MQLArtRMCreatorRes` ).
> OCO orders and `COrderGestor` live in `Account/OrderUtils`; `CTicketsTable` and the account status in `Account/MoneyStatus`.
2026-10-27 18:07:28 -05:00
## Basic usage
### Risk management
``` mql5
2026-10-30 09:44:06 -05:00
TSN : : CRiskManagemet * risk ;
TSN : : CLossProfitManager g_loss_profit_manager ; // owned by the EA, shared with the risk
// In OnInit: the creator returns the right instance (personal / prop firm)
TSN : : CRiskManagementCreator manager ;
manager . SetPropirm ( InpPropFirmBalance ) ;
TSN : : MQLArtRMCreatorRes result = manager . GetRiskPointer ( InpRiskMode , InpMagic , InpGetMode ) ;
2026-11-05 19:07:00 -05:00
if ( ! result . ok )
2026-11-06 10:40:44 -05:00
return INIT_FAILED ; // see TSN::CErrorTrace::FormatTrace(msg)
2026-10-30 09:44:06 -05:00
risk = result . ptr ;
risk . SetLossProfitManager ( g_loss_profit_manager ) ;
CAutoCleaner : : AddPtr ( risk ) ;
2026-11-06 10:40:44 -05:00
// Max loss / profit limits (each AddLP returns false on error, see TSN::CErrorTrace::FormatTrace(msg))
2026-10-30 09:44:06 -05:00
risk . AddLP ( InpPercentageOrMoneyMdlInput , InpAppliedPercentagesMdl , InpModeCalculationMdl , TSN : : LP_MDL , true ) ;
risk . EndAddProfitLoss ( ) ; // call it once all the limits are added
// In OnTick: check if a limit was hit
2026-10-27 18:07:28 -05:00
if ( g_loss_profit_manager . MaxLossIsSuperated ( ) )
{
risk . CloseAllPositions ( ) ;
CanTrade = false ;
}
// Lot size
double entry_price = 1000.0 ;
double l = risk . GetLote ( ORDER_TYPE_BUY , entry_price , 100 , 0 ) ;
2026-10-30 09:44:06 -05:00
// Stop loss, then lot
risk . SetStopLoss ( _Symbol , 100 ) ;
2026-10-27 18:07:28 -05:00
2026-10-30 09:44:06 -05:00
// And more: GetPositionsTotal, CloseAllPositions...
2026-10-27 18:07:28 -05:00
```
> Only the most representative classes are shown here. See [LossProfit/README.md](./LossProfit/README.md) for the max loss/profit inheritance structure.
2026-11-01 15:00:25 -05:00
2026-11-06 10:40:44 -05:00
> Errors: the methods that can fail return `false` and record the cause in the global error trace (`TSN::CErrorTrace`). Read it with `TSN::CErrorTrace::FormatTrace(msg)`: it prints the chain of errors from the top layer down to the root cause.
2026-11-05 19:07:00 -05:00
2026-11-01 15:00:25 -05:00
> Compile-time switches (extra logs, optional behaviors, reserves): see [DEFINES.md](../DEFINES.md).