WhitmanTrading

Pine Script: Where Vague Rules Break

Pine Script is the scripting language built into TradingView, used to write custom indicators and strategies that run on the chart. Its value is less the indicator produced than the discipline imposed: code will not accept a rule stated loosely enough to argue with afterwards.

How it works

A candlestick chart of the site's shared price history. The headline on the chart reads: The scripting language inside the chart.
The scripting language inside the chart. Illustrative chart - not real market data.

It is a small language that runs inside the charting platform. You write a script, it executes over the bars on screen, and it draws lines, plots values or places simulated orders.

A gently rising stretch of the long price series with an account equity curve beneath it. The headline on the chart reads: It turns a vague rule into something testable.
It turns a vague rule into something testable. Illustrative chart - not real market data.

The point is not the indicator, it is the specificity. “Enter on a strong breakout” cannot be written down. “Enter when the close exceeds the highest close of the previous twenty bars” can, and the difference between those two sentences is most of what separates a method from a habit.

A calmly advancing stretch of the long price series with a slowly rising equity curve beneath it. The headline on the chart reads: Every variable is a whole series, not one number.
Every variable is a whole series, not one number. Illustrative chart - not real market data.

Every variable holds a value for each bar, not a single number. The script runs once per bar, and what looks like one variable is a whole history you can index backwards into. This is the concept that confuses everyone arriving from ordinary programming, and understanding it explains most of the rest.

A flat, quiet stretch of the long price series with a gradually rising equity curve beneath it. The headline on the chart reads: And a signal that moves afterwards is the classic bug.
And a signal that moves afterwards is the classic bug. Illustrative chart - not real market data.

Repainting is the failure to know about. A script that changes what it drew on a bar after that bar has closed is showing you a signal nobody could have acted on. It is easy to write by accident and it makes a strategy look excellent.

What it can and cannot see

A strongly rising stretch of the long price series with an account curve breaching its limit. The headline on the chart reads: The last bar is not finished while you are watching it.
The last bar is not finished while you are watching it. Illustrative chart - not real market data.

On history every bar is complete; live, the last one is not. A condition true mid-bar can be false by the close, so the same script behaves differently in real time from how it behaved in the backtest.

A choppy, directionless stretch of the long price series. The headline on the chart reads: It runs on the chart, which limits what it can see.
It runs on the chart, which limits what it can see. Illustrative chart - not real market data.

It is a chart-side language, not a trading system. No order book, no account balance, no arbitrary external data. That constraint is what keeps it simple and it is also the ceiling on what it can do.

A declining stretch of the long price series. The headline on the chart reads: And its backtester fills orders optimistically.
And its backtester fills orders optimistically. Illustrative chart - not real market data.

The built-in tester is generous by default. It assumes you were filled, and unless you set the commission and slippage yourself the equity curve it draws is a curve for a market without costs.

In practice

A 72-bar candlestick section of the shared price history with an account curve shown with and without fees. The headline on the chart reads: So set commission and slippage before any result.
So set commission and slippage before any result. Illustrative chart - not real market data.

Set the costs before you look at a single result. On this site’s shared history a round trip is 2% of a median bar’s range and 45% of the smallest bar — small against a big move, decisive against a small one.

A candlestick chart with a volume histogram beneath it, with the volume histogram emphasised. The headline on the chart reads: Participation is available and rarely used well.
Participation is available and rarely used well. Illustrative chart - not real market data.

Volume is available and mostly ignored. Scripts overwhelmingly read price alone, partly by convention and partly because participation data is less consistent across instruments.

A long-horizon candlestick view of the same price series. The headline on the chart reads: A higher timeframe needs far more history to test.
A higher timeframe needs far more history to test. Illustrative chart - not real market data.

A slower chart holds fewer bars. The platform limits how far back a script can see, so a daily strategy has a far thinner sample to be judged on than a five-minute one.

A candlestick series containing several opening gaps, with the largest opening gap marked. The headline on the chart reads: And it will fill a stop the market jumped past.
And it will fill a stop the market jumped past. Illustrative chart - not real market data.

An opening gap is where the tester lies most. It will report a stop filled at its level across a gap that jumped straight over it, which quietly deletes the worst trade from the record.

