App-Chart
view release on metacpan or search on metacpan
doc/chart.texi view on Meta::CPAN
The time periods of 9 days and 26 days are parameters in Chart (@pxref{View
Style}). When Hosoda designed the system Japanese markets traded 6 days a
week, so 9 days was @m{1 1\over2,1 1/2} trading weeks and 26 days was a month.
The same calendar times in today's 5 day weeks can be had with 7 and 22 days.
@cindex Range Midpoint
@anchor{Range Midpoint}
@subsection Range Midpoint
An N-day range midpoint like the tenkan, kijun and cloud lines is also
available separately as ``Range Midpoint'' if you want to experiment. It's
under ``Low Priority'' in the averages list.
@c ---------------------------------------------------------------------------
@node Keltner Channel, Kirshenbaum Bands, Ichimoku Kinko Hyo, Channels and Boxes
@section Keltner Channel
@cindex Keltner channel
@cindex Channel, Keltner
@cindex Keltner, Chester W.
The Keltner channel is an N-day simple moving average (@pxref{Simple Moving
Average}) of ``typical price'' (which is (high+low+close)/3), and bands drawn
above and below that at a distance which is the simple moving average of daily
ranges (high to low) in the period.
@cindex Ten-day moving average trading rule
This indicator was described by Chester W. Keltner in his 1960 book @iquot{How
To Make Money in Commodities}, and those who learnt of it from him apparently
gave it the name Keltner Channel. Keltner himself called it the ``Ten-Day
Moving Average Trading Rule'' and never claimed any originality for the idea
-- it may have come from grain traders in the 1930s, or even earlier.
The idea is that closes outside the channel suggest strong bullish (or strong
bearish) sentiment and can be bought (or sold). The 10-day period is a
parameter in Chart.
The channel bands need daily high/low data, so when that's not available (a
few indices for instance) they can't be drawn. The centre line is still shown
(it ends up as just a simple moving average of the closes).
@c ---------------------------------------------------------------------------
@node Kirshenbaum Bands, Parabolic SAR, Keltner Channel, Channels and Boxes
@section Kirshenbaum Bands
@cindex Kirshenbaum bands
@cindex Average
@cindex Moving average
@cindex Variance
@cindex Deviation
@cindex Standard error
@cindex Kirshenbaum, Paul
Kirshenbaum bands are channel lines drawn around an exponential moving average
(@pxref{Exponential Moving Average}). The channel width is a multiple of the
``standard error'' from a linear regression of a past N days (@pxref{Linear
Regression}, and the EMA is smoothed using the same N days.
Kirshenbaum bands are similar to Bollinger bands (@pxref{Bollinger Bands}),
but with a linear regression standard error (stderr) instead of a standard
deviation (stddev, @pxref{Standard Deviation}). The difference is that stddev
takes no account of a trend, so the Bollinger channel widens when a trend is
in progress. But stderr is based on deviation from a fitted sloping line, so
if prices are making steady progress up or down the channel width remains
small.
The standard error values (ie.@: the channel width) can be viewed directly as
an indicator too as ``Linear Regression Stderr'' (@pxref{Linear Regression}).
@c ---------------------------------------------------------------------------
@node Parabolic SAR, , Kirshenbaum Bands, Channels and Boxes
@section Parabolic SAR
@cindex Parabolic SAR
@cindex Stop and reverse, parabolic
@cindex Wilder, J. Welles
Parabolic stop and reverse (SAR) by J.@: Welles Wilder is a trend following
system which establishes a trailing stop that's progressively advanced as a
price move continues. If penetrated then one's position short or long is
reversed and a new stop established for the move in the opposite direction.
The stop is calculated as follows. For an upward move, the initial SAR is the
lowest low made in the preceding down movement. From there an extreme high of
the current move is maintained and the stop advances towards that by a
fraction called the ``acceleration factor''.
@tex
$$ SAR = SAR_{prev} + accel \times (extreme - SAR_{prev}) $$
@end tex
@ifnottex
@example
SAR = SAR[prev] + accel * (extreme - SAR[prev])
@end example
@end ifnottex
The initial acceleration factor is 0.02, and each time a new extreme is made,
ie. a new high, it's increased by adding 0.02, up to a maximum of 0.2. (Those
values are parameters in Chart.) The effect is to move the stop up faster in
a strong move making successive new highs.
If the SAR for a given day is penetrated by any part of that day's trading
range then it's a signal to reverse one's position and the SAR becomes a
trailing stop for a short. The calculation for that is the same, using the
previous extreme high as the initial stop and maintaining a low to step
towards. Reversals can be seen in the graph when the stop mark switches from
above to below the prices, or back.
This approach is only suited to trending markets. If prices are chopping back
and forward in a ranging market then a lot of whipsaw reversals are generated.
At each reversal the new stop point can be quite a distance from the current
prices, ie.@: one's entry point, making those whipsaws expensive. Wilder
believed markets trend only about 30% of the time. His ADX
(@pxref{Directional Movement Index}) is one way to identify such conditions.
In Chart the default is not to adjust for dividend payments in the display or
in indicator calculations. If price falls on ex-dividend dates are causing
unwarranted SAR reversals you can turn on the adjustment option in the view
style (@pxref{View Style}). (Assuming the data source provides dividend
information.)
@c ---------------------------------------------------------------------------
doc/chart.texi view on Meta::CPAN
For a possible fitted line @m{L(X)=a+bX,L(X)= a + b*X}, the vertical distance
from the line to each point is squared, and a total deviation formed.
@tex
$$ SumSquares = (y_1 - L(x_1))^2 + \cdots + (y_N - L(x_N))^2 $$
@end tex
@ifnottex
@example
SumSquares = (y1 - L(x1))^2 + ... + (yN - L(xN))^2
@end example
@end ifnottex
The line parameters @math{a} and @math{b} are then chosen to make SumSquares
as small as possible (hence the name ``least squares''), and there's just one
line with that smallest SumSquares. The calculation is made easier if the X
coordinates are shifted so that @math{Mean(X)=0}. With that the formulas for
@math{a} and @math{b} are
@tex
$$ a = Mean \; Y = { y_1 + \cdots + y_N \over N } $$
$$ b = { x_1 y_1 + \cdots x_N y_N \over x_1^2 \cdots + x_N^2 } $$
@end tex
@ifnottex
@example
y1 + ... + yN
a = Mean Y = -------------
N
x1*y1 + ... + xN*yN
b = -------------------
x1^2 + ... xN^2
@end example
@end ifnottex
A least squares fit is ``best'' under certain mathematical assumptions:
basically that the data points were a straight line to which normally
distributed random amounts (positive or negative) have been added. Of course
an underlying straight line is unlikely in market price data, or in economics
generally, and in particular any cyclical component invalidates the
assumptions. Even so the algorithm is quite widely used because it offers an
objective basis for fitting a line.
@cindex Linear regression slope indicator
@cindex Regression coefficient
@cindex Coefficient, regression
@anchor{Linear Regression Slope}
@subsection Slope
The slope of the linear regression line, the @math{b} above, is sometimes
called the @dfn{regression coefficient}. This is available as an indicator
(Linear Regression Slope), to show how steep the fitted trend line is. The
units are price change per day, which is negative for a downward sloping line.
This may or may not be particularly useful so it's under ``Low Priority'' in
the indicator lists.
@cindex Standard error
@anchor{Linear Regression Standard Error}
@subsection Standard Error
Standard error (stderr) is a statistical measure of how much values differ
from an assumed underlying curve. It's calculated as the quadratic mean of
the vertical distances from each point to the curve.
Standard error from a linear regression line @math{y=a+bx} is
@tex
$$ Stderr = \sqrt { (y_1 - (a + bx_1))^2 + \cdots + (y_N - (a + bx_N))^2
\over N } $$
@end tex
@ifnottex
@example
/ (y1 - (a+b*x1))^2 + ... + (yN - (a+b*xN))^2 \
Stderr = sqrt | ------------------------------------------- |
\ N /
@end example
@end ifnottex
Notice the numerator is the same SumSquares which was minimized above.
Standard error is similar to standard deviation (@pxref{Standard Deviation});
but where stddev takes differences from a horizontal line (the @math{Y} mean),
stderr here goes from the sloping linear regression line.
For reference, there's no need to actually calculate the linear regression
@math{a} and @math{b}, the stderr can be formed directly as
@tex
$$ Stderr = \sqrt { Variance(Y) - { Covariance(X,Y)^2 \over Variance(X) }} $$
@end tex
@ifnottex
@example
/ Covariance(X,Y)^2 \
Stderr = sqrt | Variance(Y) - ----------------- |
\ Variance(X) /
@end example
@end ifnottex
@noindent
where variance and covariance are as follows (and notice they simplify if
@math{X} values are chosen to make @math{Mean(X)} zero),
@tex
$$ \mathop{Covariance} X,Y = \mathop{Mean} (XY)
- (\mathop{Mean} X) (\mathop{Mean} Y) $$
$$ \mathop{Variance} X = \mathop{Mean} (X^2) - (\mathop{Mean} X)^2 $$
@end tex
@ifnottex
@example
Covariance X,Y = Mean (X*Y) - (Mean X) * (Mean Y)
Variance X = Mean(X^2) - (Mean X)^2
@end example
@end ifnottex
Standard error from a linear regression like this is used as a channel width
in Kirshenbaum Bands (@pxref{Kirshenbaum Bands}). It can also be viewed
directly as an indicator, but this is probably of limited use and for that
reason is under ``Low Priority'' in the indicator lists.
@subsection Additional Resources
@itemize
@item
@uref{http://mathworld.wolfram.com/LeastSquaresFitting.html} -- on calculating
stderr without the a,b parameters
@end itemize
@c ---------------------------------------------------------------------------
@node Standard Deviation, True Range, Linear Regression, Common Calculations
@section Standard Deviation
@cindex Standard deviation
@cindex Stddev
Standard deviation (stddev) is a statistical measure of how much the values in
a data set deviate from their average (mean). The stddev of a past N days is
used in the calculation of Bollinger bands (@pxref{Bollinger Bands}) and VIDYA
and @ref{Variable Index Dynamic Average}).
The raw stddev values can be shown in Chart as an indicator, to see what goes
into those calculations. The values have little direct use though, and for
that reason stddev is under ``Low Priority'' in the indicator lists.
@cindex Quadratic mean
@cindex Mean, quadratic
For reference, the formula on prices @ms{p,1} to @ms{p,N} is as follows. Each
@m{p_i-m,p[i]-m} is the distance from the mean @math{m}, and those distances
are averaged with a quadratic mean.
@tex
$$ \sigma = { \sqrt{ { (p_1-m)^2 + (p_2-m)^2 + \cdots + (p_N-m)^2 } \over N }}
$$
@end tex
@ifnottex
@example
/ (p1-m)^2 + (p2-m)^2 + ... + (pN-m)^2 \
Stddev = sqrt ( ------------------------------------ )
\ N /
@end example
@end ifnottex
where @math{m} is the arithmetic mean
@tex
$$ m = { { p_1 + p_2 + \cdots + p_N } \over N } $$
@end tex
@ifnottex
@example
p1 + p2 + ... + pN
m = ------------------
N
@end example
@end ifnottex
Also, for reference, the formula can be rearranged to the following form,
``sum of the squares minus square of the sums''. This is what Chart uses,
because a new term can be shifted into the sums without recalculating all N
terms.
@tex
$$ \sigma = { \sqrt{ { { p_1^2 + p_2^2 + \dots + p_N^2 } \over N }
- { \left( { p_1 + p_2 + \dots + p_N } \over N \right)^2 }}}
$$
@end tex
@ifnottex
( run in 1.765 second using v1.01-cache-2.11-cpan-39bf76dae61 )