kotak-logo
Products
Platform
Research
Market
Learn
Partner
Support
IPO
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.

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

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