CorrelationTrend: Correlation Trend (Ehlers)
The Pearson correlation between price and time over the window, from −1 to +1. It measures how steadily price has been moving: +1 is a perfectly straight rise, −1 a perfectly straight fall, and 0 means price has no straight-line relationship with time at all.
Signature
CorrelationTrend(period, on=<source>)| Argument | Type | Required | Notes |
|---|---|---|---|
period |
int Param or literal | Yes | Window. Default 20, bounds 3–400. |
on= |
symbol, OHLCV field, or indicator | No | Source. Defaults to the trade symbol’s close. |
Formula
$$ \text{CT} = \frac{\sum_i (i - \bar{i})(C_i - \bar{C})}{\sqrt{\sum_i (i - \bar{i})^2 \sum_i (C_i - \bar{C})^2}} $$
with i the bar’s position in the window (oldest first) and C the source value.
Warmup
period bars; 0 until then, and 0 on a flat window.
Reading the value
ct.valuect.value > 0.8ct.crossed_above(0)Example
class StraightLineTrend(Strategy): """Enter only when price and time are strongly correlated.""" trade = Symbol() sizing = FixedCapital()
def execute(self): ct = CorrelationTrend(20, on=self.trade) if ct.value > 0.8 and self.trade.is_flat: self.buy(self.trade, reason=f"Correlation {ct.value:.2f}") if ct.value < 0 and self.trade.is_long: self.sell(self.trade, self.trade.position, reason="Trend correlation lost")Notes
- John F. Ehlers, “Correlation as a Trend Indicator”, Technical Analysis of Stocks & Commodities, May 2020.
- The square of this value is
LinearRegression.r2: the same fit, signed here so it carries the direction. - Proven by an assert-via-trade run on SPY daily (matched a strategy-side Pearson computation to 1e-4 on every bar; zero alarm trades).