Skip to content

Bringing Pine Scripts into TradingView

Paste the .pine file generated by alpha-forge pine generate into TradingView and configure an alert.

All the way to live orders: --with-webhook

To bridge through to automated orders on alpha-strike, generate with alpha-forge pine generate --strategy <id> --with-webhook. The output Pine already wires up the input.string (passphrase=REPLACE_ME / confirm=true) + make_payload() + alert() blocks, so you no longer hand-edit alert() calls. Follow the alert steps below, leave the Message field empty, and just point the Webhook URL at alpha-strike to complete the execution bridge (payload spec and receiver setup: TradingView × alpha-strike Integration).

1. Open the Pine Editor

Open a chart in TradingView and click the "Pine Editor" tab at the bottom of the screen.

2. Paste the script

Copy the contents of the generated .pine file into the editor and click "Add to chart" (▶).

3. Create an alert

Click the bell icon (Alerts) on the top right → "Create alert".

  • Condition: Select your script
  • Webhook URL: Enable and paste the alpha-strike endpoint
  • Message: Use the JSON payload format described in the alpha-strike integration guide

4. Tip: in-script alert conditions

Defining alertcondition inside Pine Script makes alert setup cleaner.

// Example alertcondition definition
longSignal = ta.crossover(ema_fast, ema_slow)
shortSignal = ta.crossunder(ema_fast, ema_slow)
alertcondition(longSignal, title="Long Entry", message="long")

Next step

Configure the webhook receiver in TradingView × alpha-strike Integration.


