We will explore the price hisotry of the three selected stocks, PE ratio, Dividend/Share, Earnings/Share.
We will source the data fron Yahoo Finance and make the decison to invest based on PE ratio, Dividend/Share, Earnings/Share.
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
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
library(plyr)
what_metrics <- yahooQF(c(
"P/E Ratio",
"Price/EPS Estimate Next Year",
"Dividend/Share",
"Earnings/Share"
))
tickers <- c("BABA", "BIDU","JD","TCEHY")
metrics <- getQuote(paste(tickers, sep="", collapse=";"), what=what_metrics)
start <- as.Date("2018-06-01")
end <- as.Date("2019-01-25")
getSymbols("BABA", src = "yahoo", from = start, to = end)
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
##
## WARNING: There have been significant changes to Yahoo Finance data.
## Please see the Warning section of '?getSymbols.yahoo' for details.
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.yahoo.warning"=FALSE).
## [1] "BABA"
getSymbols("BiDU", src = "yahoo", from = start, to = end)
## [1] "BIDU"
getSymbols("JD", src = "yahoo", from = start, to = end)
## [1] "JD"
getSymbols("TCEHY", src = "yahoo", from = start, to = end)
## [1] "TCEHY"
#Add tickers as the first column and remove the first column which had date stamps
metrics <- data.frame(Symbol=tickers, metrics[,3:length(metrics)])
#Change colnames
colnames(metrics) <- c("P-E Ratio", "Price EPS Estimate Next Year", "Dividend/Share", "Earnings/Share")
stocks <- as.xts(data.frame(BABA = BABA[, "BABA.Close"], BIDU = BIDU[, "BIDU.Close"],
JD = JD[, "JD.Close"], TCEHY = TCEHY[, "TCEHY.Close"]))
5.1.a First we take a look at the price movements of the stocks
P_BABA<-plot(BABA[, "BABA.Close"], main = "ALIBABA")
P_BABA
5.1.b First we take a look at the price movements of the stocks
P_BIDU<-plot(BIDU[, "BIDU.Close"], main = "BAIDU")
P_BIDU
5.1.c First we take a look at the price movements of the stocks
P_JD<-plot(JD[, "JD.Close"], main = "JD")
P_JD
# From the comparison of the three price movements, we generally could see that ALIBABA's stock price has a higher volatility.
# If we want to make some short term investments, then pick a stock with higher volatility would generally give us more opportunity.
5.1.d First we take a look at the price movements of the stocks
P_TCEHY<-plot(TCEHY[, "TCEHY.Close"], main = "TENCENT")
P_TCEHY
5.2 Secondly, we will take a look at earnings/share
barplot(metrics[,4],col=c("darkblue","red","green","yellow"),main = "Earnings/share",legend = row.names(metrics))
5.3 Also, we will take a look at Price/EPS estimate
barplot(metrics[,2],col=c("darkblue","red","green","yellow"),main = "Price/EPS estimate",legend = row.names(metrics))
5.4. We want to take a look at returns since stock prices are different
if (!require("magrittr")) {
install.packages("magrittr")
library(magrittr)
}
## Loading required package: magrittr
stocks_return = apply(stocks, 1, function(x) {x / stocks[1,]}) %>%
t %>% as.xts
head(stocks_return)
## BABA.Close BIDU.Close JD.Close TCEHY.Close
## 2018-06-01 1.0000000 1.000000 1.000000 1.000000
## 2018-06-04 1.0225604 1.050068 1.057357 1.018349
## 2018-06-05 1.0197220 1.046366 1.077030 1.029052
## 2018-06-06 1.0193795 1.062304 1.119424 1.044916
## 2018-06-07 0.9964765 1.066127 1.094486 1.031154
## 2018-06-08 1.0035725 1.060895 1.106124 1.024465
plot(as.zoo(stocks_return), screens = 1, lty = 1:3, xlab = "Date", ylab = "Return",col =c("darkblue","red","green","yellow"))
legend("topleft", c("BABA", "BIDU", "JD","TCEHY"), lty = 1:3, cex = 0.5)
5.5.a We want to take a look at the candlestick
BABA <- data.frame(BABA)
candleChart(BABA)
5.5.b We want to take a look at the candlestick
BIDU <- data.frame(BIDU)
candleChart(BIDU)
5.5.c We want to take a look at the candlestick
JD <- data.frame(JD)
candleChart(JD)
5.5.d We want to take a look at the candlestick
TCEHY <- data.frame(TCEHY)
candleChart(TCEHY)
par(mfrow=c(2,3))
P_BABA
P_BIDU
P_JD
P_TCEHY
barplot(metrics[,4],col=c("darkblue","red","green","yellow"),main = "Earnings/share",legend = row.names(metrics))
barplot(metrics[,2],col=c("darkblue","red","green","yellow"),main = "Price/EPS estimate",legend = row.names(metrics))
plot(as.zoo(stocks_return), screens = 1, lty = 1:3, xlab = "Date", ylab = "Return",col =c("darkblue","red","green","yellow"))
legend("topleft", c("BABA", "BIDU", "JD","TCEHY"), lty = 1:3, cex = 0.5)
# From the dash board, we see that in recent times, TECENT outpeforms the other three stocks with high returns.
# And its Price/EPS estimate is the seconce highest among the four.
# Also, it shows an upward trend in the recent months.
# So our conclusion is to invest in Tecent for a shor term gain.