% This file was converted to LaTeX by Writer2LaTeX ver. 0.4
% see http://www.hj-gym.dk/~hj/writer2latex for more info
\documentclass[12pt,twoside]{article}
\usepackage[ascii]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{amsmath,amssymb,amsfonts,textcomp}
\usepackage{color}
\usepackage{calc}
\usepackage{hyperref}
\hypersetup{colorlinks=true, linkcolor=blue, filecolor=blue, pagecolor=blue, urlcolor=blue}
% Text styles
\newcommand\textstyleCodefont[1]{\texttt{\textcolor{red}{#1}}}
\newcommand\textstyleFootnoteSymbol[1]{\textsuperscript{#1}}
% Outline numbering
\setcounter{secnumdepth}{2}
\renewcommand\thesection{\arabic{section}}
\renewcommand\thesubsection{\arabic{section}.\arabic{subsection}}
% List styles
\newcounter{saveenum}
\newcommand\liststyleWWviiiNumv{%
\renewcommand\theenumi{\arabic{enumi}}
\renewcommand\theenumii{\alph{enumii}}
\renewcommand\theenumiii{\roman{enumiii}}
\renewcommand\theenumiv{\arabic{enumiv}}
\renewcommand\labelenumi{\theenumi}
\renewcommand\labelenumii{\theenumii.}
\renewcommand\labelenumiii{\theenumiii.}
\renewcommand\labelenumiv{\theenumiv.}
}
% Pages styles (master pages)
\makeatletter
\newcommand\ps@Standard{%
\renewcommand\@oddhead{}%
\renewcommand\@evenhead{}%
\renewcommand\@oddfoot{}%
\renewcommand\@evenfoot{}%
\setlength\paperwidth{8.5in}\setlength\paperheight{11in}\setlength\voffset{-1in}\setlength\hoffset{-1in}\setlength\topmargin{1in}\setlength\headheight{12pt}\setlength\headsep{0cm}\setlength\footskip{12pt+0cm}\setlength\textheight{11in-1in-1in-0cm-12pt-0cm-12pt}\setlength\oddsidemargin{1.25in}\setlength\textwidth{8.5in-1.25in-1.25in}
\renewcommand\thepage{\arabic{page}}
\setlength{\skip\footins}{0.0398in}\renewcommand\footnoterule{\vspace*{-0.0071in}\noindent\textcolor{black}{\rule{0.25\columnwidth}{0.0071in}}\vspace*{0.0398in}}
}
\makeatother
\pagestyle{Standard}
% footnotes configuration
\makeatletter
\renewcommand\thefootnote{\arabic{footnote}}
\makeatother
\title{Writing an Indicator Cookbook}
\begin{document}
\clearpage\pagestyle{Standard}
{\centering\selectlanguage{english}
\textbf{Writing an Indicator Cookbook}
\par}

{\centering\selectlanguage{english}\bfseries
Thomas Weigert
\par}

{\centering\selectlanguage{english}\bfseries
Mar 2008
\par}


\bigskip


\bigskip

\section{Introduction}
{\selectlanguage{english}
This note attempts to summarize lessons learned writing GT indicators,
in the hope that this will be useful to other GT developers. I shall
use the Bollinger Band indicator as a starting point and annotate its
implementation. Subsequent sections will illustrate more advanced
aspects of writing indicators.}

{\selectlanguage{english}
In the following, code fragments are typeset in red courier font.
Throughout the examples, the \textstyleCodefont{\$self} refers to an
indicator object. Numbered code lines are from the code for the
Bollinger Band indicator.}

\section{Header}
{\selectlanguage{english}
The first section sets up the module and loads the necessary
dependencies.}

\subsection{Package definition and object initialization}
{\selectlanguage{english}
Define a package for this indicator. The following use clauses are
standard for all indicators. Then define this package to be an instance
of the indicator object.}

\liststyleWWviiiNumv
\begin{enumerate}
\item {\selectlanguage{english}\ttfamily\color{red}
package GT::Indicators::BOL;}
\item 
\bigskip
\item {\selectlanguage{english}\ttfamily\color{red}
use strict;}
\item {\selectlanguage{english}\ttfamily\color{red}
use vars qw(@ISA @NAMES @DEFAULT\_ARGS);}
\item {\selectlanguage{english}\ttfamily\color{red}
use GT::Indicators;}
\item {\selectlanguage{english}\ttfamily\color{red}
@ISA = qw(GT::Indicators);}
\end{enumerate}
\subsection{Included packages}
{\selectlanguage{english}
Load all packages this indicator depends on. For example, Bollinger
Bands are moving averages that envelope a securities price. It consists
of three series: A simple moving average of the security, and two
series plotted \textit{n} standard deviation levels above and below the
moving average. Thus, in this particular case, we need three other
indicators: the I:SMA, I:StandardDeviation, and I:Prices.}

\liststyleWWviiiNumv
\setcounter{saveenum}{\value{enumi}}
\begin{enumerate}
\setcounter{enumi}{\value{saveenum}}
\item {\selectlanguage{english}\ttfamily\color{red}
use GT::Indicators::SMA;}
\item {\selectlanguage{english}\ttfamily\color{red}
use GT::Indicators::StandardDeviation;}
\item {\selectlanguage{english}\ttfamily\color{red}
use GT::Prices;}
\end{enumerate}
\subsection{Input parameters}
{\selectlanguage{english}
The default argument statement defines the default values for the input
parameters of the indicator. These are either constant values or they
depend on the current value of another series. In this example, the
first two parameters are by default given the constant values 20 and 2,
respectively. The third parameter is the result of evaluating the
indicator \textstyleCodefont{\{I:Prices CLOSE\}} at the current period,
yielding the current close of the prices array. }

\liststyleWWviiiNumv
\setcounter{saveenum}{\value{enumi}}
\begin{enumerate}
\setcounter{enumi}{\value{saveenum}}
\item {\selectlanguage{english}\ttfamily\color{red}
@DEFAULT\_ARGS = (20, 2, {\textquotedbl}\{I:Prices
CLOSE\}{\textquotedbl});}
\end{enumerate}
{\selectlanguage{english}
The arguments of the indicator are accessed via the methods, where
\textstyleCodefont{\$n} is the number of the argument (starting at 1),
\textstyleCodefont{\$calc} is a calculator, and \textstyleCodefont{\$i}
is the current period.}

{\selectlanguage{english}\ttfamily\color{red}
\$self{}-{\textgreater}\{args\}{}-{\textgreater}get\_arg\_constant(\$n)}

{\selectlanguage{english}\ttfamily\color{red}
\$self{}-{\textgreater}\{args\}{}-{\textgreater}get\_arg\_names(\$n)}

{\selectlanguage{english}\ttfamily\color{red}
\$self{}-{\textgreater}\{args\}{}-{\textgreater}get\_arg\_values(\$calc,
\$i, \$n)}

{\selectlanguage{english}
The first form requires that the argument is a constant, which can be
tested by
\textstyleCodefont{\$self{}-{\textgreater}\{args\}{}-{\textgreater}get\_arg\_constant(\$n)}.
The second form will obtain names of the corresponding argument (for
constant arguments, the name is the same as its
value).\footnote{\ Several indicators use
\textstyleCodefont{get\_arg\_names} in a context where the argument is
not guaranteed to be constant, and thus will fail when a non{}-constant
parameter is given (e.g., the name of a series).} The final form
obtains both constant and non{}-constant values for a given period (of
course, constants are the same for all periods).}

\section[Output values]{\label{bkm:Ref192524690}Output values}
{\selectlanguage{english}
Indicators will produce one or more output series. In other words, for
each period, and indicator will output one or more values. Output
values are defined in the names clause:}

\liststyleWWviiiNumv
\setcounter{saveenum}{\value{enumi}}
\begin{enumerate}
\setcounter{enumi}{\value{saveenum}}
\item {\selectlanguage{english}\ttfamily\color{red}
@NAMES =
({\textquotedbl}BOL[\#1,\#3]{\textquotedbl},{\textquotedbl}BOLSup[\#1,\#2,\#3]{\textquotedbl},{\textquotedbl}BOLInf[\#1,\#2,\#3]{\textquotedbl});}
\end{enumerate}
{\selectlanguage{english}
In this instance, we define three output series for Bollinger Bands: the
moving average, and the upper and lower bands. These values can be
referred to by the names given in the above clause, where the arguments
(hashed numbers in brackets) are replaced by the corresponding input
parameters of the indicator. The symbol \textstyleCodefont{\#*} is
replaced by all input parameters of the indicator.}

{\selectlanguage{english}
The name of an indicator can be either used literally as a string, or it
can be obtained by the following method, where \textstyleCodefont{\$n}
refers to the position of the name (starting at
\textstyleCodefont{0}):}

{\selectlanguage{english}\ttfamily\color{red}
\$self{}-{\textgreater}get\_name(\$n)}

{\selectlanguage{english}
The output values are set and read via a calculator
\textstyleCodefont{\$calc}, where \textstyleCodefont{\$name} is the
name of the output value and \textstyleCodefont{\$i} is the period:}

{\selectlanguage{english}\ttfamily\color{red}
\$calc{}-{\textgreater}indicators{}-{\textgreater}get(\$name, \$i)}

{\selectlanguage{english}\ttfamily\color{red}
\$calc{}-{\textgreater}indicators{}-{\textgreater}set(\$name, \$i,
\$value)}

\section{Initialization}
{\selectlanguage{english}
If an indicator requires intermediate series to compute its value or
requires data from past periods, these are set up in the initialization
method. This method is passed an indicator object as the single
parameter:}

\liststyleWWviiiNumv
\setcounter{saveenum}{\value{enumi}}
\begin{enumerate}
\setcounter{enumi}{\value{saveenum}}
\item {\selectlanguage{english}\ttfamily\color{red}
sub initialize \{}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my (\$self) = @\_;}
\end{enumerate}
\subsection[Intermediate series]{Intermediate series}
{\selectlanguage{english}
Many indicators depend in their computation on other series. For
example, Bollinger bands need the simple moving average of the price of
the security for each period, as well as the standard deviation of the
price of the security for each period. Both of these intermediate
values form a series. Each intermediate series must be created in the
initialization method and be assigned to an attribute of the indicator.
A series is created by calling the new method on its class and passing
the appropriate arguments, or by evaluating the textual representation
of the indicator defining the series. These intermediate series may
rely on output values or on temporary data, see Section 7.2. For
Bollinger Band we define the I:SMA and I:StandardDeviation as
intermediate series, passing both the first (period) and third (data
array the indicator is applied to, typically I:Prices) arguments. If
the indicator was not given any parameters upon creation, the default
values are used.}

\liststyleWWviiiNumv
\setcounter{saveenum}{\value{enumi}}
\begin{enumerate}
\setcounter{enumi}{\value{saveenum}}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ \$self{}-{\textgreater}\{{\textquotesingle}sma{\textquotesingle}\}
= GT::Indicators::SMA{}-{\textgreater}new([ }
\item {\selectlanguage{english}\ttfamily\color{red}
 
\$self{}-{\textgreater}\{{\textquotesingle}args{\textquotesingle}\}{}-{\textgreater}get\_arg\_names(1),
\$self{}-{\textgreater}\{{\textquotesingle}args{\textquotesingle}\}{}-{\textgreater}get\_arg\_names(3)
]);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ \$self{}-{\textgreater}\{{\textquotesingle}sd{\textquotesingle}\}
= GT::Indicators::StandardDeviation{}-{\textgreater}new([ }
\item {\selectlanguage{english}\ttfamily\color{red}
 
\$self{}-{\textgreater}\{{\textquotesingle}args{\textquotesingle}\}{}-{\textgreater}get\_arg\_names(1),
\$self{}-{\textgreater}\{{\textquotesingle}args{\textquotesingle}\}{}-{\textgreater}get\_arg\_names(3)
]);}
\end{enumerate}
{\selectlanguage{english}
During the computation of the indicator, the intermediate series is
either computed via the dependency mechanism (see Section 4.2) or by
explicitly computing the series via:}

{\selectlanguage{english}\ttfamily\color{red}
\$self{}-{\textgreater}\{{\textquotesingle}sma{\textquotesingle}\}{}-{\textgreater}calculate(\$calc,
\$i)}

{\selectlanguage{english}\ttfamily\color{red}
\$self{}-{\textgreater}\{{\textquotesingle}sma{\textquotesingle}\}{}-{\textgreater}calculate\_interval(\$calc,
\$i, \$j)}

{\selectlanguage{english}
where \textstyleCodefont{\$calc} is a calculator, and
\textstyleCodefont{\$i} and \textstyleCodefont{\$j} are time periods.
The values of these series are obtained via the standard get method,
e.g., the \textit{i}th value of the SMA is obtained via}

{\selectlanguage{english}\ttfamily\color{red}
\$calc{}-{\textgreater}indicators{}-{\textgreater}set(\$self{}-{\textgreater}\{{\textquotesingle}sma{\textquotesingle}\}{}-{\textgreater}get\_name,
\$i)}

\subsection[Dependencies]{\label{bkm:Ref192502803}Dependencies}
{\selectlanguage{english}
Many indicators depend on past data to calculate their current value,
either on past price information, or on the previous values of the
indicator or on the previous value of intermediate series. A key
feature of GT is that the computation of those past values can be
largely driven automatically through a dependency mechanism. We can
declare the current value of an indicator to be dependent on the
previous values of its parameters, or of other series, or of the price
information it is operating on. Such dependencies are declared for
\textit{n} periods of data; when updating dependencies those \textit{n}
values will be ensured to be available. To satisfy dependencies may in
turn require additional data, the computing the dependencies may in
turn depend on other values. The dependency mechanism propagates
automatically until all dependencies are satisfied. }

{\selectlanguage{english}
Determining the correct dependencies is important to be able to compute
the indicator both correctly and efficiently. If too little data is
available, an indicator may not be able to be computed, at best, or may
give incorrect results, at worst. If too much data is required, less
history of an indicator can be computed.}

{\selectlanguage{english}
When the dependencies are known at the time the indicator is created,
the dependencies are defined in the initialization section (volatile
dependencies see Section 7.3, allow dependencies to be computed
dynamically during the computation of the values of the indicator). The
following methods can be used to define dependencies:}

{\selectlanguage{english}\ttfamily\color{red}
\$self{}-{\textgreater}add\_indicator\_dependency(\$indic, \$p)}

{\selectlanguage{english}\ttfamily\color{red}
\$self{}-{\textgreater}add\_arg\_dependency(\$n, \$p)}

{\selectlanguage{english}\ttfamily\color{red}
\$self{}-{\textgreater}add\_prices\_dependency(\$p)}

{\selectlanguage{english}
where \textstyleCodefont{\$indic} is an indicator,
\textstyleCodefont{\$n} refers to the \textit{n}{}-th parameter of the
indicator (counting from 1), and \textstyleCodefont{\$p} is the number
of periods of data this value depends on. The first form states that
the current value of the indicator depends on \textstyleCodefont{\$p}
periods of data of indicator \textstyleCodefont{\$indic}. The second
form states that the indicator depends on \textstyleCodefont{\$p}
periods of data referenced by parameter \textstyleCodefont{\$n}. The
third form states that the indicator depends on \textstyleCodefont{\$p}
periods of data of the input series (this form of dependency is only
needed when the indicator depends on more data periods than is
established by the dependency mechanism).\footnote{\ While it can be
found in a number of indicators, this indicator is rarely (if ever)
needed.}}

{\selectlanguage{english}
For the Bollinger Bands, each value depends on the current value of the
moving average and the standard deviation. However, dependencies
require at least one day of data, and thus the below declare the
current value to be dependent of 1 day of data of the moving average
and the standard deviation. Note that each of these in turn require
data to be computed, but that dependency is declared as part of the
definition of these indicators, and is automatically accounted for by
the dependency mechanism.}

\liststyleWWviiiNumv
\setcounter{saveenum}{\value{enumi}}
\begin{enumerate}
\setcounter{enumi}{\value{saveenum}}
\item {\selectlanguage{english}\ttfamily\color{red}
 
\ \ \ \$self{}-{\textgreater}add\_indicator\_dependency(\$self{}-{\textgreater}\{{\textquotesingle}sma{\textquotesingle}\},
1);}
\item {\selectlanguage{english}\ttfamily\color{red}
 
\ \ \ \$self{}-{\textgreater}add\_indicator\_dependency(\$self{}-{\textgreater}\{{\textquotesingle}sd{\textquotesingle}\},
1);}
\end{enumerate}
{\selectlanguage{english}
The Bollinger Band indicator in addition establishes a dependency on the
period passed as the first parameter, assuming that parameter is
constant. However, this declaration is technically not necessary, as
this dependency is already established by the dependencies of the
intermediate series.}

\liststyleWWviiiNumv
\setcounter{saveenum}{\value{enumi}}
\begin{enumerate}
\setcounter{enumi}{\value{saveenum}}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ if
(\$self{}-{\textgreater}\{{\textquotesingle}args{\textquotesingle}\}{}-{\textgreater}is\_constant(1))
\{}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ \ \ \ \ \$self{}-{\textgreater}add\_prices\_dependency(\$self{}-{\textgreater}\{{\textquotesingle}args{\textquotesingle}\}{}-{\textgreater}get\_arg\_constant(1));}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ \}}
\item 
\bigskip
\item {\selectlanguage{english}\ttfamily\color{red}
\}}
\end{enumerate}
\section{Calculating the value of the indicator}
{\selectlanguage{english}
The GT framework provides two means of calculating the value of an
indicator: we can either compute a single value of the indicator, given
its dependencies, or we can compute the value of the indicator
throughout a given interval. One or the other of these methods must be
defined,\footnote{\ Note that if the \textstyleCodefont{calculate}
method is omitted, the indicator may fail if this method is indirectly
invoked (e.g., when running anashell.pl), as this method is not defined
in the superclass. It is safer to omit the
\textstyleCodefont{calculate\_interval} method.} albeit often both
methods are given. Typically, calculating the value of the indicator
over the full interval required will be faster, potentially much faster
as calculating the value of the indicator one period at a time may
often repeat much of the computation needlessly.}

\subsection{Calculating a single value of the indicator}
{\selectlanguage{english}
The current value of the indicator is computed by the
\textstyleCodefont{calculate} method, which takes as arguments a
calculator and the current period. This method typically follows the
following steps:}

\liststyleWWviiiNumv
\setcounter{saveenum}{\value{enumi}}
\begin{enumerate}
\setcounter{enumi}{\value{saveenum}}
\item {\selectlanguage{english}\ttfamily\color{red}
sub calculate \{}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my (\$self, \$calc, \$i) = @\_;}
\end{enumerate}
{\selectlanguage{english}
a) Define temporary variables}

{\selectlanguage{english}
Several temporaries are defined for convenience: The distance of the
upper and lower bands from the moving average, as determined by the
second parameter, the names of the intermediate series used, and the
names of the output values.}

\liststyleWWviiiNumv
\setcounter{saveenum}{\value{enumi}}
\begin{enumerate}
\setcounter{enumi}{\value{saveenum}}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my \$nsd =
\$self{}-{\textgreater}\{{\textquotesingle}args{\textquotesingle}\}{}-{\textgreater}get\_arg\_values(\$calc,
\$i, 2);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my \$sma\_name =
\$self{}-{\textgreater}\{{\textquotesingle}sma{\textquotesingle}\}{}-{\textgreater}get\_name;}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my \$sd\_name =
\$self{}-{\textgreater}\{{\textquotesingle}sd{\textquotesingle}\}{}-{\textgreater}get\_name;}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my \$bol\_name = \$self{}-{\textgreater}get\_name(0);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my \$bolsup\_name = \$self{}-{\textgreater}get\_name(1);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my \$bolinf\_name = \$self{}-{\textgreater}get\_name(2);}
\item 
\bigskip
\end{enumerate}
{\selectlanguage{english}
b) Return if the required values of the indicator are already available;
these may have been computed earlier.}

