Indicators
Lines, histograms and zones drawn on the chart, and an indicator's numbers inside a strategy.
One indicator, one label, as many lines as you like:
import while25
from while25 import Fill, Indicator, Plot, on
from while25.inputs import Float, Int
class MyBollinger(Indicator):
id = "my_bb"
label = "My Bollinger" # ONE legend entry for all three lines
overlay = True
inputs = [Int("length", default=20, minimum=2, maximum=500, step=1),
Float("mult", default=2.0, step=0.1)]
plots = [Plot("basis", color="#e0a34a", width=2),
Plot("upper", color="#4aa3e0", style="dashed"),
Plot("lower", color="#4aa3e0", style="dashed")]
fills = [Fill("upper", "lower", color="#4aa3e0", opacity=0.08)]
warmup = lambda params: int(params.get("length", 20))
def compute(self, ctx):
n = while25.params["length"]
rows = while25.bars # While25 streams you the chart's bars
...
self.times = [int(r["time"]) for r in rows]
return {"basis": basis, "upper": upper, "lower": lower}inputsbecome a config dialog the user can edit — the same one built-in indicators use.plotsset the DEFAULT colour, width and line style. The user can still override every one of them in the chart settings, exactly as for a built-in.Plot(plot_type=...)takesline · histogram · area · stepline;Level(70)draws a reference line andBand(top, bottom)shades a zone.compute()returns a dict keyed by your plot names. Returning a name you did not declare is an error rather than a silently missing line. Every indicator writes its owncompute(self, ctx): a class without one is refused when the file is saved.- A chart starts indicators and nothing else. If a file on a chart is
saved again as a
Strategy, aMonitoror anAppunder the same id, the chart draws nothing for it and starts nothing: its log says so. Take it off the chart, and start the new module the way its kind is started. - An indicator RECEIVES its series. While25 streams the chart's series
— scrollback included — to Python, and every change to it recomputes
you; the client no longer sends bars.
while25.barsis that array, oldest first, replaced whole every pass.while25.formingis the unclosed bar and is present only while a bucket is forming (otherwiseNone; it is never insidebars).while25.priceis the price the chart's candles are built from: "last", or "mark" on a Binance futures chart set to Mark price (no volume).while25.future_timesis the chart's projected grid. The times are the chart's own wire times, soself.times = [r["time"] for r in rows]lands every value on the candle it came from. Sourcing a second series is refused:candles(..., source="live")insidecompute()raisesLIVE_IN_COMPUTE. Two series drift after a scrollback, and the chart's by-timestamp merge then blanks the line exactly where the user scrolled. warmupdeclares what you need — anint, or a callable of the params when it is derived (DEMA needs2*period-1). Short input reportswarming up — 12 of 39 barsinstead of drawing nothing. A biggerlimitis never the answer: it truncates a fetch, and here there is no fetch.Plot(name, color="", width=1, style="solid", plot_type="line", color_down="", opacity=1.0, overlay=None, visible=True).widthis 1 to 4;styleissolid · dotted · dashed · large-dashed · sparse-dotted;color_downis a histogram's or bi-colour plot's second colour;overlayNonefollows the indicator's ownoverlay.Fill(upper, lower, color="", opacity=0.08)shades between two of your plots.levels = [Level(70, color="", width=1, style="dashed")]draws reference lines andbands = [Band(top, bottom, color="", opacity=0.06)]shades zones.y_min/y_maxfix the pane's scale (RSI 0 to 100) andprecisionits decimals.intrabar = Truerecomputes as the bar forms, not only when it closes (a full recompute per tick: off by default). A declaration the chart cannot draw — no plots, a plot named twice, a fill naming a plot you did not declare, a band whose top is not above its bottom — is refused when the file is registered.while25.instrumentandwhile25.intervalare the chart's: the symbol and interval it is drawing, set per compute, so one indicator works on any chart.
An indicator's numbers in a strategy
from indicators.my_rsi import MyRsi
class TslaRsi(Strategy):
id = "tsla_rsi"
tsla = Bars("EQ:US:TSLA", "5m", keep=100)
@on.bar(tsla)
def on_bar(self, ctx, bar):
rsi = self.tsla.indicator(MyRsi, length=14)["rsi"]
if rsi[-1] > 70:
...line.indicator(cls, *, forming=False, **params) works an indicator out from
a Bars line's own bars. It is while25.indicator with bars=line.rows:
while25.indicator(cls, *, bars=None, forming=False, instrument="", interval="", **params) works another indicator out in this process, from
the bars you hand it, and returns exactly what its compute() returned.
Nothing is drawn and no chart is needed. Only a chart hands an indicator bars
by itself, so a strategy passes its own.
bars: oldest first, your warm-up and@on.barbars, orcandles()rows. The indicator reads them aswhile25.barsin a chart's row shape (timein epoch ms,open,high,low,close,volume), so one written for a chart works unchanged;while25.formingisNone.forming:Truealso counts the bar still forming, see below.instrument,interval: what it reads aswhile25.instrumentandwhile25.interval; left out, the bars' own.**params: its inputs, checked and defaulted as on a chart.
ValueError, without running it: fewer bars than its warmup, bars out of
order or two at one time, a bar without a time or a price, bars of two
series in one list (two @on.bar lines), an instrument or interval that
is not the bars' own. With no bars=, an indicator that reads
while25.bars raises ValueError saying so; only one called inside another
indicator's compute() reads its caller's bars.
Before the bar closes. @on.bar wakes the strategy once a bar has
closed. To act while it is still forming, ask from a tick handler with
forming=True:
@on.tick("EQ:US:TSLA")
def on_tick(self, ctx, tick):
rsi = self.tsla.indicator(MyRsi, forming=True, length=14)["rsi"]It adds the bar still forming on that @on.bar line after your bars, as
While25 last sampled it (about once a second; the chart's moving candle is
the same bar). The indicator reads it as its last bar; your own list is not
changed.
- Added only when it is the bar right after your last one: never a bar you already hold closed, and never past a closed bar your list has not got yet. Otherwise, and while nothing is forming, the indicator is worked out from your bars as they are.
barsmust be those of one of the module's own bar lines (aBarsline, or an@on.barline's warm-up and bars), whatever its interval: that line is what holds the symbol's prices while the module runs. Anything else raisesValueError.- Its
warmupis counted in closed bars.