# TradingView Pine Update Workflow

This repo can now stage Pine updates for human-in-the-loop TradingView validation.
It does not try to hide automation or bypass TradingView policy. Use your normal
TradingView account and visible browser session.

## What Is In Place

- `tools/tradingview_update.py` parses `.pine` files, extracts the exact
  `strategy(...)` or `indicator(...)` title, runs local preflight checks, copies
  the source to the macOS clipboard when available, writes an exact source copy,
  and writes a manifest under `.tradingview_e2e/`.
- The same helper has `smoke` and `capture` subcommands for browser launch
  verification and screenshot/text evidence.
- `tools/check_pine.py` still handles the activation-score strategy's static
  limits and TV-validation sentinel.
- `tools/check_pine_duplicates.py` is used as a generic duplicate definition
  check for any Pine file.
- `tools/compare_tv_trades.py`, `tools/check_mlp_parity.py`, and the existing
  `docs/tv_parity_workflow.md` remain the authority for exported CSV/trade-list
  parity after a TradingView compile succeeds.

## Standard Update Loop

1. Regenerate or edit the target `.pine` file locally.
2. Run the guided update command:

   ```bash
   .venv/bin/python tools/tradingview_update.py manual-update strategies/strategy_mlp_scores.pine
   ```

   For an indicator:

   ```bash
   .venv/bin/python tools/tradingview_update.py manual-update indicators/IndicatorActivationScores.pine
   ```

   This stages the source, opens a visible persistent TradingView browser, waits
   while you paste/save in Pine Editor, captures screenshot/text evidence, and
   verifies the captured text contains the script title and no known Pine
   compile-error phrase.

3. If you only want to stage source without opening a browser, run:

   ```bash
   python3 tools/tradingview_update.py prepare strategies/strategy_mlp_scores.pine
   ```

   The staged source is also written to `.tradingview_e2e/source/`, including a
   `latest_<script-name>.pine` copy for fallback manual paste when clipboard
   access is unavailable.

   For an indicator:

   ```bash
   python3 tools/tradingview_update.py prepare indicators/IndicatorActivationScores.pine
   ```

4. Open TradingView manually, go to Pine Editor, and select the existing script
   with the exact title printed by the tool. Paste from the clipboard and save.
5. Confirm TradingView shows no Pine compile errors. Inspect the chart or
   Strategy Tester visually.
6. Capture verification evidence, if you used `prepare` instead of
   `manual-update`:

   ```bash
   .venv/bin/python tools/tradingview_update.py capture --headed --save-text --label pine-editor-after-save
   ```

   Then check the captured text for the expected script name and common Pine
   compile errors:

   ```bash
   .venv/bin/python tools/tradingview_update.py verify-text \
     --text .tradingview_e2e/page_text/<capture-file>.txt \
     --expect MLPScores
   ```

7. For `strategies/strategy_activation_scores.pine`, mark the local sentinel
   after TradingView compiles:

   ```bash
   python3 tools/check_pine.py --mark-valid
   ```

8. If behavior changed, export TradingView chart data or Strategy Tester trades
   and run the parity tools in `docs/tv_parity_workflow.md`.

## Optional Browser Assistance

The helper can open TradingView in your default browser:

```bash
python3 tools/tradingview_update.py prepare strategies/strategy_mlp_scores.pine --open
```

If Python Playwright is installed, it can also open a visible persistent browser
profile and save a screenshot after your manual review:

```bash
python3 tools/tradingview_update.py prepare strategies/strategy_mlp_scores.pine --playwright-open
```

The optional dependency is recorded in `requirements-e2e.txt`. Install it in the
repo virtualenv when rebuilding this environment:

```bash
.venv/bin/python -m pip install -r requirements-e2e.txt
.venv/bin/python -m playwright install chromium
```

This requires network access and downloads a browser binary. The persistent
profile lives in `.tradingview_e2e/playwright-profile/` so login state can be
reused locally.

Verify the local browser stack after install:

```bash
.venv/bin/python tools/tradingview_update.py smoke
```

Run a static readiness check:

```bash
.venv/bin/python tools/tradingview_update.py doctor
```

Run the full browser and TradingView render check:

```bash
.venv/bin/python tools/tradingview_update.py doctor --browser --tradingview
```

Summarize the latest local artifacts:

```bash
.venv/bin/python tools/tradingview_update.py status
```

Capture a noninteractive screenshot and page-text snapshot:

```bash
.venv/bin/python tools/tradingview_update.py capture \
  --url https://www.tradingview.com/chart/ \
  --label tradingview-chart \
  --save-text
```

When you need to reuse your logged-in TradingView session, run a headed capture
once and sign in in the visible browser:

```bash
.venv/bin/python tools/tradingview_update.py capture --headed --save-text
```

Check the persistent TradingView browser profile without modifying anything:

```bash
.venv/bin/python tools/tradingview_update.py profile-status
```

Use the same command to sign in and then capture profile evidence:

```bash
.venv/bin/python tools/tradingview_update.py profile-status --headed --wait-for-user
```

Artifacts are written under `.tradingview_e2e/screenshots/`,
`.tradingview_e2e/page_text/`, and `.tradingview_e2e/*_capture.json`.

## MCP Status

I did not find a reliable, maintained TradingView-specific MCP server to install.
The workflow is structured so a future MCP adapter could replace the browser
assistance layer while keeping the same local preflight and parity steps.

## Compliance Boundary

Do not add stealth plugins, anti-detection flags, randomized obfuscation, or
other behavior designed to disguise automation. The supported approach here is:
prepare exact code locally, use a visible browser session, keep a human in the
save/compile decision, and verify results with screenshots and TradingView
exports.
