---
title: Quick Start Guide
description: >-
  Get started with kScript in 5 minutes. Build your first EMA Difference
  indicator step by step.
---


<div class="flex gap-3 mb-6">
  <span class="inline-flex items-center gap-1.5 px-3 py-1 rounded-full bg-green-50 text-green-600 text-sm font-medium">
    Beginner
  </span>
  <span class="inline-flex items-center gap-1.5 px-3 py-1 rounded-full bg-gray-100 text-gray-600 text-sm font-medium">
    5 min read
  </span>
</div>

This guide walks you through creating your very first indicator in kScript. We'll build an off-chart indicator that plots the difference between two EMAs as bars.

## Anatomy of a kScript

Every kScript has three main parts:

{% stepper %}
{% step %}
### Definition

Define the name, title, and placement of your indicator
{% endstep %}

{% step %}
### Logic

Calculate the data that will be plotted
{% endstep %}

{% step %}
### Plot

Visualize the data
{% endstep %}
{% endstepper %}

## Step 1: Basic Script Structure

We'll build an off-chart indicator that plots the difference between two EMAs as bars. Every kScript v2 starts with a compiler annotation `//@version=2` followed by a [`define()`](/kscript/functions/script-definition#define) function that tells the platform what your script does and where to display it.

```javascript title="Step 1: Basic Structure" lines wrap
//@version=2

// definition
define(title="EMA Difference", position="offchart", axis=true, customTitle="($fastPeriod, $slowPeriod)");

// logic

// plot
```

**Explanation:**
- `//@version=2` - Required compiler annotation that tells kScript to use version 2 syntax and features
- `"EMA Difference"` - The display name in the UI
- `"offchart"` - Shows the indicator in a separate panel below the main chart
- `true` - Creates an independent Y-axis for better scaling
- `"($fastPeriod, $slowPeriod)"` - Adds input values to the title, so it displays as "EMA Difference (7, 14)"

## Step 2: Get the Data

