A Better ZigZag

[This article was first published on Quintuitive » R, 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.

There are a lot of “winning” strategies for bull markets floating around. “Buy the pullbacks” is certainly one of them. Does this sound simple enough to implement to you? While I am no Sheldon Cooper (although I have a favorite couch seat), I still like to live in a somewhat well defined world, a world in which, there is much more information attached to a tip like “Buy the pullbacks”. Let’s start with a chart of the recent history of the S&P 500 ETF:

SPY

The indicator on the chart is the ZigZag indicator. It’s certainly one way to look for the proverbial pullbacks. One can use an absolute dollar amount, or a percentage to define the minimum amount the price needs to travel in the opposite direction to constitute a correction. I used a setting of 2% in the above chart. Notice, we won’t enter at the bottoms, most likely the position is taken as soon as the pullback is “registered”. Using our 2% setup, we enter the pullback as soon as the market is down 2% from the most recent peak. The rest of the road to the actual bottom is the pain we are going to suffer. :)

The chart indicates that over the last one year, there were 4 points to enter a position. That’s somewhat low if we think of buying each pullback and then closing the trade (these are not buy and hold entries).

Pretty easy you say – go to 2.5%, or even 3%, pullback. Well, before we do that, let’s look at the same chart just over a different period in time:

spy04

To me it seems that we actually have way too many “corrections” (a.k.a. pullbacks) during the 2011 turbulence.

By now it’s pretty clear what I am getting at – the pullbacks shouldn’t be a hard number, neither a total amount, nor a percentage. It seems a better approach to get volatility involved.

To address these issues, I modified the ZigZag to actually use volatility to measure the pullbacks. With this improvement, the charts look somewhat different.

SPY 2014,

Notice, we have a few more “corrections” generated by the modified ZigZag (the red line in the charts) in the low-volatility environment. Similarly, keeping the settings the same, there are a few less “corrections” generated by the volatility ZigZag in the high-volatility environment.

SPY 2011

How useful is all this? Frankly, I don’t know yet, but it doesn’t really matter, in a way. I consider the first goal achieved – I have a simple indicator, with a single setting, to model what my strategy would consider as a pullback. In the process, I have added a few useful features to my ZigZag. My R implementation is, in an amusing way, totally the opposite from the TTR’s ZigZag. The TTR’s ZigZag simply returns the indicator line, thus, one can easily plot it, but it’s hard to tell from the line when was the move “registered” (in other words, when was the 2% pullback first hit). My implementation on the other hand (available from the btutils package), does not compute the lines connecting the extremes (thus, one cannot draw it at all), but it computes three other columns which can be easily used to implement a trading strategy based on the modified ZigZag (the “age” column is 0 at the inflection points). I guess we all have different goals. :) Here is how to call the btutils version:

require(btutils)
require(quantmod)
spy = getSymbols("SPY", from="1900-01-01", auto.assign=F)
spy.zz = zig.zag(Cl(spy), 2*runSD(ROC(Cl(spy),type="discrete"),10), percent=T)
tail(spy.zz)

Last, the btutils package currently cannot be installed on Windows directly from R-Forge. You still can download the source code (the easiest way is to create a new project in RStudio and select from source control etc) and build the package though.

The post A Better ZigZag appeared first on Quintuitive.

To leave a comment for the author, please follow the link and comment on their blog: Quintuitive » R.

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)