| [← MQLArticles](../README.md)
|
|
|
| # Account/RM
|
|
|
| Risk management library: position sizing, SL/TP calculation and account-level loss/profit limits. Part of the `Account` module (`namespace TSN`).
|
|
|
| ## 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.
|
| - 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`.
|
|
|
| ## Basic usage
|
|
|
| ### Risk management
|
| ```mql5
|
| 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);
|
| if(!result.ok)
|
| return INIT_FAILED; // see TSN::CErrorTrace::FormatTrace(msg)
|
| risk = result.ptr;
|
| risk.SetLossProfitManager(g_loss_profit_manager);
|
| CAutoCleaner::AddPtr(risk);
|
|
|
| // Max loss / profit limits (each AddLP returns false on error, see TSN::CErrorTrace::FormatTrace(msg))
|
| 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
|
| 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);
|
|
|
| // Stop loss, then lot
|
| risk.SetStopLoss(_Symbol, 100);
|
|
|
| // And more: GetPositionsTotal, CloseAllPositions...
|
| ```
|
|
|
| > Only the most representative classes are shown here. See [LossProfit/README.md](./LossProfit/README.md) for the max loss/profit inheritance structure.
|
|
|
| > 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.
|
|
|
| > Compile-time switches (extra logs, optional behaviors, reserves): see [DEFINES.md](../DEFINES.md).
|