\liststyleWWviiiNumv
\setcounter{saveenum}{\value{enumi}}
\begin{enumerate}
\setcounter{enumi}{\value{saveenum}}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ return if
(\$calc{}-{\textgreater}indicators{}-{\textgreater}is\_available(\$bol\_name,
\$i) \&\&}
\item {\selectlanguage{english}\ttfamily\color{red}
 
\ \ \ \ \ \ \ \ \ \ \ \ \ \$calc{}-{\textgreater}indicators{}-{\textgreater}is\_available(\$bolsup\_name,
\$i) \&\&}
\item {\selectlanguage{english}\ttfamily\color{red}
 
\ \ \ \ \ \ \ \ \ \ \ \ \ \$calc{}-{\textgreater}indicators{}-{\textgreater}is\_available(\$bolinf\_name,
\$i));}
\end{enumerate}
{\selectlanguage{english}
c) Return if the dependencies required to compute the value of this
indicator are not satisfied. This check will attempt to compute the
dependencies but fail when the dependencies cannot be computed. This
triggers the dependency mechanism.}

\liststyleWWviiiNumv
\setcounter{saveenum}{\value{enumi}}
\begin{enumerate}
\setcounter{enumi}{\value{saveenum}}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ return if (! \$self{}-{\textgreater}check\_dependencies(\$calc,
\$i));}
\item 
\bigskip
\end{enumerate}
{\selectlanguage{english}
d) Compute the current value of the indicator.}

