주시
1
0
포크
이미 MQLArticles을(를) 포크했습니다
0
원본 프로젝트 nique_372/MQLArticles
MQLArticles/PosMgmt/README.md
Nique_372 Nique_372 1b2572d6ea
2026-11-06 10:40:44 -05:00
81 라인
3.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 (`CBreakEvenCreator` builds a `CBreakEvenBase`: `CBreakEvenSimple`, `CBreakEvenAtr` or `CBreakEvenRR`).
- Partial closes by volume/TP percentage across multiple stages (`CBasicPartials`).
- 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.
## Basic usage
### Breakeven
```mql5
//--- 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);
if(!be_res.ok)
return INIT_FAILED; // see TSN::CErrorTrace::FormatTrace(msg)
g_breakeven = be_res.ptr;
CAutoCleaner::AddPtr(g_breakeven); // auto-deleted on deinit
//--- In OnTick
g_breakeven.BreakEven();
```
### Partial closes
```mql5
TSN::CBasicPartials g_partials; // global
// In OnInit
if(!g_partials.Init(InpMagic, _Symbol, InpVolumePercentageToClose, InpTpPercentagesForPartials))
return INIT_FAILED; // see TSN::CErrorTrace::FormatTrace(msg)
// In OnTick
g_partials.CheckTrackedPositions();
```
### Conditional partial closes
```mql5
CConditionalPartials g_partials; // global instance (no pointer, not deleted by hand)
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;
// 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
if(!g_partials.Init(config))
return INIT_PARAMETERS_INCORRECT; // see TSN::CErrorTrace::FormatTrace(msg)
}
```
> Only the most representative classes are shown here.
> 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).