Short-run performance analysis using Technical Analysis on the selected stock and benchmark index

We will be fetching the data (historical prices) from the website (Yahoo) on selected stocks

Selected stocks - DABUR INDIA LTD and Nifty 500

# 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 DABUR INDIA LTD from yahoo finance from start date to end date

getSymbols("DABUR.NS", src = "yahoo", from = start, to=end)     
## [1] "DABUR.NS"

Exploring the data set

head(DABUR.NS)
##            DABUR.NS.Open DABUR.NS.High DABUR.NS.Low DABUR.NS.Close
## 2023-01-02        561.45        563.45       558.10         561.55
## 2023-01-03        564.30        568.35       557.25         560.85
## 2023-01-04        562.50        564.50       557.80         559.85
## 2023-01-05        561.10        574.15       561.00         572.75
## 2023-01-06        573.15        573.95       548.50         552.85
## 2023-01-09        556.00        556.00       545.70         549.55
##            DABUR.NS.Volume DABUR.NS.Adjusted
## 2023-01-02          564453          556.0553
## 2023-01-03          904153          555.3622
## 2023-01-04         1223101          554.3719
## 2023-01-05         2641778          567.1457
## 2023-01-06         2789534          547.4404
## 2023-01-09         3024289          544.1727
tail(DABUR.NS)
##            DABUR.NS.Open DABUR.NS.High DABUR.NS.Low DABUR.NS.Close
## 2023-11-16        532.25        535.40       526.20         534.90
## 2023-11-17        535.80        542.60       531.65         540.70
## 2023-11-20        540.70        545.45       537.20         543.20
## 2023-11-21        545.65        545.65       539.25         540.85
## 2023-11-22        540.70        541.95       537.55         541.00
## 2023-11-23        541.00        542.00       535.25         540.35
##            DABUR.NS.Volume DABUR.NS.Adjusted
## 2023-11-16         1612231            534.90
## 2023-11-17         1512369            540.70
## 2023-11-20         1910044            543.20
## 2023-11-21         1379849            540.85
## 2023-11-22          680568            541.00
## 2023-11-23          800753            540.35
# Viewing the stock price of DABUR INDIA LTD for first and last 6 days
# Viewing the line graph of DABUR INDIA LTD from start date and end date


plot(DABUR.NS[,"DABUR.NS.Close"], main = "DABUR INDIA LTD CLOSING PRICE MOVEMENT")

# Creating candle stick chart of DABUR INDIA LTD stock price

candleChart(DABUR.NS, up.col="green",dn.col="red",theme = "white")

# Let us get compare DABUR INDIA LTD with Bench mark NIFTY 500

getSymbols(c("^CRSLDX","DABUR.NS"), src = "yahoo", from =start,to=end)
## [1] "CRSLDX"   "DABUR.NS"
# Getting the stock price of DABUR INDIA LTD and NIFTY 500
# Looking the starting prices of DABUR INDIA LTD and NIFTY 500 Index

head(list(CRSLDX,DABUR.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]]
##            DABUR.NS.Open DABUR.NS.High DABUR.NS.Low DABUR.NS.Close
## 2023-01-02        561.45        563.45       558.10         561.55
## 2023-01-03        564.30        568.35       557.25         560.85
## 2023-01-04        562.50        564.50       557.80         559.85
## 2023-01-05        561.10        574.15       561.00         572.75
## 2023-01-06        573.15        573.95       548.50         552.85
## 2023-01-09        556.00        556.00       545.70         549.55
## 2023-01-10        550.20        555.20       547.50         554.60
## 2023-01-11        554.60        555.20       545.10         548.35
## 2023-01-12        547.80        549.30       541.90         544.95
## 2023-01-13        545.50        549.90       538.55         543.70
##        ...                                                        
## 2023-11-09        540.00        540.00       533.00         534.85
## 2023-11-10        532.65        535.00       526.70         533.35
## 2023-11-13        535.50        535.80       525.95         530.10
## 2023-11-15        523.05        527.50       516.70         526.00
## 2023-11-16        532.25        535.40       526.20         534.90
## 2023-11-17        535.80        542.60       531.65         540.70
## 2023-11-20        540.70        545.45       537.20         543.20
## 2023-11-21        545.65        545.65       539.25         540.85
## 2023-11-22        540.70        541.95       537.55         541.00
## 2023-11-23        541.00        542.00       535.25         540.35
##            DABUR.NS.Volume DABUR.NS.Adjusted
## 2023-01-02          564453          556.0554
## 2023-01-03          904153          555.3621
## 2023-01-04         1223101          554.3719
## 2023-01-05         2641778          567.1458
## 2023-01-06         2789534          547.4404
## 2023-01-09         3024289          544.1727
## 2023-01-10         1156889          549.1733
## 2023-01-11         1862522          542.9844
## 2023-01-12         1973109          539.6178
## 2023-01-13         3143762          538.3799
##        ...                                  
## 2023-11-09         1366713          532.1000
## 2023-11-10         1361649          533.3500
## 2023-11-13          979880          530.1000
## 2023-11-15         3398705          526.0000
## 2023-11-16         1612231          534.9000
## 2023-11-17         1512369          540.7000
## 2023-11-20         1910044          543.2000
## 2023-11-21         1379849          540.8500
## 2023-11-22          680568          541.0000
## 2023-11-23          800753          540.3500

