FiftyTwoWeekHighLow — 52 Week High/Low
תוכן זה אינו זמין עדיין בשפה שלך.
The highest high and lowest low of the last period trading days — 252 by default, one
year — on any bar size, plus where the close sits between them on a 0–1 scale. Shipped
separately from a long Donchian channel because members look for it by this name, because the
year is counted in days rather than bars, and because position is the reading they actually
want.
Signature
FiftyTwoWeekHighLow(period, on=<source>)| Argument | Type | Required | Notes |
|---|---|---|---|
period |
int Param or literal | Yes | Lookback in trading days (sessions). Default 252 (one year), bounds 2–1000. |
on= |
symbol | No | Source symbol. Defaults to the trade symbol. Reads high, low, close. |
Formula
$$ \text{year_high} = \max(H, n), \quad \text{year_low} = \min(L, n), \quad \text{position} = \frac{C - \text{year_low}}{\text{year_high} - \text{year_low}} $$
position is 0.5 when the range collapses.
Warmup
period trading days — period bars on a daily chart, about period × 7 on an hourly one. Until
the whole window is covered the indicator emits nothing, rather than a “52-week” high over a few
weeks.
Reading the value
fw.year_highfw.year_lowfw.position # a value, not a line: the chart shows it in the legendfw.position > 0.9The two extremes are named year_high / year_low because a drawn output cannot share a name
with the high / low the indicator reads.
Multi-value: cannot be chained onto or crossed.
Example
class NearHighs(Strategy): """Buy strength in the top decile of the yearly range.""" trade = Symbol() sizing = FixedCapital()
def execute(self): fw = FiftyTwoWeekHighLow(252, on=self.trade) if fw.position > 0.9 and self.trade.is_flat: self.buy(self.trade, reason=f"Position {fw.position:.2f} of the yearly range") if fw.position < 0.6 and self.trade.is_long: self.sell(self.trade, self.trade.position, reason="Fell out of the top of the range")Notes
- Standard 52-week range. Days are counted with
session_start, so the same 252 means a year on a daily, hourly or one-minute chart; on a daily chart the numbers are exactly the 252-bar range this indicator always gave. A symbol whose session boundary cannot be placed (a weekly or monthly session) has no trading days to count, and the indicator says so rather than computing something else. periodbounds cap at 1000 days — the buffers hold one high and one low per day.