

Kotak
Stockshaala
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.
1. Variables – Storing Price or Indicator Values
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)
2. Conditions – Logic You Can Act On
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.
3. Plots – Showing Things on the Chart
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.
Putting It All Together: Sample Script
//@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.
Summary Table
Variable | avgVol = ta.sma(volume, 20) | Stores a value |
Condition | ta.crossover(macd, signal) | Defines a trading signal |
Plot | plot(hist) | Displays indicator on chart |
Shape Plot | plotshape(highVol) | Marks special events |
To Sum Up
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.
Recommended Courses for you
Beyond Stockshaala
Discover our extensive knowledge center
Learn, Invest, and Grow with Kotak Videos
Explore our comprehensive video library that blends expert market insights with Kotak's innovative financial solutions to support your goals.














