Category Archives: back testing

A Candlestick Strategy With Soldiers And Crows

ndle reversal patterns—a bullish one white soldier and a bearish one black crow—that requ

The Expert Design Studio code for Jerry D’Ambrosio and Barbara Star’s article, “A Candlestick Strategy With Soldiers And Crows,” in Stocks & Commodities October 2018 issue is shown below.”Among the more well-known candlestick reversal patterns are soldiers and crows. These occur in a three-candle pattern such as three white soldiers or three black crows. Recently, on the website Candlesticker.com, we learned of two other candle reversal patterns—a bullish one white soldier and a bearish one black crow—that require fewer candles. ”

!A CANDLESTICK STRATEGY WITH SOLDIERS AND CROWS
!Author: Jerry D'Ambrosio & Barbara Star, TASC Oct 2017
!Coded by: Richard Denning 8/05/2017
!www.TradersEdgeSystems.com

!CODING ABBREVIATIONS:
O is [open].
O1 is valresult(O,1).
C is [close].
C1 is valresult(C,1).
C2 is valresult(C,2).
H is [high].
L is [low].
V is [volume].

!INPUTS:
minPriceBull is 1.
minPriceBear is 10.
minVolume is 1000. !in hundreds
volAvgLen is 50.
dayCount is 5.
longExitBars is 7.
shortExitBars is 1.

okToBuy if simpleavg(C,50) > simpleavg(C,200) or CminPriceBull and simpleavg(V,volAvgLen)>minVolume.
BullWS if C1C1 and C>O1 and O= longExitBars.

okToSell if simpleavg(C,50) < simpleavg(C,200) or C>simpleavg(C,200)*1.1.
okToSellMkt if TickerRule("SPX",okToSell).
PVfilterBear if C>minPriceBear and simpleavg(V,volAvgLen).
BearBC if C1>C2 and C1>O1 
     and OO1 
     and countof(C1>C2,dayCount)=dayCount
     and PVfilterBear and okToSellMkt.
ExitShort if {position days} >= shortExitBars.
I ran several backtests using the NASDAQ 100 list of stocks over the period from 8/04/2000 to 8/04/2017. I varied the following inputs to find the optimum set of parameters for the candlestick patterns. For longs, the “dayCount” = 5 with an “longExitBars” = 7 produced the best results, which is shown in Figure 5. For shorts, the “dayCount” = 5 with a “shortExitBars” = 1 produced the best results, which is shown in Figure 6. Neither commission nor slippage were subtracted from the results.

Sample Chart

FIGURE 5: WINWAY. EDS summary report for longs only.

Sample Chart

FIGURE 6: WINWAY. EDS summary report for shorts only.
—Richard Denning
info@TradersEdgeSystems.com
for TradingExpert Pro

ire fewer candles. “

System Development Using Artificial Intelligence

The AIQ code based on Domenico D’Errico and Giovanni Trombetta’s article in August 2017 Stock & Commodities issue, “System Development Using Artificial Intelligence,” is shown here. You can also download the EDS file from here

Are humans or computers better at trading? This question has been around on many fronts since the era of punch cards, and as technology advances, you question whether machines have limits. It’s the same with trading, and here’s an algorithm that may shed some light on which performs better…

!ARTIFICAL INTELLIGENCE FOR SYSTEM DEVELOPMENT
!Authors: Domenico D'Errico & Giovanni Trombetta, TASC August 2017
!Coded by: Richard Denning, 6/08/2017
!www.TradersEdgeSystems.com

!INPUTS:
O is [open].
C is [close].
H is [high].
L is [low].
exitBars is 8.
exitBarsP is 6.
enterGap is -0.08.

!CODE:
AvgP is (O+C+H+L)/4.
MedP is (H+L)/2.
MedB is (O+C)/2.

AvgP1 is valresult(AvgP,1).
AvgP2 is valresult(AvgP,2).
AvgP3 is valresult(AvgP,3).

MedP1 is valresult(MedP,1).
MedP2 is valresult(MedP,2).
MedP3 is valresult(MedP,3).
MedP4 is valresult(MedP,4).

