# Refactoring Plan: Multi-Timeframe Architecture (Version 3)

#### Objective
To refactor the strategy to support multiple, distinct parameter sets for different timeframes (e.g., 1D, 12H) while guaranteeing the performance of each baseline is perfectly preserved.
This plan codifies the lessons learned from both successful and failed refactoring attempts.
---

### Core Methodology: The "Pure Path" Architecture

This architecture is designed to prevent the subtle but critical Pine Script execution errors that have plagued previous attempts. It is based on two non-negotiable principles.

#### Principle 1: The Golden Rule of Pine Script Types
> **A `series` type variable must NEVER be used as a `length`, `period`, or historical offset (`[]`) in any calculation.**

-   A **`simple`** type is a constant known before the script runs (e.g., `input.int`, a literal `14`).
-   A **`series`** type is a value that can change on each bar (e.g., `close`, or the result of `timeframe.period == "D"`).
-   Passing a `series` where a `simple` is required corrupts the script's execution state and leads to incorrect calculations and invalid backtest results. **This is the root cause of all previous regressions.**

#### Principle 2: Isolate Calculation Paths
To honor the Golden Rule, every indicator with timeframe-dependent parameters must be calculated in completely separate, parallel pipelines.

1.  **Independent Calculation:** A `_1D` version of an indicator (e.g., `macd_1D`) must be calculated using *only* `_1D` `simple` inputs. A `_12H` version (`macd_12H`) must be calculated using *only* `_12H` `simple` inputs.
2.  **Unconditional Execution:** Both the `_1D` and `_12H` calculation blocks must run on every bar, without any `if/else` logic surrounding them.
3.  **Unify at the End:** Only after both pure values have been calculated should the final variable be chosen using a single, global timeframe-checking boolean.

**Example 1: Correct Implementation for `ta.*` functions**
```pinescript
// --- 1D Path (Pure) ---
// Uses only simple int `_1D` inputs.
atr_1D = ta.atr(atr_length_1D)

// --- 12H Path (Pure) ---
// Uses only simple int `_12H` inputs.
atr_12H = ta.atr(atr_length_12H)

// Unify the final result
isDaily = timeframe.period == "D"
atr = isDaily ? atr_1D : atr_12H
```

**Example of FAILED Implementation (To Be Avoided):**
```pinescript
// INCORRECT: Creates a 'series int' and causes regression
isDaily = timeframe.period == "D"
fast_length = isDaily ? fast_length_1D : fast_length_12H
macd = ta.ema(close, fast_length) // ERROR: 'fast_length' is a series!
```

---

### Action Plan

This plan is incremental, applying the "Pure Path" architecture one component at a time to minimize risk and ensure each step is verifiable.

#### Phase 1: Establish a Clean Baseline

*   **T101: Revert to a Single-Timeframe State.**
    *   **Action:** Remove all `_12H` inputs and all dual-path logic from the entire script.
    *   **Goal:** Create a simple, linear script that calculates using only the `_1D` inputs. This is our known-good starting point.

*   **T102 [V]: Verify the Golden Baseline.**
    *   **Action:** Run a backtest on the `1D` timeframe.
    *   **Expected Result:** The Net Profit must exactly match the **256,537% "Golden Baseline"**. Do not proceed until this is verified.

#### Phase 2: Refactor Component by Component

For each of the following components, repeat these steps:
1.  Add the `_12H` inputs for that specific component.
2.  Implement the "Pure Path" architecture as described above.
3.  Verify both 1D and 12H backtests. The 1D result must not change.

*   **T201: Refactor `M3/HMA` Indicators.**
*   **T202: Refactor `MACD` (main).**
*   **T203: Refactor `Gaussian Channel`.**
*   **T204: Refactor `Stochastic RSI`.**
*   **T205: Refactor `RSI Divergence` and its related calculations.**
*   **T206: Refactor `Realized Price`.**
*   **T207: Refactor `M2 Leading Indicator` & `M2 MACD`.**
*   **T208: Refactor all remaining minor indicators** (`ATR Stop`, `Volatility`, etc.).

#### Phase 3: Code Cleanup and Future-Proofing

*   **T301: Consolidate Timeframe Checks.**
    *   **Action:** Ensure a single, global boolean (`isDaily = timeframe.period == "D"`) is used for all unification steps. Remove redundant checks like `isDailyM3`, `isDailyMACD`, etc.

*   **T302: Plan for `switch` statements.**
    *   **Action:** For future work involving more than two timeframes (e.g., 4H), the unification block should be converted from ternaries to a `switch` statement for better readability.
    ```pinescript
    // Future implementation for 3+ timeframes
    string tf = timeframe.period
    float final_macd = switch tf
        "D" => macd_1D
        "720" => macd_12H
        "240" => macd_4H
        => na
    ```