BandSignal#
Three-state mean-reversion position from a z-score: short above +entry, long below -entry, flat again inside the exit level, with an optional stop and lockout.
BandSignal(entry=2.0, exit=0.0, stop=inf)
BandSignal()
Examples#
Usage example#
import numpy as np
import plotly.graph_objects as go
from screamer import BandSignal
rng = np.random.default_rng(2)
z = np.zeros(600)
for t in range(1, 600):
z[t] = 0.95 * z[t - 1] + rng.standard_normal() * 0.45
z[400:440] += np.linspace(0, 5, 40) # a divergence that hits the stop
pos = BandSignal(entry=2.0, exit=0.0, stop=3.5)(z)
fig = go.Figure()
fig.add_trace(go.Scatter(y=z, name='z-score', line=dict(color='gray')))
fig.add_trace(go.Scatter(y=pos, name='position', line=dict(shape='hv', color='crimson')))
for level, dash in [(2, 'dash'), (-2, 'dash'), (3.5, 'dot'), (-3.5, 'dot')]:
fig.add_hline(y=level, line_dash=dash, line_color='steelblue')
fig.update_layout(title='BandSignal(entry=2, exit=0, stop=3.5)', xaxis_title='Step',
margin=dict(l=20, r=20, t=80, b=20))
fig.show()
Description#
BandSignal(entry, exit, stop)(z) turns a z-score into a position of -1, 0,
or +1 in a mean-reverting spread. The position is short (-1) when the
spread is high, because it is expected to fall, and long (+1) when it is low:
Flat: go short when
z >= entry, long whenz <= -entry.Short: exit when
z <= exit. Long: exit whenz >= -exit.Stop: a short exits when
z >= stopand a long whenz <= -stop. That side is then locked until the z-score returns inside the exit level, so a spread that keeps diverging is not entered again at once. No entry is made beyond the stop.
The gap between the entry and exit levels is hysteresis: a z-score that hovers
near one level does not switch the position on and off. A negative exit
holds a position until the spread overshoots its mean. If the z-score jumps
from beyond one entry level to beyond the other in one step, the position
exits and enters the other side on that step.
Multiply the position by the leg sizes to get the targets of a pairs trade; see the portfolio backtest.
Parameters#
Parameter |
Type |
Default |
Description |
|---|---|---|---|
|
float |
|
Entry level. A flat position goes short when the z-score reaches +entry and long when it reaches -entry. |
|
float |
|
Exit level, in (-entry, entry). A short exits when the z-score falls to exit, a long when it rises to -exit. |
|
float |
|
Stop level, above entry (inf = no stop). A short exits when the z-score reaches +stop, a long at -stop, and that side cannot re-enter until the z-score returns inside the exit level. |
NaN handling#
Policy: ignore. A NaN in any input gives a NaN output at that step. The step is skipped: it does not enter the window or update the internal state. See NaN and warmup.
Used in the Guide#
See also#
SchmittTrigger: Hysteresis comparator. Latches 1.0 above the upper threshold, 0.0 below the lower threshold, and retains its previous value in betweenKalmanRegression: Regression with a random-walk intercept and slope, estimated by a Kalman filter: the dynamic hedge ratio of a pairs trade, its spread, and the spread's predicted deviationRollingZscore: Latest sample standardized by the trailing-window mean and standard deviationHold: Time-latch operator. Latches a nonzero finite input and holds it for n steps; returns the release value once the hold expires