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
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.
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.
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.
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
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.
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.
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
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.
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 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.
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.
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.
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
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 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.
Related
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.
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.