SuperCharting
Sign in Get started Account Open charts

Learn / Strategies

Write your first indicator: a step-by-step Pine Script tutorial

Write your first indicator in Pine-compatible script: an EMA crossover with inputs, a coloured fill, markers and alerts, built step by step in SuperCharting.

To write your first indicator, open the Script Editor, declare indicator(), calculate a value and plot() it; everything else is detail you add one step at a time. This tutorial builds a complete EMA crossover indicator in five steps, with settings, a coloured fill, buy and sell style markers and alert conditions. Each step compiles, so you can add it to a chart and see the change.

The code uses Pine Script v6 syntax. Pine Script is TradingView's language; SuperCharting runs Pine-compatible scripts in the same syntax.

Before you start

You need a SuperCharting account (it is currently free) and a chart. This guide uses NSE:RELIANCE on the 1-hour timeframe, but any symbol works: XAUUSD, EURUSD or BTCUSD on 15 minutes are fine too. If the syntax is new to you, skim Pine v6 basics first; you do not need to memorise it.

How to write an indicator in SuperCharting

  1. Open the chart and press Ctrl + E (or click the Script Editor button in the right-hand bar). The editor opens beside the chart with a short template.
  2. Select all the template text and type or paste the code from step 1 below.
  3. Wait for the console under the code to show a ✓ line with your script's title. If it shows ✗ and a line number, fix that line first.
  4. Click Add to chart. The indicator appears on the chart and the console says "Added to chart".
  5. For each later step, change the code and click the update button (the same place as Add to chart). Your settings are kept.
  6. Press Ctrl + S, give the script a name in the Save script dialog, and it is stored in your account.

Build it in five steps

Step 1: one line on the chart

//@version=6
indicator("My first indicator", overlay=true)
plot(ta.sma(close, 20), "SMA 20", color=color.blue)

Three lines are enough. //@version=6 sets the language version. indicator(...) names the script, and overlay=true draws it on the price pane. plot() draws one value per bar: here the 20-bar simple moving average of the close. The first 19 bars stay empty because the average needs 20 bars of history.

Step 2: two averages with settings

Hard-coded numbers are awkward to change. input.int turns them into fields in the indicator's settings. Replace the SMA with two exponential moving averages:

Then fast = ta.ema(src, fastLen) and slow = ta.ema(src, slowLen), and plot both. After you update the chart, open the indicator's settings from its name in the chart legend: the three inputs are on the Inputs tab. Try 20 and 50 on a daily chart for a slower view. For more about the averages themselves, see moving averages: EMA and SMA.

Step 3: fill between the lines

plot() returns a handle that fill() can use. Colour the space teal when the fast line is above and orange when it is below. color.new(color.teal, 85) makes a colour 85% transparent, so the candles stay visible.

Step 4: mark the crossovers

ta.crossover(fast, slow) is true on the bar where the fast EMA moves from below to above the slow one; ta.crossunder is the opposite. plotshape() draws a small shape on those bars: a triangle below the bar for a cross up and above the bar for a cross down.

Step 5: add alert conditions

alertcondition() does not draw anything. It adds a named condition that you can pick when you create an alert. The message can carry placeholders such as {{ticker}}, which is replaced by the symbol when the alert fires.

The complete indicator

Here is the finished script with all five steps:

//@version=6
indicator("EMA Cross Signals", shorttitle="EMA X", overlay=true)

fastLen = input.int(9, "Fast EMA length", minval=1)
slowLen = input.int(21, "Slow EMA length", minval=1)
src     = input.source(close, "Source")

fast = ta.ema(src, fastLen)
slow = ta.ema(src, slowLen)

pFast = plot(fast, "Fast EMA", color=color.new(color.teal, 0), linewidth=2)
pSlow = plot(slow, "Slow EMA", color=color.new(color.orange, 0), linewidth=2)
fill(pFast, pSlow, color=fast > slow ? color.new(color.teal, 85) : color.new(color.orange, 85))

crossUp   = ta.crossover(fast, slow)
crossDown = ta.crossunder(fast, slow)
plotshape(crossUp, "Cross up", shape.triangleup, location.belowbar, color.teal, size=size.small)
plotshape(crossDown, "Cross down", shape.triangledown, location.abovebar, color.orange, size=size.small)

alertcondition(crossUp, "Fast EMA crossed above", "{{ticker}}: fast EMA crossed above slow EMA")
alertcondition(crossDown, "Fast EMA crossed below", "{{ticker}}: fast EMA crossed below slow EMA")

shorttitle="EMA X" is the short name shown in the chart legend. On a 1-hour NSE:RELIANCE chart you will see the two lines, the shaded band between them and a triangle at each cross. Crossovers lag price by design and give many false signals in a sideways market; that is normal for this kind of indicator, not a bug in your code.

Turn the crossover into an alert

  1. Right-click the indicator's name in the chart legend and choose Add alert on EMA X… (or press Alt + A with the chart selected).
  2. In the Create alert dialog, choose the script as the condition and pick "Fast EMA crossed above".
  3. Choose how often it may trigger, the expiry and where to be told, then create it.

Price alerts, triggers and delivery are covered in price alerts. If you want the crosses to place simulated trades instead, the next tutorial, write your first strategy, turns this same idea into a strategy() and tests it.

Frequently asked questions

Why does my indicator appear in a separate pane?

overlay is false by default. Add overlay=true to the indicator() line to draw on the price pane, then update the chart.

My script compiles but the chart shows an error. Why?

The compile check finds syntax mistakes. Some problems, such as a misspelt function name, only appear when the script runs on the bars. The console then shows a "Runtime:" line with the line number.

How do I change the colours without editing the code?

Open the indicator's settings and use the Style tab. Each plot has its own colour, thickness and style.

Can I share my indicator?

Yes. Click Publish script in the editor to add it to the community scripts library, with the code open, hidden or shared only with invited users.

Try it on a live chart

SuperCharting runs in your browser: charts, Pine-compatible scripts, the Strategy Tester, alerts and the screener, with Indian and global markets.

Create a free account More guides