Trend Continuation Factor
All functions · nseries package
TrendContinuationMinus
func (s Series) TrendContinuationMinus(n int) Series
TrendContinuationMinus returns the negative Trend Continuation Factor of s over the last n one-bar changes, the sum of the down changes minus the sum of the up-run gains, which reads as a downtrend when positive.
At bar t the window holds the changes of bars t-n+1 to t and so reads the n+1 closes s[t-n] to s[t]. With ch-[t] the change s[t-1] - s[t] on a down bar (s[t] < s[t-1], both closes finite, as in ConsecDownLoss) and 0 on every other bar, and with +CF the gain of ConsecUpGain, the value is the window sum of ch- minus the window sum of +CF. Both window sums are the package’s Sum(n) applied to the whole series and fed from bar 0, and the last step is one subtraction of the two sums, so on data without infinities (NaN allowed) the composition s.Delta(1).Mul(-1).Max(0).Sum(n).Sub(s.ConsecUpGain().Sum(n)) equals the result bit for bit from bar n.
The source is M.H. Pee, “Trend Continuation Factor”, Stocks & Commodities V20:3 (March 2002). His default is n = 35, tested over 25 to 45. A value above 0 reads as a downtrend and a TrendContinuationPlus above 0 as an uptrend; the two cannot both be positive, both can be 0 on a flat window, and both negative is Pee’s consolidation reading, which he did not test. His always-in latch, short while this is positive and long while the other is, is a recipe built on the two values rather than part of the method.
The leading term is the negation of Momentum(n): the window sum of ch- minus the window sum of the up changes is the negation of Momentum(n) in exact arithmetic, so the value is at most the negation of Momentum(n), with equality when every up run in the window has length 1. On strictly alternating closes, every bar from bar 1 up or down in turn so that every run has length 1, the result equals 0 - TrendContinuationPlus(n) bit for bit from bar n, the 0 written so that a zero is +0. The units are those of s and the value is unbounded on both sides.
The warm-up reads 0, not NaN: bars 0 to n-1 are 0 and the first valid index is n. The minimum length is n+1, and n < 1 or n > len(s)-1 gives len(s) zeros, so n = len(s)-1 gives one valid bar, the last.
A close at bar p that is NaN or an infinity gives NaN on the bars [max(p, n), p+n] and on no other bar; the later bars recover, with new runs measured from the bar after the bad close. Finite closes whose sums overflow can still give +Inf, -Inf or NaN on bars with no bad close: {0, 1e308, 0, 1e308, …} gives an infinity at n = 3, and {-1e308, 1e308, -1e308, …} gives NaN at n = 2.
The value is not window-local: the gain of a window bar is measured from the anchor of its up run, which can lie before the window. Dropping bars from the front of s leaves the value at a bar unchanged to rounding, as long as the bar stays at slice index n or later and the slice keeps the anchor of every up run active at the window’s bars; the compensated Sum(n) is not guaranteed to be correctly rounded, so the bits can differ.
The mirror s.Mul(-1).TrendContinuationMinus(n) equals TrendContinuationPlus(n) bit for bit on every input. The cost is O(1) amortised per bar.
TrendContinuationPlus
func (s Series) TrendContinuationPlus(n int) Series
TrendContinuationPlus returns the positive Trend Continuation Factor of s over the last n one-bar changes, the sum of the up changes minus the sum of the down-run losses, which reads as an uptrend when positive.
At bar t the window holds the changes of bars t-n+1 to t and so reads the n+1 closes s[t-n] to s[t]. With ch+[t] the change s[t] - s[t-1] on an up bar (s[t] > s[t-1], both closes finite, as in ConsecUpGain) and 0 on every other bar, and with -CF the loss of ConsecDownLoss, the value is the window sum of ch+ minus the window sum of -CF. Both window sums are the package’s Sum(n) applied to the whole series and fed from bar 0, and the last step is one subtraction of the two sums, so on data without infinities (NaN allowed) the composition s.Delta(1).Max(0).Sum(n).Sub(s.ConsecDownLoss().Sum(n)) equals the result bit for bit from bar n.
The source is M.H. Pee, “Trend Continuation Factor”, Stocks & Commodities V20:3 (March 2002). His default is n = 35, tested over 25 to 45. A value above 0 reads as an uptrend and a TrendContinuationMinus above 0 as a downtrend; the two cannot both be positive, both can be 0 on a flat window, and both negative is Pee’s consolidation reading, which he did not test. His always-in latch, long while this is positive and short while the other is, is a recipe built on the two values rather than part of the method.
The leading term is Momentum(n): the window sum of ch+ minus the window sum of the down changes is Momentum(n) in exact arithmetic, so the value is at most Momentum(n), with equality when every down run in the window has length 1. The units are those of s and the value is unbounded on both sides.
The warm-up reads 0, not NaN: bars 0 to n-1 are 0 and the first valid index is n. The minimum length is n+1, and n < 1 or n > len(s)-1 gives len(s) zeros, so n = len(s)-1 gives one valid bar, the last.
A close at bar p that is NaN or an infinity gives NaN on the bars [max(p, n), p+n] and on no other bar; the later bars recover, with new runs measured from the bar after the bad close. Finite closes whose sums overflow can still give +Inf, -Inf or NaN on bars with no bad close: {0, 1e308, 0, 1e308, …} gives an infinity at n = 3, and {-1e308, 1e308, -1e308, …} gives NaN at n = 2.
The value is not window-local: the loss of a window bar is measured from the anchor of its down run, which can lie before the window. Dropping bars from the front of s leaves the value at a bar unchanged to rounding, as long as the bar stays at slice index n or later and the slice keeps the anchor of every down run active at the window’s bars; the compensated Sum(n) is not guaranteed to be correctly rounded, so the bits can differ.
The mirror s.Mul(-1).TrendContinuationPlus(n) equals TrendContinuationMinus(n) bit for bit on every input. The cost is O(1) amortised per bar.