Category Archives: Stocks & Commodities traders tips

Weekly and Daily MACD

The AIQ code based on Vitali Apirine’s article in December 2017 issue of Stocks and Commodities magazine, “Weekly & Daily MACD,” is provided below.
The moving average convergence/divergence oscillator (MACD), developed by Gerald Appel, is one of the more popular technical analysis indicators. The MACD is typically used on a single timeframe, but what if we looked at two timeframes on one chart?

Traders can look for relative daily MACD line crossovers, weekly and daily centerline crossovers, and divergences to generate trading signals. 
Figure 5 shows the daily & weekly MACD indicator on a chart of Apple Inc. (AAPL) during 2016 and 2017, when there was a change from a downtrend to an uptrend.
Sample Chart

FIGURE 5: AIQ. Here is an example of the daily & weekly MACD on a chart of AAPL.
The code and EDS file can be downloaded from http://aiqsystems.com/dailyweeklyMACD.EDS, or copied here:
!WEEKLY & DAILY MACD
!Author: Vitali Apirine, TASC Dec 2017
!Coded by: Richard Denning 10/13/17
!www.TradersEdgeSystems.com

!INPUTS:
S is 12.
L is 25.

EMA1 is expavg([Close],S).
EMA2 is expavg([Close],L).
EMA3 is expavg([Close],S*5).
EMA4 is expavg([Close],L*5).
MACD is EMA1 - EMA2.
MACDW is EMA3 - EMA4.
rdMACD is MACD + MACDW.
—Richard Denning
info@TradersEdgeSystems.com
for AIQ Systems

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 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

Exponential Standard Deviation Bands

The AIQ code based on Vitali Apirine’s article in the 2017 issue of Stocks & Commodities magazine, “Exponential Standard Deviation Bands”

Editor note: “Author Vitali Apirine presented a method intended to help traders see volatility while a stock is trending. These bands, while similar to Bollinger Bands, are calculated using exponential moving averages rather than simple moving averages. Like Bollinger Bands, they widen when volatility increases and narrow as volatility decreases. He suggests that the indicator can be used as a confirming indication along with other indicators such as the ADX. Here’s an AIQ Chart with the Upper, Lower and Middle Exponential SD added as custom indicators.”

 

 

To compare the exponential bands to Bollinger Bands, I created a trend-following trading system that trades long only according to the following rules:
  1. Buy when there is an uptrend and the close crosses over the upper band. An uptrend is in place when the middle band is higher than it was one bar ago.
  2. Sell when the low is less than the lower band.
Figure 8 shows the summary test results for taking all signals from the Bollinger Band system run on NASDAQ 100 stocks over the period 12/9/2000 to 12/09/2016. Figure 9 shows the summary test results for taking all signals from the exponential band system on NASDAQ 100 stocks over the same period. The exponential band system improved the average profit per trade while reducing the total number of trades.

Sample Chart

FIGURE 8: AIQ. Here are summary test results for taking all signals from the Bollinger Band system run on NASDAQ 100 stocks over the period 12/9/2000 to 12/09/2016.

Sample Chart

FIGURE 9: AIQ. Here are summary test results for taking all signals from the exponential band system run on NASDAQ 100 stocks over the period 12/9/2000 to 12/09/2016.
The EDS file can be downloaded from http://aiqsystems.com/EDS/Exponential_Standard_Deviation_Bands.EDS 
and is also shown here:
!Exponential Standard Deviation Bands
!Author: Vitali Apirine, TASC February 2017
!Coded by: Richard Denning 12/11/2016
!www.TradersEdgeSystems.com!INPUT:
xlen is 20.
numSD is 2.

!INDICATOR CODE:
ExpAvg is expavg([close],xlen).
Dev is [close] – ExpAvg.
DevSqr is Dev*Dev.
SumSqr is sum(DevSqr,xlen).
AvgSumSqr is SumSqr / xlen.
ExpSD is sqrt(AvgSumSqr).

!UPPER EXPONENTIAL SD BAND:
UpExpSD is ExpAvg + numSD*ExpSD.  !PLOT ON CHART

!LOWER EXPONENTIAL SD BAND:
DnExpSD is ExpAvg – numSD*ExpSD.   !PLOT ON CHART

