Pine Script Faq Backtests Alerts
5 min read · Updated
This is a plain-language FAQ for the two Pine questions that trip up almost everyone on TradingView: why a strategy's backtest looks better than live trading feels, and why an alert sometimes fires at a different moment or price than you expected. Both come from the same root cause — a mismatch between what data was actually available when a decision was made and what your eyes see after the bars have closed.
What is the difference between an indicator, a strategy, and an alert?
An indicator (indicator()) only draws and calculates — it plots lines, colors, and labels. A strategy (strategy()) simulates orders: it calls strategy.entry/strategy.exit, fills them against historical bars, and produces the Strategy Tester report with net profit, drawdown, and win rate. An alert is a separate notification layer TradingView fires when a condition is true; it does not trade anything by itself. Confusion starts when people read a backtest as a promise, or expect an alert to be a fill — they are three different jobs. For the deeper split, see the indicator vs. strategy guide.
Why does my backtest look great but live results don't match?
Four things inflate a backtest relative to reality, and all are fixable:
- Bar-close vs. intrabar assumptions. By default the Strategy Tester evaluates logic on bar close. If your condition uses the current bar's high/low or
closein a way that would only be known after the bar completes, the historical fill uses information the live trade could not have had. - Default fills at unrealistic prices. A market order in the tester fills at the open of the next bar; on a fast open that price can be several ticks or points away live.
- No commission or slippage modeled. If you leave
commission_valueandslippageat zero instrategy(), every simulated trade is friction-free. Add realistic values — even a fraction of a tick of slippage flips many "profitable" scalping systems negative. (Commission is set throughcommission_typeandcommission_value, not a singlecommissionfield.) - Tiny sample sizes. Twelve trades that all worked is not evidence of an edge; it is noise. See how to spot overfit trading systems before you trust any curve.
Worked example: a system showing +$4,200 over 40 ES trades with zero costs looks very different once you add a $2.50 round-turn commission and one tick of slippage on each side — entry and exit. One tick on ES is 0.25 point = $12.50, so two ticks is $25.00; add the $2.50 commission and you get $27.50 of friction per trade. Across 40 trades that is about $1,100, turning a headline +$4,200 into roughly +$3,100 — and worse if entries cluster on volatile opens where real slippage runs wider than a single tick.
What is repainting, and does it mean the script is broken?
Not necessarily. "Repainting" is an umbrella term for a script showing different values on historical bars than it produced in real time. The honest test is simple: could the decision have been made with the same information, at the same moment, live? A signal that only confirms after the bar closes has not lied to you — it just requires you to wait for the close. Trouble comes from scripts that plot a signal on the developing bar and then move it once the bar finishes, or that pull higher-timeframe data with lookahead enabled. If you want a signal you can act on, evaluate it on barstate.isconfirmed (bar close) rather than reacting to the live, still-forming bar.
Why did my alert fire late, at a different price, or twice?
- "Once Per Bar Close" vs. "Once Per Bar." An alert set to fire on the developing bar can trigger the instant a condition flickers true intrabar — then the bar closes elsewhere and it looks "wrong." Firing on bar close is later, but it matches the moment a confirmed strategy actually makes its decision (the strategy then acts at the next bar's open).
- The alert price is not a fill. The price in an alert message is the value when the alert fired, not a guaranteed execution price. Your broker fills you at the live market, which moves in the milliseconds between the alert and your order.
- Duplicate fires usually mean the condition stayed true across multiple ticks and the alert was set to fire every time rather than once per bar.
How does this connect to the Trade Plans method?
Trade Plans sidesteps the whole "did my signal repaint?" problem by giving you fixed, pre-planned levels instead of a reactive intrabar indicator. The daily levels — the Lean Lines drawn on ES, SPY, NQ, QQQ and 100+ tickers before the open — are calculated after the prior close and do not shift during the session, so there is nothing to repaint: the level where bias flips between headwinds and tailwinds is set before 9:30 ET and stays put. Our MyLinedChart Pine indicators only draw those levels; they never simulate orders, so they carry none of the backtest-fill assumptions above. If a Lean Line disagrees with your own alert or strategy, treat that as information — often it means "perch," i.e. wait. For the vocabulary, see the Trade Plans glossary.
Practical checklist before you trust any Pine result
- Confirm logic on
barstate.isconfirmedso backtest and live share the same decision moment. - Set realistic commission and slippage —
commission_type,commission_value, andslippageinstrategy()— for the instrument you actually trade. - Choose alert frequency deliberately — bar close for confirmation, once-per-bar to avoid duplicates.
- Judge results on enough trades to matter, not the handful that happened to win.
- Keep contextual overlays (like Lean Lines) separate from execution authority — the chart informs the decision; your written rules make it.
Related Reading
- Market Regime Indicator for TradingView (Risk-On/Off)
- Pine Script Indicator Vs Strategy Guide
- How To Spot Overfit Trading Systems
- Confirmation Vs First Touch Trading Guide
Sources
- TradingView Pine Script Docs: Strategies
- TradingView Pine Script Docs: Repainting
- TradingView Pine Script Docs: Alerts
- CFTC: Trading System Fraud Advisory
Educational content only. Not investment advice.
Educational content only. Not investment advice.