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.

&lt;/pre&gt;<br />
&lt;pre class=&quot;brush: python; gutter: true; first-line: 1&quot;&gt;# 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&#8230;<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) &amp;gt; 1) x</p>
<p># Cross-checks<br />
# Fotis ok, Dec. 07, 2011<br />
if ((k1 k2) &amp;amp; (k2 &amp;gt; 0))) { stop(&quot;k1,k2 must both be positive and k1 &amp;lt; k2&quot;) }</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 == &quot;simple&quot;)<br />
{<br />
ma1ma2</p>
<p># Save the MA values # fotis change/edit<br />
ma.values[i,]</p>
<p>}<br />
if (mtype == &quot;weighted&quot;)<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==&quot;exponential&quot;)<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 &amp;gt; 0) &amp;amp; (pentry1 &amp;gt; 0) &amp;amp; (plast &amp;gt;= pentry1)) { signals.im1[i+1] if ((mark2 &amp;gt; 0) &amp;amp; (pentry2 &amp;gt; 0) &amp;amp; (plast &amp;gt;= pentry2)) { signals.im2[i+1] if ((markc &amp;gt; 0) &amp;amp; (pentryc &amp;gt; 0) &amp;amp; (plast &amp;gt;= pentryc)) { signals.imc[i+1] # Exit<br />
# Fotis check, Dec. 06, 2011<br />
if ((pentry1 &amp;gt; 0) &amp;amp; (plast &amp;lt; pentry1*(1-exitt))) { signals.im1[i+1] if ((pentry2 &amp;gt; 0) &amp;amp; (plast &amp;lt; pentry2*(1-exitt))) { signals.im2[i+1]if ((pentryc &amp;gt; 0) &amp;amp; (plast &amp;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 &amp;gt; 0) &amp;amp; (plast &amp;gt;= pentry1) &amp;amp; (plast &amp;gt; ma1)) { signals.iv1[i+1] if ((pentry2 &amp;gt; 0) &amp;amp; (plast &amp;gt;= pentry2) &amp;amp; (plast &amp;gt; ma2)) { signals.iv2[i+1] if ((pentryc &amp;gt; 0) &amp;amp; (plast &amp;gt;= pentryc) &amp;amp; (ma1 &amp;gt; ma2)) { signals.ivc[i+1] # Exit<br />
# Fotis ok, Dec. 07, 2011<br />
if ((pentry1 &amp;gt; 0) &amp;amp; (plast &amp;lt; pentry1*(1-exitt))) { signals.iv1[i+1] if ((pentry2 &amp;gt; 0) &amp;amp; (plast &amp;lt; pentry2*(1-exitt))) { signals.iv2[i+1]if ((pentryc &amp;gt; 0) &amp;amp; (plast &amp;lt; pentryc*(1-exitt))) { signals.ivc[i+1]</p>
<p># Here are the standard MA signals<br />
if (plast &amp;gt; ma1) { signals.ma1[i+1] if (plast &amp;gt; ma2) { signals.ma2[i+1]if (ma1 &amp;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] &amp;gt; signals.ma1[i]) { pentry1 if (signals.ma2[i+1] &amp;gt; signals.ma2[i]) { pentry2if (signals.mac[i+1] &amp;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

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.