Draw your first indicator

Forge API 1Updated 13 Oct 20263 min read

Open the file

Press Open in the editor on the code below.

rsi_bands.pypythonOpen in the editor
import forge as fg
from forge import param, ta

FORGE_API = 1


class RsiBands(fg.Indicator):
    name = "RSI bands"
    short_name = "RSIB"
    pane = "new"
    precision = 2
    format = "number"

    period = param.Int(14, min=2, max=200)
    source = param.Source("close")

    rsi = fg.Output.line("RSI")
    hot = fg.Output.state("Overbought")
    levels = [fg.Level(70, "Overbought"), fg.Level(30, "Oversold")]
    warmup = lambda self: self.period

    def calculate(self, bars, start) -> None:
        r = ta.rsi(bars[self.source], self.period)
        self.out.rsi[start:] = r[start:]
        self.out.hot[start:] = r[start:] > 70
Open in the editor

FORGE_API = 1 is required. A file without it is refused. A file holds exactly one fg.Indicator class, and pane = "new" draws it in its own pane under the price.

Read what it declares

Settings are class attributes made with param. param.Int(14, min=2, max=200) is a whole number that defaults to 14 and cannot leave 2 to 200. param.Source("close") picks which price the indicator reads.

An output is declared once and filled in calculate. fg.Output.line("RSI") holds one float for each bar, and fg.Output.state("Overbought") holds true or false for each bar. levels draws the two lines at 70 and 30.

Change it

Change the period from 14 to 7 and the levels to 80 and 20. Press Format to tidy the file.

You now have

A file that draws an RSI with two levels and an overbought state, and a settings form that came with it.

Next: Write your first strategy

Ask about this in the forum