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

How a script is built

One import, the declarations, a function that calculates and a dictionary of series.

Bands
"""An average with bands."""
from totaltrade.v1 import indicator, request, input, plot, color, ta

# Module names: constants and declarations to use more than once.
BLUE = color("#2962FF")
period = input.int("Length", 20, min=1)

@indicator(short="Bands", overlay=True, plots=[
    plot.series("middle", "Average", color=BLUE),
    plot.series("upper", "Upper", color=BLUE, pattern="dashed"),
    plot.series("lower", "Lower", color=BLUE, pattern="dashed"),
])
def calculate(
    bars=request.bars(min_bars=period),
    length=period,
    mult=input.float("Deviations", 2.0, min=0, step=0.5),
):
    middle, upper, lower = ta.bb(bars.close, length, mult)
    return {"middle": middle, "upper": upper, "lower": lower}

The import

The first line of code imports from totaltrade.v1 only what you use: the declarations (indicator, request, input, plot, color) and the libraries (ta, math, format, draw). There are no other imports. v1 is the language version: a script written for it gives the same results even when a new one arrives.

Module names

Before the functions you can give a name to a constant or a declaration, and use it wherever you need it. Each name is assigned only once.

  • A named input is a single setting, even if you use it several times: above, period sets both the length and how many candles to load.
  • Constants, like BLUE or LEVELS = [30, 70], can be used anywhere and are read-only: to modify a list, make a copy, like list(LEVELS).
  • In the function body, a name that holds an input, a request or a plot is an error: there the value comes from the arguments.

@indicator

The decorator says how the indicator looks: the short name in the legend, whether it sits on the price or in its own pane, the plots it draws, the scale and the number format. You only write the arguments you need: an indicator made only of drawings is @indicator(overlay=True). All the arguments are in the reference.

The name isn’t in the code

Name and description are saved separately: the name goes in the field above the script, the description when you publish it. This way a script is saved even when it doesn’t compile.

The function that calculates

Only one function has @indicator, with any name you like. Each of its arguments has a declaration as its default value: request.bars(…) for the candles, request.events(…) for the news, an input.* for a setting, written there or through a module name. An input’s key is the name it’s assigned to: the name of the variable or the argument.

The function receives the whole series, one list per field, from the oldest candle to the latest, and the news as a list in time order, and calculates everything in one go.

What it returns

A dictionary with one entry per plot, under its key. Each series is as long as the chart’s candles, and nan, None or infinity leave a gap. A missing or extra key is an error. Fills between two plots don’t have an entry, and neither do drawings: an indicator made only of drawings returns nothing.

Declarations and calculations

Declarations, that is @indicator, input.*, plot.*, request.bars and request.events, contain only literal values: text, numbers, True, False, None, lists, tuples, dictionaries, colors and module names. This way settings and legend are known without running the code.

Python
plot.series("sma", "SMA", width=2)          # OK
plot.series("sma", "SMA", width=1 + 1)      # error: a calculation in a declaration

Your own functions and classes

After the module names you can write other functions and simple classes, to call from the function that calculates. A docstring at the start of the file stays a comment.