# Strategy Optimization Workflow

This document defines the standard workflow for optimizing and executing the `ActivationScores` strategy.

## Core Principle
**Python should optimize, not calculate.**
The Python strategy script (`strategy_activation_scores.py`) should act primarily as a consumer of data calculated by TradingView. It should not attempt to replicate complex indicator logic unless absolutely necessary for fallback.

## The Cycle

### 1. Data Export (TradingView -> CSV)
*   **Source**: `strategy_activation_scores.pine` running on TradingView.
*   **Action**: The Pine Script calculates all indicators (RSI, MACD, M2/M3, Divergences, etc.) and exports them via `plotchar()` to the Data Window.
*   **Export**: Use the "Export Chart Data" feature in TradingView to save the CSV.
*   **File**: Save as `TV_Export.csv` (or similar) in the bot's data directory.

### 2. Optimization (Python)
*   **Source**: `auto_optimize_loop.py` calling `strategy_activation_scores.py`.
*   **Input**: The optimizer loads `TV_Export.csv`.
*   **Logic**: 
    *   The Python script checks for the existence of specific column names exported by Pine (e.g., `stoch_value`, `macd_prediction`, `rsid_osc`).
    *   **Crucial**: If these columns exist, Python **MUST** use them directly. It should *never* calculate indicators internally if the columns are missing. If these columns are missing, then the user **MUST** be informed and this needs to be fixed before continuing. 
    *   The optimizer runs millions of combinations of **Weights** and **Thresholds** against these fixed indicator values using the GPU. When this process seems to have found sets of parameters that seem to yield a high sharpe ratio. Then each of those sets of parameters are checked by running them on the CPU to see which of these yields the highest sharpe ratio. The best of these, if better performing than the previously saved best set of parameters, then is saved as the new best set of parameters.  
*   **Output**: A JSON file (e.g., `params_strategy_activation_scores.json`) containing the best weights and thresholds found.

### 3. Execution (TradingView)
*   **Source**: `strategy_activation_scores.pine`.
*   **Action**: Manually update the `Input` variables in the Pine Script with the **Weights** and **Thresholds**  from the optimized JSON file.
*   **Result**: The strategy runs on TradingView using the exact same logic, market data and parameters should yield the same results that were simulated in Python. If the results are not the same or very similar then the process needs to be debugged until this is the case. 

## Maintenance
*   **Adding Indicators**: If a new indicator is added, it must be:
    1.  Calculated in Pine Script.
    2.  Added to `plotchar()` outputs in Pine.
    3.  Added to the input reading logic in `strategy_activation_scores.py` to read the new column.
*   **Verification**: Use `tools/analyze_tv_export.py` to verify that Python is reading the data correctly and that `DB_` columns (Python's view) match `Original` columns (Pine's view).
