# List of packages
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
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(xts)
library(rvest)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::first() masks xts::first()
## ✖ readr::guess_encoding() masks rvest::guess_encoding()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::last() masks xts::last()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(stringr)
library(forcats)
library(lubridate)
library(plotly)
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
library(dplyr)
library(PerformanceAnalytics)
##
## Attaching package: 'PerformanceAnalytics'
##
## The following object is masked from 'package:graphics':
##
## legend
# Activating all the packages from library
# Let us set start data and end date to fetch the data from yahoo finance
start <- as.Date("2023-01-01")
end <- as.Date("2023-11-24")
# Lets get the stock price of TTK PRESTIGE Ltd from yahoo finance from start date to end date
getSymbols("TTKPRESTIG.NS", src = "yahoo", from = start, to=end)
## [1] "TTKPRESTIG.NS"
Exploring the data set
head(TTKPRESTIG.NS)
## TTKPRESTIG.NS.Open TTKPRESTIG.NS.High TTKPRESTIG.NS.Low
## 2023-01-02 801.0 806.25 791.95
## 2023-01-03 799.5 806.00 796.15
## 2023-01-04 806.4 806.40 792.35
## 2023-01-05 809.0 809.00 789.35
## 2023-01-06 796.5 813.00 790.55
## 2023-01-09 811.9 812.00 782.00
## TTKPRESTIG.NS.Close TTKPRESTIG.NS.Volume TTKPRESTIG.NS.Adjusted
## 2023-01-02 798.75 317075 792.7512
## 2023-01-03 802.15 26064 796.1256
## 2023-01-04 794.90 21906 788.9301
## 2023-01-05 792.55 64463 786.5977
## 2023-01-06 800.05 50232 794.0414
## 2023-01-09 794.65 46131 788.6819
tail(TTKPRESTIG.NS)
## TTKPRESTIG.NS.Open TTKPRESTIG.NS.High TTKPRESTIG.NS.Low
## 2023-11-16 784.50 798.90 784.50
## 2023-11-17 793.65 796.00 780.15
## 2023-11-20 787.50 791.60 780.10
## 2023-11-21 786.00 794.90 781.05
## 2023-11-22 782.00 787.90 772.70
## 2023-11-23 781.10 781.75 766.25
## TTKPRESTIG.NS.Close TTKPRESTIG.NS.Volume TTKPRESTIG.NS.Adjusted
## 2023-11-16 793.65 38820 793.65
## 2023-11-17 786.25 37823 786.25
## 2023-11-20 790.05 25898 790.05
## 2023-11-21 783.95 28264 783.95
## 2023-11-22 781.10 51896 781.10
## 2023-11-23 769.65 85464 769.65
# Viewing the stock price of TTK PRESTIGE Ltd for first and last 6 days
# Creating the line graph of TTK PRESTIGE from start date and end date
plot(TTKPRESTIG.NS[,"TTKPRESTIG.NS.Close"], main = "TTKPRESTIG CLOSING PRICE MOVEMENT")
# Creating candle stick chart of TTK PRESTIGE Ltd stock price
candleChart(TTKPRESTIG.NS, up.col="green",dn.col="red",theme = "white")
# Let us get compare TTK PRESTIGE LTD with Bench mark NIFTY 500
getSymbols(c("^CRSLDX","TTKPRESTIG.NS"), src = "yahoo", from =start,to=end)
## [1] "CRSLDX" "TTKPRESTIG.NS"
# Getting the stock price of TTK PRESTIGE Ltd and NIFTY 500
# Looking the starting prices of TTK PRESTIGE Ltd and NIFTY 500 Index
head(list(CRSLDX,TTKPRESTIG.NS))
## [[1]]
## CRSLDX.Open CRSLDX.High CRSLDX.Low CRSLDX.Close CRSLDX.Volume
## 2023-01-02 15479.95 15536.35 15433.30 15523.65 2100336300
## 2023-01-03 15515.75 15575.70 15500.75 15557.80 20771100
## 2023-01-04 15563.15 15574.50 15375.80 15393.00 21390500
## 2023-01-05 15446.50 15462.15 15286.40 15386.20 17566700
## 2023-01-06 15401.35 15419.40 15219.85 15272.00 17314600
## 2023-01-09 15360.40 15472.15 15353.35 15450.10 16224900
## 2023-01-10 15470.00 15472.45 15262.10 15325.25 16031600
## 2023-01-11 15342.75 15375.10 15257.00 15301.35 16188500
## 2023-01-12 15329.70 15347.20 15203.55 15279.60 15254100
## 2023-01-13 15295.90 15370.05 15215.00 15346.10 16543300
## ...
## 2023-11-09 17284.95 17288.60 17209.55 17218.90 21496900
## 2023-11-10 17189.70 17274.90 17156.85 17256.60 16533000
## 2023-11-13 17335.00 17339.15 17278.65 17318.60 22743900
## 2023-11-15 17493.30 17518.90 17448.30 17508.05 27787100
## 2023-11-16 17518.10 17668.65 17486.50 17593.10 22354800
## 2023-11-17 17537.45 17649.05 17528.85 17600.50 29718400
## 2023-11-20 17618.55 17636.40 17556.10 17573.65 21843200
## 2023-11-21 17644.50 17665.00 17617.20 17627.60 20175000
## 2023-11-22 17637.45 17654.60 17533.75 17627.70 18139100
## 2023-11-23 17654.15 17693.25 17627.35 17639.40 16889200
## CRSLDX.Adjusted
## 2023-01-02 15523.65
## 2023-01-03 15557.80
## 2023-01-04 15393.00
## 2023-01-05 15386.20
## 2023-01-06 15272.00
## 2023-01-09 15450.10
## 2023-01-10 15325.25
## 2023-01-11 15301.35
## 2023-01-12 15279.60
## 2023-01-13 15346.10
## ...
## 2023-11-09 17218.90
## 2023-11-10 17256.60
## 2023-11-13 17318.60
## 2023-11-15 17508.05
## 2023-11-16 17593.10
## 2023-11-17 17600.50
## 2023-11-20 17573.65
## 2023-11-21 17627.60
## 2023-11-22 17627.70
## 2023-11-23 17639.40
##
## [[2]]
## TTKPRESTIG.NS.Open TTKPRESTIG.NS.High TTKPRESTIG.NS.Low
## 2023-01-02 801.00 806.25 791.95
## 2023-01-03 799.50 806.00 796.15
## 2023-01-04 806.40 806.40 792.35
## 2023-01-05 809.00 809.00 789.35
## 2023-01-06 796.50 813.00 790.55
## 2023-01-09 811.90 812.00 782.00
## 2023-01-10 795.10 798.35 787.40
## 2023-01-11 793.60 797.10 792.00
## 2023-01-12 797.00 808.20 789.90
## 2023-01-13 795.00 798.70 788.00
## ...
## 2023-11-09 796.35 798.00 786.85
## 2023-11-10 790.00 799.70 776.90
## 2023-11-13 803.00 805.00 782.00
## 2023-11-15 794.00 794.00 781.15
## 2023-11-16 784.50 798.90 784.50
## 2023-11-17 793.65 796.00 780.15
## 2023-11-20 787.50 791.60 780.10
## 2023-11-21 786.00 794.90 781.05
## 2023-11-22 782.00 787.90 772.70
## 2023-11-23 781.10 781.75 766.25
## TTKPRESTIG.NS.Close TTKPRESTIG.NS.Volume TTKPRESTIG.NS.Adjusted
## 2023-01-02 798.75 317075 792.7512
## 2023-01-03 802.15 26064 796.1256
## 2023-01-04 794.90 21906 788.9301
## 2023-01-05 792.55 64463 786.5977
## 2023-01-06 800.05 50232 794.0414
## 2023-01-09 794.65 46131 788.6819
## 2023-01-10 789.45 16836 783.5210
## 2023-01-11 793.75 56943 787.7887
## 2023-01-12 794.05 48829 788.0864
## 2023-01-13 790.65 28886 784.7120
## ...
## 2023-11-09 796.30 33941 796.3000
## 2023-11-10 797.95 103968 797.9500
## 2023-11-13 783.70 33434 783.7000
## 2023-11-15 791.55 27708 791.5500
## 2023-11-16 793.65 38820 793.6500
## 2023-11-17 786.25 37823 786.2500
## 2023-11-20 790.05 25898 790.0500
## 2023-11-21 783.95 28264 783.9500
## 2023-11-22 781.10 51896 781.1000
## 2023-11-23 769.65 85464 769.6500
#Let us get the multiple stocks into our study
stocks <- as.xts(data.frame(TTKPRESTIG.NS=TTKPRESTIG.NS[,"TTKPRESTIG.NS.Close"],CRSLDX=CRSLDX[,"CRSLDX.Close"]))
stocks
## TTKPRESTIG.NS.Close CRSLDX.Close
## 2023-01-02 798.75 15523.65
## 2023-01-03 802.15 15557.80
## 2023-01-04 794.90 15393.00
## 2023-01-05 792.55 15386.20
## 2023-01-06 800.05 15272.00
## 2023-01-09 794.65 15450.10
## 2023-01-10 789.45 15325.25
## 2023-01-11 793.75 15301.35
## 2023-01-12 794.05 15279.60
## 2023-01-13 790.65 15346.10
## ...
## 2023-11-09 796.30 17218.90
## 2023-11-10 797.95 17256.60
## 2023-11-13 783.70 17318.60
## 2023-11-15 791.55 17508.05
## 2023-11-16 793.65 17593.10
## 2023-11-17 786.25 17600.50
## 2023-11-20 790.05 17573.65
## 2023-11-21 783.95 17627.60
## 2023-11-22 781.10 17627.70
## 2023-11-23 769.65 17639.40
# Creating a new data frame stocks from closing price of TTK PRESTIGE Ltd and NIFTY 500
# Looking the starting prices of TTK PRESTIGE Ltd and NIFTY 500 Index in stocks data frame
head(stocks)
## TTKPRESTIG.NS.Close CRSLDX.Close
## 2023-01-02 798.75 15523.65
## 2023-01-03 802.15 15557.80
## 2023-01-04 794.90 15393.00
## 2023-01-05 792.55 15386.20
## 2023-01-06 800.05 15272.00
## 2023-01-09 794.65 15450.10
# Creating graph from the variable stocks
plot(as.zoo(stocks), screens=1, lty=1:2, xlab="Date", ylab="Price")
# Adding label to the graph
legend("topleft", c("TTKPRESTIG.NS", "CRSLDX"), lty= 1:2, cex=0.5)
Let us calculate the returns
library(magrittr)
##
## Attaching package: 'magrittr'
## The following object is masked from 'package:purrr':
##
## set_names
## The following object is masked from 'package:tidyr':
##
## extract
stock_return <- apply(stocks, 1, function(x) {x / stocks[1,]}) %>% t %>% as.xts
(stock_return)
## TTKPRESTIG.NS.Close CRSLDX.Close
## 2023-01-02 1.0000000 1.0000000
## 2023-01-03 1.0042567 1.0021998
## 2023-01-04 0.9951800 0.9915838
## 2023-01-05 0.9922379 0.9911458
## 2023-01-06 1.0016275 0.9837892
## 2023-01-09 0.9948670 0.9952620
## 2023-01-10 0.9883568 0.9872195
## 2023-01-11 0.9937402 0.9856799
## 2023-01-12 0.9941158 0.9842788
## 2023-01-13 0.9898592 0.9885626
## ...
## 2023-11-09 0.9969327 1.1092043
## 2023-11-10 0.9989985 1.1116328
## 2023-11-13 0.9811581 1.1156267
## 2023-11-15 0.9909859 1.1278308
## 2023-11-16 0.9936151 1.1333094
## 2023-11-17 0.9843505 1.1337862
## 2023-11-20 0.9891080 1.1320566
## 2023-11-21 0.9814711 1.1355319
## 2023-11-22 0.9779029 1.1355383
## 2023-11-23 0.9635681 1.1362920
head(stock_return)
## TTKPRESTIG.NS.Close CRSLDX.Close
## 2023-01-02 1.0000000 1.0000000
## 2023-01-03 1.0042567 1.0021998
## 2023-01-04 0.9951800 0.9915838
## 2023-01-05 0.9922379 0.9911458
## 2023-01-06 1.0016275 0.9837892
## 2023-01-09 0.9948670 0.9952620
# Making our returns more accurate and insightful perspectives on investment returns
stock_change <- stocks %>% log%>% diff
# Calculating log return to the stock and index
head(stock_change)
## TTKPRESTIG.NS.Close CRSLDX.Close
## 2023-01-02 NA NA
## 2023-01-03 0.004247648 0.0021974153
## 2023-01-04 -0.009079302 -0.0106492474
## 2023-01-05 -0.002960771 -0.0004418442
## 2023-01-06 0.009418631 -0.0074499301
## 2023-01-09 -0.006772414 0.0115943641
# Looking the result or the return calculation
# Creating a graph for the return of TTK PRESTIGE Ltd and NIFTY 500 index
plot(as.zoo(stock_change), screen=1, lty=1:2, xlab="Date", ylab="Price")
# Adding label to the graph
legend("topright", c("TTKPRESTIG.NS", "CRSLDX"), lty= 1:2, cex=0.5)
simple moving average
candleChart(TTKPRESTIG.NS, up.col="green", dn.col="red", theme="white")
# Creating candle chart of returns
addSMA(n=20)
# Adding simple moving average with a period of 20
addSMA(n=c(20,50,200), col = c("green", "red","blue"))
# Adding simple moving average with a period of 20, 50, 200