MQLArticles/PosMgmt/README.md
2026-09-06 11:15:27 -05:00

66 lines
2.5 KiB
Markdown

[← MQLArticles](../README.md)
# PosMgmt
Open position management: breakeven, partial closes by volume, and partial closes conditioned on indicators.
## Main features
- Breakeven by fixed points, ATR or risk/reward ratio (`CBreakEven`, `CBreakEvenSimple`, `CBreakEvenAtr`, `CBreakEvenRR`).
- Partial closes by volume/TP percentage across multiple stages (`CPartials`).
- Conditional partial closes, triggered by a pluggable condition (e.g. RSI) instead of a fixed level (`CConditionalPartials`, `CConditionalPartialsFactory`) — see [ConditionalPartial](./ConditionalPartial) for the class hierarchy.
## Basic usage
### Breakeven
```mql5
//--- Atr
atr_ultra_optimized.SetVariables(_Period, _Symbol, 0, 14);
atr_ultra_optimized.SetInternalPointer();
//--- Configure the breakeven values so it can be used
break_even.SetBeByAtr(InpBeAtrMultiplier, InpBeAtrMultiplierExtra, GetPointer(atr_ultra_optimized));
break_even.SetBeByFixedPoints(InpBeFixedPointsToPutBe, InpBeFixedPointsExtra);
break_even.SetBeByRR(InpBeRrDbl, InpBeTypeExtraRr, InpBeExtraPointsRrOrAtrMultiplier, GetPointer(atr_ultra_optimized));
break_even.SetInternalPointer(InpTypeBreakEven);
break_even.obj.AddLogFlags(InpLogLevelBe);
```
### Partial closes
```mql5
g_partials.AddLogFlags(InpLogLevelPartials);
g_partials.Init(InpMagic, _Symbol, InpVolumePercentageToClose, InpTpPercentagesForPartials);
```
### Conditional partial closes
```mql5
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
// Dynamic instance from the factory
g_partials = CConditionalPartialsFactory::Create(InpPartialsClassManagerType);
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;
if(!g_partials.Init(config))
return INIT_PARAMETERS_INCORRECT;
else
CAutoCleaner::AddPtr(g_partials);
}
```
> Only the most representative classes are shown here.