etf6 <- read.table('ETF6_20080101-20200430.csv', sep = ',', header = T)
#
head(etf6)
##   證券代碼          簡稱   年月日 收盤價.元.
## 1  0050    元大台灣50    20080102    39.6472
## 2  0052    富邦科技      20080102    27.0983
## 3  0056    元大高股息    20080102    14.5739
## 4  0050    元大台灣50    20080103    38.9876
## 5  0052    富邦科技      20080103    26.0676
## 6  0056    元大高股息    20080103    14.3758
etf6 <- etf6[, -2]
colnames(etf6) <- c('id', 'date', 'price')
head(etf6)
##        id     date   price
## 1 0050    20080102 39.6472
## 2 0052    20080102 27.0983
## 3 0056    20080102 14.5739
## 4 0050    20080103 38.9876
## 5 0052    20080103 26.0676
## 6 0056    20080103 14.3758
library(pacman)
p_load(reshape2, xts, quantmod)

etf6.l <- dcast(etf6, date~id)
## Using price as value column: use value.var to override.
head(etf6.l)
##       date 0050    0052    0056    0061    006206  00638R 
## 1 20080102 39.6472 27.0983 14.5739      NA      NA      NA
## 2 20080103 38.9876 26.0676 14.3758      NA      NA      NA
## 3 20080104 38.9876 25.9346 14.4041      NA      NA      NA
## 4 20080107 37.2064 24.1391 14.1777      NA      NA      NA
## 5 20080108 37.5692 24.1391 14.3531      NA      NA      NA
## 6 20080109 38.2619 24.2721 14.4663      NA      NA      NA
etf6.l <- na.omit(etf6.l)
# head(etf6.l)

str(etf6.l)
## 'data.frame':    1214 obs. of  7 variables:
##  $ date   : int  20150518 20150519 20150520 20150521 20150522 20150525 20150526 20150527 20150528 20150529 ...
##  $ 0050   : num  58.7 59.6 59.1 58.6 59.1 ...
##  $ 0052   : num  34.8 34.9 35 34.6 35.1 ...
##  $ 0056   : num  19.1 19.2 19.2 19 19 ...
##  $ 0061   : num  21.8 22.4 22.7 22.8 23.3 ...
##  $ 006206 : num  33.5 34.5 34.9 35 35.9 ...
##  $ 00638R : num  20 19.5 19.2 19.2 18.6 ...
##  - attr(*, "na.action")= 'omit' Named int [1:1827] 1 2 3 4 5 6 7 8 9 10 ...
##   ..- attr(*, "names")= chr [1:1827] "1" "2" "3" "4" ...
# convert into xts
etf6.xts <- xts(etf6.l[, -1], order.by = as.Date(as.character(etf6.l$date), format = '%Y%m%d'))
class(etf6.xts)
## [1] "xts" "zoo"
head(etf6.xts)
##            0050    0052    0056    0061    006206  00638R 
## 2015-05-18 58.6843 34.8094 19.0693   21.77   33.46   20.03
## 2015-05-19 59.5614 34.8596 19.1931   22.43   34.46   19.51
## 2015-05-20 59.1437 35.0268 19.1931   22.70   34.91   19.19
## 2015-05-21 58.6007 34.6339 18.9687   22.79   35.03   19.17
## 2015-05-22 59.0602 35.1104 18.9919   23.33   35.90   18.60
## 2015-05-25 59.0602 34.8596 18.9687   23.65   36.80   17.99
library(SIT)
data <- new.env()

model <- list()

etf3 <- etf6.xts[, 1:3]
head(etf3)
##            0050    0052    0056   
## 2015-05-18 58.6843 34.8094 19.0693
## 2015-05-19 59.5614 34.8596 19.1931
## 2015-05-20 59.1437 35.0268 19.1931
## 2015-05-21 58.6007 34.6339 18.9687
## 2015-05-22 59.0602 35.1104 18.9919
## 2015-05-25 59.0602 34.8596 18.9687
names(etf3)
## [1] "0050   " "0052   " "0056   "
colnames(etf3) <- c('e50', 'e52', 'e56')
names(etf3)
## [1] "e50" "e52" "e56"
library(pacman)
p_load(reshape2, xts, quantmod)

