Pine Script basics: write your first indicator
A gentle introduction to Pine Script: series, plots, inputs and a moving-average crossover - step by step, ready to run on a live chart.
What Pine Script is
Pine Script® is a small programming language designed for one job: calculating things on price charts. It is popular because it hides almost everything that makes programming hard. You don't write loops over candles or manage memory - you describe what to calculate for one candle, and the language runs your code on every candle from left to right.
TradeMuv includes its own engine that runs Pine Script code (most v4 and v5 scripts), so you can write a script, paste one you found, or open one from the community library and see it on a live chart.
The key idea: series
In Pine, a variable like close is not a single number. It is a series: the close of the current candle, with the history of every previous candle attached. close means "this candle's close" and close[1] means "the previous candle's close". Most built-in functions take a series and return a series.
Step 1: the smallest script
//@version=5
indicator("My first indicator", overlay=true)
plot(close)
Three lines: the version, a declaration that this is an indicator drawn on top of the price (overlay=true), and a plot of the close. Open the Pine Editor on TradeMuv, paste it, and click Add to chart. A line follows the closing prices.
Step 2: two moving averages
//@version=5
indicator("EMA cross", overlay=true)
fast = ta.ema(close, 9)
slow = ta.ema(close, 21)
plot(fast, "Fast EMA", color=color.aqua)
plot(slow, "Slow EMA", color=color.orange)
ta.ema(close, 9) calculates a 9-period exponential moving average of the close. The ta. namespace holds the technical-analysis functions: ta.sma, ta.rsi, ta.atr, ta.highest and many more. (Not sure what an EMA is? See moving averages explained.)
Step 3: let the user choose the lengths
fastLen = input.int(9, "Fast length", minval=1)
slowLen = input.int(21, "Slow length", minval=1)
fast = ta.ema(close, fastLen)
slow = ta.ema(close, slowLen)
input.int creates a setting. After adding the script, open its settings from the indicator's legend on the chart and change the lengths without touching the code. There are inputs for numbers, colours, sources, sessions, symbols and more.
Step 4: mark the crossovers
up = ta.crossover(fast, slow)
down = ta.crossunder(fast, slow)
plotshape(up, "Cross up", shape.triangleup, location.belowbar, color.green, size=size.small)
plotshape(down, "Cross down", shape.triangledown, location.abovebar, color.red, size=size.small)
bgcolor(fast > slow ? color.new(color.green, 92) : color.new(color.red, 92))
ta.crossover(a, b) is true on the candle where a crosses above b. plotshape draws a marker on those candles, and bgcolor tints the background depending on which average is on top. The ? : is a compact if/else: condition ? value if true : value if false.
Step 5: from indicator to strategy
Once a condition is a series of true/false values, you can combine it with other conditions using and / or, or turn the script into a strategy. Replace indicator(...) with strategy(...) and send orders when the conditions are true:
if up
strategy.entry("Long", strategy.long)
if down
strategy.close("Long")
TradeMuv's strategy tester then simulates the trades on the chart and shows a report with the list of trades and the results.
Remember: a strategy report on historical data is hypothetical. It does not include every real-world cost, and a rule that fits the past perfectly often fails in the future. Test on different markets and periods, and keep rules simple.
Common beginner mistakes
- Forgetting the version line. Start every script with
//@version=5. - Indentation. Like Python, Pine uses indentation for blocks under
if,forand functions. - Looking into the future. Using the current candle's close to decide something "at its open" makes backtests look better than reality.
- Copying scripts you don't understand. Read the code first; a script is only as good as its logic.
Next steps
Browse the community library in the Pine Editor to see how others build indicators, change one small thing at a time, and watch the result on the chart. When your script works, you can publish it so other TradeMuv users can add it to their charts too.
Pine Script® is a trademark of its owner. TradeMuv's script engine is an independent implementation and is not affiliated with that owner.
One account. Every screen.
TradeMuv is the free trading chart platform: live charts, footprint and order flow, bar replay, paper trading, Pine Script, unlimited indicators and alerts - on your computer, tablet and phone, with one free account.
Explore all TradeMuv featuresFree · no card · no downloadEverything in this guide works in TradeMuv, free - no card, no trial.
Create free accountThis guide is for education only and is not financial advice. Trading involves risk. Risk disclaimer.
Keep learning
How to set, move and manage price alerts so the chart watches the market for you - and the habits that stop alerts from turning into noise.
5 min readRiskRisk management basics: position size and stop lossWhy risk management matters more than entries: the 1% rule, how to size a position from your stop, risk-reward and expectancy - with worked examples.
8 min readBasicsCandlestick patterns: the 10 worth knowingThe candlestick patterns worth knowing: doji, hammer, engulfing, morning star and more - what each one shows and how to read it in context.
8 min read