Trading Strategy 1: What goes up, goes up…

June 26, 2013
By

(This article was first published on Trading and travelling and other things » R, and kindly contributed to R-bloggers)

As I said earlier, my main task at my internship is to hunt for profitable strategies. As you can imagine, strategies can range from the exceedingly simple and easy to implement, to the crazily complex. Let’s start out with one of the simplest trading strategies out there.

The gist of this strategy is you buy the security tomorrow if the closing price today was higher than the opening price today. You hold the security until the end of the day and sell it at close.

This strategy requires buying at open and selling at close so we will calculate Open to Close returns.

1-Get data:
getSymbols(‘SPY’)

2-Calculate Open to Close returns:
retVec=Delt(Op(SPY),Cl(SPY))

3-Create a trading signal vector:
binaryVec=lag(ifelse(Cl(SPY)>Op(SPY),1,0),1) #This is saying that for any given index in the Opening and Closing price vectors, when the closing price is greater than the closing price, enter it as a ’1′ in the binaryVec vector, otherwise enter a ’0′. Then lag it by one day as you see the signal today but are only able to act on it tomorrow.

4-Finally, multiply the trading signal vector with the return vector and run performance analytics on it:
stratVec=retVec*binaryVec

5-charts.PerformanceSummary(stratVec)

Image

Note: Lagging creates NA values in the vector, which you can set to 0 by saying laggedVector[is.na(laggedVector)]=0.

This was an easy to run strategy and, not surprisingly, it didn’t do too well (although 2007 has to be one of the worst times historically to enter the market with long-only positions). Our intuition for this strategy was that if a stock ends higher today, it will carry that momentum forward to tomorrow. Several modifications can be made, such as tightening our criteria for what we label as momentum. To avoid capturing just random price fluctuations (to a marginal extent), we can raise the bar for what we judge as momentum by saying that instead of trading tomorrow if today’s price return was positive, we will only trade tomorrow if the last 2 days’ price return was positive as well, or the last 5 days’ price return was positive. There are two ways (that I can think of) something like this can be done in R, which I will cover in the next post.


To leave a comment for the author, please follow the link and comment on his blog: Trading and travelling and other things » R.

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.