A declining stretch of the long price series, with the entry price and the level at which a stop would trigger drawn as horizontal lines. The headline on the chart reads: The stop has to be written in, not assumed.
The stop has to be written in, not assumed. Illustrative chart - not real market data.

The stop exists only if you code it. A strategy script has no risk management it was not given, and an exit you meant to take is not an exit the tester knows about.

A candlestick chart of the site's shared price history, annotated with the round-trip cost. The headline on the chart reads: Every round trip costs 2% of a bar.
Every round trip costs 2% of a bar. Illustrative chart - not real market data.

Trade count is a design decision. Every signal a script generates has a price attached, so how often it fires belongs in the plan rather than falling out of it.

Learning it without writing an indicator

The fastest useful exercise is to code a rule you already trade. Not a new idea — an existing one, the one you would describe to somebody over coffee. You will get perhaps two lines in before hitting the part you have never actually specified.

That moment is the whole return on the effort. Whether you finish the script barely matters; you now know which of your rules were rules and which were impressions. Most people discover they have three real conditions and one large area of judgement, which is worth knowing before risking money on it.

The second exercise is to break something deliberately. Write a script that peeks at the next bar, watch the equity curve become perfect, and remember what that looks like — because the next time you see a curve that shape in somebody else’s screenshot, you will recognise it.

What Pine Script is not

It is not a trading system. It runs on a chart, not an account.

It is not proof anything works. It is a way of asking.

It is not hard to learn. It is hard to be honest in.

And a script that tests well is not a strategy. It is a hypothesis.

When it fails

A sideways, range-bound candlestick series. The headline on the chart reads: In a range the indicator fires constantly.
In a range the indicator fires constantly. Illustrative chart - not real market data.

In a trading range a signal script fires over and over, and each one costs. A condition that looked selective on a trending chart turns out to have been selecting for the trend.

The second failure is repainting you did not notice. The strategy is not wrong so much as impossible.

A third is optimising in the editor. Changing a number, re-running, and keeping what looks best is overfitting with a nicer interface, and the platform makes it effortless.

A fourth is trusting default costs. Zero commission and zero slippage is a market that does not exist.

A fifth is judging a slow strategy on a short history. Fewer bars means fewer trades, and a handful of trades cannot separate a method from luck.

And a sixth is confusing the script with the discipline. The code cannot make you take the signal it generates, which is where most systematic attempts actually fail.

The original data

Of the 24,971 videos in research/search-study-corpus.jsonl, 225 have “pine script” in the title, at a median of 504 views across 103 channels — against 1,028 mentioning TradingView at a median of 4,914. The counts are recorded in research/broker-coverage.json.

A strongly rising stretch of the long price series, cut short at the decision bar. The headline on the chart reads: The script shows a perfect entry last week. Real?
The script shows a perfect entry last week. Real? Illustrative chart - not real market data.

A median of 504 views is the lowest of any well-supplied term measured on this site. Two hundred and twenty-five people made a tutorial and almost nobody watched: coding is where trading-content demand collapses, because it is the point at which the thing stops being a story and becomes work. Notice what that implies for the platform videos too — 1,028 covering TradingView at a median of 4,914 is a crowded subject with a modest audience each.

The answer to that final question is: check whether the signal is still there on the bar after next. Reload the chart, let two more bars close, and see whether last week’s perfect entry is still drawn where it was. If it moved, the script is repainting and the result is fiction — and that single check takes thirty seconds against the hours a fitted strategy will otherwise cost you.

Backtesting is what a strategy script is for, along with the six ways its output misleads. Overfitting is the specific failure an editor with an optimise button invites. And systematic trading is the wider discipline a script is one small part of.

What I actually do

I did not learn much from the indicators I wrote. I learned from the rules that would not compile. Every time I sat down to code something I had been trading by feel, the language stopped me at the same place — the bit I had never actually decided. What counts as a strong close. How far is far enough. You can argue with yourself about that forever on a chart, and you cannot argue with an editor that wants a number.

— Michael Whitman

This page is educational, not financial advice. Test every idea on your own charts before risking money. Some links on this page earn a commission if you buy through them. It costs you nothing and it does not decide what appears here or in what order — how these pages are made is set out in our methodology.