ScriptIQ - Examples

This tutorial will outline how to crete scripts by re-creating the Simple Moving Average study. This practical example will cover key features and usability modes, which you can then leverage to create your own scripts.


Remember that ScriptIQ uses CoffeeScript.

For more details on CoffeeScript see

The list of available field selectors and plotters can be found here: CIQ.Scripting.Builtins

The list of available built-in calculations can be found here: CIQ.Scripting.Descriptors

Example 1

First lets get something simple onto the chart, a simple series that will plot the 'Close' values of the current dataset of the chart. Inline comments will describe what is going on in the code.

# Define the study giving it a name
study("Hello World")

# Define a Study Descriptor input
field = input("Field", "Close")

# Now draw those 'Close' values and color the line chart orange
plot(field, color: "orange")            

You should now have a study panel with the 'Close' values from the dataset drawn as a line chart.


NOTE When defining the field input it is important to follow the naming scheme your dataSet defines. By default it is an OHLC format and it is case sensitive. For example, if you put 'close' instead of 'Close' nothing will be plotted.

Example 2

The first example is a nice start but somewhat useless. Let's make it something worth displaying.

study("Hello World")
field = input("Field", "Close")

# Define the Period input for the study
period = input("Period", 50)        

# Just for fun lets add this as an overlay to the chart
overlay = input('Overlay', true)    

# Use the sma builtin that calculates a simple moving average
simple = sma(field, period)    

plot(simple, color: "orange")

Now the study has some value in what it is displaying.


Here the sma builtin provided by the ScriptIQ api is being used and provides a simple moving average calculation. Replace sma with ema (Exponential), tma (Triangular), or any of the other moving average builtins defined on our API documentation page

Example 3

Say the sma calculation in the previous example was not to your liking.

You can either

-1 Create your own builtin -2 Do the calculation with ScriptIQ!

study("Hello World")
field = input("Field", "Close")
period = input("Period", 50)
offset = input("Offset", 10)

# Coffeescript function definition
sum = (length) ->                            
    total = 0
    while length
        value=dataset(field, -(--length))    # Use the dataset builtin, start at the last data point and work backwards
        return null if not value?
        total += value

# Call the defined sum function    
total = sum(period)                            
if total?
    average = total / period                # Calculate simple moving average
    plot(average, color: "orange")


This was just an example of the standard movng average study already defnded in our Studies library. With ScriptIQ you will be able to manipulate the data however you like and create exactly what you want!

More examples:

Customization of the built-in MACD

study("MACD Custom")
field = input("Field", "field")
fast = input("Fast Period", 12)
slow = input("Slow Period", 26)
signal = input("Signal Period", 9)
fastMA = ema(field, fast)
slowMA = ema(field, slow)
macd = fastMA - slowMA
signalMA = sma(macd, signal)
plot(macd, color: "orange", display:"MACD")

Simple dataSet field manipulation (add 4 to the selected field)

study("Custom Study")
field = input("Field", "field")
value = dataset(field)
plusfour = value + 4

Standard deviation

study("Custom Study")
field = input("Field", "field")
period = input("Period", 20)
value = dataset(field)
deviation = stddev(value, period)

Study overlay

study("Custom Overlay")
field = input("Field", "field")
fast = input("Fast Period", 12)
overlay = input("Overlay", true)
fastMA = ema(field, fast)
plot(fastMA, color: "blue", display:"Custom")

Computing a value using the previous bar

study("Custom Study")
field = input("Field", "field")
value = dataset(field)
previousBar = dataset(field, -1)
compare = value-previousBar

Next Steps: