Improved Moving Average Code is available for download!

[This article was first published on Quantitative Finance & Technical Trading » r-bloggers, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

[sourcecode language=”r”]

# 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) > 1) x

# Cross-checks
# Fotis ok, Dec. 07, 2011
if ((k1 k2) & (k2 > 0))) { stop("k1,k2 must both be positive and k1 < 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 > 0) & (pentry1 > 0) & (plast >= pentry1)) { signals.im1[i+1] if ((mark2 > 0) & (pentry2 > 0) & (plast >= pentry2)) { signals.im2[i+1] if ((markc > 0) & (pentryc > 0) & (plast >= pentryc)) { signals.imc[i+1] # Exit
# Fotis check, Dec. 06, 2011
if ((pentry1 > 0) & (plast < pentry1*(1-exitt))) { signals.im1[i+1] if ((pentry2 > 0) & (plast < pentry2*(1-exitt))) { signals.im2[i+1]if ((pentryc > 0) & (plast < 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 > 0) & (plast >= pentry1) & (plast > ma1)) { signals.iv1[i+1] if ((pentry2 > 0) & (plast >= pentry2) & (plast > ma2)) { signals.iv2[i+1] if ((pentryc > 0) & (plast >= pentryc) & (ma1 > ma2)) { signals.ivc[i+1] # Exit
# Fotis ok, Dec. 07, 2011
if ((pentry1 > 0) & (plast < pentry1*(1-exitt))) { signals.iv1[i+1] if ((pentry2 > 0) & (plast < pentry2*(1-exitt))) { signals.iv2[i+1]if ((pentryc > 0) & (plast < pentryc*(1-exitt))) { signals.ivc[i+1]

# Here are the standard MA signals
if (plast > ma1) { signals.ma1[i+1] if (plast > ma2) { signals.ma2[i+1]if (ma1 > 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] > signals.ma1[i]) { pentry1 if (signals.ma2[i+1] > signals.ma2[i]) { pentry2if (signals.mac[i+1] > 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 Fotis 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 their blog: Quantitative Finance & Technical Trading » r-bloggers.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)