Improved Moving Average Code is available for download!

December 7, 2011
By

(This article was first published on Quantitative Finance & Technical Trading » r-bloggers, and kindly contributed to R-bloggers)

Quantitative Finance, Technical Trading & Analysis. Fotis Papailias, Dimitrios Thomakos Quantitative Finance & Technical Trading Improved Moving Average Code is available for download!

Hi everyone,

in the last few days we have received great feedback from you. Due to increasing demand we have made a short version of the original code available for personal use. Please let us know of your comments, thoughts and suggestions.

http://www.quantf.com/ima-code

The main function that does most of the things follows. Here is an output on SPY.

The green line denotes our improved rule in its original form we have used in the papers.

Graphs and full downloads can be found in the above link.

[sourcecode language="r"]</pre>
<pre class="brush: python; gutter: true; first-line: 1"># Explicit code for improved MA signals
ma.trading.improved {
# Keep attributes for later use, need next day as well,
# although this is done for convenience, it does not
# have to be the next trading day and its not used for
# anything thereafter…
dates

# Pass the data as a matrix; this strips all other attributes
x

# Select correctly the closing price if OHLC is loaded
if(NCOL(x) &gt; 1) x

# Cross-checks
# Fotis ok, Dec. 07, 2011
if ((k1 k2) &amp; (k2 &gt; 0))) { stop("k1,k2 must both be positive and k1 &lt; k2") }

# Full sample
Nobs

# Daily return
ret

# Initialize moving average values
ma.values

# Initialize signal storage
signals.ma1 signals.ma2 signals.mac signals.im1 signals.im2 signals.imc signals.iv1 signals.iv2signals.ivc

# Initialize markers of entries for improved MA
mark1 mark2 markc pentry1 pentry2pentryc

# Do things via a standard loop to maintain consistency
for (i in seq(k2,Nobs,1)) # fotis change/edit
{
# Compute the moving averages # fotis change/edit
if (mtype == "simple")
{
ma1ma2

# Save the MA values # fotis change/edit
ma.values[i,]

}
if (mtype == "weighted")
{
ma1ma2

# Save the MA values # fotis change/edit
ma.values[i,] }

# fotis add, Dec. 07, 2011
if (mtype=="exponential")
{
# Get the starting signal to be used in the EMA calc., fotis
if(i==k2)
{
ma1ma2

# Save the MA values # fotis change/edit
ma.values[i,] }
else
{
ma1ma2

# Save the MA values
ma.values[i,] }
}

# Get the last observed price
plast

# Version AB of improved MA: once you exit you wait for next signal;
# exiting is controlled by the exit threshold
#
# Stay
# Fotis ok, Dec. 07, 2011
if ((mark1 &gt; 0) &amp; (pentry1 &gt; 0) &amp; (plast &gt;= pentry1)) { signals.im1[i+1] if ((mark2 &gt; 0) &amp; (pentry2 &gt; 0) &amp; (plast &gt;= pentry2)) { signals.im2[i+1] if ((markc &gt; 0) &amp; (pentryc &gt; 0) &amp; (plast &gt;= pentryc)) { signals.imc[i+1] # Exit
# Fotis check, Dec. 06, 2011
if ((pentry1 &gt; 0) &amp; (plast &lt; pentry1*(1-exitt))) { signals.im1[i+1] if ((pentry2 &gt; 0) &amp; (plast &lt; pentry2*(1-exitt))) { signals.im2[i+1]if ((pentryc &gt; 0) &amp; (plast &lt; pentryc*(1-exitt))) { signals.imc[i+1]

# Version BB of improved MA: once you exit you can re-enter when above the current
# entry price; exiting is controlled by the exit threshold
#
# Fotis ok, Dec. 07, 2011
# Stay
if ((pentry1 &gt; 0) &amp; (plast &gt;= pentry1) &amp; (plast &gt; ma1)) { signals.iv1[i+1] if ((pentry2 &gt; 0) &amp; (plast &gt;= pentry2) &amp; (plast &gt; ma2)) { signals.iv2[i+1] if ((pentryc &gt; 0) &amp; (plast &gt;= pentryc) &amp; (ma1 &gt; ma2)) { signals.ivc[i+1] # Exit
# Fotis ok, Dec. 07, 2011
if ((pentry1 &gt; 0) &amp; (plast &lt; pentry1*(1-exitt))) { signals.iv1[i+1] if ((pentry2 &gt; 0) &amp; (plast &lt; pentry2*(1-exitt))) { signals.iv2[i+1]if ((pentryc &gt; 0) &amp; (plast &lt; pentryc*(1-exitt))) { signals.ivc[i+1]

# Here are the standard MA signals
if (plast &gt; ma1) { signals.ma1[i+1] if (plast &gt; ma2) { signals.ma2[i+1]if (ma1 &gt; ma2) { signals.mac[i+1]

# Here are the initializations of the improved MAs, both versions
# Fotis ok, Dec. 07, 2011
if (signals.ma1[i+1] &gt; signals.ma1[i]) { pentry1 if (signals.ma2[i+1] &gt; signals.ma2[i]) { pentry2if (signals.mac[i+1] &gt; signals.mac[i]) { pentryc

}

# Bind together and correctly aling things and returns
bind1 signals.im1,signals.im2,signals.imc,signals.iv1,signals.iv2,signals.ivc,c(ret,NA,NA))
bind2 matrix(c(ret,NA,NA),nrow=Nobs+1,ncol=9),c(ret,NA,NA))
colnames(bind1)colnames(bind2)

bind3# Fotis ok, Dec. 07, 2011

# Convert to zoo objects
bind1 bind2bind3

# Done!
return(list(values=bind1,returns=bind2,cumulative=bind3))
}
[/sourcecode]

Quantitative Finance, Technical Trading & Analysis. Fotis Papailias, Dimitrios Thomakos Quantitative Finance & Technical Trading Improved Moving Average Code is available for download!

To leave a comment for the author, please follow the link and comment on his blog: Quantitative Finance & Technical Trading » r-bloggers.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.