MedB1 is valresult(MedB,1).
MedB2 is valresult(MedB,2).
MedB3 is valresult(MedB,3).
MedB4 is valresult(MedB,4).

!ENTRY & EXIT RULESl
Gandalf if 
  (AvgP1exitBars-1)
 or ({position days}>=exitBars-1)
 or ({position days}>=exitBarsP-1 and (C-{position entry price}>0)).

EntryPr is min(val([low],1) + enterGap,[open]).

Buy if Gandalf and [low] <= EntryPr.

See Figure 10 for how to set up the pricing in a backtest.
Sample Chart

FIGURE 10: AIQ. This shows the EDS backtest settings for entry pricing.
—Richard Denning
info@TradersEdgeSystems.com
for AIQ Systems

Detecting Swings

The AIQ code based on Domenico D’Errico’s article in the May 2017 issue of Stocks & Commodities issue, “Detecting Swings,” is provided here.

I tested the author’s four systems using the NASDAQ 100 list of stocks on weekly bars, as did the author, from 3/16/2005 through 3/14/2017. Figure 7 shows the comparative metrics of the four systems using the four-week exit. The results were quite different than the author’s, probably due to a different test portfolio and also a 10-year test period rather than the author’s 20-year period. In addition, my test results show longs only, whereas the author’s results are the average of both the longs and shorts.

Sample Chart

FIGURE 7: AIQ. As coded in EDS, this shows the metrics for the author’s four systems run on NASDAQ 100 stocks (weekly bar data) over the period 3/16/2005 to 3/14/2007.

The Bollinger Band (Buy2) system showed the worst results, whereas the author’s results showed the Bollinger Band system as the best. The pivot system (Buy1) showed the best results, whereas the author’s results showed the pivot system as the worst. I am not showing here the comparative test results for the Sell1 thru Sell4 rules, as all showed an average loss over this test period.

!DECTECTING SWINGS
!Author: Domenico D'Errico, TASC May 2017
!Coded by: Richard Denning, 3/15/17
!www.TradersEdgeSystems.com

!Set to WEEKLY in properties

Low is  [low].
Low1  is valresult(Low,1).
Low2  is valresult(Low,2). 
High is [high].
High1  is valresult(High,1).
High2  is valresult(High,2). 
PivotLow if Low1 &lt; Low2  and Low1 &lt; Low.
PivotHigh if High1 &gt; High2  and High1 &gt; High.

Buy1 if  PivotLow.  
Sell1 if  PivotHigh.    

!Set parameter for bollinger bands to 12 with 2 sigma (weekly) in charts:
Buy2 if [close] &gt; [Lower BB] and valrule([close] &lt;= [Lower BB],1).
Sell2 if [close] &lt; [Upper BB] and valrule([close] &gt;= [Upper BB],1).

!Set parameter for Wilder RSI to 5 (weekly) in charts:
Buy3 if [RSI Wilder] &gt; 40 and valrule([RSI Wilder] &lt;= 40,1).
Sell3 if [RSI Wilder] &lt; 60 and valrule([RSI Wilder] &gt;= 60,1).

Buy4 if [RSI Wilder] &lt; 40  And Low &gt; Low1.
Sell4 if [RSI Wilder] &gt; 60  And High &lt; High1.    

Exit if {position days} &gt;= 4.

The code and EDS file can be downloaded from http://aiqsystems.com/detectingswings.EDS

—Richard Denning

info@TradersEdgeSystems.com

for AIQ Systems

One Good Reason NOT to Pick a Bottom in DIS

