Back to all posts

PineScript Tutorial: Complete Beginner's Guide to TradingView Strategies

Learn PineScript from scratch. Build your first TradingView strategy step-by-step with entry logic, stop losses, and backtesting.

SpendDock Team··6 min read
PineScriptTradingViewTutorialBeginnerStrategy Development

PineScript is TradingView's built-in programming language for creating custom indicators and automated trading strategies. This tutorial walks you through PineScript from zero — no prior coding experience needed.

What is PineScript?

PineScript is a domain-specific language designed exclusively for TradingView. It lets you:

  • Create custom indicators and overlays
  • Build backtestable trading strategies
  • Set automated alerts based on conditions
  • Share strategies with the TradingView community

Current version: PineScript v6 (recommended for all new scripts)

Getting Started: The Pine Editor

  1. Open TradingView and log in
  2. Click the Pine Editor tab at the bottom of any chart
  3. You'll see a blank editor — this is where you write PineScript code
  4. Click Add to Chart to run your script

Script Structure

Every PineScript script starts with a version declaration and a function call that defines the script type:

pinescript
//@version=5
indicator("My Indicator", overlay=true)
// or
strategy("My Strategy", overlay=true)
  • indicator() — Creates a visual indicator (no backtesting)
  • strategy() — Creates a backtestable strategy with entries and exits

Your First Indicator: A Simple Moving Average

Let's start with the basics — plotting a moving average on your chart:

pinescript
//@version=5
indicator("Simple SMA", overlay=true)

// Define an input so users can change the period
length = input(20, "SMA Length")

// Calculate the SMA
smaValue = ta.sma(close, length)

// Plot it on the chart
plot(smaValue, "SMA", color=color.blue, linewidth=2)

What each line does:

  • input() — Creates a settings field users can adjust
  • ta.sma() — Calculates the Simple Moving Average
  • plot() — Draws the line on the chart
  • close — The closing price of each bar

Variables and Data Types

PineScript has a few core data types:

pinescript
//@version=5
indicator("Data Types Demo")

// Integer
myLength = 14

// Float
myMultiplier = 2.5

// Boolean
isUptrend = close > open

// String
myLabel = "Buy Signal"

// Color
myColor = close > open ? color.green : color.red

Built-in Variables

PineScript provides price data automatically:

VariableDescription
openOpening price
highHighest price
lowLowest price
closeClosing price
volumeBar volume
timeBar timestamp
bar_indexCurrent bar number

Common Indicators (ta. library)

pinescript
// Moving Averages
ta.sma(close, 20)     // Simple Moving Average
ta.ema(close, 20)     // Exponential Moving Average
ta.hma(close, 20)     // Hull Moving Average

// Oscillators
ta.rsi(close, 14)     // Relative Strength Index
ta.cci(close, 20)     // Commodity Channel Index
ta.stoch(close, high, low, 14)  // Stochastic

// MACD (returns a tuple)
[macdLine, signalLine, histogram] = ta.macd(close, 12, 26, 9)

// Bollinger Bands (returns a tuple)
[middle, upper, lower] = ta.bb(close, 20, 2)

// Volatility
ta.atr(14)             // Average True Range

// Trend
ta.adx(high, low, close, 14)  // Average Directional Index

Conditions and Crossovers

Detecting crossovers is essential for strategy building:

pinescript
//@version=5
indicator("Crossover Demo", overlay=true)

fastEMA = ta.ema(close, 9)
slowEMA = ta.ema(close, 21)

// Detect crossovers
bullishCross = ta.crossover(fastEMA, slowEMA)   // fast crosses ABOVE slow
bearishCross = ta.crossunder(fastEMA, slowEMA)   // fast crosses BELOW slow

// Visual markers on chart
plotshape(bullishCross, "Buy", shape.triangleup, location.belowbar, color.green, size=size.small)
plotshape(bearishCross, "Sell", shape.triangledown, location.abovebar, color.red, size=size.small)

plot(fastEMA, "Fast", color=color.green)
plot(slowEMA, "Slow", color=color.red)

Building Your First Strategy

Now let's build a complete backtestable strategy. We'll use an RSI strategy with a stop loss:

