PureBytes Links
Trading Reference Links
|
I am having major problems understanding how I can be receiving certain
results from Amibroker, and am wondering if you could help me.
Attached is an AFL which is based on the Range Expansion Index formula that
appears in the AFL Formula Library. I have attached certain buy and short
conditions to create trading rules. I would like to close out any position
that does not hit either a Stop Loss or Profit Target in 30 days. In the
"Settings" section, I have set the Stop-Loss target at 4 points and the
Profit Target at 6 points.
However, when I backtest this system, I get some trades which are open for
far longer that the 30 days, and the stop-loss doesn't close the
trade. For example, I ran this system against the DJIA for 1998 and 1999,
and have attached the *.csv file which it produced. The second entry
listed is a short trade in Caterpillar on 4/17/98 which is not closed out
until 7/24/98. Unfortunately, a look at the chart for CAT shows that the
position clearly moved up by more than 4 points long before that July date.
I am sure I must be doing something wrong, and would appreciate any help
you could offer.
Sincerely,
David Beaudoin
/*
** Tom DeMark's Range Expansion Index
** AFL Implementation by Tomasz Janeczko
**Also uses TD Price Oscillator Qualifier
*/
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 ) ;
buycond1 = ref( TDREI,-1) <= -45;
buycond2 = barssince( cross( -45, TDREI )) <= 6;
buycond3 = ref( close,-1 ) > ref( close,-2 );
buycond4 = open <= ref( high,-1 );
buycond5 = high > ref( high,-1 );
buycond = buycond1 + buycond2 + buycond3 + buycond4 + buycond5;
buyprice = ref( high,-1 ) + 0.05;
sellcond1 = ref( TDREI,-1) >= 45;
sellcond2 = barssince( cross( TDREI,45 )) <= 6;
sellcond3 = ref( close,-1 ) < ref( close,-2 );
sellcond4 = open >= ref( low,-1 );
sellcond5 = low < ref( low,-1 );
sellcond = sellcond1 + sellcond2 + sellcond3 + sellcond4 + sellcond5;
shortprice = ref( low,-1 ) - 0.05;
buy = ((buycond == 5) AND (close >=50) AND (cross ( high,buyprice )));
sell = (barssince( buy )) == 30;
short = ((sellcond == 5) AND (close >=50) AND (cross ( shortprice,low )));
cover = (barssince( short )) == 30;
Formula used:,"/* ** Tom DeMark's Range Expansion Index ** AFL Implementation by Tomasz Janeczko **Also uses TD Price Oscillator Qualifier*/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 ) ORRef( 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 ) ;buycond1 = ref( TDREI,-1) <= -45;buycond2 = barssince( cross( -45, TDREI )) <= 6;buycond3 = ref( close,-1 ) > ref( close,-2 );buycond4 = open <= ref( high,-1 );buycond5 = high > ref( high,-1 );buycond = buycond1 + buycond2 + buycond3 + buycond4 + buycond5;buyprice = ref( high,-1 ) + 0.05;sellcond1 = ref( TDREI,-1) >= 45;sellcond2 = barssince( cross( TDREI,45 )) <= 6;sellcond3 = ref( close,-1 ) < ref( close,-2 );sellcond4 = open >= ref( low,-1 );sellcond5 = low < ref( low,-1 );sellcond = sellcond1 + sellcond2 + sellcond3 + sellcond4 + sellcond5;shortprice = ref( low,-1 ) - 0.05;buy = ((buycond == 5) AND (close >=50) AND (cross ( high,buyprice)));sell = barssince( buy ) == 30;short = ((sellcond == 5) AND(close >=50) AND (cross ( shortprice,low )));cover = barssince( short ) ==30;buy = ExRem( buy, sell );sell = ExRem( sell, buy );short = ExRem( short, cover );cover = ExRem( cover, short );"
Trade list:
Ticker,Trade type,Entry date,Entry price,Entry value,Exit date,Exit price,Exit value
CAT,short,3/3/98,53.08,10000,3/17/98,57.08,9299.23
CAT,short,4/17/98,55.95,9299.23,7/24/98,49.95,10416.3
CAT,long,4/26/99,60.99,10416.3,5/28/99,56.99,9733.11
CAT,short,8/18/99,61.64,9733.11,9/2/99,58.88,10189.3
CAT,long,9/2/99,58.74,10189.3,9/27/99,54.74,9495.49
DD,short,1/23/98,55.45,10000,2/6/98,59.45,9327.17
DD,long,4/22/98,75.61,9327.17,5/13/98,81.61,10067.3
DD,short,6/9/98,79.2,10067.3,6/15/98,73.2,10892.5
DD,long,6/17/98,73.61,10892.5,7/15/98,69.61,10300.6
DD,short,11/10/98,61.58,10300.6,12/3/98,55.58,11412.6
DD,long,4/26/99,67.24,11412.6,4/28/99,73.24,12431
DD,short,4/30/99,72.2,12431,5/27/99,66.2,13557.6
DD,short,8/4/99,71.58,13557.6,8/31/99,65.58,14798
DD,long,11/15/99,61.99,14798,12/17/99,67.99,16230.3
EK,long,1/20/98,59.8,10000,2/3/98,65.8,11003.3
EK,short,2/18/98,66.51,11003.3,4/1/98,65.88,11108.6
EK,short,4/17/98,70.33,11108.6,5/4/98,74.33,10510.8
EK,short,7/20/98,85.2,10510.8,7/30/98,85.5,10473.9
EK,long,7/30/98,84.05,10473.9,9/11/98,80.05,9975.43
EK,short,12/23/98,72.95,9975.43,1/13/99,76.95,9456.89
EK,long,1/25/99,66.3,9456.89,3/9/99,65.44,9334.22
EK,short,5/12/99,77.95,9334.22,5/20/99,75.56,9629.46
EK,long,5/20/99,75.49,9629.46,5/26/99,71.49,9119.23
EK,long,7/28/99,70.36,9119.23,9/9/99,73.81,9566.37
EK,short,9/20/99,74.95,9566.37,10/21/99,68.95,10398.8
EK,long,11/26/99,64.8,10398.8,12/21/99,60.8,9756.93
GM,long,4/30/98,56.41,10000,5/22/98,62.41,11063.6
GM,long,6/17/98,57.33,11063.6,7/30/98,60.49,11673.5
GM,short,7/31/98,60.19,11673.5,8/18/98,56.82,12365.8
GM,long,8/18/98,56.3,12365.8,8/27/98,52.3,11487.3
GM,short,1/29/99,75.57,11487.3,2/12/99,69.57,12478
GM,long,3/2/99,69.27,12478,5/3/99,75.27,13558.8
GM,short,5/5/99,72.73,13558.8,5/17/99,66.73,14777.9
GM,long,7/27/99,66.18,14777.9,8/6/99,62.18,13884.7
GM,short,10/12/99,66.7,13884.7,11/15/99,70.7,13099.2
HD,short,11/22/99,54.27,10000,12/10/99,58.27,9313.54
HON,short,5/3/99,58.45,10000,5/10/99,62.45,9359.49
HON,long,8/12/99,62.8,9359.49,9/24/99,57.63,8588.97
IBM,short,2/17/98,51.14,10000,3/31/98,51.94,9845.98
IBM,short,5/7/98,58.58,9845.98,7/21/98,62.58,9216.64
IBM,long,9/4/98,61.4,9216.64,10/15/98,67.4,10117.3
IBM,long,12/7/98,82.74,10117.3,12/22/98,88.74,10851
IBM,long,1/29/99,90.7,10851,2/5/99,86.7,10372.4
IBM,short,2/24/99,87.83,10372.4,4/8/99,93.5,9743.41
IBM,long,4/21/99,84.93,9743.41,4/22/99,90.93,10431.7
IBM,short,5/5/99,105.64,10431.7,5/13/99,109.64,10051.2
IBM,short,7/7/99,130.76,10051.2,7/12/99,134.76,9752.82
IBM,short,11/2/99,96.32,9752.82,11/5/99,90.32,10400.7
IP,short,5/10/99,56.33,10000,5/28/99,50.33,11192.1
IP,long,8/3/99,52.24,11192.1,9/15/99,51.19,10967.2
JNJ,short,4/7/98,75.64,10000,4/27/98,69.64,10861.6
JNJ,long,8/6/98,75.24,10861.6,10/15/98,82.63,11928.4
JNJ,short,10/20/98,82.39,11928.4,11/3/98,82.19,11957.4
JNJ,long,11/3/98,82.8,11957.4,11/20/98,88.8,12823.9
JNJ,short,12/18/98,79.89,12823.9,1/29/99,83.89,12212.4
JNJ,short,2/24/99,86.58,12212.4,3/2/99,85.06,12430.7
JNJ,long,3/2/99,85.55,12430.7,3/22/99,91.55,13302.5
JNJ,short,4/26/99,101.2,13302.5,5/4/99,95.2,14140.9
JNJ,long,6/16/99,91.68,14140.9,6/30/99,97.68,15066.3
JNJ,long,9/20/99,96.74,15066.3,9/24/99,92.74,14443.4
JPM,short,2/2/99,50.64,10000,2/11/99,50.78,9972.43
JPM,long,2/11/99,48.91,9972.43,3/4/99,54.91,11195.8
JPM,long,4/22/99,56.58,11195.8,5/24/99,52.58,10404.3
JPM,short,7/6/99,56.64,10404.3,8/3/99,52.57,11209.8
JPM,long,8/3/99,52.41,11209.8,9/15/99,51.65,11047.2
KO,long,1/28/98,64.18,10000,2/9/98,67.38,10498.6
KO,short,2/9/98,67.14,10498.6,3/10/98,71.14,9908.29
KO,long,7/27/98,84.3,9908.29,8/11/98,80.3,9438.15
KO,short,10/12/98,62.7,9438.15,10/16/98,66.7,8872.14
KO,long,11/17/98,70.49,8872.14,12/11/98,66.49,8368.69
KO,long,1/19/99,65.05,8368.69,3/3/99,61.63,7928.7
KO,short,4/20/99,64.01,7928.7,5/5/99,68.01,7462.38
KO,long,6/30/99,61.24,7462.38,7/6/99,63.63,7753.61
KO,short,7/6/99,64.14,7753.61,8/17/99,60.13,8270.69
KO,short,10/21/99,52.51,8270.69,10/28/99,56.51,7685.26
MMM,short,2/5/98,86.83,10000,2/26/98,86.25,10067.2
MMM,long,2/26/98,85.68,10067.2,3/17/98,91.68,10772.2
MMM,short,5/6/98,95.64,10772.2,6/10/98,89.64,11493.3
MMM,long,7/7/98,82.68,11493.3,7/21/98,82,11398.7
MMM,short,7/21/98,82.95,11398.7,7/28/98,76.95,12287.5
MMM,short,9/10/98,72.33,12287.5,9/29/98,76.33,11643.6
MMM,long,10/7/98,74.8,11643.6,10/15/98,80.8,12577.6
MMM,short,11/9/98,80.33,12577.6,12/11/98,74.33,13592.9
MMM,short,12/31/98,73.14,13592.9,2/16/99,76.44,13006.1
MMM,long,3/4/99,75.05,13006.1,4/19/99,81.05,14045.8
MMM,short,6/9/99,88.76,14045.8,7/22/99,90.69,13746.9
MMM,long,9/2/99,95.11,13746.9,10/15/99,89.38,12918.7
MMM,long,12/13/99,94.43,12918.7,12/31/99,97.88,13390.7
MO,short,10/26/98,49.76,10000,11/13/98,53.76,9255.95
MO,long,12/14/98,53.05,9255.95,1/20/99,49.05,8558.05
MRK,long,2/9/98,57.86,10000,2/23/98,63.86,11037
MRK,short,3/19/98,65.33,11037,4/24/98,59.33,12153.1
MRK,short,5/22/98,59.14,12153.1,6/17/98,63.14,11383.2
MRK,short,7/20/98,68.89,11383.2,7/22/98,62.89,12469.2
MRK,long,7/27/98,63.24,12469.2,9/23/98,69.24,13652.3
MRK,long,12/16/98,73.05,13652.3,1/8/99,76.9,14371.8
MRK,short,1/8/99,75.7,14371.8,1/22/99,69.7,15609
MRK,long,2/11/99,73.86,15609,2/22/99,79.86,16877
MRK,long,4/22/99,76.55,16877,4/29/99,72.55,15995.1
MRK,short,6/8/99,69.14,15995.1,7/1/99,73.14,15120.3
MRK,long,10/1/99,65.74,15120.3,10/6/99,71.74,16500.3
MRK,long,11/17/99,76.05,16500.3,11/22/99,78.63,17060.1
MRK,short,11/22/99,78.01,17060.1,12/9/99,72.01,18481.6
MSFT,long,7/23/98,58.68,10000,8/5/98,54.68,9318.34
MSFT,short,9/25/98,54.45,9318.34,10/7/98,48.45,10472.3
MSFT,short,1/8/99,74.08,10472.3,1/20/99,78.08,9935.82
MSFT,long,5/20/99,79.93,9935.82,6/21/99,85.93,10681.7
MSFT,long,7/28/99,89.8,10681.7,8/10/99,85.8,10205.9
MSFT,short,8/27/99,93.64,10205.9,10/11/99,94.31,10133.4
MSFT,short,10/25/99,91.7,10133.4,11/17/99,85.7,10842.8
PG,long,3/31/98,85.43,10000,4/6/98,86.94,10176.8
PG,short,4/6/98,87.51,10176.8,4/28/98,81.51,10925.9
PG,long,5/28/98,83.99,10925.9,6/24/98,89.99,11706.4
PG,long,9/15/98,69.55,11706.4,10/7/98,75.55,12716.3
PG,long,12/2/98,89.18,12716.3,12/9/98,85.18,12145.9
PG,short,2/2/99,89.2,12145.9,2/9/99,86.25,12561.3
PG,long,2/9/99,86.36,12561.3,2/22/99,92.36,13434.1
PG,short,8/5/99,92.51,13434.1,8/23/99,96.51,12877.3
PG,long,9/17/99,100.93,12877.3,9/30/99,96.93,12366.9
PG,long,11/17/99,107.49,12366.9,12/3/99,113.49,13057.2
SBC,short,12/23/98,51.33,10000,1/7/99,55.33,9277.06
SBC,short,4/26/99,55.26,9277.06,5/20/99,52,9858.67
SBC,long,5/20/99,51.86,9858.67,6/30/99,57.86,10999.3
SBC,short,7/2/99,55.45,10999.3,8/20/99,51.88,11756.2
T,short,12/31/98,50.31,10000,1/7/99,54.31,9263.49
T,short,2/24/99,57.15,9263.49,3/3/99,55.61,9520.02
T,long,3/3/99,55.58,9520.02,4/15/99,55.95,9583.4
T,long,4/19/99,57.05,9583.4,5/3/99,53.05,8911.47
T,short,5/10/99,60.01,8911.47,6/4/99,54.01,9901.45
T,long,7/19/99,55.74,9901.45,8/4/99,51.74,9190.9
UTX,long,12/7/98,51.36,10000,12/11/98,51.85,10095.4
UTX,short,12/11/98,52.26,10095.4,1/8/99,56.26,9377.64
UTX,long,2/17/99,60.8,9377.64,2/24/99,62.06,9571.98
UTX,short,2/24/99,62.01,9571.98,3/19/99,66.01,8991.94
UTX,long,9/27/99,58.11,8991.94,10/15/99,54.11,8372.98
UTX,short,11/4/99,59.08,8372.98,11/9/99,54.88,9013.77
UTX,long,11/9/99,56.8,9013.77,12/22/99,57.5,9124.86
WMT,short,12/15/99,66.89,10000,12/22/99,65.69,10182.7
XOM,short,3/17/98,63.89,10000,3/24/98,67.89,9410.81
XOM,long,4/13/98,67.11,9410.81,4/21/98,73.11,10252.2
XOM,long,5/28/98,70.8,10252.2,7/10/98,71.69,10381.1
XOM,long,7/27/98,70.86,10381.1,8/6/98,66.86,9795.06
XOM,long,10/29/98,71.05,9795.06,11/5/98,72.88,10047.3
XOM,short,11/5/98,71.76,10047.3,12/18/98,75.94,9494.31
XOM,long,3/26/99,71.74,9494.31,4/14/99,74.19,9818.55
XOM,short,4/14/99,74.51,9818.55,4/19/99,78.51,9318.3
|