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

Examples

Complete scripts: open them in the editor, add them to the chart and change them as you like.

Hand-written RSI

The same value as ta.rsi, with the explicit formula: Wilder averages of the gains and losses, the fill between 70 and 30 and two dashed lines.

Hand-written RSI
Hand-written RSI
from totaltrade.v1 import indicator, request, input, plot, color, draw, ta

@indicator(
    short="RSI",
    precision=2,
    plots=[
        plot.fill("band", "Background", 70, 30, fill={"color": color("#7E57C2", 10)}),
        plot.series("rsi", "RSI", color=color("#7E57C2")),
    ],
)
def calculate(bars=request.bars(), length=input.int("Length", 14, min=1)):
    change = ta.change(bars.close)
    gains = ta.rma([max(c, 0.0) for c in change], length)
    losses = ta.rma([max(-c, 0.0) for c in change], length)

    rsi = []
    for gain, loss in zip(gains, losses):
        if loss == 0:
            rsi.append(100.0)
        else:
            rsi.append(100 - 100 / (1 + gain / loss))

    # Overbought and oversold: two gray dashed horizontal lines.
    for level in (70, 30):
        draw.horizontal_line((0, level), line={"color": color("#787B86"), "pattern": "dashed"})

    return {"rsi": rsi}

Four-color MACD

The histogram changes color if it rises or falls, above or below zero: four named colors, each with its own button in the settings.

Four-color MACD
Four-color MACD
from totaltrade.v1 import indicator, request, input, plot, color, draw, ta

@indicator(
    short="MACD",
    plots=[
        plot.series("hist", "Histogram", type="columns", color=[
            ("rising_above", "Rising above zero", color("#26A69A")),
            ("falling_above", "Falling above zero", color("#B2DFDB")),
            ("rising_below", "Rising below zero", color("#FFCDD2")),
            ("falling_below", "Falling below zero", color("#FF5252")),
        ]),
        plot.series("macd", "MACD", color=color("#2962FF")),
        plot.series("signal", "Signal", color=color("#FF6D00")),
    ],
)
def calculate(
    bars=request.bars(),
    fast=input.int("Fast", 12, min=1),
    slow=input.int("Slow", 26, min=1),
    smoothing=input.int("Signal", 9, min=1),
):
    macd, signal, hist = ta.macd(bars.close, fast, slow, smoothing)

    colors = []
    for i, value in enumerate(hist):
        rising = i > 0 and value > hist[i - 1]
        if value >= 0:
            colors.append("rising_above" if rising else "falling_above")
        else:
            colors.append("rising_below" if rising else "falling_below")

    draw.horizontal_line((0, 0.0))    # the zero line, with the default style
    return {"hist": (hist, colors), "macd": macd, "signal": signal}

Moving average cross

Two exponential averages and an arrow where they cross: plot.shape with a list of True and False.

Moving average cross
Moving average cross
from totaltrade.v1 import indicator, request, input, plot, color, ta

@indicator(short="Cross", overlay=True, plots=[
    plot.series("fast", "Fast", color=color("#2962FF")),
    plot.series("slow", "Slow", color=color("#FF6D00")),
    plot.shape("up", "Bullish cross", type="arrow_up", location="below",
               color=color("up")),
    plot.shape("down", "Bearish cross", type="arrow_down", location="above",
               color=color("down")),
])
def calculate(
    bars=request.bars(min_bars=100),
    fast_length=input.int("Fast", 9, min=1),
    slow_length=input.int("Slow", 21, min=1),
):
    fast = ta.ema(bars.close, fast_length)
    slow = ta.ema(bars.close, slow_length)
    return {
        "fast": fast,
        "slow": slow,
        "up": ta.crossover(fast, slow),
        "down": ta.crossunder(fast, slow),
    }

VWAP with bands

The volume-weighted average price, which restarts every day, with a band at one standard deviation and the fill between the two.

VWAP with bands
VWAP with bands
from totaltrade.v1 import indicator, request, input, plot, color, ta

@indicator(short="VWAP", overlay=True, plots=[
    plot.fill("band", "Background", "upper", "lower", fill={"color": color("#26A69A", 8)}),
    plot.series("upper", "Upper band", color=color("#26A69A")),
    plot.series("vwap", "VWAP", color=color("#2962FF"), width=2),
    plot.series("lower", "Lower band", color=color("#26A69A")),
])
def calculate(
    bars=request.bars(min_bars="D"),
    mult=input.float("Deviations", 1.0, min=0, step=0.5),
):
    hlc3 = ta.source(bars.open, bars.high, bars.low, bars.close, "hlc3")
    vwap, stdev = ta.vwap(hlc3, bars.volume, bars.new_period("D"))
    upper = [v + mult * s for v, s in zip(vwap, stdev)]
    lower = [v - mult * s for v, s in zip(vwap, stdev)]
    return {"upper": upper, "vwap": vwap, "lower": lower}

