# Tasks: 8hr Long Exit Optimization

**Input**: Design documents from `../../docs/8hr_long_exit_optimization/`
**Prerequisites**: `plan.md`

## Methodology
Tasks are structured to follow a Test-Driven Development (TDD) approach. We will first add a visualization for the new feature, which will act as our failing "test" (RED). We will then implement the logic to make the test pass (GREEN), and finally refactor if needed.
- **[P]**: Can run in parallel.
- **[V]**: Verification task. Must be completed before proceeding.

## Path Conventions
- All file paths are relative to the repository root.

---

## Phase 3.1: Setup

- [ ] **T001:** Create the new feature branch `feature/8hr-long-exit-optimization` from the `main` branch.

## Phase 3.2: Contracts & Test Visualization (TDD)

- [ ] **T002:** In `LibraryLongExit.pine`, update the library's public contract.
    - Add a new boolean field `i_enable_longExit_8hrCandlePattern` to the `EnablementInputs` type.
    - Add a new boolean field `longExit_8hrCandlePattern` to the `Results` type.

- [ ] **T003 (RED):** In `indicators/IndicatorLongExitBooleans.pine`, add the visualization for the new exit condition.
    - Add the new `input.bool()` for `i_enable_longExit_8hrCandlePattern`, mirroring the planned strategy input.
    - Update the call to `libLongExit.f_calculateLongExit` to pass the new enablement input.
    - Add a new `plot()` call for `longEntryResults.longExit_8hrCandlePattern`.
    - **Expected Result:** The script will compile, but the new plot will never fire. This is our failing test.

## Phase 3.3: Core Implementation (GREEN)

- [ ] **T004 [P]:** In `libraries/LibraryUtility/LibraryUtility.pine`, create the `f_isThreeBlackCrows` detection function.
    - The function should take `open`, `high`, `low`, `close` as arguments.
    - It must implement the quality filters specified in `FR-008` of the spec (e.g., check that candle bodies are a significant portion of the total candle range).

- [ ] **T005 [P]:** In `libraries/LibraryUtility/LibraryUtility.pine`, create the `f_isBearishEngulfing` detection function.
    - The function should take `open`, `high`, `low`, `close` as arguments.
    - It must also implement quality filters per `FR-008`.

- [ ] **T006 [P]:** In `libraries/LibraryUtility/LibraryUtility.pine`, create the `f_isEveningStar` detection function.
    - The function should take `open`, `high`, `low`, `close` as arguments.
    - It must also implement quality filters per `FR-008`.

- [ ] **T007:** In `LibraryLongExit.pine`, implement the core exit logic within the `f_calculateLongExit` function.
    - Check if `timeframe.period == "480"`.
    - If true, call the three new pattern detection functions from `LibraryUtility`.
    - Aggregate the results: `longExit_8hrCandlePattern := isThreeBlackCrows or isBearishEngulfing or isEveningStar`.
    - Add the new condition to the final `finalExitCondition` boolean.
    - Add an `else if` block to set the `longExitConditionName` to the specific pattern that fired (e.g., "Exit: 8hr Three Black Crows").

## Phase 3.4: Integration & Verification

- [ ] **T008:** In `strategies/MainStrategy.pine`, integrate the new feature.
    - Add the new `input.bool()` for `i_enable_longExit_8hrCandlePattern` in the "Long Exit Types Enabled" group, disabled by default.
    - Update the call to `libLongExit.f_calculateLongExit` to pass the new enablement input.

- [ ] **T009 [V]:** Perform visual verification.
    - On an 8-hour chart, enable the new condition in `IndicatorLongExitBooleans.pine`.
    - Find historical examples of "Three Black Crows", "Bearish Engulfing", and "Evening Star".
    - **Expected Result:** Confirm that the plot for `longExit_8hrCandlePattern` fires correctly on the bars where these patterns complete.

- [ ] **T010 [V]:** Run a full backtest on the `1D` timeframe with the new feature's input switch **disabled**.
    - **Expected Result:** The Net Profit must exactly match the **256,537%** baseline, confirming no regressions have been introduced.

- [ ] **T011 [V]:** Run a backtest on the `8H` timeframe with the new feature's input switch **enabled**.
    - **Expected Result:** The strategy should now take trades based on the new exit condition. Analyze the "List of Trades" to confirm the exit reason is logged correctly. Evaluate the impact on performance.

## Phase 3.5: Cleanup

- [ ] **T012:** Review all changes, remove any temporary debug plots, and ensure code is clean and well-commented.
- [ ] **T013:** Merge the `feature/8hr-long-exit-optimization` branch into `main`.