OGT Owl Group Trading by Dr. Ken Long
Home About Learn The Loop Code Courses Essays Store Partners FAQ
Python · Indicator Code

The River, Flood Plain & Red Line in Python

Owl Group Trading's 30-period Bollinger zones around the Mean: River (+/-1 SD), Flood Plain (+/-2 SD), Red Line (+/-3 SD).

Concept: what RIVER-FLOOD-PLAIN means →

Verified. Python and JavaScript implementations agree to 2.84e-14 on a 60-bar reference price series (Python vs JavaScript across the River/Flood Plain/Red Line bands (+/-1, +/-2, +/-3 SD over 30 periods) - float64 round-off only).

Python

import numpy as np

def _sma(values, n):
    v = np.asarray(values, float)
    out = np.full(len(v), np.nan)
    for i in range(n - 1, len(v)):
        out[i] = v[i - n + 1:i + 1].mean()
    return out

def _rolling_std(values, n):              # population std (ddof=0)
    v = np.asarray(values, float)
    out = np.full(len(v), np.nan)
    for i in range(n - 1, len(v)):
        out[i] = v[i - n + 1:i + 1].std()
    return out

def river_flood_plain(closes, n=30):
    """The River, Flood Plain and Red Line - 30-period Bollinger zones around
    the Mean (Z0 = the 30-period SMA). River = +/-1 SD (Z1), Flood Plain =
    +/-2 SD (Z2), Red Line = +/-3 SD (Z3). Returns the Mean plus each (upper,
    lower) band pair."""
    mean = _sma(closes, n)
    sd = _rolling_std(closes, n)
    band = lambda k: (mean + k * sd, mean - k * sd)
    return {"mean": mean,
            "river": band(1),        # Z1 / Z-1
            "flood_plain": band(2),  # Z2 / Z-2
            "red_line": band(3)}     # Z3 / Z-3