# Tasks: M2 Indicator Multi-Timeframe Refactor (Version 2)
Input: MainStrategy.pine Goal: Refactor the monolithic M2 Leading Indicator block to support multiple timeframes using the "dual-path" architecture, ensuring the 1D baseline performance is perfectly preserved.

**Input:** `MainStrategy.pine`
**Goal:** Refactor the monolithic M2 Leading Indicator block to support multiple timeframes using the "dual-path" architecture, ensuring the 1D baseline performance is perfectly preserved.

## Methodology

This is a high-risk refactoring. The tasks are designed to be extremely cautious, following the "Execution Flow" principle discovered in our previous attempts. The core principle is to create two completely separate, parallel calculation pipelines that run unconditionally, and only select the final result at the very end.

**Key Principles:**
1.  **Isolate Execution Paths:** The calculation for the 1D timeframe must be completely independent of the 12H timeframe calculation.
2.  **Guard Inputs:** `_1D` calculations must *only* use `_1D` inputs. `_12H` calculations must *only* use `_12H` inputs. There can be no cross-contamination.
3.  **Verify Baseline:** Any regression in the 1D performance is evidence that a principle has been violated. The process must stop and be re-evaluated.

**Legend:**
-   `[V]`: Critical verification task. Must be completed with exact results before proceeding.

---

## Phase 1: Reset to a Known Good State

### T001: Revert to Baseline
**Action:** Update all M2 inputs to have `_1D` and `_12H` versions.
-   The old `if i_enable_timeframe_adjustment` block is removed.
-   The M2 calculation block is still a single, linear pipeline using only the `_1D` inputs.

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

---

## Phase 2: Implement the Correct Dual-Path Architecture

### T003: Declare All Output Variables
**Action:** In `strategies/MainStrategy.pine`, at the top of the M2 calculation block, declare two full sets of output variables (one for `_1D`, one for `_12H`). These variables must be initialized but not `var` declared.
**Example:**
```pinescript
float m2_slope_shortOffset_1D = na, m2_slope_shortOffset_12H = na
bool isM2SmoothedShortOffsetRising_1D = false, isM2SmoothedShortOffsetRising_12H = false
// ...and so on for every variable the M2 block produces.
```

### T004: Implement Parallel Pipelines
**Step A (1D Pipeline):**
1.  Take the existing M2 calculation logic.
2.  Rename every calculated variable by adding a `_1D` suffix (e.g., `m2_slope_shortOffset` becomes `m2_slope_shortOffset_1D`).
3.  Ensure it calculates and assigns values only to these new `_1D` variables (e.g., `m2_slope_shortOffset_1D := ...`).
4.  Verify that this pipeline *only* uses `_1D` inputs (e.g., `i_m2LeadingIndicator_shortOffset_1D`).

**Step B (12H Pipeline):**
1.  Immediately following the 1D pipeline, copy and paste the entire block.
2.  Modify this new block to assign its results only to the `_12H` variables (e.g., `m2_slope_shortOffset_12H := ...`).
3.  Verify that this pipeline *only* uses `_12H` inputs (e.g., `i_m2LeadingIndicator_shortOffset_12H`).

**Crucially:** These two calculation blocks must run sequentially and unconditionally, with no `if/else` statement surrounding them.

### T005: Unify the Pipelines
**Action:** After both pipelines have been calculated, create the final, unified set of variables that the rest of the strategy will use. This is done using ternary operators.
**Example:**
```pinescript
m2_slope_shortOffset = timeframe.period == "D" ? m2_slope_shortOffset_1D : m2_slope_shortOffset_12H
isM2SmoothedShortOffsetRising = timeframe.period == "D" ? isM2SmoothedShortOffsetRising_1D : isM2SmoothedShortOffsetRising_12H
// ...and so on for every M2 output variable.
```
## Phase 3: Final Verification

### T006 [V]: Verify 1D Performance
**Action:** Run a full backtest on the `1D` timeframe.
**Expected Result:** The Net Profit must **exactly match the 256,537% baseline**. This architecture correctly isolates the 1D execution flow and should not cause a regression. If it does, we have misunderstood something fundamental, and we must stop and re-evaluate.

### T007 [V]: Verify 12H Functionality
**Action:** Run a full backtest on the `12H` timeframe.
**Expected Result:** The strategy should run without errors and produce a different performance result, confirming that the 12H data path is active and using the `_12H` parameters.