Envelopes
תוכן זה אינו זמין עדיין בשפה שלך.
Fixed-percentage bands around a simple average — the simplest band there is, and worth having precisely because it does not adapt: it is the control against which Bollinger’s and Keltner’s volatility adaptation is judged.
Signature
Envelopes(period, percent, on=<source>)| Argument | Type | Required | Notes |
|---|---|---|---|
period |
int Param or literal | Yes | SMA length. Default 20, bounds 1–400. |
percent |
float Param or literal | Yes | Band distance in percent. Default 2.5, bounds 0.1–50.0. |
on= |
symbol, OHLCV field, or indicator | No | Source. Defaults to the trade symbol’s close. |
Formula
$$ \text{mid} = \text{SMA}(C, n), \quad \text{upper/lower} = \text{mid},(1 \pm p/100) $$
Warmup
period bars; partial average during the lead-in.
Reading the value
en.upperen.miden.lowerMulti-value: cannot be chained onto or crossed.
Example
class EnvelopeFade(Strategy): """Fade a touch of the lower envelope in a flat market.""" trade = Symbol() sizing = FixedCapital()
def execute(self): en = Envelopes(20, 2.5, on=self.trade) if self.trade.close < en.lower and self.trade.is_flat: self.buy(self.trade, reason="Lower envelope") if self.trade.close > en.mid and self.trade.is_long: self.sell(self.trade, self.trade.position, reason="Back to mid")Notes
- The classic moving-average envelope; the right
percentis instrument- and timeframe-specific by construction. - Proven by an assert-via-trade run on SPY daily (midline and bands matched a strategy-side reference to 1e-3 on every bar; zero alarm trades).