**rbresearch » R**, and kindly contributed to R-bloggers)

The strategies used in Strategy Diversification in R were labeled as Strategy1 and Strategy2.

Strategy1

- Indicator: 52 week Simple Moving Average
- Entry Rule: Buy 1000 shares when price crosses and closes above 52 week Simple Moving Average
- Exit Rule: Exit all positions when prices crosses and closes below 52 week Simple Moving Average
- Classification: Long term trend following strategy

Strategy 2

- Indicator: RSI(2) on weekly data
- Entry Rule: Buy 100 shares when RSI(2) is less than 20 (Note that if RSI(2) is below 20 for N days, then you will have accumulated N * 100 shares)
- Exit Rule: Exit all positions when RSI(2) is greater than 50
- Classification: Short-Medium term reversal (dip buying) strategy

What did we diversify?

- Symbols? – No, the exact same instruments were used in the strategy.
- Markets? – No, see #1.
- Timeframe? Sort of, Strategy1 is a long term strategy and Strategy2 is a shorter term strategy, but both are on the weekly timeframe. We could diversify further by trading even shorter timeframes (i.e. Daily, Hourly, minute, tick, etc.)
- Strategy? Yes, Strategy1 is a trend following strategy and Strategy2 is a reversal strategy.
- Risk Levels? Yes, Strategy2 trades more often, but in smaller increments.

We achieved fairly low correlations by achieving only three “levels” of diversification. Think what we could do by using a “kitchen sink” portfolio with grains, softs, metals, currencies, stocks, fixed income, international stocks, international fixed income, style ETFs, etc.

Three R script files were used in the last post.

strategy1.R, strategy2.R, and correlation chart.R

The R scripts are pretty self explanatory so I won’t go into much detail. However, I do want to call attention to 2 lines of code from strategy1.R. The code for strategy2.R is virtually identical.

# logarithmic returns of the equity curve of strategy1. strategy1_eclogret <- ec$logret # write the logarithmic returns of strategy 1 to a csv file with the filename "strategy1.csv" # you will have to change the file where you want to save it write.zoo(strategy1_eclogret, file = "~/R/strats_for_cor/strategy1.csv", sep=",")

Here is the code to make the correlation chart.

#Load the packages used require(PerformanceAnalytics) # load the strategy 1 returns strat1 <- as.xts(read.zoo(file = "~/R/strats_for_cor/strategy1.csv", header = TRUE, sep=",")) colnames(strat1) <- "strat1" # load the strategy 2 returns strat2 <- as.xts(read.zoo(file = "~/R/strats_for_cor/strategy2.csv", header = TRUE, sep=",")) colnames(strat2) <- "strat2" suppressWarnings(chart.RollingCorrelation(strat1, strat2, width = 52, xaxis = TRUE, colorset = rich8equal, legend.loc = "bottomright", main = "Rolling 52 Week Correlation"))

And that is all there is to it. (run strategy1.R, run strategy2.R, then run correlation chart.R – don’t forget to change the file directory!)

I listed 5 “levels” or ways to achieve diversification… what are other ways we can diversify? – post your ideas in the comments

**leave a comment**for the author, please follow the link and comment on their blog:

**rbresearch » R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...