4.5 Post-generate Pine v6 validation (issue #786)

alpha-forge pine generate / pine preview automatically run a post-generate validator against an internal Pine v6 signature database. The validator detects arguments that were removed in Pine v6 (e.g. strategy.exit(trail_percent=...), strategy.entry(allow_short=...)) and typos, and stops the command at the CLI stage before the script is pasted into TradingView Pine Editor.

  • Error (exit code 2): deprecated or unknown arguments are detected. Offending line numbers are printed to stderr.
  • Warning only: the function is not yet registered in the signature DB. The command still succeeds (to avoid false positives).
  • Use --no-validate to bypass the validator in emergencies, but fixing the underlying strategy JSON or generator output is strongly preferred — Pine v6 syntax errors will block the script in TradingView anyway.

About the _optimized suffix

The sma_crossover_v1_optimized ID used in the examples below is what the CLI registers automatically when you run alpha-forge optimize apply <result.json> --to-strategy sma_crossover_v1. If the base ID before apply is <base>, the registered optimized strategy ID becomes <base>_optimized (pass the bare base ID to --to-strategy, without the suffix).

# Normal generation (validator runs automatically)
alpha-forge pine generate --strategy sma_crossover_v1_optimized

# Emergency bypass
alpha-forge pine generate --strategy sma_crossover_v1_optimized --no-validate

The signature DB (alpha_forge/pinescript/v6_signatures.yaml) covers the main APIs under strategy.* / ta.* / input.* / request.security. It is updated whenever Pine v6 changes or the generator emits new APIs.


4.6 Bake a backtest period filter into the Pine output (issue #823)

Passing --backtest-period 'YYYY-MM-DD:YYYY-MM-DD' declares an in_backtest_period = time >= timestamp("from") and time <= timestamp("to") variable at the top of the Pine output and ANDs and in_backtest_period onto every entry condition (exits are left untouched).

alpha-forge pine generate --strategy sma_crossover_v1_optimized \
  --backtest-period 2022-01-01:2024-12-31

Why: TradingView's Strategy Tester Date Range UI is gated by paid plan tier (Premium and above); on the Essential tier the Custom date range UI silently fails. Baking the time guard into the Pine itself bypasses this entirely, and scripts/tv_cross_validate.py update --from/--to now uses this path automatically.

Real-world verification (TradingView Essential plan, SPY 2022-01-01 to 2024-12-31):

Strategy alpha-forge trades TV trades (before) TV trades (after)
sma_crossover_v1 17 207 17 (exact match)
bollinger_mr_v1 69 1317 128 (90% reduction)

Design: exits intentionally lack the guard so that any position open at the end of the period exits naturally via the normal exit logic.


4.7 Intra-bar fill semantics differ between alpha-forge and TV for SL/TP strategies (issue #827)

For strategies that use risk_management.stop_loss_pct / take_profit_pct, alpha-forge and TV interpret the trade count differently. This is a backtest engine semantics difference that users need to be aware of:

  • alpha-forge (vectorbt) fills SL/TP at the next bar's close
  • TV's strategy.exit(stop=..., limit=...) is evaluated every bar and fills intra-bar whenever the high/low touches the threshold

The same Pine script tends to record substantially more trades on TV than in alpha-forge.

Strategy SL/TP alpha-forge TV Diff
sma_crossover_v1 none 17 17 0% (exact match)
bollinger_mr_v1 yes (2%/4%) 69 128 86%

Metric reliability:

Metric intra-bar fill influence Interpretation
win_rate_pct low high reliability
profit_factor low high reliability
total_trades high take with caution
total_return_pct high take with caution

bollinger_mr_v1 reports win rates of 41.18% (alpha) vs 41.41% (TV) — a mere 0.23pp gap, indicating identical signal quality. The 1.86x trade-count gap is purely an artifact of how entry-exit pairs are counted.

Automatic profile selection: --tolerance-profile auto (alpha-forge issue #828)

When cross-validating SL/TP strategies, scripts/tv_cross_validate.py check automatically switches the tolerance profile via --tolerance-profile auto (the default).

scripts/tv_cross_validate.py is source-repository only

scripts/tv_cross_validate.py and the golden fixtures (tests/fixtures/tv_golden/) ship only with the alpha-forge source repository — they are not bundled with the distributed binary. If you are on the binary build, the commands in this section cannot be run as-is (run cross-validation from a development checkout of the alpha-forge source instead).

# Run from the alpha-forge directory
uv run python scripts/tv_cross_validate.py check \
  --strategy bollinger_mr_v1 --symbol SPY \
  --golden tests/fixtures/tv_golden/bollinger_mr_v1__SPY__1d.json
# auto (default): inspects risk_management on the strategy JSON
#   → applies the sl_tp profile automatically
profile total_trades win_rate total_return profit_factor Use case
default ±20% ±5pp ±15pp ±0.30 event-based strategies (no SL/TP)
sl_tp ±100% ±5pp ±20pp ±0.50 SL/TP / trailing strategies (absorbs intra-bar fill diff)
auto (default) inferred from the strategy JSON

Unit of total_return tolerance (alpha-forge #828 follow-up)

total_return_pct was originally judged by relative % diff, but sign-flip cases like alpha=-4.22 / tv=2.15 blow up the relative percent due to a small denominator. PR #834 switched it to absolute pp diff, so bollinger_mr_v1 (alpha 69 vs TV 128 trades) now reaches 4/4 PASS with overall_pass=True.

auto selects sl_tp if any of risk_management.stop_loss_pct / take_profit_pct / trailing_stop_pct is set; otherwise default. You can force a profile with --tolerance-profile {default,sl_tp} or override individual fields with --total-trades-rel-pct 200.0 etc.


5. Verifying the Pine Script through an MCP server (issue #523)

Paid plans only

alpha-forge pine verify is available on paid plans (Lifetime / Annual / Monthly) only. See Trial limits for Trial plan restrictions.

alpha-forge pine verify ships the generated Pine Script to TradingView Desktop via a third-party MCP server for verification. Beyond compile checks, it can compare TV's Strategy Tester aggregate metrics or per-trade list against the matching alpha-forge backtest, surfacing translation errors mechanically.

5.1 Prerequisites

  1. Launch TradingView Desktop with --remote-debugging-port=9222
  2. Run a third-party MCP server in a separate process:
  3. tradesdontlie/tradingview-mcp — good for compile checks and chart manipulation
  4. oviniciusramosp/tradingview-mcp (vinicius fork) — strong on Strategy Tester aggregates; recommended for metrics / signal modes
  5. Configure the endpoint and flavor in forge.yaml:
tv_mcp:
  pine_verify:
    enabled: true
    endpoint: "node /opt/tv-mcp/server.js"
    runtime: node
    flavor: vinicius     # use vinicius for metrics/signal
    timeout_seconds: 60

5.2 verify modes

Mode What it verifies Recommended flavor
compile_only Pine Script syntax / compilation only tradesdontlie is sufficient
metrics TV Strategy Tester aggregates (PF, win rate, total trades, etc.) vs alpha-forge metrics vinicius (avoids the data_get_strategy_results bug in tradesdontlie)
signal tradesdontlie: match TV trade list against alpha-forge trades by entry time and compute a match rate.
vinicius: returns no timestamps, so the comparison auto-switches to count-based (totals only, issue #580)
tradesdontlie (when time matching is required) / vinicius (when counts alone suffice)
regime Not implemented (parked) — upstream MCP servers do not expose a time-series study tool, so the HMM state column cannot be retrieved per-bar (issue #581). Selecting this mode fails fast with an explicit error.

5.3 Workflow

# 1. Compile-only verification (fastest)
alpha-forge pine verify --strategy spy_sma_v1 \
  --mcp-server "node /opt/tv-mcp/server.js"

# 2. Strategy Tester metrics comparison (vinicius recommended)
alpha-forge pine verify --strategy spy_sma_v1 \
  --check-mode metrics \
  --symbol SPY --interval D \
  --mcp-server-flavor vinicius \
  --auto-backtest \
  --output reports/verify_spy.md

# 3. Per-trade time matching (±60s tolerance, 95% match required)
alpha-forge pine verify --strategy spy_sma_v1 \
  --check-mode signal \
  --symbol SPY --interval D \
  --mcp-server-flavor vinicius \
  --auto-backtest \
  --match-tolerance-seconds 60 \
  --min-match-rate 0.95

5.4 Avoiding period mismatches

A large total_trades gap in metrics mode is usually a data-period mismatch (yfinance's ~5y vs TradingView's decades). To bring alpha-forge's history closer to TradingView's, switch the data fetch to the TradingView MCP provider:

alpha-forge data fetch SPY --provider tv_mcp \
  --mcp-server "node /opt/tv-mcp/server.js" --period max

See the alpha-forge data command reference for details.

5.5 Report output

With --output reports/xxx.md, the Markdown report includes:

  • Strategy ID and verification mode
  • Comparison table (alpha-forge ↔ TradingView)
  • Detected mismatches (items beyond tolerance)
  • Verdict (PASS / FAIL) and recommended actions

Combine with alpha-forge journal report --with-chart --symbol SPY --interval D to get strategy history + verification + TV chart in a single page.