pinescript
//@version=5
strategy("My First Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10)

// --- Inputs ---
rsiLength = input(14, "RSI Length")
oversold = input(30, "Oversold Level")
overbought = input(70, "Overbought Level")
stopLossPercent = input.float(2.0, "Stop Loss %") / 100

// --- Calculate RSI ---
rsiValue = ta.rsi(close, rsiLength)

// --- Entry Conditions ---
longCondition = ta.crossover(rsiValue, oversold)

// --- Execute Trades ---
if (longCondition)
    strategy.entry("Long", strategy.long)
    stopPrice = close * (1 - stopLossPercent)
    strategy.exit("Stop Loss", "Long", stop=stopPrice)

// --- Exit Conditions ---
if (rsiValue > overbought)
    strategy.close("Long")

Key strategy functions:

FunctionPurpose
strategy.entry()Open a new position
strategy.close()Close a position by name
strategy.exit()Set stop loss and/or take profit
strategy.position_sizeCheck current position size

Adding a Take Profit

Extend the strategy with both stop loss and take profit:

pinescript
//@version=5
strategy("RSI with SL/TP", overlay=true)

rsiLength = input(14, "RSI Length")
slPercent = input.float(2.0, "Stop Loss %") / 100
tpPercent = input.float(4.0, "Take Profit %") / 100

rsiValue = ta.rsi(close, rsiLength)

if (ta.crossover(rsiValue, 30))
    strategy.entry("Long", strategy.long)
    strategy.exit("SL/TP", "Long",
         stop=close * (1 - slPercent),
         limit=close * (1 + tpPercent))

if (rsiValue > 70)
    strategy.close("Long")

Running a Backtest

After adding your strategy to a chart:

  1. Click the Strategy Tester tab at the bottom
  2. View the Overview for key metrics (net profit, win rate, drawdown)
  3. Check List of Trades for individual trade details
  4. Use Performance Summary for detailed statistics
  5. Adjust inputs and re-run to optimize

Important backtest settings:

  • Set realistic commission (0.1% for crypto, $1 per trade for stocks)
  • Use appropriate slippage (1-3 ticks)
  • Test on sufficient historical data (at least 100 trades)

Common PineScript Mistakes

  1. Using strategy() when you want indicator() — Strategies have overhead; use indicators for visual-only tools
  2. Not handling na values — Early bars may not have enough data for indicators
  3. Repainting — Using close on the current bar can repaint; use close[1] for confirmed signals
  4. Over-complicated conditions — Keep entry logic simple and readable
  5. No risk management — Always include stop losses in strategies

PineScript vs Other Languages

FeaturePineScriptMQL5NinjaScriptPython
PlatformTradingViewMetaTrader 5NinjaTraderAny
DifficultyEasyHardMediumMedium
BacktestingBuilt-inBuilt-inBuilt-inLibraries needed
Live tradingVia brokerDirectDirectVia API
CommunityHugeLargeMediumHuge

Skip the Learning Curve with SpendDock

Learning PineScript takes time. What if you could describe your strategy in plain English instead?

"Buy when RSI drops below 30 and price is above the 200 EMA. Set a 2% stop loss and 4% take profit. Only trade during New York session."

SpendDock generates production-ready PineScript code from natural language descriptions — plus exports to MQL5, NinjaScript, and Python. No coding required.

SpendDock advantages:

  • Describe strategies in plain English
  • Get clean, optimized PineScript instantly
  • Export to any platform (TradingView, MetaTrader, NinjaTrader, Python)
  • Built-in risk management and position sizing
  • Skip weeks of learning PineScript syntax

Conclusion

PineScript is the easiest way to start building trading strategies. Key takeaways:

  1. Start with simple indicators before building strategies
  2. Use ta.crossover() and ta.crossunder() for signal detection
  3. Always include stop losses with strategy.exit()
  4. Backtest with realistic commission and slippage settings
  5. Use close[1] instead of close to avoid repainting

Ready to build strategies faster? Try SpendDock and generate PineScript code from plain English descriptions.

Skip the Coding — Generate Your Strategy

Describe your trading strategy in plain English and get production-ready code for TradingView, MetaTrader, NinjaTrader, or Python.

Try SpendDock Free