How to Create an Indicator in TradingView from Scratch
Pine Script is TradingView's programming language for creating custom indicators and strategies directly on price charts. You can calculate moving averages, detect chart patterns, or build entirely new metrics without leaving your browser. I built my first indicator back in January 2023 — a simple moving average crossover on BTC/USDT. Took about 30 minutes to get it running.
Getting to Know TradingView and Pine Script
Pine Script was designed for one job: turning market data into custom visuals and signals. Its syntax is simpler than most programming languages, but it handles heavy math — think multi-timeframe calculations or custom scoring systems.
I prefer Pine Script over Python for quick charting experiments because I skip the data export step. Everything runs inside TradingView's browser. That said, I haven't tested it for serious quantitative backtesting — the execution model processes bar by bar and differs from vectorized libraries.
The community keeps the language evolving. Here are the versions you'll run into:
| Version | Status | Key Characteristics |
|---|---|---|
| Pine Script v1 | Deprecated | The original version, no longer supported. |
| Pine Script v2 | Deprecated | Saw improvements but is also now deprecated. |
| Pine Script v3 | Deprecated | Introduced significant new features before being phased out. |
| Pine Script v4 | Still Supported | Brought major enhancements and is widely used. |
| Pine Script v5 | Latest & Recommended | Offers the newest features, best performance, and is the current standard. |
Pine Script v5 is what you should use. It has better performance, newer functions, and active support. Keep your scripts private or share them with millions of TradingView users — your call.
Accessing the Pine Script Editor
Open the Pine Script Editor from any chart in TradingView. Look at the bottom of your screen for the "Pine Editor" tab. Click it, and you'll see a workspace with a toolbar for saving and formatting.
The editor color-codes your code and flags errors as you type. That saved me hours when I was learning — I'd mistype a function name and the editor underlined it in red before I even ran the script.
If you're not sure where to start, click "New" at the top-right and select "Blank indicator script."
If coding line by line is not your preference, visual editors like Pineify let you build indicators through a point-and-click interface. You set the parameters, and it generates clean Pine Script automatically. I use it when prototyping an idea without going back and forth with the compiler.
Creating Your First Indicator: Step-by-Step
Step 1: Version Declaration and Indicator Setup
Why: The //@version=5 line tells TradingView which language rules to use. Without it, the editor throws an error immediately.
//@version=5
Next, the indicator() function names your script and decides where it draws. Set overlay=true for lines on the price chart, false for a separate pane below.
indicator("My First Indicator", overlay=true)
What can go wrong: Set overlay=true for an oscillator like RSI, and you'll get a line floating over the candles with no useful axis. Set overlay=false for a moving average, and it sits in its own pane away from the price — defeating its purpose.
Step 2: Creating Input Variables
Why: Hard-coding values means editing the script every time you want to change a parameter. Inputs expose those settings through the chart's indicator menu so anyone can adjust them.
length = input.int(20, "MA Length", minval=1)
This creates a slider labeled "MA Length" starting at 20 with a minimum of 1.
What can go wrong: No upper bound. If you only set minval=1, a user could enter 300 for the period on a daily chart and get a line so slow it misses every reversal. I usually add both minval and maxval with a sensible default.
Step 3: Performing Calculations
Why: Price data — open, high, low, close, volume — needs transformation before it becomes a useful signal. Pine Script has built-in functions for this.
smaValue = ta.sma(close, length)
ta.sma(close, length) averages the closing price over length bars.
What can go wrong: Using close when you should use hl2 (the high/low midpoint). On volatile days the close can sit far from the average price, and your indicator will react differently than expected. Check what price source fits your logic.
Step 4: Plotting Your Indicator
Why: Calculated values you can't see don't help you trade. plot() renders the result on your chart.
plot(smaValue, color=color.blue, linewidth=2)
What can go wrong: Plotting too many lines in the same color. I once stacked three moving averages — 10, 50, 200 — all in blue. The chart looked like a tangled phone cord. Use distinct colors and separate panes for different indicator types.
Step 5: Saving and Adding to Chart
Save your script in the Pine Editor, name it, then click "Add to Chart." Your indicator appears on the chart or in its own pane depending on the overlay setting.
What can go wrong: The script saves but nothing appears. Check for division by zero or references to non-existent data. I've debugged this more times than I'd like to admit — the culprit is usually a missing value on the first few bars.
Getting Your Indicator to Look and Work Just Right
Making It Your Own Visually
Click the gear icon next to your indicator's name on the chart. Adjust the period length, price source, line color, thickness, and opacity. Getting the visuals right makes the chart readable at a glance, especially with multiple indicators running.
Layering Indicators Like a Pro
Most traders stack two or three indicators. The trick is knowing where each one goes:
- Trend-following tools on the price chart. Moving averages and Bollinger Bands relate directly to price levels, so they work best as overlays.
- Momentum tools in a separate window below. RSI and MACD measure speed and force. A dedicated pane keeps them readable.
If the chart feels busy, lower the opacity of secondary indicators. On AAPL's daily chart last week, I kept a 50-period SMA opaque and dropped my volume profile to 40% opacity — clean enough to spot support immediately.
Testing Before You Trust It
Before committing real money, stress your indicator across different conditions:
- Timeframes — a 5-minute ES chart behaves nothing like a daily BTC chart.
- Market phases — trending up, trending down, sideways chop. Each exposes different weaknesses.
- Asset classes — stocks, forex, crypto. An indicator that works on SPY might fail on EUR/USD.
I tested my first breakout indicator on TSLA's 2023 rally. The signals looked great. Then I ran it on XAU/USD from 2022, and it triggered false breakouts all through the sideways consolidation. I haven't used it on forex since.
TradingView's built-in backtester replays your logic against historical bars. Run it before you go live.
Building Better TradingView Indicators
Start simple. Get comfortable with basic calculations — a moving average, a price channel — before tackling multi-condition strategies.
| Practice | Why It Matters |
|---|---|
| Clear variable names | entryPrice beats tempVar1 when you revisit the code months later |
| Comment your logic | One line per calculation block. Your future self will thank you |
| Test multiple timeframes | A pattern on 1-minute ETH/USDT might vanish on the daily chart |
Performance matters. TradingView recalculates your indicator on every bar, so inefficient loops slow things down. I avoid nested loops and use var for values that only need computing once.
Once you've built something solid, share it on TradingView's community. Feedback from other traders helps you spot blind spots. Some developers sell premium indicators if they've built something genuinely unique.
FAQ
Q: Do I need coding experience to build indicators in TradingView? A: Not really. Pine Script is simpler than most programming languages. If you've used formulas in a spreadsheet, you can pick up the basics. A little logical thinking helps when things go wrong.
Q: Can I make an indicator that signals buy and sell? A: Yes. You write condition checks in Pine Script — for example, when a fast moving average crosses above a slow one, plot a buy arrow. You can also trigger alerts or change bar colors.
Q: Where do I find indicators other people created? A: Click "Indicators" at the top of your chart, then the "Public Library" tab. Thousands of free scripts you can add with one click.
Q: Can I edit a public indicator to fit my needs? A: Yes. Open the source code of any public script, modify it, and save a copy. It's one of the best ways to learn Pine Script — you see real working code and adapt it to your style.
Q: What's the difference between overlay and non-overlay indicators?
A: Overlay draws on the price chart — moving averages, for example. Non-overlay gets its own pane below, like RSI or MACD. Use overlay=true for price-related lines and overlay=false for oscillators.
Q: How many indicators can I add to one chart? A: Free accounts have lower limits; paid accounts can add more. Practically, more than five creates visual clutter. Pick two or three that work together.
Next Steps
You have a working indicator on your chart. Open the Pine Editor, make one change — switch ta.sma to ta.ema, or add a second plot — and see what happens. The fastest way to learn Pine Script is to break something and fix it.
If you hit a wall, the TradingView community forums have answered most beginner questions at least once. Search before you ask. Resources like TradingView Pine Script Programming from Scratch fill gaps the documentation skips.
When a specific error stops you, guides like How to Fix No Viable Alternative at Character Pine Script Error can turn a thirty-minute frustration into a two-minute fix.
Study how public indicators work. I spent an afternoon reading through the built-in VWAP code and learned more about Pine Script's barstate functions than from any tutorial. For example, VWAP Pine Script implementations show you how to handle intraday resets and volume weighting.
The real test is your indicator running on live data. Load a chart, watch how it behaves bar by bar, and note where it surprises you. Those surprises — false signals, late entries, over-fitted conditions — tell you exactly where the logic needs work.
Open the editor and try one new thing today. Even a broken indicator teaches you something.