A better title for this article might be “How to Avoid Losing 98% in Disney.”
The recent dip in the price of Disney stock may ultimately prove to be a buying opportunity.  But for reasons detailed below I am going to let this one pass.
If you have read my stuff in the past you know that I look a lot at seasonal trends.  This is especially true for sectors and commodities – which in some cases can be tied to recurring fundamental factors.  I have occasionally looked at individual stocks (here and here and here), but tend to think that an individual company’s fundamentals can change so drastically over time that a persistent seasonal trend is less likely.
It appears that there are exceptions to every rule.
In Figure 1 below we see that after a strong run up from its 2009 low, Disney finally topped out in August of 2015. Since that time it’s been a string of large moves up and down – with the latest being down. This might prompt one to consider the latest dip as a buying opportunity.  And in fact, maybe it is. But I won’t be making that play myself based simply on a seasonal trend in DIS stock that was highlighted by Brooke Thackray in his book Thackray’s 2017 Investor’s Guide.
0Figure 1 – Is latest dip in DIS a buying opportunity?  Maybe, but history suggests we look elsewhere….(Courtesy AIQ TradingExpert)
When NOT to Own Disney Stock
In his book, Thackray highlights the period from June 5th through the end of September as an “unfavorable” period for DIS stock.  He also listed a specific “favorable” period that I’ll not mention here.  For purposes of this article I made the following changes:
*The “unfavorable” period begins at the close on the 5th trading day of June and ends at the close on the last trading day of September.
*The rest of the year – i.e., end of September until the close on the 5th trading day of June – is considered the “favorable” period.
Also, the test uses price data only.  No dividends are included nor is any interest assumed to be earned while out of DIS stock.
The results are fairly striking.  From the end of 1971 through the end of 2016:
*$1,000 invested in DIS on a buy-and-hold basis grew +8,042% to $81,422 (average annual +/- = +15.8%)
*$1,000 invested in DIS only during the “favorable” period grew +430,874% to $4,309,735 (average annual +/- = +25.0%)
*$1,000 invested in DIS only during the “unfavorable” period declined -98% to $18.89 (average annual +/- = (-6.9%))
It’s sort of hard to ignore the difference between +430,784% and -98%.
Figure 1 displays the cumulative performance during the unfavorable period from 1971 through 2016.
1
Figure 1 – Growth of $1,000 invested in DIS only from close of June Trading Day #5 through the end of September (1971-2016)
Figure 2 displays the growth of $1,000 during the favorable period (blue line) versus a buy-and-hold approach (red line).
2
Figure 2 – Growth of $1,000 invested in DIS only from the end of September through June Trading Day #5 (blue) versus Buy-and-Hold (red); 1971-2016
*The favorable period showed a net gain in 39 out of 45 years (87%)
*The unfavorable period showed a net gain in only 13 out of 45 years (29%)
*Buy-and-hold showed a net gain in 28 out of 45 years (62%)
Figure 3 displays year-by-year results.
Year Favorable Unfavorable Buy/Hold
1972 78.1 (3.5) 71.9
1973 (53.5) (12.0) (59.1)
1974 4.4 (56.4) (54.4)
1975 175.6 (12.4) 141.5
1976 5.2 (6.9) (2.0)
1977 (28.2) 19.1 (14.4)
1978 4.6 (3.3) 1.2
1979 1.2 10.5 11.9
1980 20.8 (5.8) 13.8
1981 42.8 (28.7) 1.9
1982 16.0 4.4 21.1
1983 (5.7) (11.6) (16.7)
1984 25.6 (9.6) 13.6
1985 94.4 (3.3) 88.0
1986 98.3 (23.0) 52.8
1987 14.6 20.1 37.6
1988 4.2 6.5 10.9
1989 32.7 28.3 70.3
1990 28.4 (29.4) (9.3)
1991 14.4 (1.5) 12.8
1992 51.3 (0.7) 50.2
1993 16.0 (14.5) (0.8)
1994 23.2 (12.4) 7.9
1995 27.7 0.3 28.0
1996 18.4 0.0 18.4
1997 43.3 (0.9) 41.9
1998 36.9 (33.6) (9.1)
1999 15.8 (15.8) (2.5)
2000 4.0 (4.8) (1.1)
2001 23.6 (42.1) (28.4)
2002 12.6 (30.1) (21.3)
2003 50.9 (5.2) 43.0
2004 28.9 (7.6) 19.2
2005 (2.5) (11.6) (13.8)
2006 41.8 0.8 43.0
2007 (6.2) 0.4 (5.8)
2008 (24.4) (7.0) (29.7)
2009 29.1 10.1 42.1
2010 16.1 0.2 16.3
2011 30.4 (23.4) (0.0)
2012 15.9 14.6 32.8
2013 54.3 (0.6) 53.4
2014 17.2 5.2 23.3
2015 20.4 (7.3) 11.6
2016 5.0 (5.6) (0.8)
2017 ? ? ?
# Years UP 39 13 28
# Years DOWN 6 32 17
Average % +/- 25.0 (6.9) 15.8
Figure 3 – Year-by-Year Results
Summary
Brooke Thackray found an extremely interesting and robust “unfavorable” seasonal trend in DIS stock.  Of course none of the data above guarantees that DIS stock is doomed to languish and/or decline in the months ahead.  But I for one do not intend to “buck the odds” and play the long side of DIS for a while.
Jay Kaeppel  Chief Market Analyst at JayOnTheMarkets.com and AIQ TradingExpert Pro http://www.aiqsystems.com) client. 
Disclaimer:  The data presented herein were obtained from various third-party sources.  While I believe the data to be reliable, no representation is made as to, and no responsibility, warranty or liability is accepted for the accuracy or completeness of such information.  The information, opinions and ideas expressed herein are for informational and educational purposes only and do not constitute and should not be construed as investment advice, an advertisement or offering of investment advisory services, or an offer to sell or a solicitation to buy any security.