Let us get the multiple stocks into our study

stocks <- as.xts(data.frame(DABUR.NS=DABUR.NS[,"DABUR.NS.Close"],CRSLDX=CRSLDX[,"CRSLDX.Close"]))
stocks
##            DABUR.NS.Close CRSLDX.Close
## 2023-01-02         561.55     15523.65
## 2023-01-03         560.85     15557.80
## 2023-01-04         559.85     15393.00
## 2023-01-05         572.75     15386.20
## 2023-01-06         552.85     15272.00
## 2023-01-09         549.55     15450.10
## 2023-01-10         554.60     15325.25
## 2023-01-11         548.35     15301.35
## 2023-01-12         544.95     15279.60
## 2023-01-13         543.70     15346.10
##        ...                            
## 2023-11-09         534.85     17218.90
## 2023-11-10         533.35     17256.60
## 2023-11-13         530.10     17318.60
## 2023-11-15         526.00     17508.05
## 2023-11-16         534.90     17593.10
## 2023-11-17         540.70     17600.50
## 2023-11-20         543.20     17573.65
## 2023-11-21         540.85     17627.60
## 2023-11-22         541.00     17627.70
## 2023-11-23         540.35     17639.40
# Creating a new data frame stocks from closing price of DABUR INDIA LTD and NIFTY 500

# Looking the starting prices of DABUR INDIA LTD and NIFTY 500 Index in stocks data frame

head(stocks)
##            DABUR.NS.Close CRSLDX.Close
## 2023-01-02         561.55     15523.65
## 2023-01-03         560.85     15557.80
## 2023-01-04         559.85     15393.00
## 2023-01-05         572.75     15386.20
## 2023-01-06         552.85     15272.00
## 2023-01-09         549.55     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("DABUR.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)
##            DABUR.NS.Close CRSLDX.Close
## 2023-01-02      1.0000000    1.0000000
## 2023-01-03      0.9987534    1.0021998
## 2023-01-04      0.9969726    0.9915838
## 2023-01-05      1.0199448    0.9911458
## 2023-01-06      0.9845071    0.9837892
## 2023-01-09      0.9786306    0.9952620
## 2023-01-10      0.9876235    0.9872195
## 2023-01-11      0.9764936    0.9856799
## 2023-01-12      0.9704390    0.9842788
## 2023-01-13      0.9682130    0.9885626
##        ...                            
## 2023-11-09      0.9524530    1.1092043
## 2023-11-10      0.9497818    1.1116328
## 2023-11-13      0.9439943    1.1156267
## 2023-11-15      0.9366931    1.1278308
## 2023-11-16      0.9525421    1.1333094
## 2023-11-17      0.9628707    1.1337862
## 2023-11-20      0.9673226    1.1320566
## 2023-11-21      0.9631377    1.1355319
## 2023-11-22      0.9634049    1.1355383
## 2023-11-23      0.9622473    1.1362920
head(stock_return)
##            DABUR.NS.Close CRSLDX.Close
## 2023-01-02      1.0000000    1.0000000
## 2023-01-03      0.9987534    1.0021998
## 2023-01-04      0.9969726    0.9915838
## 2023-01-05      1.0199448    0.9911458
## 2023-01-06      0.9845071    0.9837892
## 2023-01-09      0.9786306    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)
##            DABUR.NS.Close  CRSLDX.Close
## 2023-01-02             NA            NA
## 2023-01-03   -0.001247349  0.0021974153
## 2023-01-04   -0.001784599 -0.0106492474
## 2023-01-05    0.022780474 -0.0004418442
## 2023-01-06   -0.035362649 -0.0074499301
## 2023-01-09   -0.005986934  0.0115943641
# Looking the result or the return calculation


# Creating a graph for the return of DABUR INDIA 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("DABUR.NS", "CRSLDX"), lty= 1:2, cex=0.5)

simple moving average

candleChart(DABUR.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