Notice!
This is documentation for kScript v1.0, which may not include the latest features. For up-to-date documentation, see the latest version.

Anatomy of a kScript

Every kScript has three main parts:

1

Definition

Define the name, title, and placement of your indicator.

2

Logic

Logic part of the script where you calculate the data that will be plotted.

3

Plot

Visualize the data.

1

Basic Script Structure

We'll build an off-chart indicator that plots the difference between two EMAs as bars. Every kScript starts with a define() function that tells the platform what your script does and where to display it.

Step 1: Basic Structure

Explanation:

  • "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)"
2

Get the Data

To plot anything, we first need data. In kScript, that starts with subscribing to a data source. The source() function gives us OHLCV (trade data). For this example, we'll focus on close prices.

Step 2: Get the Data

Data source explained:

  • timeseries - A special type for time-series data. All other types can be declared using var
  • source("ohlcv", ...) - Subscribes to OHLCV data of the current chart
  • currentSymbol - Uses the currently selected trading pair
  • currentExchange - Uses the currently selected exchange

πŸ’‘ Pro Tip

Curious about the shape of this dataset? Click Apply, save the script, then hit the Data icon at the top to inspect it.

3

Build the Logic

Now let's calculate our EMAs. The standard library includes an ema() function. We'll calculate a fast EMA (7 periods) and slow EMA (14 periods), then find their difference.

Step 3: Build the Logic

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

πŸ’‘ Pro Tip

You can inspect the difference dataset to confirm calculations by using the Data inspection feature.

4

Plot the Data

Now let's plot the difference as bars using the plotBar() function. We'll use green bars for positive values and red bars for negative values.

Step 4: Plot the Data

plotBar parameters:

  • difference - The EMA difference data series to plot as bars
  • ["green", "red"] - Color array for positive/negative bars
  • 1 - Bar width
EMA Difference indicator plotted as bars on chart
5

Add User Inputs

Hardcoding values isn't very flexible. Instead, let's allow users to set the periods via inputs using the input() function. This makes your indicator customizable.

Step 5: Add User Inputs

Input parameters:

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

Note: We use var instead of timeseries because user inputs are scalar values, not time series.

πŸ’‘ Pro Tip

If you want to include the user settings in the indicator title, you can change the definition like this: define("EMA Difference", "offchart", true, "($fastPeriod, $slowPeriod)"); The title will then read as "EMA Difference (7, 14)".

Final Script

Here's your complete EMA Difference indicator script:

Complete EMA Difference Indicator Script

πŸŽ‰ Congratulations!

You've built your first kScript indicator. Try experimenting with different functions, plots, and inputs nextβ€”the sky's the limit.

πŸ“–
Introduction
Overview of kScript language
πŸš€
Quick Start
Get started with kScript basics
πŸ“‹
Function Reference
Complete API reference guide
πŸ”§
Core Concepts
Variables, data types & data sources
πŸ”—
Script Definition
Defining inputs and metadata
⏰
TimeSeries Management
Working with time-aligned data
🎯
Utility Functions
Helper functions and calculations
πŸ“ˆ
Moving Averages
SMA, EMA and trend-following indicators
πŸ“Š
Oscillators
RSI, Stochastic and momentum indicators
πŸ“ˆ
Trend Indicators
Trend direction and strength analysis
πŸ“‰
Volume Indicators
Volume-based analysis tools
πŸ“¦
Orderbook Functions
Market depth analysis tools
🎨
Plotting & Visualization
Chart rendering and styling
🌈
Color Functions
Color manipulation and styling
🏠
Go Home
Return to main landing page