{\selectlanguage{english}
For the Bollinger Band indicator, we firsts obtain the values of the
moving average and the standard deviation. The upper band is obtained
by adding the appropriate factor of the standard deviation to the
moving average; the lower band is calculated similarly.}

\liststyleWWviiiNumv
\setcounter{saveenum}{\value{enumi}}
\begin{enumerate}
\setcounter{enumi}{\value{saveenum}}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my \$sma\_value =
\$calc{}-{\textgreater}indicators{}-{\textgreater}get(\$sma\_name,
\$i);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my \$sd\_value =
\$calc{}-{\textgreater}indicators{}-{\textgreater}get(\$sd\_name,
\$i);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ }
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my \$bolsup\_value = \$sma\_value + (\$nsd * \$sd\_value);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my \$bolinf\_value = \$sma\_value {}- (\$nsd * \$sd\_value);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ }
\end{enumerate}
{\selectlanguage{english}
Note that computing the current value of the indicator may in fact
require iterating over past periods.}

{\selectlanguage{english}
e) Update the output values for the current period}

{\selectlanguage{english}
For the Bollinger Band store the moving average value into the first
output series, the upper band value into the second output series, and
the lower band value into the last output series.}

\liststyleWWviiiNumv
\setcounter{saveenum}{\value{enumi}}
\begin{enumerate}
\setcounter{enumi}{\value{saveenum}}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ \$calc{}-{\textgreater}indicators{}-{\textgreater}set(\$bol\_name,
\$i, \$sma\_value);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ \$calc{}-{\textgreater}indicators{}-{\textgreater}set(\$bolsup\_name,
\$i, \$bolsup\_value);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ \$calc{}-{\textgreater}indicators{}-{\textgreater}set(\$bolinf\_name,
\$i, \$bolinf\_value);}
\item {\selectlanguage{english}\ttfamily\color{red}
\}}
\item 
\bigskip
\end{enumerate}
\subsection[Calculating a the indicator throughout an
interval]{\label{bkm:Ref192603010}Calculating a the indicator
throughout an interval}
{\selectlanguage{english}
The \textstyleCodefont{calculate\_interval} method computes the value of
the indicator over a given interval. It is passed a calculator as well
as the beginning and end of the interval of interest. This method can
be obtained systematically from the \textstyleCodefont{calculate}
method by the following steps:}

