7. Position Management — Protection Layers
ManageAllPositions() executes on EVERY tick for every open position. Protection layers are applied in a strict priority order.
Layer 0: Virtual Stop Loss (Highest Priority — Emergency Cut)
This is not an SL sent to the broker — it is an internal loss-level monitor:
active_cap = base_capital (depending on mode)
pos_profit = profit + swap + commission
max_loss = active_cap × Inp_Virtual_SL_Percent / 100
If |pos_profit| >= max_loss → immediate position close
Log: "VIRTUAL SL HIT! Position #123 loss (-45.23) exceeded 4.00% of Active Capital (1132.50). Closing immediately."
This safeguard acts as the last line of defence against a catastrophic loss, regardless of spread, slippage or broker conditions.
Layer 0.5: Profit Lock (Aggressive Trailing Near TP)
Activates when price approaches TP within Inp_E_PL_Trigger_Points (500 pts):
- Enables an aggressive trailing SL based on
Inp_E_PL_TSL_Points (200 pts) from the current price
- SL only moves in the direction of profit (ratchet — never backwards)
- Protects accumulated profit just before TP is hit
Log: "Position #123: PROFIT LOCK Activated!"
Layer 1: Physical TSL BB (Bollinger Band Trailing SL)
A two-phase mechanism:
- Phase 1: Standard Hard SL sent to the broker at the time of opening
- Phase 2: Activates when
profit_in_points >= Inp_E_ProfitToActivateBB (1,500 pts)
Once Phase 2 is active:
- BUY: SL =
MAX(BB_Lower, open_price + floor_dist) — follows the lower BB band (never below open + floor)
- SELL: SL =
MIN(BB_Upper, open_price - floor_dist) — follows the upper BB band
TSL BB has higher priority than Hard BE — once Phase 2 is active, Hard BE is ignored.
Log: "Position #123 entered Phase 2 (TSL BB)."
Layer 2: Hard Break-Even (Classic BE)
Moves the physical SL to (or above) the entry price when profit reaches Inp_HardBE_Trigger_Points (200 pts):
BUY: new_SL = open_price + (Inp_HardBE_Level_Points × _Point) // e.g. +100 pts
SELL: new_SL = open_price - (Inp_HardBE_Level_Points × _Point)
SL is only moved in the direction of profit. Once activated, the flag is_breakeven1_set = true — Hard BE is never repeated.
Log: "Position #123: Hard Break-Even set at +100 pts."
Layer 3: Virtual Negative Break-Even
An innovative mechanism that allows a position to „breathe” after achieving a significant profit:
- After reaching
Inp_NegBE_Trigger_Points (5,000 pts) profit → activates is_breakeven2_set = true
- If the position retraces to
Inp_NegBE_Level_Points (e.g. −3,000 pts) → position is closed
- Active only when Hard BE has NOT yet been set
Rationale: the position achieved a large profit (5,000 pts), so the system „allows” it to retrace to −3,000 pts before closing it. This dramatically reduces the number of prematurely exited trends.
Log: "Position #123: Virtual Negative BE Activated." → "Position #123: Closed by Virtual Negative BE."
Dynamic Take Profit (TP Extension on Strong Trend)
When price approaches TP within Inp_E_PL_Trigger_Points:
- Checks the BB (using a separate, wider deviation of
Inp_E_BB_TP_Deviation = 3.0)
- If price breaches the outer BB band (strong trend confirmed!) → TP is pushed further by
Inp_E_TP_Extension_Points (500 pts)
- Can be extended multiple times — no limit on the number of extensions
Log: "Position #123: Dynamic TP Extended by 500 points!"