# Python Parity Debug Log

This document tracks the process of debugging `strategies/strategy_activation_scores.py` to match the output of the TradingView script `strategies/strategy_activation_scores.pine`.

## Initial Diagnosis

The initial diagnosis showed that many indicators have low correlation with the TradingView version.

**PASS:**
*   `db_osc`
*   `db_stoch`
*   `db_vwap_div`

**WARN:**
*   `db_rsid`

**FAIL:**
*   `db_bearish`
*   `db_m2_diff`
*   `db_m2_div_nooff`
*   `db_m2_div_tiny`
*   `db_m3_div`
*   `db_m3_mom`
*   `db_macd_pred`
*   `db_score`
*   `db_stoch_div`

## Investigation Log

### `db_m3_mom`
*   **Observation:** The python code was calculating a Rate of Change (`(current - prev)/prev * 100`), while the PineScript code was calculating a simple difference (`current - prev`).
*   **Action:** Changed the Python code to calculate a simple difference.
*   **Result:** The Mean Absolute Error (MAE) for `db_m3_mom` dropped significantly (from ~8 to ~0.2), but the correlation is still near zero. This is likely because the values are very small, so even small differences can lead to low correlation. I'll consider this "fixed for now".
*   **Update:** I found that the python script was applying a 5-period EMA to the M3 data before the momentum calculation, while the PineScript was not. I removed the EMA. This improved the correlation of `db_m3_div` from `0.2914` to `0.4611`.

### `safe_roc` function
*   **Observation:** The `safe_roc` function in python had some suspicious logic (`.bfill()` and `.fillna(0.0)`).
*   **Action:** I tried several variations of this function.
*   **Result:** The changes had no impact on the correlations. I have reverted it to its original state. This suggests the issue is not in this function, or the NaNs are not the primary problem.

### MACD (`db_macd_pred`)
*   **Observation:** The `pandas-ta` MACD implementation might differ from TradingView's, specifically regarding the `adjust` parameter in the underlying EMA calculations.
*   **Action:** I replaced the `pandas-ta` MACD with a manual implementation using `ewm(adjust=False)`.
*   **Result:** The correlation did not change. I reverted the change.

### `db_stoch_div`
*   **Observation:** The python code was calculating a stochastic of the RSI series, while the PineScript code was calculating a stochastic of the price (`high`, `low`, `close`).
*   **Action:** I changed the python code to calculate the stochastic of the price.
*   **Result:** The correlation for `db_stoch_div` improved from `0.0696` to `0.3185`. This is a significant improvement, but still not correct.

### RSI (`db_rsid`)
*   **Observation:** The `pandas-ta` `rsi` implementation with `mamode='rma'` might differ from the manual EMA-based RSI in the PineScript.
*   **Action:** I replaced the `ta.rsi` call with a manual implementation using `ewm` with `span` instead of `alpha`.
*   **Result:** The correlation for `db_rsid` got worse (from `0.9849` to `0.8882`). I reverted the change.

### Current Status
I have tried to fix several indicators, but I am unable to get them to match the TradingView output. The remaining discrepancies are likely due to:
1.  **Subtle implementation differences:** The way `pandas-ta` calculates indicators might be slightly different from TradingView's internal implementation.
2.  **Input data differences:** The M2/M3 data used in the python script is from a pre-computed CSV file, while the PineScript calculates it from multiple sources. This is likely a major source of error.
3.  **Missing library code:** I do not have the source code for all the PineScript libraries used (e.g., `LibraryCandlestickPatterns`), so I cannot be certain of the exact implementation of all indicators.

I am unable to proceed further without more information or a different approach.

## Recommended Next Steps: The "Feature Injection" Pivot

**Status:** DO NOT ABANDON. The diagnosis above confirms that "Input Data" is the primary blocker.

**The Pivot:**
Instead of attempting to replicate the *calculation* of complex indicators (M2, M3, Candlesticks) from raw data in Python, we will treat the TradingView output as the "Ground Truth" and inject it into the Python engine.

### Action Plan

1.  **Export "Ground Truth" Features:**
    *   Modify the Pine Script to export the *final processed values* of the failing indicators (`m2_diff`, `m3_div`, `candlestick_pattern_bool`) into the CSV used for comparison.
    *   Do not try to recalculate these in Python from FRED data.

2.  **Inject into Python:**
    *   Update `strategy_activation_scores.py` to check if these columns exist in the input DataFrame.
    *   If they exist, bypass the internal Python calculation and use the CSV values directly.

3.  **Validate Logic vs. Math:**
    *   Run the comparison again.
    *   **Goal:** If the *Entry/Exit signals* match when using injected data, the Python **Trading Engine** is validated. We can then proceed with Walk-Forward Optimization on the *thresholds* and *logic*, even if the indicator construction remains external for now.

## Resolution: Using Pre-calculated Data

We have decided to use the pre-calculated data from the PineScript version of the strategy, which is available in `strategies/DEBUG-ActivationScores-TV.csv`. This file contains the "ground truth" data for the indicators that were failing to correlate in the Python version.

This approach allows us to bypass the complex and error-prone process of replicating the PineScript calculations in Python. Instead, we will directly use the known correct values from the CSV file. This will ensure that the Python version of the strategy is using the exact same data as the PineScript version, which should resolve the parity issues.

While the `LibraryCandleStickPatterns.pine` library is now available, the data injection method is a more direct path to resolving the immediate issue of data parity.

### Next Steps

1.  **Verify CSV Data:** Examine the `strategies/DEBUG-ActivationScores-TV.csv` file to understand its structure and confirm that it contains the necessary data.
2.  **Modify Python Script:** Update `strategy_activation_scores.py` to load and use the data from the CSV file, mapping the columns to the corresponding variables in the script.
3.  **Run Diagnostics:** Use `tools/diagnose_components.py` to verify that the parity issues are resolved and that the Python script now produces results that match the PineScript version.
4.  **Optimize Strategy:** Once parity is confirmed, use `strategies/optimize_strategy.py` to find optimized parameters for the strategy.

## Implementation: Feature Injection

**Target Variables for Injection:**
Based on the "FAIL" list in the diagnosis, the following variables will be exported from Pine Script using `plot(var, title="INJECT_var", display=display.none)`:

1.  `m2_diff`
2.  `m2_div_nooff`
3.  `m2_div_tiny`
4.  `m3_div`
5.  `m3_mom`
6.  `stoch_div`
7.  `macd_pred`
8.  `activation_score` (Final check)

## After Actions

Here are the recommended after-actions:

**File Cleanup:**
*   Move `strategies/optimize_strategy.py` to the `tools/` directory.
*   Rename `DEBUG-ActivationScores-Py.csv` and `DEBUG-ActivationScores-TV.csv` to be more descriptive and/or move them to a dedicated `output` directory.

**Code Adjustments:**
*   The path to `DEBUG-ActivationScores-TV.csv` is hardcoded in `strategy_activation_scores.py`. This should be made a parameter.
*   If `optimize_strategy.py` is moved, any scripts that call it will need to be updated.

**Documentation:**
*   Update `README.md` and/or `GEMINI.md` to reflect the new workflow of using pre-calculated data.