!MIDDLE EXPONENTIAL SD BAND:
MidExpSD is ExpAvg.

!BOLLINGER BANDS FOR COMPARISON:
DnBB is [Lower BB].  !Lower Bollinger Band
UpBB is [Upper BB].  !Upper Bollinger Band
MidBB is simpleavg([close],xlen). !Middle Bollinger Band
!REPORT RULE TO DISPLAY VALUES:
ShowValures if 1.

!TRADING SYSTEM USING EXPPONENTIAL SD BANDS:
UpTrend if MidExpSD > valresult(MidExpSD,1).
BreakUp if [close] > UpExpSD.
BuyExpSD if UpTrend and BreakUp and valrule(Breakup=0,1).
ExitExpSD if [Low] < DnExpSD.  ! or UpTrend=0.

!TRADING SYSTEM USING BOLLINGER BANDS:
UpTrendBB if MidBB > valresult(MidBB,1).
BreakUpBB if [close] > UpBB.
BuyBB if UpTrendBB and BreakUpBB and valrule(BreakupBB=0,1).
ExitBB if [Low] < DnBB.  ! or UpTrend=0.

—Richard Denning
info@TradersEdgeSystems.com
for AIQ Systems

Zero In On The MACD

The AIQ code based on Barbara Star’s article in May issue of Stocks and Commodities “Zero In On The MACD,” is provided at www.TradersEdgeSystems.com/traderstips.htm, and is also shown below.
Sample Chart

FIGURE 7: AIQ. Here is a sample chart of VIAB with MACDhist, the color bars, and the 34- and 55-bar EMAs.
Figure 7 shows the MACD histogram on a chart of Viacom (VIAB) with the color bars and the 34- and 55-bar exponential moving averages (EMA). Note that I did not code the weighted moving average (WMA) but substituted the EMA for the WMA. I chose to view the chart of VIAB by running the EDS “Zero MACD.eds” on 3/14/2016 and examining the alert messages on the report “List.” VIAB is the only one on that date that showed a cross up on the MACDhist (see Figure 8 for a look at part of this report for 3/14/2016).
Sample Chart

FIGURE 8: AIQ. This shows part of the EDS custom report “List” that shows the MACDhist values on 3/14/2016, the color status, and any alerts that were generated for that day.
! ZERO IN ON THE MACD

! Author: Barbara Star, TASC May 2016

! Coded by: Richard Denning 3/14/16

! www.TradersEdgeSystems.com



! INPUTS:

macd1  is  12.

macd2  is  26.

macdSig is  1.



! INDICATORS:

emaST  is expavg([Close],macd1).

emaLT  is expavg([Close],macd2).

MACD  is emaST - emaLT.  ! MACD line

SigMACD is expavg(MACD,macdSig). ! MACD Signal line

MACDosc is MACD - SigMACD. ! MACD Oscillator



HD if hasdatafor(macd2) = macd2.

MACDhist is MACD.                                 ! plot as historigram

MACDblue if MACDhist > 0.  ! use these rules to color MACDhist

MACDred  if MACDhist < 0.  ! use these rules to color MACDhist

MACDcolor is iff(MACDblue and HD,"Blue",iff(MACDred and HD,"Red","White")). !for report list

List if 1.



!ALERTS:

EMA1 is expavg([close],34).

EMA2 is expavg([close],55).

xupEMA1 if [close] > EMA1 and valrule([close] < EMA1,1).

xdnEMA1 if [close] < EMA1 and valrule([close] > EMA1,1).

xupEMA2 if [close] > EMA2 and valrule([close] < EMA2,1).

xdnEMA2 if [close] < EMA2 and valrule([close] > EMA2,1).

xupMACD if MACDhist > 0 and valrule(MACDhist < 0,1).

xdnMACD if MACDhist < 0 and valrule(MACDhist > 0,1).

UpAlerts is iff(xupEMA1,"xupEMA1",iff(xupEMA2,"xupEMA2",iff(xupMACD,"xupMACD"," "))).

DnAlerts is iff(xdnEMA1,"xdnEMA1",iff(xdnEMA2,"xdnEMA2",iff(xdnMACD,"xdnMACD"," "))).

The code and EDS file can be downloaded from www.TradersEdgeSystems.com/traderstips.htm.