To plot anything, we first need data. In kScript, that starts with subscribing to a data source. The [`ohlcv()`](/kscript/core-concepts/data-sources#ohlcv) function gives us OHLCV (trade data). For this example, we'll focus on close prices.

```javascript title="Step 2: Get the Data" lines wrap
//@version=2

// definition
define(title="EMA Difference", position="offchart", axis=true, customTitle="($fastPeriod, $slowPeriod)");

// logic
timeseries ohlcvDataSet = ohlcv(symbol=currentSymbol, exchange=currentExchange);

// plot
```

**Data source explained:**
- `timeseries` - A special type for time-series data. All other types can be declared using `var`
- `ohlcv(...)` - Gets OHLCV data of the current chart
- `currentSymbol` - Uses the currently selected trading pair
- `currentExchange` - Uses the currently selected exchange

{% hint style="success" %}
Curious about the shape of this dataset? Click **Apply**, save the script, then hit the **Data** icon at the top to inspect it.
{% endhint %}


  <video autoplay muted loop playsinline class="kscript-tutorial-video w-full aspect-auto rounded-xl" src="/features/kScript-data.mp4"></video>


## Step 3: Build the Logic

Now let's calculate our EMAs. The standard library includes an [`ema()`](/kscript/functions/moving-averages#ema) function. We'll calculate a fast EMA (7 periods) and slow EMA (14 periods), then find their difference.

```javascript title="Step 3: Build the Logic" lines wrap
//@version=2

// definition
define(title="EMA Difference", position="offchart", axis=true, customTitle="($fastPeriod, $slowPeriod)");

// logic
timeseries ohlcvDataSet = ohlcv(symbol=currentSymbol, exchange=currentExchange);

var fastEma = ema(source=ohlcvDataSet, period=7, priceIndex=4); // dataset, period, close column (4)
var slowEma = ema(source=ohlcvDataSet, period=14, priceIndex=4); // dataset, period, close column (4)

// Alternatively, you can use the .close property for cleaner syntax
// var fastEma = ema(source=ohlcvDataSet.close, period=7);
// var slowEma = ema(source=ohlcvDataSet.close, period=14);

var difference = fastEma - slowEma;

// plot
```

**EMA calculations:**
- `ema(ohlcvDataSet, 7, 4)` - Fast EMA with 7 periods using close price (column 4)
- `ema(ohlcvDataSet, 14, 4)` - Slow EMA with 14 periods using close price (column 4)
- `fastEma - slowEma` - Calculate the difference between the two EMAs

{% hint style="success" %}
You can inspect the `difference` dataset to confirm calculations by using the Data inspection feature.
{% endhint %}


  <video autoplay muted loop playsinline class="kscript-tutorial-video w-full aspect-auto rounded-xl" src="/features/kScript-inspect.mp4"></video>


## Step 4: Plot the Data

Now let's plot the difference as bars using the [`plotBar()`](/kscript/functions/plotting#plotBar) function. We'll use green bars for positive values and red bars for negative values.

```javascript title="Step 4: Plot the Data" lines wrap
//@version=2

// definition
define(title="EMA Difference", position="offchart", axis=true, customTitle="($fastPeriod, $slowPeriod)");

// logic
timeseries ohlcvDataSet = ohlcv(symbol=currentSymbol, exchange=currentExchange);

var fastEma = ema(source=ohlcvDataSet.close, period=7); // dataset, period, close column
var slowEma = ema(source=ohlcvDataSet.close, period=14); // dataset, period, close column

var difference = fastEma - slowEma;

var colorIndex = difference > 0 ? 0 : 1;

// plot
plotBar(value=difference, width=1, colors=["green", "red"], colorIndex=colorIndex, label=["EMA Difference"], desc=["Fast EMA - Slow EMA"]);
```

**plotBar parameters:**
- `difference` - The EMA difference data series to plot as bars
- `["green", "red"]` - Color array for positive/negative bars
- `colorIndex` - Index to select specific color from colors array (0 for green, 1 for red)
- `1` - Bar width


  <img class="kscript-tutorial-video w-full rounded-xl" src="/images/kScript/kScript-plot.png" alt="EMA Difference indicator">


## Step 5: Add User Inputs

Hardcoding values isn't very flexible. Instead, let's allow users to set the periods via inputs using the [`input()`](/kscript/functions/script-definition#input) function. This makes your indicator customizable.

```javascript title="Step 5: Add User Inputs" lines wrap
//@version=2

// definition
define(title="EMA Difference", position="offchart", axis=true, customTitle="($fastPeriod, $slowPeriod)");

var fastPeriod = input(name="fastPeriod", type="number", defaultValue=7, label="Fast Period");
var slowPeriod = input(name="slowPeriod", type="number", defaultValue=14, label="Slow Period");

// logic
timeseries ohlcvDataSet = ohlcv(symbol=currentSymbol, exchange=currentExchange);

var fastEma = ema(source=ohlcvDataSet.close, period=fastPeriod);
var slowEma = ema(source=ohlcvDataSet.close, period=slowPeriod);

var difference = fastEma - slowEma;

var colorIndex = difference > 0 ? 0 : 1;

// plot
plotBar(value=difference, width=1, colors=["green", "red"], colorIndex=colorIndex, label=["EMA Difference"], desc=["Fast EMA - Slow EMA"]);
```

**Input parameters:**
- `fastPeriod` - Key for the input (used in code)
- `"number"` - Input type (number)
- `7` - Default value
- `"Fast Period"` - Label shown to users

{% hint style="info" %}
We use `var` instead of `timeseries` because user inputs are scalar values, not time series.
{% endhint %}

{% hint style="success" %}
If you want to include the user settings in the indicator title, you can change the definition like this:
`define(title="EMA Difference", position="offchart", axis=true, customTitle="($fastPeriod, $slowPeriod)");`
The title will then read as "EMA Difference (7, 14)".
{% endhint %}

## Final Script

Here's your complete EMA Difference indicator script:

```javascript title="Complete EMA Difference Indicator Script" lines wrap
//@version=2

// definition
define(title="EMA Difference", position="offchart", axis=true, customTitle="($fastPeriod, $slowPeriod)");

var fastPeriod = input(name="fastPeriod", type="number", defaultValue=7, label="Fast Period");
var slowPeriod = input(name="slowPeriod", type="number", defaultValue=14, label="Slow Period");

// logic
timeseries ohlcvDataSet = ohlcv(symbol=currentSymbol, exchange=currentExchange);

var fastEma = ema(source=ohlcvDataSet.close, period=fastPeriod);
var slowEma = ema(source=ohlcvDataSet.close, period=slowPeriod);

var difference = fastEma - slowEma;

var colorIndex = difference > 0 ? 0 : 1;

// plot
plotBar(value=difference, width=1, colors=["green", "red"], colorIndex=colorIndex, label=["EMA Difference"], desc=["Fast EMA - Slow EMA"]);
```

{% hint style="info" %}
**Congratulations!** You've built your first kScript indicator. Try experimenting with different functions, plots, and inputs next.
{% endhint %}

## What's Next?


<table data-view="cards"><tbody>
<tr><td>Moving Averages</td><td>Learn about SMA, EMA, WMA, and other moving average functions</td><td><a href="/kscript/functions/moving-averages"></a></td></tr>
<tr><td>Core Variables</td><td>Understand data types and built-in variables in kScript</td><td><a href="/kscript/core-concepts/core-variables"></a></td></tr>
<tr><td>Advanced Plotting</td><td>Master visualization techniques and chart styling options</td><td><a href="/kscript/functions/plotting"></a></td></tr>
</tbody></table>

