← Back to Blog

How to Build a TradingView Indicator: Step-by-Step Pine Script Guide

Introduction: Why Build Custom Indicators?

TradingView's built-in indicators are useful, but they're generic. To gain a real edge in trading, you need custom indicators tailored to your specific strategy. This is where Pine Script comes in.

Pine Script is TradingView's programming language that lets you create custom indicators, strategies, and alerts. In this guide, you'll learn to build your first indicator from scratch.

What is Pine Script?

Pine Script is a lightweight programming language designed specifically for TradingView. It's beginner-friendly yet powerful enough to create complex trading systems.

Key Advantages

  • Easy to learn (even for non-programmers)
  • Direct access to price data and technical indicators
  • Can create alerts and automate trading signals
  • Share indicators with the TradingView community
  • Free to use on TradingView

Getting Started: The Pine Script Editor

Step 1: Open the Pine Script Editor

  1. Go to TradingView.com and log in
  2. Open any chart
  3. Click "Pine Script Editor" (bottom left)
  4. Click "New" to create a new script

Step 2: Understand the Basic Structure

Every Pine Script indicator has this basic structure:

//@version=5
indicator("My First Indicator", overlay=true)

// Your code here

plot(close)

Breakdown:

  • //@version=5 - Specifies Pine Script version
  • indicator() - Declares this is an indicator
  • overlay=true - Displays on the price chart
  • plot() - Draws a line on the chart

Building Your First Indicator: Simple Moving Average

Step 1: Create the Basic Structure

//@version=5
indicator("Simple Moving Average", overlay=true)

// Define the period
period = input(20, title="SMA Period")

// Calculate SMA
sma = ta.sma(close, period)

// Plot the SMA
plot(sma, color=color.blue, linewidth=2)

Step 2: Add User Input

The input() function lets users customize your indicator:

period = input(20, title="SMA Period")
color_input = input(color.blue, title="Line Color")

Step 3: Test Your Indicator

  1. Click "Add to Chart"
  2. Your SMA should appear on the chart
  3. Right-click → Settings to adjust the period

Advanced: Building an FVG Detector

Now let's build something more useful: an indicator that detects Fair Value Gaps.

//@version=5
indicator("FVG Detector", overlay=true)

// Check for bullish FVG
bullish_fvg = high[2] < low[0]

// Check for bearish FVG
bearish_fvg = low[2] > high[0]

// Plot signals
plotshape(bullish_fvg, style=shape.triangleup, color=color.green)
plotshape(bearish_fvg, style=shape.triangledown, color=color.red)

How It Works

  • high[2] - High price 2 candles ago
  • low[0] - Current candle's low
  • bullish_fvg - True when gap detected
  • plotshape() - Draws a triangle on the chart

Creating Alerts

Alerts notify you when your indicator triggers a signal:

alertcondition(bullish_fvg, title="Bullish FVG", message="Bullish FVG detected!")

Now you can set up alerts in TradingView to notify you via email, SMS, or push notification.

Common Pine Script Functions

Price Data Functions

  • close - Current candle's closing price
  • open - Opening price
  • high - Highest price
  • low - Lowest price
  • volume - Trading volume

Technical Indicator Functions

  • ta.sma() - Simple Moving Average
  • ta.ema() - Exponential Moving Average
  • ta.rsi() - Relative Strength Index
  • ta.macd() - MACD
  • ta.stoch() - Stochastic

Plotting Functions

  • plot() - Draw a line
  • plotshape() - Draw shapes (triangles, circles, etc.)
  • plotbar() - Draw bars
  • fill() - Fill between two plots

Debugging Your Indicator

Common Errors

  1. Syntax Error: Check for missing parentheses or semicolons
  2. Undefined Variable: Make sure you defined all variables
  3. Type Mismatch: Ensure variables are the correct type (number, string, bool)

Using Print Statements

Debug your code with print statements:

sma = ta.sma(close, 20)
print("SMA: " + str(sma))

Publishing Your Indicator

Once your indicator is working, you can publish it to the TradingView community:

  1. Click "Publish Script" in the Pine Script editor
  2. Choose "Publish as Public Library"
  3. Add description and category
  4. Click "Publish"

Your indicator is now available for other traders to use!

Best Practices for Pine Script

Rule 1: Always use the latest Pine Script version (@version=5 or higher)
  • Use descriptive variable names
  • Add comments to explain your logic
  • Test on multiple timeframes
  • Backtest your strategy before trading
  • Use proper risk management with alerts

Next Steps: Building Trading Systems

Once you master indicators, you can create full trading systems with entry and exit signals. This allows you to backtest your strategy and even automate trades.

The possibilities are endless: SMT divergence detectors, FVG finders, market structure analyzers, and more.

Conclusion

Pine Script is a powerful tool that separates professional traders from amateurs. By learning to code custom indicators, you gain the ability to automate your analysis and execute your strategy with precision.

Start simple, practice consistently, and gradually build more complex indicators. Before you know it, you'll have a complete trading system tailored to your strategy.

Related Articles

Learn more about trading strategies and systems:

Need Custom Pine Script Development?

I develop professional-grade TradingView indicators and trading systems. Whether you need an SMT divergence detector, FVG analyzer, or complete automated trading system, I can build it for you.

Request Custom Indicator Discuss Your Idea