{\selectlanguage{english}
a)~Change all occurrences of \textstyleCodefont{get\_arg\_values} to the
corresponding \textstyleCodefont{get\_arg\_constant}}

{\selectlanguage{english}
b)~Change all occurrences of \textstyleCodefont{check\_dependencies} to
the corresponding \textstyleCodefont{check\_dependencies \_interval}}

{\selectlanguage{english}
c)~Change all occurrences of \textstyleCodefont{is\_available} to the
corresponding \textstyleCodefont{is\_available\_interval}}

{\selectlanguage{english}
d)~Compute the current value of the indicator within a loop from the
beginning of the interval to the end of the interval.}

\liststyleWWviiiNumv
\setcounter{saveenum}{\value{enumi}}
\begin{enumerate}
\setcounter{enumi}{\value{saveenum}}
\item {\selectlanguage{english}\ttfamily\color{red}
sub calculate\_interval \{}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my (\$self, \$calc, \$first, \$last) = @\_;}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my \$nsd =
\$self{}-{\textgreater}\{{\textquotesingle}args{\textquotesingle}\}{}-{\textgreater}get\_arg\_constant(2);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my \$sma\_name =
\$self{}-{\textgreater}\{{\textquotesingle}sma{\textquotesingle}\}{}-{\textgreater}get\_name;}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my \$sd\_name =
\$self{}-{\textgreater}\{{\textquotesingle}sd{\textquotesingle}\}{}-{\textgreater}get\_name;}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my \$bol\_name = \$self{}-{\textgreater}get\_name(0);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my \$bolsup\_name = \$self{}-{\textgreater}get\_name(1);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my \$bolinf\_name = \$self{}-{\textgreater}get\_name(2);}
\item 
\bigskip
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ return if
(\$calc{}-{\textgreater}indicators{}-{\textgreater}is\_available\_interval(\$bol\_name,
\$first, \$last) \&\&}
\item {\selectlanguage{english}\ttfamily\color{red}
 
\ \ \ \ \ \ \$calc{}-{\textgreater}indicators{}-{\textgreater}is\_available\_interval(\$bolsup\_name,
\$first, \$last) \&\&}
\item {\selectlanguage{english}\ttfamily\color{red}
 
\ \ \ \ \ \ \$calc{}-{\textgreater}indicators{}-{\textgreater}is\_available\_interval(\$bolinf\_name,
\$first, \$last));}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ return if (!
\$self{}-{\textgreater}check\_dependencies\_interval(\$calc, \$first,
\$last));}
\item 
\bigskip
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ for (my \$i=\$first;\$i{\textless}=\$last;\$i++) \{}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ \ \ my \$sma\_value =
\$calc{}-{\textgreater}indicators{}-{\textgreater}get(\$sma\_name,
\$i);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ \ \ my \$sd\_value =
\$calc{}-{\textgreater}indicators{}-{\textgreater}get(\$sd\_name,
\$i);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ }
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ \ \ my \$bolsup\_value = \$sma\_value + (\$nsd * \$sd\_value);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ \ \ my \$bolinf\_value = \$sma\_value {}- (\$nsd *
\$sd\_value);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ }
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ \ \ \$calc{}-{\textgreater}indicators{}-{\textgreater}set(\$bol\_name,
\$i, \$sma\_value);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ \ \ \$calc{}-{\textgreater}indicators{}-{\textgreater}set(\$bolsup\_name,
\$i, \$bolsup\_value);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ \ \ \$calc{}-{\textgreater}indicators{}-{\textgreater}set(\$bolinf\_name,
\$i, \$bolinf\_value);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ \}}
\item {\selectlanguage{english}\ttfamily\color{red}
\}}
\end{enumerate}
\section{End of file}
{\selectlanguage{english}
As common practice in Perl modules, conclude the file with a successful
value.}

\liststyleWWviiiNumv
\setcounter{saveenum}{\value{enumi}}
\begin{enumerate}
\setcounter{enumi}{\value{saveenum}}
\item {\selectlanguage{english}\ttfamily\color{red}
1;}
\end{enumerate}
\section{Additional capabilities}
{\selectlanguage{english}
There are a number of additional tools provided by GT which are not
leveraged in the Bollinger Bands indicator illustrated above. These are
discussed below. }

\subsection[Temporary series]{\label{bkm:Ref192502379}Temporary series}
{\selectlanguage{english}
In addition to storing results in output values, as discussed in Section
3, and indicator may also store data into temporary series that are not
visible outside of the indicator. To create a temporary series, assign
a I:Generic:Container indicator to an attribute of the indicator
object:}

{\selectlanguage{english}\ttfamily\color{red}
\$self{}-{\textgreater}\{{\textquotesingle}temp{\textquotesingle}\} =
GT::Indicators::Generic::Container{}-{\textgreater}new([{\textquotesingle}temp{\textquotesingle}]);}

{\selectlanguage{english}
Above creates a new temporary series with the name
\textstyleCodefont{temp}. This series is an indicator and thus values
can be read and written to this series as to any indicator:}

{\selectlanguage{english}\ttfamily\color{red}
\$calc{}-{\textgreater}indicators{}-{\textgreater}get(\$self{}-{\textgreater}\{{\textquotesingle}temp{\textquotesingle}\}{}-{\textgreater}get\_name,
\$i)}

