# Load required packages
#install.packages(c("quantmod", "xts", "lubridate"))
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
## ################################### WARNING ###################################
## # We noticed you have dplyr installed. The dplyr lag() function breaks how #
## # base R's lag() function is supposed to work, which breaks lag(my_xts). #
## # #
## # If you call library(dplyr) later in this session, then calls to lag(my_xts) #
## # that you enter or source() into this session won't work correctly. #
## # #
## # All package code is unaffected because it is protected by the R namespace #
## # mechanism. #
## # #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #
## # #
## # You can use stats::lag() to make sure you're not using dplyr::lag(), or you #
## # can add conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop #
## # dplyr from breaking base R's lag() function. #
## ################################### WARNING ###################################
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(xts)
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
# Read data
etf56 <- read.table("C:\\Users\\emman\\Downloads\\tw0056_20070101_20191231(1).txt", header = TRUE)
etf56 <- etf56[c(3, 7)]
colnames(etf56) <- c("date", "price")
# Convert to xts object
etf56.xts <- xts(etf56$price, order.by = as.Date(as.character(etf56$date), format = "%Y%m%d"))
colnames(etf56.xts) <- 'price'
# Simple Moving Average (SMA) Calculation
md <- 50
sma <- SMA(etf56.xts, n = md)
# Generate Buy/Sell Signals based on SMA crossover
signals <- ifelse(etf56.xts > sma, 1, 0) # 1 for buy, 0 for sell
# Plot the data and SMA
plot(etf56.xts, main = "TW56 ETF Prices", type = "l") # Use type = "l" for a line plot
lines(sma, col = "red")
# Plot Buy/Sell Signals
points(index(etf56.xts), signals * max(etf56.xts),
col = ifelse(signals == 1, "green", "red"), pch = 19)
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.