Backtesting on TradingView (Step-by-Step)
TradingView offers two ways to backtest: Pine Script for automated strategy testing, and Bar Replay for manual replay. Pine Script is the easiest scripting language in forex — a simple strategy is only 10–15 lines of code — and results appear instantly on your chart.
Risk warning: This content is for educational purposes only and not financial advice. Forex trading involves risk, and you can lose money.
TradingView Backtesting
Not sure what backtesting is? Read the backtesting overview first.
- Pine Script backtesting
- Bar Replay
- Community scripts
- Limitations
Automated backtesting with Pine Script
Pine Script is TradingView’s built-in scripting language. You write your strategy rules, add them to a chart, and TradingView runs the strategy against all visible historical data — marking every entry and exit on the chart and generating a full report.
Step by step
- Open a chart on your pair and timeframe.
- Click Pine Editor at the bottom of the screen.
- Write your strategy. TradingView has built-in templates: click Open → New strategy to start with an example. A basic EMA crossover with a fixed stop-loss and take profit is about 10–15 lines of code.
- Click “Add to chart”. TradingView immediately runs the strategy and plots every entry and exit directly on the chart — you see the trades visually.
- Click the “Strategy Tester” tab at the bottom of the chart to see the performance report.
Pine Script example: EMA crossover
Here is what a simple strategy looks like in Pine Script. This buys when the 20 EMA crosses above the 50 EMA and sells when it crosses below, with a 30-pip stop-loss and 60-pip take profit.
//@version=6
strategy("EMA Cross", overlay=true)
ema20 = ta.ema(close, 20)
ema50 = ta.ema(close, 50)
if ta.crossover(ema20, ema50)
strategy.entry("Long", strategy.long)
strategy.exit("Exit Long", "Long", loss=300, profit=600)
if ta.crossunder(ema20, ema50)
strategy.entry("Short", strategy.short)
strategy.exit("Exit Short", "Short", loss=300, profit=600)
plot(ema20, color=color.blue)
plot(ema50, color=color.red)
Paste this into the Pine Editor, click “Add to chart”, and you instantly see the results. The loss and profit values are in ticks (1 pip = 10 ticks on most forex pairs), so 300 ticks = 30 pips.
Reading the results
Click the “Strategy Tester” tab at the bottom of the chart. You get three sub-tabs:
- Overview: net profit, total trades, win rate, profit factor, max drawdown, Sharpe ratio, and average trade.
- Performance Summary: detailed breakdown with long vs. short statistics, gross profit/loss, and average win/loss.
- List of Trades: every individual trade with entry, exit, profit/loss, and duration.
Deep Backtesting in TradingView
- Click the “Deep Backtest” button in the Strategy Tester tab. TradingView runs a more thorough backtest with additional data.
- Generates a monthly returns table, equity curve, and more detailed risk metrics.
- Useful for getting a more complete picture, especially on lower timeframes where standard backtesting may use limited data.
Using community scripts
- You do not have to write Pine Script from scratch. Thousands of free community strategies are available in TradingView’s Indicators & Strategies library.
- Click Indicators on the top toolbar, search for a strategy (e.g., “EMA crossover strategy”), and add it to your chart. The backtest runs automatically.
- You can also open the source code of any public script, study it, and modify it to match your own rules.
Bar Replay (manual backtesting)
If you prefer to test a discretionary strategy without coding, TradingView’s Bar Replay lets you replay the market candle by candle.
- Click the Replay button in the top toolbar (the rewind icon).
- Click on the chart at the date where you want to start. All candles after that point disappear.
- Press Play to let candles advance automatically, or click Step Forward to go one candle at a time.
- When you spot a setup, record it in a spreadsheet or use TradingView’s drawing tools to mark entry, stop, and target.
- Note: Bar Replay on intraday timeframes (below Daily) requires a paid TradingView plan. Daily and above works on the free plan.
What makes TradingView good for backtesting
- Easiest scripting language. Pine Script is simpler than MQL4, MQL5, or C#. If you have never coded before, this is the best place to start.
- Instant visual feedback. Trades appear on the chart the moment you add the strategy. No separate tester window.
- No installation needed. Everything runs in the browser.
- Huge community. Thousands of free strategies to study, modify, or use directly.
- Combines analysis and testing. You can use the same charts you trade on for backtesting — no switching between tools.
Limitations
- Candle data only. TradingView uses OHLC candle data, not tick-by-tick data. For strategies that depend on exact intra-candle price movement (e.g., whether the stop or target gets hit first within the same candle), results can be less accurate than MT5 or cTrader with real tick data.
- Intraday replay requires a paid plan. Bar Replay on timeframes below Daily is locked behind TradingView’s paid subscription.
- Limited historical data on lower timeframes. On M1 or M5, you may only get a few months of data. Deep Backtesting helps but has limits.
- No multi-threaded optimization. TradingView does not have a built-in parameter optimization tool like MT5. You would need to manually adjust inputs and re-run.