{\selectlanguage{english}\ttfamily\color{red}
\$calc{}-{\textgreater}indicators{}-{\textgreater}set(\$self{}-{\textgreater}\{{\textquotesingle}temp{\textquotesingle}\}{}-{\textgreater}get\_name,
\$i, \$value)}

\subsection[Constructing intermediate series from other
series]{\label{bkm:Ref192502529}Constructing intermediate series from
other series}
{\selectlanguage{english}
An intermediate series may rely on another intermediate series, on a
temporary series, or on an output series. In this situation, when
defining an intermediate series, the dependent series are provided as
parameters.}

{\selectlanguage{english}
For example, to define a standard moving average of the upper band of
the Bollinger Band indicator:}

{\selectlanguage{english}\ttfamily\color{red}
\$self{}-{\textgreater}\{{\textquotesingle}upper{\textquotesingle}\} =
GT::Indicators::SMA{}-{\textgreater}new([
\$self{}-{\textgreater}\{{\textquotesingle}args{\textquotesingle}\}{}-{\textgreater}get\_arg\_names(1),
{\textquotedbl}\{I:Generic:ByName {\textquotedbl} .
\$self{}-{\textgreater}get\_name(1) . {\textquotedbl}\}{\textquotedbl}
]);}

{\selectlanguage{english}
This constructs an intermediate SMA series of the second output value of
the current indicator, with the period taken from the first parameter
of the current indicator and assigns it to an attribute of the
indicator object.}

