# Long Exit State Machine — Mermaid Diagrams

> Source: `long_exit_state_machine_v1_5_2.md`
> **Version:** v1.3.1 | **Updated:** 2026-04-18 | **Change:** Add [XMA] EXIT_MA and [XLR] EXIT_LWR as shared standard transitions across states 09/10/11 (both bar-close only), while preserving [PEM]/[PEL] counter behavior.
> Transitions labeled with sequential number (matches `long_exit_state_machine_v1_5_2.md`).
> State names show full `NN — long_NN_StateName` format.

---

## Entry Point

Entered from the Entry State Machine when a position is opened:
- (110) `TRADE_ENTRY_SUBMITTED` (t_07.04_09) from `07 — long_07_ENTRY_GATE` → `09 — long_09_YELLOW`
- (120) `ENTRY_BLOCK_CLEARED` (t_08.04_09) from `08 — long_08_ENTRY_BLOCKED` → `09 — long_09_YELLOW`

See [`long_entry_state_machine_diagrams_v2_2_0.md`] for states 00–08.

---

## Diagram — Long Exit State Machine (States 09 – 11)

> States 09–11: active position management flow.
> Transitions labeled with sequential number (matches `long_exit_state_machine_v1_5_2.md`).
> Shared [XSE] and COUNT_ self-transitions included in legend; OC and EPTC shown in diagram.

```mermaid
stateDiagram-v2
    direction TB
    [*] --> yellow

    state "09 — long_09_YELLOW" as yellow
    state "10 — long_10_GREEN" as green
    state "11 — long_11_RED" as red

    yellow --> yellow : COUNT_PREMARKET_S09 (242)
    yellow --> green : YELLOW_TO_GREEN (240)
    yellow --> red : YELLOW_TO_RED (241)
    yellow --> yellow : OFFSET_CHANGE (283)
    yellow --> [*] : EXIT_Profit_Target_Count (284)
    green --> yellow : GREEN_TO_YELLOW (250)
    green --> green : OFFSET_CHANGE (283)
    green --> [*] : EXIT_Profit_Target_Count (284)
    red --> red : COUNT_PREMARKET_S11 (261)
    red --> yellow : RED_TO_YELLOW (262)
    red --> red : OFFSET_CHANGE (283)
    red --> [*] : EXIT_Profit_Target_Count (284)
```

> **Note:** `[*]` on input = position entered from Entry State Machine via (110) or (120). `[*]` on output = return to `01 — long_01_WAIT_BEAR_TREND` via EXIT_Profit_Target_Count (t_284), EXIT_MA (t_223), or EXIT_LWR (t_233/t_234/t_235), or to `00 — long_00_IDLE` via [XSE]. COUNT_ self-transitions ([HS]/[PEM]/[PEL]) and shared [XMA]/[XLR] are documented in the standard-transition legend below.

### Shared Transition Legend (States 09–11)

| Symbol | Transition Name | Guard | Target | Applies to States |
|--------|----------------|-------|--------|-------------------|
| [XSE] | EXIT_SESSION_CLOSE | `tradingTime_isAuthorized == False` | `00 — long_00_IDLE` | 09 (200), 10 (201), 11 (202) |
| [HS] | COUNT_STOP_LOSS | `bar_low < stop_loss` | *(self)* | 09 (210), 10 (211), 11 (212) |
| [PEM] | COUNT_MA | `bar_close < ma_exit AND bar_close > entry_price` | *(self)* | 09 (220), 10 (221), 11 (222) |
| [XMA] | EXIT_MA | `is_bar_close == True AND bar_close < ma_exit AND bar_close > entry_price` | `01 — long_01_WAIT_BEAR_TREND` | 09, 10, 11 (223) |
| [PEL] | COUNT_LWR | `lwr_reading == -1 AND bar_close > entry_price` | *(self)* | 09 (230), 10 (231), 11 (232) |
| [XLR] | EXIT_LWR | `is_bar_close == True AND lwr_reading == -1 AND bar_close > entry_price` | `01 — long_01_WAIT_BEAR_TREND` | 09 (233), 10 (234), 11 (235) |
| [OC] | OFFSET_CHANGE | `exit_counter >= next_range.min_count` | *(self)* | 09, 10, 11 (283) |
| [EPTC] | EXIT_Profit_Target_Count | `bar_high >= type2_profit_limit` | `01 — long_01_WAIT_BEAR_TREND` | 09, 10, 11 (284) |

> **COUNT_ transitions increment `exit_counter` (no immediate exit):** `[HS]` × scale, `[PEM]` × scale, `[PEL]` × scale, `COUNT_PREMARKET_*` × scale (scale = 2/1/3 for Yellow/Green/Red). **[XMA]** and **[XLR]** are immediate exits that are evaluated only at the actual minute bar close (`is_bar_close == True`). **[OC]** advances `exit_counter_range_idx` and sets `type2_profit_limit`. **[EPTC]** is the primary counter-driven exit path once a profit limit is active.

---

## Change Log

| Date | Version | Project | Change Summary |
|------|---------|---------|----------------|
| 2026-04-18 | v1.3.1 | 20260418_TWM_combine_Debug_higher_tf | Added [XMA] EXIT_MA and [XLR] EXIT_LWR as shared standard transitions for states 09/10/11 with bar-close-only guards (`is_bar_close == True`). Updated header source/version metadata and shared-transition legend/notes. |
| 2026-04-13 | v1.3.0 | 20260412_TWM_exit_optimization | Replace State12_EXIT_COUNTER with in-state self-transitions. Remove EXIT_RED_TIMEOUT and EXIT_PREMARKET exit arrows from diagram; add COUNT_PREMARKET_S09/S11 self-loops; add OFFSET_CHANGE (t_283) self-loops; add EXIT_Profit_Target_Count (t_284) exit arrows to `[*]`. Update legend: [HS]/[PEM]/[PEL] → COUNT_ self-transitions; add [OC] and [EPTC] rows. |
| 2026-04-12 | v1.2.0 | 20260412_TWM_housekeeping | Task 2: Added transition names inline on Mermaid arrows. |
| 2026-04-12 | v1.1.0 | 20260412_TWM_housekeeping | Task 1: Updated state labels and transition numbers to new ALL_CAPS standard. |
| 2026-03-24 | v1.0.0 | 20260319_refactor_strategy | Initial document — exit states 09–11 split from long_entry_state_machine_diagrams_v1_2_11.md; transitions renumbered to 100-series per long_exit_state_machine_v1_3_0.md |

> Prior Diagram 2 history (as part of the combined file) preserved in archived predecessor: `long_entry_state_machine_diagrams_v1_2_11.md`.
