Position Management — Protection Layers

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:

  1. After reaching Inp_NegBE_Trigger_Points (5,000 pts) profit → activates is_breakeven2_set = true
  2. If the position retraces to Inp_NegBE_Level_Points (e.g. −3,000 pts) → position is closed
  3. 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:

  1. Checks the BB (using a separate, wider deviation of Inp_E_BB_TP_Deviation = 3.0)
  2. If price breaches the outer BB band (strong trend confirmed!) → TP is pushed further by Inp_E_TP_Extension_Points (500 pts)
  3. Can be extended multiple times — no limit on the number of extensions

Log: "Position #123: Dynamic TP Extended by 500 points!"

Zarządzanie pozycjami — warstwy ochrony

7. Zarządzanie pozycjami — warstwy ochrony

ManagePositions() wykonuje się na KAŻDYM ticku dla każdej otwartej pozycji. Warstwy ochrony są stosowane w ściśle określonej kolejności priorytetów.

Warstwa 0: Virtual Stop Loss (najwyższy priorytet — awaryjne cięcie)

Nie jest to SL wysłany do brokera — to wewnętrzna kontrola poziomu straty:

active_cap = base_capital (zależnie od trybu)
pos_profit = zysk + swap + prowizja
max_loss = active_cap × Inp_Virtual_SL_Percent / 100

Jeśli |pos_profit| >= max_loss → natychmiastowe zamknięcie pozycji

Log: "VIRTUAL SL HIT! Position #123 loss (-45.23) exceeded 4.00% of Active Capital (1132.50). Closing immediately."

To zabezpieczenie działa jako ostatnia linia obrony przed katastroficzną stratą, niezależnie od spreadu, poślizgu czy warunków brokera.

Warstwa 0.5: Profit Lock (agresywne trailing bliskie TP)

Aktywuje się gdy cena zbliży się do TP na odległość Inp_E_PL_Trigger_Points (500 pkt):

  • Włącza agresywny trailing SL oparty o Inp_E_PL_TSL_Points (200 pkt) od aktualnej ceny
  • SL przesuwa się tylko w kierunku zysku (ratchet — nigdy wstecz)
  • Chroni wypracowany zysk tuż przed osiągnięciem TP

Log: "Position #123: PROFIT LOCK Activated!"

Warstwa 1: Physical TSL BB (Bollinger Band Trailing SL)

Dwufazowy mechanizm:

  • Faza 1: Standard Hard SL wysłany do brokera przy otwarciu
  • Faza 2: Aktywuje się gdy profit_in_points >= Inp_E_ProfitToActivateBB (1500 pkt)

Po wejściu w Fazę 2:

  • BUY: SL = MAX(BB_Lower, open_price + floor_dist) — podąża za dolnym pasmem BB (nigdy poniżej otwarcia + podłoga)
  • SELL: SL = MIN(BB_Upper, open_price - floor_dist) — podąża za górnym pasmem BB

BB TSL ma wyższy priorytet niż Hard BE — gdy Faza 2 aktywna, Hard BE jest ignorowane.

Log: "Position #123 entered Phase 2 (TSL BB)."

Warstwa 2: Hard Break-Even (klasyczny BE)

Przenosi fizyczny SL do (lub powyżej) ceny otwarcia gdy zysk osiągnie Inp_HardBE_Trigger_Points (200 pkt):

BUY: new_SL = open_price + (Inp_HardBE_Level_Points × _Point)  // np. +100 pkt
SELL: new_SL = open_price - (Inp_HardBE_Level_Points × _Point)

SL jest przesuwany tylko w kierunku zysku. Po jednorazowej aktywacji flaga is_breakeven1_set = true — Hard BE nie jest powtarzany.

Log: "Position #123: Hard Break-Even set at +100 pts."

Warstwa 3: Virtual Negative Break-Even

Innowacyjny mechanizm pozwalający pozycji „oddychać” po osiągnięciu pewnego zysku:

  1. Po osiągnięciu Inp_NegBE_Trigger_Points (5000 pkt) zysku → aktywuje się is_breakeven2_set = true
  2. Jeśli pozycja cofnie się do poziomu Inp_NegBE_Level_Points (np. -3000 pkt) → pozycja jest zamykana
  3. Działa wyłącznie gdy Hard BE NIE jest jeszcze ustawiony

Sens: pozycja osiągnęła duży zysk (5000 pkt), system „pozwala” jej wrócić do -3000 pkt zanim ją zatnie. Drastycznie zmniejsza liczbę przedwcześnie zamkniętych trendów.

Log: "Position #123: Virtual Negative BE Activated." → "Position #123: Closed by Virtual Negative BE."

Dynamic Take Profit (rozszerzanie TP przy silnym trendzie)

Gdy cena zbliży się do TP na Inp_E_PL_Trigger_Points:

  1. Sprawdza BB (z osobnym szerszym odchyleniem Inp_E_BB_TP_Deviation = 3.0)
  2. Jeśli cena przebija zewnętrzne pasmo BB (silny trend!) → TP jest przesuwany dalej o Inp_E_TP_Extension_Points (500 pkt)
  3. Można wielokrotnie rozszerzać — bez limitu liczby rozszerzeń

Log: "Position #123: Dynamic TP Extended by 500 points!"