Trends ... and when they End

motivated by email from Jay K.
There are plenty of techniques to identify
trends
in the stock market
... and (hopefully) to identify when they start and end.
I reckon the best method to identify a trend is to gaze in awe at a price vs time chart, like Figure 1.
>Great, but how will you know when a trends starts or ends?
Ay, there's the rub.
When we eyeball a chart we're looking for places where the curve roughly follows some line up ... or down.
The closer it follows such a line, the more comfortable we are in claiming that we have a trend.
 Figure 1 
>Huh?
Look at these two charts:
We have roughly the same straight line (in red) which starts and ends where the price does.
Which are you more comfortable with ... in calling it a trend?
>You want me to say the one on the right, right?
I'd say so. But that's because the deviations are less for the right chart.
That suggests that we study the deviations of the prices from straight lines which move with the price chart.
>Huh?
Is that your favourite word?
Here's what we'll do:
 Pick a 50week window of Price vs Time.
 Construct a straight line to start and end on the Price graph.
(This is the 50week window, enlarged.)
 Calculate the deviations of the Price from the Line and gCycle, the Standard Deviation of these deviations.
 Plot gCycle versus Time as the 50week window moves along ...
 Say to yourself ten times:
When gCycle bends, the trend ends
When gCycle bends, the trend ends
When gCycle bends ...
>Okay! Stop! I assume you can actually prove something and you have a spreadsheet and ...
Patience.
However (unless I goofed), the magic formula for calculating the gCycle
involves the Upper and Lower Bollinger bands and the current price and ...
>And the formula is?
>You're kidding, right?
Would I kid you?
Here are a few more:
>But, it's not perfect and it tells you a mite late and ...
For perfection, use this
Consider the following:
 Prices y = p(t) (for t = 0, 1, 2, ... k) goes from p(0) = 0 to p(k).
 The straight line y = L(t) = M t (where the slope is M = p(k)/k) has the same start and end.
 At any place x between 0 and k, the deviation is p(x)  L(x).
 The standard deviation of k deviations is:
SD^{2} = (1/k) Σ {p(t)  L(t)}^{2}
where the sum is from t = 1 to t = k
 Expanding gives:
SD^{2} = (1/k) Σ
{ p^{2}(t) + L^{2}(t)  2 p(t) L(t) }
 Now comes our first approximation:
We assume that p(t) is continuous and we replace Σ( ) by ∫( )dt
... the integral going from t = 0 to t = k.
We also replace M by p(k)/k.
 We get:
SD^{2} = (1/k) ∫
{ p^{2}(t) + M^{2}t^{2}  2 p(t) M t }dt
 Integrating:
SD^{2} = (1/k) ∫
p^{2}(t)dt + (1/3)M^{2}k^{2}  2 (M/k) ∫ p(t) t dt
 Figure 1 
 Now comes our second approximation:
Although ∫ p(t) t dt is related to the tcoordinate of the centroid of the area beneath y = p(t)
(from t=0 to t=k), we'll assume that p(t) is close enough to Mt so that we can put p(t) = Mt in that last integral.
Remember: the integral goes from t = 0 to t = k.
>Huh? That's cheating!
The proof is in the pudding.
 Continuing:
SD^{2} = (1/k) ∫p^{2}(t)dt
+ (1/3)M^{2}k^{2}  2 (M/k) ∫ M t^{2} dt
= (1/k) ∫p^{2}(t)dt + (1/3)M^{2}k^{2}  2 (M/k) M k^{3}/3
So far we have:
[A] SD^{2} = (1/k) ∫p^{2}(t)dt  (1/3)M^{2}k^{2}
Now recall something about Bollinger Bands (using "1" standard deviation):
Upper Bollinger Band = UB = M[p] + SD[p]
... where M[p] and SD[p] are the Mean and Standard Deviation of p(t) from t = 0 to t = k.
Lower Bollinger Band = LB = M[p]  SD[p]
Then (1/2) {UB^{2} + LB^{2}} = M^{2}[p] + SD^{2}[p]
Now recall something about Standard Deviation:
SD^{2}[p] = (1/k) ∫p^{2}(t)dt  M^{2}[p]
... (the average of the squares)  (the square of the average).
Hence we can write:
(1/k) ∫p^{2}(t)dt = M^{2}[p] + SD^{2}[p] = (1/2) {UB^{2} + LB^{2}}.
Substituting into [A] above and setting M = p(k)/k, we get (finally!):
[B] SD^{2} = (1/2) {UB^{2} + LB^{2}}  (1/3)p^{2}(k)

