Black Friday 30% off Basic and ProBlack Friday −30%Ends in––d––h––m––sSee the deal

Your first indicator

From an empty editor to a moving average on the chart, in five steps.

Animation: a new indicator added to the chart
Create indicator, a name, Add to chart: the average is on the chart.
  1. Open the editor

    In the Indicators dialog press Create indicator, at the bottom of the sections column. The editor opens in the right panel with a ready-made example.

  2. Give it a name

    Type it in the field above the script, up to 40 characters. The name isn’t in the code: you see it in the catalog and in the legend.

  3. Read the example

    It’s the smallest indicator that works: a simple average of the close, with the length among the inputs.

  4. Add to chart

    The button with the chart, next to the name, saves the script, compiles it and, if there are no errors, adds it to the active chart. If there is an error, you find it in the console below the script, with the line.

  5. Change the inputs

    Double-click the line, or press the gear in the legend: the Inputs tab has the Length. The chart updates as you change it.

The example, line by line

Moving average
# Example: a simple moving average of the close
from totaltrade.v1 import indicator, request, input, plot, ta

@indicator(overlay=True, plots=[plot.series("sma", "SMA")])
def calculate(bars=request.bars(), length=input.int("Length", 20, min=1)):
    # One series per plot, by key
    return {"sma": ta.sma(bars.close, length)}
LineWhat it does
from totaltrade.v1 import …The only import: the declarations and libraries you use. v1 is the language version, and a script written for it always gives the same results.
@indicator(…)How it looks: overlay=True puts it on the price, plots lists what it draws.
def calculate(…)The function that calculates. Its arguments are the candles, with request.bars(), the news, with request.events("news"), and the inputs, which become the settings.
return {…}One series per plot, with its key: here "sma", one value per candle.
What next?

Try changing ta.sma to ta.ema and press Add to chart again: the indicator on the chart updates, without adding another one. Then read How a script is built.