etf6.l <- dcast(etf6, date~id)
## Using price as value column: use value.var to override.
head(etf6.l)
##       date 0050    0052    0056    0061    006206  00638R 
## 1 20080102 39.6472 27.0983 14.5739      NA      NA      NA
## 2 20080103 38.9876 26.0676 14.3758      NA      NA      NA
## 3 20080104 38.9876 25.9346 14.4041      NA      NA      NA
## 4 20080107 37.2064 24.1391 14.1777      NA      NA      NA
## 5 20080108 37.5692 24.1391 14.3531      NA      NA      NA
## 6 20080109 38.2619 24.2721 14.4663      NA      NA      NA
str(etf6.l)
## 'data.frame':    3041 obs. of  7 variables:
##  $ date   : int  20080102 20080103 20080104 20080107 20080108 20080109 20080110 20080111 20080114 20080115 ...
##  $ 0050   : num  39.6 39 39 37.2 37.6 ...
##  $ 0052   : num  27.1 26.1 25.9 24.1 24.1 ...
##  $ 0056   : num  14.6 14.4 14.4 14.2 14.4 ...
##  $ 0061   : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ 006206 : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ 00638R : num  NA NA NA NA NA NA NA NA NA NA ...
etf6.xts <- xts(etf6.l[, -1], order.by = as.Date(as.character(etf6.l$date), format = '%Y%m%d'))
class(etf6.xts)
## [1] "xts" "zoo"
head(etf6.xts)
##            0050    0052    0056    0061    006206  00638R 
## 2008-01-02 39.6472 27.0983 14.5739      NA      NA      NA
## 2008-01-03 38.9876 26.0676 14.3758      NA      NA      NA
## 2008-01-04 38.9876 25.9346 14.4041      NA      NA      NA
## 2008-01-07 37.2064 24.1391 14.1777      NA      NA      NA
## 2008-01-08 37.5692 24.1391 14.3531      NA      NA      NA
## 2008-01-09 38.2619 24.2721 14.4663      NA      NA      NA
library(SIT) 
library(quantmod)
data <- new.env()
model <- list()
#
etf3 <- etf6.xts[, 1:3]
head(etf3)
##            0050    0052    0056   
## 2008-01-02 39.6472 27.0983 14.5739
## 2008-01-03 38.9876 26.0676 14.3758
## 2008-01-04 38.9876 25.9346 14.4041
## 2008-01-07 37.2064 24.1391 14.1777
## 2008-01-08 37.5692 24.1391 14.3531
## 2008-01-09 38.2619 24.2721 14.4663
names(etf3)
## [1] "0050   " "0052   " "0056   "
colnames(etf3) <- c('e50', 'e52', 'e56')
names(etf3)
## [1] "e50" "e52" "e56"
md = 50
i = 'e50'
# Assuming 'etf3' is your final data to plot which consists of 3 ETFs: 'e50', 'e52', 'e56'
library(quantmod)
library(ggplot2)

# Calculate moving averages and create signals
etf3$e50.sma <- SMA(etf3$e50, n=md)  # Modify 'md' as per required moving average days
etf3$e52.sma <- SMA(etf3$e52, n=md)
etf3$e56.sma <- SMA(etf3$e56, n=md)

# Create plots
etf_plot <- ggplot() +
  geom_line(data = etf3, aes(x = index(etf3), y = e50, colour = "e50")) +
  geom_line(data = etf3, aes(x = index(etf3), y = e50.sma, colour = "e50.sma.cross")) +
  geom_line(data = etf3, aes(x = index(etf3), y = e52, colour = "e52")) +
  geom_line(data = etf3, aes(x = index(etf3), y = e52.sma, colour = "e52.sma.cross")) +
  geom_line(data = etf3, aes(x = index(etf3), y = e56, colour = "e56")) +
  geom_line(data = etf3, aes(x = index(etf3), y = e56.sma, colour = "e56.sma.cross")) +
  scale_colour_manual(values = c("e50" = "black", "e50.sma.cross" = "red", 
                                 "e52" = "green", "e52.sma.cross" = "blue",
                                 "e56" = "cyan", "e56.sma.cross" = "magenta")) +
  labs(title = "ETF Cumulative Performance with SMA Cross", x = "Date", y = "Cumulative Performance") +
  theme_minimal()

print(etf_plot)
## Warning: Removed 49 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Removed 49 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Removed 49 rows containing missing values or values outside the scale range
## (`geom_line()`).