{\selectlanguage{english}
Similarly one constructs the simple moving average of the temp indicator
from Section 7.1 as}

{\selectlanguage{english}\ttfamily\color{red}
\$self{}-{\textgreater}\{{\textquotesingle}sma{\textquotesingle}\} =
GT::Indicators::SMA{}-{\textgreater}new([
\$self{}-{\textgreater}\{{\textquotesingle}args{\textquotesingle}\}{}-{\textgreater}get\_arg\_names(1),
{\textquotedbl}\{I:Generic:ByName temp\}{\textquotedbl} ]);}

{\selectlanguage{english}
It appears that it is not possible to construct a new series that based
on an intermediate series. As a workaround, explicitly specify that
intermediate series in the construction of the second
series.\footnote{\ This will involve a duplication of the computation
of this series. Implement a means of obtaining the value of the
intermediate series to avoid needless duplicate computation.}}

\subsection[Volatile dependencies]{\label{bkm:Ref192505424}Volatile
dependencies}
{\selectlanguage{english}
It is also possible for indicator dependencies to dynamically change
during the computation of a series, either by the length of the
dependency being computed at each iteration, or by it depending on the
value of a series. Dynamically changing dependencies are referred to as
``volatile''. They are defined analogously to static dependencies using
the following methods, where \textstyleCodefont{\$indic} is an
indicator, \textstyleCodefont{\$n} refers to the \textit{n}{}-th
parameter of the indicator (counting from 1), and
\textstyleCodefont{\$p} is the number of periods of data this value
depends on:}

{\selectlanguage{english}\ttfamily\color{red}
\$self{}-{\textgreater}add\_volatile\_indicator\_dependency(\$indic,
\$p)}

{\selectlanguage{english}\ttfamily\color{red}
\$self{}-{\textgreater}add\_volatile\_arg\_dependency(\$n, \$p)}

{\selectlanguage{english}\ttfamily\color{red}
\$self{}-{\textgreater}add\_volatile\_prices\_dependency(\$p)}

{\selectlanguage{english}
Before defining volatile dependencies, all volatile dependencies from
the previous period must be removed through calling}

{\selectlanguage{english}\ttfamily\color{red}
\$self{}-{\textgreater}remove\_volatile\_dependencies()}

{\selectlanguage{english}
Volatile dependencies are mostly useful only when indicators are
calculated one period at a time (i.e., in the
\textstyleCodefont{calculate} method).\footnote{\ Note that several
indicators add volatile indicators in the
\textstyleCodefont{calculate\_interval }method. This will work only if
careful attention is paid to that the dependency period is correctly
obtained. In many such situations, the dependency period is established
correctly only when the corresponding parameter is both constant and
positive. Further, unless the dependencies are updated throughout the
loop, they reduce to static dependencies (in those situations, if
calculate is desired to support volatile dependencies, it is useful to
define the volatile dependencies also in calculate\_interval to avoid
duplicated dependency computation in calculate where static
dependencies defined).}}

\section{Styles of calculating indicators}
{\selectlanguage{english}
The value of an indicator in the following three ways: (i) by obtaining
the value of an input data series, (ii) by applying an indicator to a
data series (either an input series or a temporary our output series,
or (iii) by performing some computation on the current or past values
of one or more available data series. These can be combined in
arbitrary ways.}

{\selectlanguage{english}
The Bollinger Band indicator above used each of these: It obtains the
value of the input data series, applies two indicators (SMA,
StandardDeviation) to these values, and then performs a calculation on
the current value of these indicators. Other indicators require more
complicated scenarios: For example, an indicator may require smoothing
of the calculated value (as in the stochastic indicator I:STO, the
Fisher indicator I:FISH, or the Volume Oscillator I:VOSC). The
stochastic momentum indicator (I:SMI) first obtains values from an
input series and applies an indicator to these values, then performs
some calculation to produce a temporary series, then applies smoothing
to these temporary series, perform some computation on the results, and
apply a final smoothing. These more complicated calculations can be
constructed in the following manner: Consider the dependencies required
by each of the steps in the calculation and begin the calculation at
the earliest point in the chain of dependencies.}

{\selectlanguage{english}
a) The current or previous value of an indicator can always be obtained
as described above.}

{\selectlanguage{english}
b) If an indicator application is not the final step, then calculate the
value of that indicator starting from the earliest period it satisfies
a dependency for subsequent computations up to the current period.}

{\selectlanguage{english}
c) If a computation on current or past values of one or more series is
not the final step, then calculate all subsequent values in a loop from
the earliest period the computation satisfies a dependency for
subsequent computations up to the current period.}

