Improved MA R Code

After the increasing demand and interest from our readers, we have decided to make publicly available a new “short” version of the original code used in the paper. With this way any  implementation error is avoided and everyone can test our modifications.

The code is written in R.

Download Now (.rar, size 5KB) 

Kindly let us know if you are using this code for non-commercial use or research.

Inside the .rar archive you will find two files:

  • maimpfunctions.r: this is the file that contains the function. Is is used as a source file to the other one, so make sure you have changed your working directory
  • maimp-testing.r: this is the file that loads data from yahoo and analyses the strategies

 Here are some guidelines and important issues:

  1. Change the working directory and make sure both files are there
  2. Set the series details (ticker, start date etc.)
  3. Choose the MA type you want. Available options are Simple, Weighted, Exponential
  4. Set the fast and slow MA lags
  5. Set an exit threshold (optional)
  6. Run the code

The exit threshold is a new improvement that came out of discussions and e-mails with readers of our site.

The output you get contains statistics for strategies using the following rules:

  1. Ret-MA1: using the returns of the standard fast MA (black line in the top panel)
  2. Ret-MA2: using the returns of the standard slow MA (black line in the mid panel)
  3. Ret-MAC: using the returns of the cross-over of MA1, MA2 (black line in the bottom panel)
  4. Ret-IMA1: using the returns of the improved fast MA in its original form, i.e. once you exit you wait for the next signal (as described in the paper) (green line in the top panel)
  5. Ret-IMA2: using the returns of the improved slow MA in its original form,  i.e. once you exit you wait for the next signal  (as described in the paper) (green line in the mid panel)
  6. Ret-IMC: using the returns of the improved MAC in its original form,  i.e. once you exit you wait for the next signal  (as described in the paper) (green line in the bottom panel)
  7. Ret-IV1: using the returns of the improved fast MA in an entry variation: once you exit you can re-enter when you are above the current entry price (red line in the top panel)
  8. Ret-IV2: using the returns of the improved slow MA in an entry variation: once you exit you can re-enter when you are above the current entry price (red line in the top panel)
  9. Ret-IVC: using the returns of the improved MAC in an entry variation: once you exit you can re-enter when you are above the current entry price (red line in the top panel)
  10. Ret-BnH: using the returns of the usual Buy and hold (blue line in all panels)

Here are some outputs on SPY, GLD, DIA and SP500 using k1=20, k2=50. For SPY we use the exponential with threshold=0 and the weighted with threshold=0.03. Then for the GLD, DIA we use threshold=0.We see that the green line of cumulative return of the improved rule described in the papers performs better. In the SP500 we are using a threshold of 5%.

These results further enhance the conclusion in our papers: “on average, an investor who is willing to do backtest different rules and strategies, she will be better off using the improved MA and their variations”.






Be Sociable, Share!


Leave a Reply

Your email address will not be published. Required fields are marked *


*

Comment

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>