Detecting Swings

The AIQ code based on Domenico D’Errico’s article in the May 2017 issue of Stoks Commodities, “Detecting Swings,” is provided below.

I tested the author’s four systems using the NASDAQ 100 list of stocks on weekly bars, as did the author, from 3/16/2005 through 3/14/2017. Figure 7 shows the comparative metrics of the four systems using the four-week exit. The results were quite different than the author’s, probably due to a different test portfolio and also a 10-year test period rather than the author’s 20-year period. In addition, my test results show longs only, whereas the author’s results are the average of both the longs and shorts.

Sample Chart
 
FIGURE 7: AIQ. As coded in EDS, this shows the metrics for the author’s four systems run on NASDAQ 100 stocks (weekly bar data) over the period 3/16/2005 to 3/14/2007.

The Bollinger Band (Buy2) system showed the worst results, whereas the author’s results showed the Bollinger Band system as the best. The pivot system (Buy1) showed the best results, whereas the author’s results showed the pivot system as the worst. I am not showing here the comparative test results for the Sell1 thru Sell4 rules, as all showed an average loss over this test period.

!DECTECTING SWINGS
!Author: Domenico D'Errico, TASC May 2017
!Coded by: Richard Denning, 3/15/17
!www.TradersEdgeSystems.com

!Set to WEEKLY in properties

Low is  [low].
Low1  is valresult(Low,1).
Low2  is valresult(Low,2). 
High is [high].
High1  is valresult(High,1).
High2  is valresult(High,2). 
PivotLow if Low1 < Low2  and Low1 < Low.
PivotHigh if High1 > High2  and High1 > High.

Buy1 if  PivotLow.  
Sell1 if  PivotHigh.    

!Set parameter for bollinger bands to 12 with 2 sigma (weekly) in charts:
Buy2 if [close] > [Lower BB] and valrule([close] <= [Lower BB],1).
Sell2 if [close] < [Upper BB] and valrule([close] >= [Upper BB],1).

!Set parameter for Wilder RSI to 5 (weekly) in charts:
Buy3 if [RSI Wilder] > 40 and valrule([RSI Wilder] <= 40,1).
Sell3 if [RSI Wilder] < 60 and valrule([RSI Wilder] >= 60,1).

Buy4 if [RSI Wilder] < 40  And Low > Low1.
Sell4 if [RSI Wilder] > 60  And High < High1.    

Exit if {position days} >= 4.
—Richard Denning
info@TradersEdgeSystems.com
for AIQ Systems
Editor note: The code and EDS file can be downloaded from http://aiqsystems.com/Detecting_Swings_TASC_May_2017.EDS