2026-09-06 11:15:27 -05:00
[← MQLArticles ](../README.md )
# PosMgmt
Open position management: breakeven, partial closes by volume, and partial closes conditioned on indicators.
## Main features
2026-10-30 09:44:06 -05:00
- Breakeven by fixed points, ATR or risk/reward ratio (`CBreakEvenCreator` builds a `CBreakEvenBase` : `CBreakEvenSimple` , `CBreakEvenAtr` or `CBreakEvenRR` ).
- Partial closes by volume/TP percentage across multiple stages (`CBasicPartials` ).
2026-11-02 15:25:42 -05:00
- Conditional partial closes, triggered by a pluggable condition (e.g. RSI) instead of a fixed level (`CConditionalPartials` , configured with a `ConditionalPartialConfig` ; it can also work in constant mode, one percentage for every step) — see [ConditionalPartial ](./ConditionalPartial ) for the class hierarchy.
2026-09-06 11:15:27 -05:00
## Basic usage
### Breakeven
``` mql5
2026-10-30 09:44:06 -05:00
//--- Global (not owned by the breakeven, it is only shared)
TSN : : CAtrOptimizedZero atr_ultra_optimized ( _Period , _Symbol , 14 ) ;
TSN : : CBreakEvenBase * g_breakeven ;
//--- In OnInit: configure the breakeven values and create it
TSN : : CBreakEvenCreator be ;
be . SetBeByAtr ( InpBeAtrMultiplier , InpBeAtrMultiplierExtra , GetPointer ( atr_ultra_optimized ) ) ;
be . SetBeByFixedPoints ( InpBeFixedPointsToPutBe , InpBeFixedPointsExtra ) ;
be . SetBeByRR ( InpBeRrDbl , InpBeTypeExtraRr , InpBeExtraPointsRrOrAtrMultiplier , GetPointer ( atr_ultra_optimized ) ) ;
TSN : : BreakEvenCreatorRes be_res = be . Create ( InpTypeBreakEven , _Symbol , InpMagic ) ;
2026-11-05 19:07:00 -05:00
if ( ! be_res . 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
g_breakeven = be_res . ptr ;
CAutoCleaner : : AddPtr ( g_breakeven ) ; // auto-deleted on deinit
//--- In OnTick
g_breakeven . BreakEven ( ) ;
2026-09-06 11:15:27 -05:00
```
### Partial closes
``` mql5
2026-10-30 09:44:06 -05:00
TSN : : CBasicPartials g_partials ; // global
// In OnInit
if ( ! g_partials .Init ( InpMagic , _Symbol , InpVolumePercentageToClose , InpTpPercentagesForPartials ) )
2026-11-06 10:40:44 -05:00
return INIT_FAILED ; // see TSN::CErrorTrace::FormatTrace(msg)
2026-10-30 09:44:06 -05:00
// In OnTick
g_partials . CheckTrackedPositions ( ) ;
2026-09-06 11:15:27 -05:00
```
### Conditional partial closes
``` mql5
2026-11-02 15:25:42 -05:00
CConditionalPartials g_partials ; // global instance (no pointer, not deleted by hand)
2026-09-06 11:15:27 -05:00
if ( InpPartialsIsEnable )
{
// Atr creation
g_atr = new CAtr ( ) ;
g_atr . Create ( _Period , _Symbol , 14 , true , true ) ;
g_atr . SetAsSeries ( true ) ;
CAutoCleaner : : AddPtr ( g_atr ) ; // auto-deleted on deinit
g_partials . AddLogFlags ( InpPartialsLogLevel ) ;
// RSI condition
CConditionalPartialsIndRsi * rsi_condition = new CConditionalPartialsIndRsi ( ) ;
if ( ! rsi_condition .Init ( InpPartialsRsiTimeframe , _Symbol , InpPartialsRsiPeriod , InpPartialsRsiOverBoughtLevel , InpPartialsRsiOverSoldLevel ) )
return INIT_PARAMETERS_INCORRECT ;
ConditionalPartialConfig config ;
config . condition = rsi_condition ;
config . min_distance_to_close_pos = CreateDiffptr ( MODE_DIFF_BY_ATR , _Symbol , g_atr , 0 , InpPartialsMinDistanceInAtrMul ) ;
config . str_percentage_volume_to_close = InpPartialsVolumeToClosePercentage ;
config . magic_number = InpMagic ;
2026-11-02 15:25:42 -05:00
// config.constant = true; // constant mode: one percentage on every step
// config.forze_to_close = true; // constant mode: close the position if the lot is below the minimum volume
2026-09-06 11:15:27 -05:00
if ( ! g_partials .Init ( config ) )
2026-11-06 10:40:44 -05:00
return INIT_PARAMETERS_INCORRECT ; // see TSN::CErrorTrace::FormatTrace(msg)
2026-09-06 11:15:27 -05:00
}
```
> Only the most representative classes are shown here.
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).