{\selectlanguage{english}
For example, the following is the \textstyleCodefont{calculate} method
for I:VOSC. The oscillator is calculated by first computing the value
of the volume and then smoothing that value with a period given by the
first parameter. The smoothing is performed after the computation of
the volume measure, and thus the indicator first computes sufficient
data values for the smoothing operator in the loop on lines 11{}-25.
After that the smoothing operator is applied (line 27).}

\liststyleWWviiiNumv
\begin{enumerate}
\item {\selectlanguage{english}\ttfamily\color{red}
sub calculate \{}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my (\$self, \$calc, \$i) = @\_;}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my \$vosc\_name = \$self{}-{\textgreater}get\_name(0);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my \$volume\_name = \$self{}-{\textgreater}get\_name(1);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my \$volume = 0;}
\item 
\bigskip
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ return if
(\$calc{}-{\textgreater}indicators{}-{\textgreater}is\_available(\$vosc\_name,
\$i));}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ return if (! \$self{}-{\textgreater}check\_dependencies(\$calc,
\$i));}
\item 
\bigskip
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my \$nb\_days =
\$self{}-{\textgreater}\{{\textquotesingle}args{\textquotesingle}\}{}-{\textgreater}get\_arg\_values(\$calc,
\$i, 1);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ for (my \$n = 0; \$n {\textless} \$nb\_days; \$n++) \{}
\item 
\bigskip
\item {\selectlanguage{english}\ttfamily\color{red}
 next if
\$calc{}-{\textgreater}indicators{}-{\textgreater}is\_available(\$volume\_name,
\$i {}- \$n);}
\item {\selectlanguage{english}\ttfamily\color{red}
 if (\$calc{}-{\textgreater}prices{}-{\textgreater}at(\$i {}-
\$n){}-{\textgreater}[\$CLOSE] {\textgreater}
\$calc{}-{\textgreater}prices{}-{\textgreater}at(\$i {}-
\$n){}-{\textgreater}[\$OPEN]) \{}
\item {\selectlanguage{english}\ttfamily\color{red}
  \ \ \ \$volume = \$calc{}-{\textgreater}prices{}-{\textgreater}at(\$i
{}- \$n){}-{\textgreater}[\$VOLUME];}
\item {\selectlanguage{english}\ttfamily\color{red}
 \}}
\item {\selectlanguage{english}\ttfamily\color{red}
 if (\$calc{}-{\textgreater}prices{}-{\textgreater}at(\$i {}-
\$n){}-{\textgreater}[\$CLOSE] {\textless}
\$calc{}-{\textgreater}prices{}-{\textgreater}at(\$i {}-
\$n){}-{\textgreater}[\$OPEN]) \{}
\item {\selectlanguage{english}\ttfamily\color{red}
  \ \ \ \$volume =
{}-\$calc{}-{\textgreater}prices{}-{\textgreater}at(\$i {}-
\$n){}-{\textgreater}[\$VOLUME];}
\item {\selectlanguage{english}\ttfamily\color{red}
 \}}
\item {\selectlanguage{english}\ttfamily\color{red}
 if (\$calc{}-{\textgreater}prices{}-{\textgreater}at(\$i {}-
\$n){}-{\textgreater}[\$CLOSE] eq
\$calc{}-{\textgreater}prices{}-{\textgreater}at(\$i {}-
\$n){}-{\textgreater}[\$OPEN]) \{}
\item {\selectlanguage{english}\ttfamily\color{red}
  \ \ \ \$volume = 0;}
\item {\selectlanguage{english}\ttfamily\color{red}
 \}}
\item {\selectlanguage{english}\ttfamily\color{red}
 \$calc{}-{\textgreater}indicators{}-{\textgreater}set(\$volume\_name,
\$i {}- \$n, \$volume);}
\item 
\bigskip
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ \}}
\item 
\bigskip
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ \$self{}-{\textgreater}\{{\textquotesingle}sma{\textquotesingle}\}{}-{\textgreater}calculate(\$calc,
\$i);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ my \$vosc\_value =
\$calc{}-{\textgreater}indicators{}-{\textgreater}get(\$self{}-{\textgreater}\{{\textquotesingle}sma{\textquotesingle}\}{}-{\textgreater}get\_name,
\$i);}
\item {\selectlanguage{english}\ttfamily\color{red}
\ \ \ \ \$calc{}-{\textgreater}indicators{}-{\textgreater}set(\$vosc\_name,
\$i, \$vosc\_value);}
\item {\selectlanguage{english}\ttfamily\color{red}
\}}
\end{enumerate}
{\selectlanguage{english}
The transformation to the \textstyleCodefont{calculate\_interval} method
is similar to as described in Section 5.2, with the exception that the
bounds of any loop used in \textstyleCodefont{calculate} will have to
take the required data history in account. For an example of a more
complex indicator as well as for the transformation of the
\textstyleCodefont{calculate\_interval} method see the Stochastic
Momentum Indicator I:SMI.}

\section{Documentation}
{\selectlanguage{english}
Adequate documentation in pod format should be provided for each
indicator.}
\end{document}
