# Set Working Directory to folder with project files
setwd("C:/Users/Administrator/Dropbox/Programming/PredictiveAnalytics/TimeSeriesAnalysis/StockTrading/AutomatedTradingStrategies")
# Required Packages
# if (!require("xts")) { install.packages("xts"); require("xts") }
# if (!require("zoo")) { install.packages("zoo"); require("zoo") }
# if (!require("quantmod")) { install.packages("quantmod"); require("quantmod") }
# if (!require("PerformanceAnalytics")) { install.packages("PerformanceAnalytics"); require("PerformanceAnalytics") }
# if (!require("TTR")) { install.packages("TTR"); require("TTR") }
library(xts)
library(zoo)
library(quantmod) # data, plotting, quant modelling
library(PerformanceAnalytics) # performance and risk management
library(TTR)
library(knitr) # formatting tables
# Downloading Stock Ticker Data from Yahoo Finances
# Enter Ticker Symbol and Date Range
SYMBL <- getSymbols("TSLA", auto.assign=F, from="2015-10-18", to="2017-10-18")
# Display first line of Stock Ticker Data
head(SYMBL, 1)
## TSLA.Open TSLA.High TSLA.Low TSLA.Close TSLA.Volume
## 2015-10-19 226.5 231.15 224.94 228.1 2507900
## TSLA.Adjusted
## 2015-10-19 228.1
# Extract the closing prices
data <- SYMBL[,4]
# Calculate the SMA
# Set "n=" to the SMA period
sma <- SMA(Cl(data), n=50)
# Calculate MACD
# Set "nsig=" to the SMA period
macd <- MACD(data, nFast=12, nSlow=26, nSig=50, maType=SMA, percent = FALSE)
# Calculate the buy/sell signals
signal <- Lag(ifelse(macd$macd < macd$signal, -1, 1))
# Calculate the returns from the Trading Strategy
returns <- ROC(data)*signal
# Remove not available values from the returns
returns <- na.omit(returns)
# Calculate the Portfolio
portfolio <- exp(cumsum(returns))
# Table of Calendar Returns
kable(table.CalendarReturns(returns), caption = "Calendar Returns")
| Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec | TSLA.Close | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2016 | NA | 0.8 | 1.3 | 2.8 | -0.1 | -1.0 | 1.8 | -0.3 | 1.6 | -1.1 | 0.1 | -0.5 | 5.5 |
| 2017 | 0.5 | -1.5 | 0.1 | 1.7 | -1.7 | 0.2 | 3.5 | 0.8 | 0.4 | -1.5 | NA | NA | 2.6 |
# Table of Drawdowns
kable(table.Drawdowns(returns, top=10), caption = "Table of Drawdowns")
| From | Trough | To | Depth | Length | To Trough | Recovery |
|---|---|---|---|---|---|---|
| 2017-02-15 | 2017-10-06 | NA | -0.3424 | 171 | 163 | NA |
| 2016-05-18 | 2016-11-25 | 2017-01-23 | -0.3109 | 172 | 134 | 38 |
| 2016-02-11 | 2016-03-01 | 2016-04-06 | -0.2678 | 38 | 13 | 25 |
| 2016-04-07 | 2016-04-19 | 2016-05-03 | -0.0696 | 19 | 9 | 10 |
| 2017-01-25 | 2017-02-01 | 2017-02-06 | -0.0212 | 9 | 6 | 3 |
| 2016-05-06 | 2016-05-06 | 2016-05-09 | -0.0159 | 2 | 1 | 1 |
| 2016-05-13 | 2016-05-16 | 2016-05-17 | -0.0049 | 3 | 2 | 1 |
| 2016-02-09 | 2016-02-09 | 2016-02-10 | -0.0018 | 2 | 1 | 1 |
| 2016-05-11 | 2016-05-11 | 2016-05-12 | -0.0013 | 2 | 1 | 1 |
| 2017-02-07 | 2017-02-07 | 2017-02-08 | -0.0011 | 2 | 1 | 1 |
# Table of Downside Risk
kable(table.DownsideRisk(returns), caption = "Downside Risk Table")
| TSLA.Close | |
|---|---|
| Semi Deviation | 0.0165 |
| Gain Deviation | 0.0155 |
| Loss Deviation | 0.0159 |
| Downside Deviation (MAR=210%) | 0.0210 |
| Downside Deviation (Rf=0%) | 0.0163 |
| Downside Deviation (0%) | 0.0163 |
| Maximum Drawdown | 0.3424 |
| Historical VaR (95%) | -0.0360 |
| Historical ES (95%) | -0.0515 |
| Modified VaR (95%) | -0.0375 |
| Modified ES (95%) | -0.0554 |
# Plot the SMA
# Select next two lines of code, and run
plotSMA <- plot(data, main="Returns with SMA Trend Line"); lines(sma, col="red")
# Plot of Buy/Sell/Hold signals
# Select next two lines of code, and run
plotBuySell <- plot(macd$signal, main="Buy/Sell/Hold Signal
Plot");lines(signal,col="red")
# Performance Summary Chart
charts.PerformanceSummary(returns, main = "Performance Summary Chart")
# Create Chart Series with MACD
# Set "signal=" to SMA period
chartSeries(data, name = "Strategy Chart Series", theme = chartTheme('white'),
TA=c(addVo(), addBBands(), addMACD(signal=50)))