Trends ... and when they End
motivated by e-mail 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:

  1. Pick a 50-week window of Price vs Time.
  2. Construct a straight line to start and end on the Price graph.

    (This is the 50-week window, enlarged.)
  3. Calculate the deviations of the Price from the Line and g-Cycle, the Standard Deviation of these deviations.
  4. Plot g-Cycle versus Time as the 50-week window moves along ...
  5. Say to yourself ten times:
    When g-Cycle bends, the trend ends
    When g-Cycle bends, the trend ends
    When g-Cycle 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 g-Cycle 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


g-Cycles

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:
          SD2 = (1/k) Σ {p(t) - L(t)}2
          where the sum is from t = 1 to t = k
  • Expanding gives:
          SD2 = (1/k) Σ { p2(t) + L2(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:
          SD2 = (1/k) { p2(t) + M2t2 - 2 p(t) M t }dt
  • Integrating:
          SD2 = (1/k) p2(t)dt + (1/3)M2k2 - 2 (M/k) p(t) t dt

Figure 1

  • Now comes our second approximation:
    Although p(t) t dt is related to the t-coordinate 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:
          SD2 = (1/k) p2(t)dt + (1/3)M2k2 - 2 (M/k) M t2 dt = (1/k) p2(t)dt + (1/3)M2k2 - 2 (M/k) M k3/3
So far we have:
[A]      SD2 = (1/k) p2(t)dt - (1/3)M2k2

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) {UB2 + LB2} = M2[p] + SD2[p]

Now recall something about Standard Deviation:
SD2[p] = (1/k) p2(t)dt - M2[p] ... (the average of the squares) - (the square of the average).

Hence we can write:
(1/k) p2(t)dt = M2[p] + SD2[p] = (1/2) {UB2 + LB2}.

Substituting into [A] above and setting M = p(k)/k, we get (finally!):
[B]      SD2 = (1/2) {UB2 + LB2} - (1/3)p2(k)
>That's it?
Hardly.
We've been talking about a k-week window from week T = n-k to week T = n.
The prices start at P(n-k) and end at P(n).
We then deducted P(n-k) from each price.
The shifted prices start at 0 and end at P(n) - P(n-k).
These shifted prices we're calling p(t), where t goes from 0 to k.

>Huh?
Okay, looking carefully at that k-week 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:
      SD2 = (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 ...

>g-Cycles?
Yes, g-Cycles.


Figure 2a


Figure 2b

In fact, we'll introduce some extra parameters before we define our g-Cycles:

  • 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 g-Cycle 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 ta-DUM !
      g-Cycles2 = UB2 + LB2 - Mean2(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 n-k to week n, using λ standard deviations.

>So why should your g-Cycles 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 g-Cycles 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 g-Cycles 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 g-Cycle Vigor as the percentage change in the g-Cycle value over the k-week 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(n-k)} / k (for our k-day window)
and re-insert that (1/2) in front of the Bollinger bands:
SD2 = (1/2) {UB2 + LB2} - (1/3){P(n) - P(n-k)}2

You can ask for that, if you like, using ... but I like g-Cycle better.