The AIQ code for the monthly moving average and related system described in “Trading High-Yield Bonds Using ETFs” by Brooke Gardner in this issue is provided at the website noted below.

Although this is a simple moving average system, the use of the monthly
data series presented a challenge, since the EDS module of the AIQ
software does not provide access to a monthly bar. The charting module
does support the monthly chart, but in the EDS code, the monthly bar
cannot be accessed directly.

I tried two different approaches, and both seemed to work. The first
one involved creating a monthly data series by downloading a monthly
.csv file from Yahoo! Finance and then importing the data file into a
newly created ticker using the DTU import utility. This worked but
proved to be too much effort if I wanted to get several bond funds. In
addition, the update would have to be done manually. To use this data
file, we set the input “UseMoDataFile” to 1. Then I tried coding up a
monthly close using daily data files. This took a bit of code but any
daily data file will work, without modification, with this approach. To
use a daily data file, set the “UseMoDataFile” to zero.

The other input parameter allows us to find the end of the month when
using a daily data file. As one option, you could use the first day of
the new month as the signal day by setting “UseEndOfMonthC” to zero.
However, for backtesting and also to match the author’s approach, I set
the “UseEndOfMonthC” to “1” so that we get the signals from the last bar of the month. In the EDS file for the backtest, I then enter on the open of the first bar of the month.

Figure 8 shows a daily chart of VVR with an eight-month moving average.

This code and EDS file can be downloaded from (The code is also shown below.

!Author: Brooke Gardner
!Coded by: Richard Denning 04/17/12

UseMoDataFile is 0.
UseEndOfMonthC is 1.

C is [close].
O is [open].
C1 is valresult(C,1).
OSD is offsettodate(month(),day(),year()).
SMA8 is simpleavg(C,8).
HD if hasdatafor(21*8+5)>= 21*8.

Mo is month().
Mo1 is valresult(month(),1).
NewMo if Mo <> Mo1.
OneMoBackDate is scanany(NewMo,25,1). 
OneMoBackOS is scanany(NewMo,25,1) then OSD.
TwoMoBackOS is scanany(NewMo,25,^OneMoBackOS+1) then OSD.
ThreeMoBackOS is scanany(NewMo,25,^TwoMoBackOS+1) then OSD.
FourMoBackOS is scanany(NewMo,25,^ThreeMoBackOS+1) then OSD.
FiveMoBackOS is scanany(NewMo,25,^FourMoBackOS+1) then OSD.
SixMoBackOS is scanany(NewMo,25,^FiveMoBackOS+1) then OSD.
SevenMoBackOS is scanany(NewMo,25,^SixMoBackOS+1) then OSD.
EightMoBackOS is scanany(NewMo,25,^SevenMoBackOS+1) then OSD.

Cm is iff(UseEndOfMonthC = 1,C1,C).

MonC1 is valresult(Cm,^OneMoBackOS).
MonC2 is valresult(Cm,^TwoMoBackOS).
MonC3 is valresult(Cm,^ThreeMoBackOS).
MonC4 is valresult(Cm,^FourMoBackOS).
MonC5 is valresult(Cm,^FiveMoBackOS).
MonC6 is valresult(Cm,^SixMoBackOS).
MonC7 is valresult(Cm,^SevenMoBackOS).
MonC8 is valresult(Cm,^EightMoBackOS).

Cmo is iff(NewMo,Cm,MonC1).
Cmo1 is iff(NewMo,MonC1,MonC2).
Cmo2 is iff(NewMo,MonC2,MonC3).
Cmo3 is iff(NewMo,MonC3,MonC4).
Cmo4 is iff(NewMo,MonC4,MonC5).
Cmo5 is iff(NewMo,MonC5,MonC6).
Cmo6 is iff(NewMo,MonC6,MonC7).
Cmo7 is iff(NewMo,MonC7,MonC8).

EightMoSMA is iff(UseMoDataFile=0,(Cmo+Cmo1+Cmo2+Cmo3+Cmo4+Cmo5+Cmo6+Cmo7)/8,SMA8).

Buy if Cmo > EightMoSMA and HD.
Sell if Cmo < EightMoSMA and HD.

ListValues if 1.

—Richard Denning
for AIQ Systems

Leave a Reply