Pyramiding (Position Scaling)

14. Pyramiding (Position Scaling)

Inp_Pyramid_Multiplier     = 3.0   // Scale multiplier (probe → main)
Inp_Pyramid_Max_Lots       = 2.0   // Hard cap: max lots for any single position
Inp_Pyramid_OnlyProfitable = true  // Pyramid only if last position is in profit

Logic:

  • No open positions: lots = target_lots / 3.0 — a small probe is opened (1/3 of normal)
  • Position exists: lots = last_volume × 3.0 — scaling up to full size
  • If OnlyProfitable = true and the last position is in loss → pyramid is blocked

Log: "Pyramid blocked: last position not in profit." / "LOT CAP: 4.50 -> 2.00"

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!"

Architecture & Module Connections

1. Architecture & Module Connections

The system is composed of five header files included into the compiled main file _AHS_Main.ex5:

_AHS_Main.ex5
  ├── AHS_Inputs.mqh      → All input parameters, enumerations, global state variables
  ├── AHS_Filters.mqh     → Market filters (The Shield): Macro Matrix, Impulse, Channel, ADX/MA, SmartDI, MTF, 3Candles
  ├── AHS_Strategy.mqh    → Multi-Core Engine: position sizing, order execution
  ├── AHS_Management.mqh  → Position management: TSL BB, Hard BE, Neg BE, Dynamic TP, EOD Terminator
  └── AHS_Panel.mqh       → Graphical information panel: status, MTF dashboard, positions list

OnTick() Flow — Step by Step

Every incoming tick passes through a strictly defined sequence in the main loop:

OnTick()
  1.  License check (CLicenseManager.IsExpired)
  2.  Session schedule check (IsScheduleAllowed)
  3.  EOD Terminator (CheckForEODClose) → emergency close all positions
  4.  Daily P/L update and daily reset
  5.  Account Protection checks:
        → DailyLossReached?    → block new orders
        → DrawdownFreeze?      → block new orders
        → DailyTargetReached?  → block new orders
  6.  Equity Trailing Lock (master equity trailing SL)
  7.  Institutional Impulse Detection (CheckInstitutionalImpulse)
  8.  Dynamic Channel Update (UpdateDynamicChannel) every X hours
  9.  News Filter → freeze before/after news events
 10.  ManageAllPositions() → manage all open positions
 11.  Cooldown check + position count limit check
 12.  EvaluateStrategies() → Multi-Core Engine searches for a signal
 13.  Information panel update

All stages are independent — position management (step 10) runs regardless of whether new signals are being sought (step 12). Even when new orders are blocked, open positions are always managed.