**Quantitative Finance & Technical Trading » r-bloggers**, and kindly contributed to R-bloggers)

Quantitative Finance, Technical Trading & Analysis. Fotis Papailias, Dimitrios Thomakos

Fotis

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.

</pre><br /> <pre class="brush: python; gutter: true; first-line: 1"># Explicit code for improved MA signals<br /> ma.trading.improved {<br /> # Keep attributes for later use, need next day as well,<br /> # although this is done for convenience, it does not<br /> # have to be the next trading day and its not used for<br /> # anything thereafter…<br /> dates</p> <p># Pass the data as a matrix; this strips all other attributes<br /> x</p> <p># Select correctly the closing price if OHLC is loaded<br /> if(NCOL(x) &gt; 1) x</p> <p># Cross-checks<br /> # Fotis ok, Dec. 07, 2011<br /> if ((k1 k2) &amp; (k2 &gt; 0))) { stop("k1,k2 must both be positive and k1 &lt; k2") }</p> <p># Full sample<br /> Nobs</p> <p># Daily return<br /> ret</p> <p># Initialize moving average values<br /> ma.values</p> <p># Initialize signal storage<br /> signals.ma1 signals.ma2 signals.mac signals.im1 signals.im2 signals.imc signals.iv1 signals.iv2signals.ivc</p> <p># Initialize markers of entries for improved MA<br /> mark1 mark2 markc pentry1 pentry2pentryc</p> <p># Do things via a standard loop to maintain consistency<br /> for (i in seq(k2,Nobs,1)) # fotis change/edit<br /> {<br /> # Compute the moving averages # fotis change/edit<br /> if (mtype == "simple")<br /> {<br /> ma1ma2</p> <p># Save the MA values # fotis change/edit<br /> ma.values[i,]</p> <p>}<br /> if (mtype == "weighted")<br /> {<br /> ma1ma2</p> <p># Save the MA values # fotis change/edit<br /> ma.values[i,] }</p> <p># fotis add, Dec. 07, 2011<br /> if (mtype=="exponential")<br /> {<br /> # Get the starting signal to be used in the EMA calc., fotis<br /> if(i==k2)<br /> {<br /> ma1ma2</p> <p># Save the MA values # fotis change/edit<br /> ma.values[i,] }<br /> else<br /> {<br /> ma1ma2</p> <p># Save the MA values<br /> ma.values[i,] }<br /> }</p> <p># Get the last observed price<br /> plast</p> <p># Version AB of improved MA: once you exit you wait for next signal;<br /> # exiting is controlled by the exit threshold<br /> #<br /> # Stay<br /> # Fotis ok, Dec. 07, 2011<br /> 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<br /> # Fotis check, Dec. 06, 2011<br /> 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]</p> <p># Version BB of improved MA: once you exit you can re-enter when above the current<br /> # entry price; exiting is controlled by the exit threshold<br /> #<br /> # Fotis ok, Dec. 07, 2011<br /> # Stay<br /> 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<br /> # Fotis ok, Dec. 07, 2011<br /> 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]</p> <p># Here are the standard MA signals<br /> if (plast &gt; ma1) { signals.ma1[i+1] if (plast &gt; ma2) { signals.ma2[i+1]if (ma1 &gt; ma2) { signals.mac[i+1]</p> <p># Here are the initializations of the improved MAs, both versions<br /> # Fotis ok, Dec. 07, 2011<br /> 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</p> <p>}</p> <p># Bind together and correctly aling things and returns<br /> bind1 signals.im1,signals.im2,signals.imc,signals.iv1,signals.iv2,signals.ivc,c(ret,NA,NA))<br /> bind2 matrix(c(ret,NA,NA),nrow=Nobs+1,ncol=9),c(ret,NA,NA))<br /> colnames(bind1)colnames(bind2)</p> <p>bind3# Fotis ok, Dec. 07, 2011</p> <p># Convert to zoo objects<br /> bind1 bind2bind3</p> <p># Done!<br /> return(list(values=bind1,returns=bind2,cumulative=bind3))<br /> }<br />

Quantitative Finance, Technical Trading & Analysis. Fotis Papailias, Dimitrios Thomakos

Fotis

Quantitative Finance & Technical Trading

Improved Moving Average Code is available for download!

**leave a comment**for the author, please follow the link and comment on their blog:

**Quantitative Finance & Technical Trading » r-bloggers**.

R-bloggers.com offers

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