kotak-logo
TradingView Simplified Logo Light Mode

Kotak

Stockshaala

Module 7
Pine Script – Build & Backtest
Course Index

Chapter 2 | 3 min read

Pine Scripts Basics (Variables, Plots, Conditions)

Pine Scripts™ may look like code, but at its core, it’s just a way of telling TradingView what you want to see on your chart.

In this chapter, you’ll learn the three building blocks of any Pine Scripts™:

  • Variables – Store data
  • Conditions – Define logic
  • Plots – Display outputs on your chart

By the end, you’ll be able to write a simple script that shows MACD signals and high-volume zones without needing any coding background.

A variable is like a container that holds information — for example, the closing price, volume, or an indicator value.

Example:

macdLine = ta.macd(close, 12, 26, 9)

Here:

  • macdLine is the variable name
  • ta.macd is the function that calculates MACD
  • close is the price data being used
  • 12, 26, 9 are the standard MACD periods

You can store any indicator or value you want:

price = close

avgVol = ta.sma(volume, 20)

Conditions help you check when something happens. For example, when the MACD crosses its signal line, or when volume spikes.

Example:

[macd, signal, hist] = ta.macd(close, 12, 26, 9)

bullishCross = ta.crossover(macd, signal)

highVol = volume > ta.sma(volume, 20)

These conditions return true or false.

  • bullishCross = true when MACD crosses above its signal line.

highVol = true when volume is greater than its 20-day average.

If you want to see a value or signal on the chart, you use plot() or plotshape().

Plot the MACD Histogram:

plot(hist, title="MACD Histogram", style=plot.style_columns, color=color.new(color.blue, 0))

Plot a Signal Dot for High Volume:

plotshape(highVol, title="High Volume", location=location.belowbar, color=color.orange, style=shape.circle)

This adds an orange dot below the candle whenever volume is above average.

//@version=6
indicator("MACD + Volume Highlight", overlay=false)

// 1. Calculate MACD
[macd, signal, hist] = ta.macd(close, 12, 26, 9)

// 2. Detect Conditions
bullishCross = ta.crossover(macd, signal)
highVol = volume > ta.sma(volume, 20)

// 3. Plots
plot(hist, title="MACD Histogram", style=plot.style_columns, color=bullishCross ? color.green : color.red)
plotshape(highVol, title="High Volume", location=location.belowbar, color=color.orange, style=shape.circle)

This script:

  • Plots the MACD histogram as green/red bars.
  • Adds orange dots below bars on high-volume days.
  • Highlights momentum shifts and volume confirmations together.

With just variables, conditions, and plots, you can already start building custom visual tools in TradingView.

No coding background needed — just logic and curiosity.

You’ve now seen how to combine momentum (MACD) with confirmation (Volume) to create your first data-driven visual.

Note: Pine Script™ is a trademark of TradingView Inc.

Is this chapter helpful?
Previous
What is Pine Scripts & Where It Fits in a Trader’s Toolkit
Next
Hands-On: Create Your First Indicator

Disclaimer: This article is for informational purposes only and does not constitute financial advice. It is not produced by the desk of the Kotak Neo Research Team, nor is it a report published by the Kotak Neo Research Team. The information presented is compiled from several secondary sources available on the internet and may change over time. Investors should conduct their own research and consult with financial professionals before making any investment decisions. Read the full disclaimer here.

Investments in securities market are subject to market risks, read all the related documents carefully before investing. Brokerage will not exceed SEBI prescribed limit. The securities are quoted as an example and not as a recommendation. SEBI Registration No-INZ000200137 Member Id NSE-08081; BSE-673; MSE-1024, MCX-56285, NCDEX-1262.

Discover our extensive knowledge center

Explore our comprehensive video library that blends expert market insights with Kotak's innovative financial solutions to support your goals.

PreviousCourse IndexNext