>That's it?
Hardly.
We've been talking about a kweek window from week T = nk to week T = n.
The prices start at P(nk) and end at P(n).
We then deducted P(nk) from each price.
The shifted prices start at 0 and end at P(n)  P(nk).
These shifted prices we're calling p(t), where t goes from 0 to k.
>Huh?
Okay, looking carefully at that kweek window, we see Figure 2a where prices start at 925.
Now we subtract that starting price from each price in this window, getting Figure 2b.
It's this shifted window where we've done the above analysis.
>So you've still got some work to do, eh?
Uh ... yes.
Remember how we started? The standard deviation we're looking at is:
SD^{2} = (1/k) Σ {p(t)  L(t)}^{2}
Shifting the window (up or down) won't change the difference p(t)  L(t)
(since both terms get shifted by the same amount).
>But you changed to integrals and used Bollinger and set the slope as ...
Yes, yes. We'll play with our result, but can't claim it's the standard deviation of the deviations, so we'll give it a new name, namely ...
>gCycles?
Yes, gCycles.
 Figure 2a
Figure 2b

In fact, we'll introduce some extra parameters before we define our gCycles:
 The Bollinger Bands (above) used "1" standard deviation. We'll use, instead, λ standard deviations in our Bollinger Bands.
 The magic formula (above) had a second term with a divisor of "3". We'll change that to d.
 We'll ignore the (1/2) out in front of the Bollinger terms ... since we're only interested in the gCycle ups and downs, not it's size.
 Instead of the current price P(n), we'll use the Mean price over the last k days.
We now have taDUM !
gCycles^{2} = UB^{2} + LB^{2}  Mean^{2}(n) / d
where:
Mean = Mean of the prices over the last k days
d = a parameter ... default value is "3"
UB and LB = Upper and Lower Bollinger Bands for Prices from week nk to week n, using λ standard deviations.

>So why should your gCycles have a maximum or minimum where the Price does?
Good question. I have no idea.
>Why don't you just admit you goofed and your gCycles have nothing to do with standard deviation and ...?
Okay, I admit it ... but they're interesting, no?
>No! I assume there's a spreadsheet.
Yup! It looks like this:
Click on picture to download the spreadsheet
That SD number is our λ and you can choose the divisor d and you can pick your stock and ...
>zzzZZZ
Fine! While you sleep, I'll look at some examples for k = 50 week windows and λ = 1 and d = 3
(the "usual" values):
I think the charts look great! Look at the Nasdaq ... how gCycles go up and down.
Of course we could change the parameters:
k=10 λ=1 d=3 
k=50 λ=5 d=3
 k=50 λ=1 d=1

Then we might want to measure the strength of the trend.
We could define the gCycle Vigor as the percentage change in the gCycle value over the kweek window.
That'd give a neat chart like so:
>Huh? Vigor?
Neat word, eh? I figured that'd wake you up.
You might SELL when the vigor exceeds +30% and BUY when it drops below 30%. See the potential?
>zzzZZZ
I might also point out that, in order to calculate the real SD as per [B],
we'd have to take as our slope: M = {P(n)  P(nk)} / k (for our kday window)
and reinsert that (1/2) in front of the Bollinger bands:
SD^{2} = (1/2) {UB^{2} + LB^{2}}  (1/3){P(n)  P(nk)}^{2}
You can ask for that, if you like, using
... but I like gCycle better.
 
