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 for the full inheritance model.
- Creators that build the right instance from the selected mode:
CRiskManagementCreator(returns aMQLArtRMCreatorRes).
OCO orders and
COrderGestorlive inAccount/OrderUtils;CTicketsTableand the account status inAccount/MoneyStatus.
Basic usage
Risk management
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 for the max loss/profit inheritance structure.
Errors: the methods that can fail return
falseand record the cause in the global error trace (TSN::CErrorTrace). Read it withTSN::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.