Session zones

Each session’s range becomes a rectangle, which ends when a close breaks out of it: a simple class and drawings.

Session zones
Session zones
from totaltrade.v1 import indicator, request, input, color, draw

class Zone:
    def __init__(self, top, bottom, start):
        self.top = top
        self.bottom = bottom
        self.start = start
        self.end = None

@indicator(short="Zones", overlay=True)
def calculate(
    bars=request.bars(),
    keep=input.int("Zones to keep", 5, min=1),
    fill=input.fill("Fill", {"color": color("#2962FF", 15)}),
):
    new_session = bars.new_period("D")
    zones = []
    start = 0
    for i in range(1, len(bars.close)):
        # A zone ends when a close breaks out of its range.
        for zone in zones:
            if zone.end is None and not zone.bottom <= bars.close[i] <= zone.top:
                zone.end = i

        if new_session[i]:
            high = max(bars.high[start:i])
            low = min(bars.low[start:i])
            zones.append(Zone(high, low, i))
            start = i

    # A zone not yet broken extends to the latest candle. Without plots nothing is
    # returned: the rectangles created appear by themselves.
    last = len(bars.close) - 1
    for z in zones[-keep:]:
        end = last if z.end is None else z.end
        draw.rectangle((z.start, z.top), (end, z.bottom), fill=fill)

Yesterday’s high and low

The daily candles on an intraday chart, brought onto the chart’s candles with match.

Yesterday’s high and low
Yesterday’s high and low
from totaltrade.v1 import indicator, request, plot, color, math

@indicator(short="Yesterday", overlay=True, plots=[
    plot.series("high", "High", type="step", color=color("#26A69A")),
    plot.series("low", "Low", type="step", color=color("#EF5350")),
])
def calculate(bars=request.bars(), daily=request.bars(timeframe="D", min_bars=2)):
    high = []
    low = []
    for start, end in bars.match(daily):
        # start is this candle's day: the day before is start - 1.
        if 0 < start < end:
            high.append(daily.high[start - 1])
            low.append(daily.low[start - 1])
        else:
            high.append(math.nan)
            low.append(math.nan)
    return {"high": high, "low": low}

POC of each candle

The price with the most volume in each candle, from the futures price profile: order flow with only the group it needs.

POC of each candle
POC of each candle
from totaltrade.v1 import indicator, request, plot, color, ta

@indicator(short="POC", overlay=True, plots=[
    plot.series("poc", "POC", type="circle", color=color("#FF9800"), width=2),
])
def calculate(bars=request.bars(orderflow="volume")):
    return {"poc": ta.poc(bars.orderflow.levels)}

News on the chart

One line for each hour with high-impact news, green if the data is better than forecast and red if worse, with the title and the actual value. The background marks the candles with a news event, and the impact is chosen in the settings.

News on the chart
News on the chart
from totaltrade.v1 import indicator, request, input, plot, color, draw

TONES = {"better": color("#26A69A"), "worse": color("#EF5350")}
GRAY = color("#787B86")

@indicator(short="News", overlay=True, plots=[
    plot.background("news", "Candles with news", color=color("#FF9800", 12)),
])
def calculate(
    bars=request.bars(),
    news=request.events("news", impacts=input.choice("Impact", "high", options=[
        (None, "All"),
        ("high", "High"),
        ("medium", "Medium"),
    ])),
):
    # How many news events there are at each time: one line per time.
    count = {}
    for event in news:
        count[event.time] = count.get(event.time, 0) + 1

    drawn = {}
    for event in news:
        if event.time in drawn:
            continue
        drawn[event.time] = True
        # Green if the data beats the forecast, red if worse, gray if not released yet.
        tone = TONES.get(event.tone, GRAY)
        label = f"{event.title} {event.actual_text}"
        if count[event.time] > 1:
            label = f"{label} +{count[event.time] - 1}"
        draw.vertical_line((event.time, bars.close[-1]), xloc="time",
                           line={"color": tone, "pattern": "dashed"},
                           text={"value": label, "align": "right", "style": {"color": tone}},
                           show_time_label=False)

    # The background marks the candles with at least one news event.
    return {"news": [end > start for start, end in bars.match(news)]}