PureBytes Links
Trading Reference Links
|
Issue 2/2000 AmiBroker Tips weekly newsletter.
Issue 2/2000.
Copyright (C)2000 Tomasz Janeczko.
All back issues available from:
http://www.amibroker.com/newsletter
IN THIS ISSUE
1 Welcome
2 AFL Formula Library: DeMarker and Range Expansion Index
3 Tip of the week: Using graph styles, colors and titles in Indicator Builder
1 Welcome
Welcom to the second issue of AmiBroker Tips newsletter. With this release a new regular column will start: "AFL Formula Library". In this column I will present new AFL formulas for indicators, trading systems and commentaries. All formulas presented in this column will also appear in http://www.amibroker.com/library.html
By the way: Do you find this newsletter useful? Have any comments/suggestions or article ideas. Please don't hesitate to drop a line to newsletter@xxxxxxxxxxxxxx
2 AFL Formula Library: DeMarker and Range Expansion Index
DeMarker
The DeMarker indicator is an attempt to overcome the shortcomings of classical overbought / oversold indicators. The DeMarker Indicator identifies potential price bottoms and tops. It accomplishes this by making price comparisons from one bar to the next and measuring the level of price demand. The formula for DeMarker is quite simple - in AFL it looks like this:
/*
** Tom Demark's DeMarker Indicator
** AFL Implementation by Tomasz Janeczko
*/
highm = IIF( H > Ref( H, -1 ), H - Ref( H, - 1), 0 );
lowm = IIF( L < Ref( L, -1 ), Ref( L, - 1 ) - L, 0 );
DeMarker = 100 * Sum( highm, 13 )/( Sum( lowm, 13 ) + Sum( highm, 13 ) );
graph0 = DeMarker;
DeMarker should be interpreted as other overbought / oversold indicators such as RSI with the levels of 30 and 70. Compared to RSI it is smoother but still able to detect tops and bottoms a little bit better.
Range Expansion Index
The DeMark Range Expansion Index is a market-timing oscillator described inDeMark on Day Trading Options, by T.R. DeMark and T.R. Demark, Jr., McGrawHill, 1999. The oscillator is arithmetically calculated and is designed toovercome problems with exponentially calculated oscillators, like MACD. The TD REI oscillator typically produces values of -100 to +100 with 45 or higher indicating overbought conditions and -45 or lower indicating oversold.Here is how Tom DeMark describes the calculation of Range Expansion Index:
"The first step in calculating the REI is to add together the respective differences between the current day's high and the high two days earlier and the current day's low and the low two days earlier. These values will be positive or negative depending on whether the current day's high and low are greater or less than the high and low two days earlier. To prevent buying or selling prematurely into a steep price decline or advance, two additionalconditions should be met to qualify a positive or negative value on a particular day: 1) either the high two days earlier must be greater than or equal to the close seven or eight days ago, or the current day's high must be greater than or equal to the low five or six days ago; 2) either the low two days earlier must be less than or equal to the close seven or eight days ago, or the current day's low must be less than or equal to the high five or six days ago. If either of these conditions are not satisfied, a zero value is assigned to that day. If they both are, the daily values (the differences between the highs and lows) are summed , and the specific value for that next day is determined. Next, all the positives and negative values are added together over a five-day period. This value is then divided by the absolute value price movement of each day over the five-day period. The numerator of the calculation can be either positive, negative or zero, because each day's value is summed for five days, but the denominator is always positive because it is only concerned with the differential price movement itself. This value is then multiplied by 100. Consequently, the REI can fluctuate between +100 and -100."
Following this description I wrote the AFL formula for REI:
/*
** Tom DeMark's Range Expansion Index
** AFL Implementation by Tomasz Janeczko
*/
HighMom = H - Ref( H, -2 );
LowMom = L - Ref( L, -2 );
Cond1 = ( H >= Ref( L,-5) OR H >= Ref( L, -6 ) );
Cond2 = ( Ref( H, -2 ) >= Ref( C, -7 ) OR Ref( H, -2 ) >= Ref( C, -8 ) );
Cond3 = ( L <= Ref( H, -5 ) OR L <= Ref( H, -6) );
Cond4 = ( Ref( L, -2 ) <= Ref( C, -7 ) OR Ref( L, -2 ) <= Ref( C, -8 ) );
Cond = ( Cond1 OR Cond2 ) AND ( Cond3 OR Cond4 );
Num = IIf( Cond, HighMom + LowMom, 0 );
Den = Abs( HighMom ) + Abs( LowMom );
TDREI = 100 * Sum( Num, 5 )/Sum( Den, 5 ) ;
graph0 = TDREI;
DeMark advises against trading in extreme overbought or oversold conditionsindicated by six or more bars above or below the 45 thresholds. For more information on calculation and using both TD REI and DeMarker indicator check this Tom DeMark's article.
3 Tip of the week: Using graph styles, colors and titles in Indicator Builder
Note: Some of the functionality described here is available only in AmiBroker version 3.4 (definable styles and colors)
AmiBroker 3.4 introduced customizable styles and colors of graphs in customindicators. These features allow more flexibility in designing your indicators. This article will explain how to use styles and colors. It will also explain how to define chart title that appears at the top of the chart.
Let's begin with a list of special variable names available in Indicator Builder:
Variable Usage Applies to
title defines title text Indicator Builder
maxgraph specifies maximum number of graphs to be drawn in custom indicator window (default=3) Indicator Builder
graphN defines the formula for the graph number N (where N is a number 0,1,2,..., maxgraph-1) Indicator Builder
graphNcolor defines the color index of Nth graph line (color indexes are related to the current palette - see Preferences/Color) Indicator Builder
graphNstyle defines the style of Nth graph. Style is defined as a combination (sum) of one or more following flags:
1 - normal (line) chart (default)
2 - histogram chart
4 - fat (thick)
8 - include dots
16 - no line
32 - semi-logarithmic scale
64 - candlestick chart
128 - traditional bar chart
256 - no draw (perform axis scaling only)
Not all flag combinations make sense, for example (64+1) (candlestick+ line) will result in candlestick chart (style=64)
Note on candlestick/bar charts: these styles use indirectly O, H, L arrays in addition to graphN. So ordinary candlestick price chart formula isgraph0=close; graph0style=64;.
But if you want to draw something else than close price you have to assign new values to predefined O,H,L arrays.
Indicator Builder
New graphNstyle and graphNcolor variables (where N is 0,1,2...) allow you to change the default plain line style. Let me show you some examples, here is how ADX/DMI chart defined in AFL would look like:
maxgraph = 3;
graph2 = pdi(14);
graph1 = mdi(14);
graph0 = adx(14);
graph2style = graph1style = 1;
graph0style = 5;
graph1color = 5;
graph2color = 6;
graph0color = 7;
title=name() + " - +DM" + writeval( graph2 )+ ", -DM" + writeval( graph1 )+
", ADX" + writeval( graph0 );
The first line defines that we will use 3 graphs (this is the default so itis not really needed). Next three lines define the formulas for +DM, -DM and ADX graphs. Then we define that graph2 and graph1 (+DM and -DM) will have plain line style and ADX line (graph0) will have fat line style (1+4). Then we define the colors according to palette settings. AmiBroker uses a pallette that can be changed by user. Go to Tools->Preferences->Colors, click Palette Editor and take a look at "Custom colors" - these 16 color boxes display AmiBrokers pallette - the first box is represent color index 0, next one - 1 and so on. So if you write graph0color = 7 you tell AmiBroker to take the color with index 7 (this is the color with which Pallette editor'sbox number eight is painted). And now we are at the last line which defines the formula for the chart title. Instead of plain default <TICKERNAME> - <CustomIndicatorName> we can show something more informative. In this example we concatenate a string which contains ticker name and the values of +DM, -DM and ADX. If you assign any string to the title variable it will be displayed instead of the default one.
Next example is a Parabolic SAR chart drawn over traditional bar chart:
graph0 = close;
graph0style = 128;
graph0color = 2;
graph1 = sar();
graph1style = 8+16;
graph1color = 8;
title = "This is my title: " + name() + " - SAR - " + writeval( graph1 );
In this example we plot SAR chart with only dots (graph1style = 8 (dots) + 16 (no line)). A price chart (graph0) is drawn with traditional bar chartstyle (graph0style=128). In order to do that we have to assign a close price to graph0 variable. Note that for candlesticks and traditional bar charts AmiBroker uses indirectly O,H, L arrays, since more data is needed thanone array. So if you want to plot something different than close price youhave to assign new values also to O,H,L predefined arrays. Please note also that redefining O,H,L,C arrays affects many built-in indicators that are using these arrays indirectly so to be 100% sure the following code is needed:
/* save predefined arrays */
sclose = close;
shigh = high;
slow = low:
sopen = open;
/* redefine arrays */
close = 2 * close;
high = 2 * high;
low = 2 * low;
open = 2 * open;
/* plot graph0 with a new values of predefined arrays */
graph0 = close;
graph0style = 64; /* candlesticks - indirectly use O,H,L */
/* restore arrays */
close = sclose;
high = shigh;
low = slow:
open = sopen;
/* now you can continue with calculations and use built in functions since built in arrays are restored */
As you already noticed you can combine styles by adding the values of styleflags: 128+4 will result in thick traditional bar chart, while 1+8 will give you dotted line chart. Note that style=256 does not draw anything - instead AmiBroker takes graph formula, calculates it and performs chart scaling (in automatic scale mode). This is useful if you want to rescale the chart according to some array values that should not be plotted at all.
However, not all flag combinations make sense, for example (64+1) (candlestick + line) will result in candlestick chart (style=64).
.... and that's all for this week - hope you enjoyed reading
--------------------------------------------------------------------------------
AmiBroker Tips weekly newsletter. Issue 2/2000. Copyright (C)2000 Tomasz Janeczko. All back issues available from: http://www.amibroker.com/newsletter
------=_NextPart_001_000F_01C04CBD.85207280
Content-Type: text/html;
charset="iso-8859-2"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=iso-8859-2" http-equiv=Content-Type>
<META content="MSHTML 5.00.2614.3500" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV>
<TABLE border=0 cellPadding=0 cellSpacing=0 width="100%">
<TBODY>
<TR>
<TD>
<DIV align=center><B><IMG alt="" border=0 hspace=0
src="cid:000d01c04cb5$233a78c0$0100007f@xxxx"><BR>Issue 2/2000</B></DIV></TD>
<TD width="15%"><FONT size=-2>AmiBroker Tips weekly newsletter.<BR>Issue
2/2000.<BR>Copyright (C)2000 Tomasz Janeczko. <BR>All back
issues available from:<BR><A
href="http://www.amibroker.com/newsletter">http://www.amibroker.com/newsletter</A></FONT></TD></TR></TBODY></TABLE>
<H5>IN THIS ISSUE</H5>
<H5>1 Welcome<BR>2 AFL Formula Library: DeMarker and Range Expansion Index<BR>3
Tip of the week: Using graph styles, colors and titles in Indicator Builder</H5>
<H5>1 Welcome</H5>
<P>Welcom to the second issue of AmiBroker Tips newsletter. With this release a
new regular column will start: "AFL Formula Library". In this column I will
present new AFL formulas for indicators, trading systems and commentaries. All
formulas presented in this column will also appear in <A
href="http://www.amibroker.com/library.html">http://www.amibroker.com/library.html</A>
<BR></P>
<P>By the way: Do you find this newsletter useful? Have any comments/suggestions
or article ideas. Please don't hesitate to drop a line to <A
href="mailto:newsletter@xxxx">newsletter@xxxx</A></P>
<H5>2 AFL Formula Library: DeMarker and Range Expansion Index</H5>
<P><I>DeMarker</I></P>
<P>The DeMarker indicator is an attempt to overcome the shortcomings of
classical overbought / oversold indicators. The DeMarker Indicator identifies
potential price bottoms and tops. It accomplishes this by making price
comparisons from one bar to the next and measuring the level of price demand.
The formula for DeMarker is quite simple - in AFL it looks like this:</P>
<BLOCKQUOTE>
<P><CODE>/*<BR>** Tom Demark's DeMarker Indicator<BR>** AFL Implementation by
Tomasz Janeczko<BR>*/</CODE></P>
<P><CODE>highm = IIF( H > Ref( H, -1 ), H - Ref( H, - 1), 0 );<BR>lowm =
IIF( L < Ref( L, -1 ), Ref( L, - 1 ) - L, 0 );</CODE></P>
<P><CODE>DeMarker = 100 * Sum( highm, 13 )/( Sum( lowm, 13 ) + Sum( highm, 13
) );</CODE></P>
<P><CODE>graph0 = DeMarker;</CODE></P></BLOCKQUOTE>
<P></P>
<P>DeMarker should be interpreted as other overbought / oversold indicatorssuch
as RSI with the levels of 30 and 70. Compared to RSI it is smoother but still
able to detect tops and bottoms a little bit better.</P>
<P><I>Range Expansion Index</I></P>
<P>The DeMark Range Expansion Index is a market-timing oscillator describedin
DeMark on Day Trading Options, by T.R. DeMark and T.R. Demark, Jr., McGraw Hill,
1999. The oscillator is arithmetically calculated and is designed to overcome
problems with exponentially calculated oscillators, like MACD. The TD REI
oscillator typically produces values of -100 to +100 with 45 or higher
indicating overbought conditions and -45 or lower indicating oversold. Hereis
how Tom DeMark describes the calculation of Range Expansion Index:</P>
<P>"The first step in calculating the REI is to add together the respective
differences between the current day's high and the high two days earlier and the
current day's low and the low two days earlier. These values will be positive or
negative depending on whether the current day's high and low are greater orless
than the high and low two days earlier. To prevent buying or selling prematurely
into a steep price decline or advance, two additional conditions should be met
to qualify a positive or negative value on a particular day: 1) either the high
two days earlier must be greater than or equal to the close seven or eight days
ago, or the current day's high must be greater than or equal to the low five or
six days ago; 2) either the low two days earlier must be less than or equalto
the close seven or eight days ago, or the current day's low must be less than or
equal to the high five or six days ago. If either of these conditions are not
satisfied, a zero value is assigned to that day. If they both are, the daily
values (the differences between the highs and lows) are summed , and the
specific value for that next day is determined. Next, all the positives and
negative values are added together over a five-day period. This value is then
divided by the absolute value price movement of each day over the five-day
period. The numerator of the calculation can be either positive, negative or
zero, because each day's value is summed for five days, but the denominatoris
always positive because it is only concerned with the differential price
movement itself. This value is then multiplied by 100. Consequently, the REI can
fluctuate between +100 and -100."</P>
<P>Following this description I wrote the AFL formula for REI:</P>
<BLOCKQUOTE>
<P><CODE>/* <BR>** Tom DeMark's Range Expansion Index <BR>** AFL
Implementation by Tomasz Janeczko <BR>*/</CODE></P>
<P><CODE><BR>HighMom = H - Ref( H, -2 );<BR>LowMom = L - Ref( L, -2
);</CODE></P>
<P><CODE>Cond1 = ( H >= Ref( L,-5) OR H >= Ref( L, -6 ) ); <BR>Cond2 = (
Ref( H, -2 ) >= Ref( C, -7 ) OR Ref( H, -2 ) >= Ref( C, -8 ) );
<BR>Cond3 = ( L <= Ref( H, -5 ) OR L <= Ref( H, -6) ); <BR>Cond4 = (
Ref( L, -2 ) <= Ref( C, -7 ) OR Ref( L, -2 ) <= Ref( C, -8 )
);</CODE></P>
<P><CODE>Cond = ( Cond1 OR Cond2 ) AND ( Cond3 OR Cond4 );</CODE></P>
<P><CODE>Num = IIf( Cond, HighMom + LowMom, 0 );<BR>Den = Abs( HighMom ) +
Abs( LowMom );</CODE></P>
<P><CODE>TDREI = 100 * Sum( Num, 5 )/Sum( Den, 5 ) ;</CODE></P>
<P><CODE>graph0 = TDREI;</CODE></P></BLOCKQUOTE>
<P>DeMark advises against trading in extreme overbought or oversold conditions
indicated by six or more bars above or below the 45 thresholds. For more
information on calculation and using both TD REI and DeMarker indicator check <A
href="http://www.cwigroup.com/cqg.htm">this Tom DeMark's article</A>.</P>
<P><B>3 Tip of the week: Using graph styles, colors and titles in Indicator
Builder</B></P>
<P><I><FONT size=-2>Note: Some of the functionality described here is available
only in AmiBroker version 3.4 (definable styles and colors)</FONT></I></P>
<P>AmiBroker 3.4 introduced customizable styles and colors of graphs in custom
indicators. These features allow more flexibility in designing your indicators.
This article will explain how to use styles and colors. It will also explain how
to define chart title that appears at the top of the chart.</P>
<P>Let's begin with a list of special variable names available in Indicator
Builder:</P>
<TABLE border=1 borderColorDark=#990000 borderColorLight=#990000 cellPadding=3
cellSpacing=0 width="100%">
<TBODY>
<TR bgColor=#990000>
<TD vAlign=top><FONT color=#ffffff>Variable </FONT></TD>
<TD vAlign=top><FONT color=#ffffff>Usage</FONT></TD>
<TD vAlign=top><FONT color=#ffffff>Applies to</FONT></TD></TR>
<TR>
<TD vAlign=top>title</TD>
<TD vAlign=top>defines title text </TD>
<TD vAlign=top>Indicator Builder</TD></TR>
<TR>
<TD vAlign=top>maxgraph</TD>
<TD vAlign=top>specifies maximum number of graphs to be drawn in custom
indicator window (default=3)</TD>
<TD vAlign=top>Indicator Builder</TD></TR>
<TR>
<TD vAlign=top>graph<I>N</I></TD>
<TD vAlign=top>defines the formula for the graph number <I>N</I> (where
<I>N</I> is a number 0,1,2,..., maxgraph-1)</TD>
<TD vAlign=top>Indicator Builder</TD></TR>
<TR>
<TD vAlign=top>graph<I>N</I>color</TD>
<TD vAlign=top>defines the color index of <I>N</I>th graph line (color
indexes are related to the current palette - see Preferences/Color)</TD>
<TD vAlign=top>Indicator Builder</TD></TR>
<TR>
<TD vAlign=top>graph<I>N</I>style</TD>
<TD vAlign=top>
<P>defines the style of <I>N</I>th graph. Style is defined as a
combination (sum) of one or more following flags:</P>
<P>1 - normal (line) chart (default)<BR>2 - histogram chart<BR>4 - fat
(thick)<BR>8 - include dots<BR>16 - no line<BR>32 - semi-logarithmic
scale<BR>64 - candlestick chart<BR>128 - traditional bar chart<BR>256- no
draw (perform axis scaling only)</P>
<P>Not all flag combinations make sense, for example (64+1) (candlestick +
line) will result in candlestick chart (style=64)<BR><BR>Note on
candlestick/bar charts: these styles use indirectly O, H, L arrays in
addition to graph<I>N</I>. So ordinary candlestick price chart formula is
graph0=close; graph0style=64;.<BR>But if you want to draw something else
than close price you have to assign new values to predefined O,H,L
arrays.</P></TD>
<TD vAlign=top>Indicator Builder</TD></TR></TBODY></TABLE>
<P>New graph<I>N</I>style and graph<I>N</I>color variables (where <I>N</I> is
0,1,2...) allow you to change the default plain line style. Let me show yousome
examples, here is how ADX/DMI chart defined in AFL would look like:</P>
<BLOCKQUOTE>
<P><CODE>maxgraph = 3;<BR>graph2 = pdi(14);<BR>graph1 = mdi(14);<BR>graph0 =
adx(14);<BR>graph2style = graph1style = 1;<BR>graph0style = 5;<BR>graph1color
= 5;<BR>graph2color = 6;<BR>graph0color = 7;<BR>title=name() + " - +DM" +
writeval( graph2 )+ ", -DM" + writeval( graph1 )+<BR>", ADX" + writeval(
graph0 );</CODE></P></BLOCKQUOTE>
<P>The first line defines that we will use 3 graphs (this is the default soit
is not really needed). Next three lines define the formulas for +DM, -DM and ADX
graphs. Then we define that graph2 and graph1 (+DM and -DM) will have plainline
style and ADX line (graph0) will have fat line style (1+4). Then we define the
colors according to palette settings. AmiBroker uses a pallette that can be
changed by user. Go to <I>Tools->Preferences->Colors</I>, click <I>Palette
Editor</I> and take a look at "Custom colors" - these 16 color boxes display
AmiBrokers pallette - the first box is represent color index 0, next one - 1 and
so on. So if you write graph0color = 7 you tell AmiBroker to take the color with
index 7 (this is the color with which Pallette editor's box number eight is
painted). And now we are at the last line which defines the formula for the
chart title. Instead of plain default <TICKERNAME> -
<CustomIndicatorName> we can show something more informative. In this
example we concatenate a string which contains ticker name and the values of
+DM, -DM and ADX. If you assign any string to the title variable it will be
displayed instead of the default one. </P>
<P>Next example is a Parabolic SAR chart drawn over traditional bar chart:</P>
<BLOCKQUOTE>
<P><CODE>graph0 = close;<BR>graph0style = 128;<BR>graph0color = 2;<BR>graph1 =
sar();<BR>graph1style = 8+16;<BR>graph1color = 8;<BR>title = "This is my
title: " + name() + " - SAR - " + writeval( graph1 );</CODE></P></BLOCKQUOTE>
<P>In this example we plot SAR chart with only dots (graph1style = 8 (dots) + 16
(no line)). A price chart (graph0) is drawn with traditional bar chart style
(graph0style=128). In order to do that we have to assign a close price tograph0
variable. Note that for candlesticks and traditional bar charts AmiBroker uses
indirectly O,H, L arrays, since more data is needed than one array. So if you
want to plot something different than close price you have to assign new values
also to O,H,L predefined arrays. Please note also that redefining O,H,L,C arrays
affects many built-in indicators that are using these arrays indirectly so to be
100% sure the following code is needed:</P>
<BLOCKQUOTE>
<P><CODE>/* save predefined arrays */<BR>sclose = close;<BR>shigh =
high;<BR>slow = low:<BR>sopen = open;<BR><BR>/* redefine arrays */<BR>close =
2 * close;<BR>high = 2 * high;<BR>low = 2 * low;<BR>open = 2 * open;<BR><BR>/*
plot graph0 with a new values of predefined arrays */<BR>graph0 =
close;<BR>graph0style = 64; /* candlesticks - indirectly use O,H,L
*/<BR><BR>/* restore arrays */<BR>close = sclose;<BR>high = shigh;<BR>low =
slow:<BR>open = sopen;<BR><BR>/* now you can continue with calculationsand
use built in functions since built in arrays are restored
*/</CODE></P></BLOCKQUOTE>
<P>As you already noticed you can combine styles by adding the values of style
flags: 128+4 will result in thick traditional bar chart, while 1+8 will give you
dotted line chart. Note that style=256 does not draw anything - instead
AmiBroker takes graph formula, calculates it and performs chart scaling (in
automatic scale mode). This is useful if you want to rescale the chart according
to some array values that should not be plotted at all.</P>
<P>However, not all flag combinations make sense, for example (64+1)
(candlestick + line) will result in candlestick chart (style=64).</P><I>.... and
that's all for this week - hope you enjoyed reading</I>
<HR>
<P><FONT size=-2>AmiBroker Tips weekly newsletter. Issue 2/2000.
Copyright (C)2000 Tomasz Janeczko. All back issues available
from: <A
href="http://www.amibroker.com/newsletter">http://www.amibroker.com/newsletter</A></FONT></P>
<P> </P></DIV></BODY></HTML>
------=_NextPart_001_000F_01C04CBD.85207280--
Attachment:
Description: ""
|