Short-run performance analysis using Technical Analysis on the selected stocks

We will be fetching the data from the website (Yahoo) on selected stock.

Selected stocks - ICICI Bank Limited and Nifty 50

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

Let us start date and end date to fetch the data from Yahoo

start <- as.Date("2022-04-01")
end <- as.Date("2023-03-31")
getSymbols(c("ICICIBANK.NS", "^NSEI"), src = "yahoo", from = start, to = end)
## [1] "ICICIBANK.NS" "NSEI"

Exploring the dataset

ICICI Bank LIMITED

head(ICICIBANK.NS)
##            ICICIBANK.NS.Open ICICIBANK.NS.High ICICIBANK.NS.Low
## 2022-04-01            725.00            738.30            723.1
## 2022-04-04            742.00            749.80            725.0
## 2022-04-05            746.80            755.45            738.0
## 2022-04-06            732.30            744.90            732.1
## 2022-04-07            737.10            757.80            737.1
## 2022-04-08            749.85            756.00            743.6
##            ICICIBANK.NS.Close ICICIBANK.NS.Volume ICICIBANK.NS.Adjusted
## 2022-04-01             736.25            12658848              725.8832
## 2022-04-04             746.60            17192197              736.0874
## 2022-04-05             741.80            12290391              731.3550
## 2022-04-06             741.60            13518142              731.1578
## 2022-04-07             748.55            15087175              738.0100
## 2022-04-08             754.30            15480705              743.6790
tail(ICICIBANK.NS)
##            ICICIBANK.NS.Open ICICIBANK.NS.High ICICIBANK.NS.Low
## 2023-03-22            857.65            862.55           851.00
## 2023-03-23            857.00            867.45           852.80
## 2023-03-24            852.50            860.00           849.15
## 2023-03-27            854.95            856.40           844.05
## 2023-03-28            855.55            858.90           848.20
## 2023-03-29            857.05            862.10           848.00
##            ICICIBANK.NS.Close ICICIBANK.NS.Volume ICICIBANK.NS.Adjusted
## 2023-03-22             860.20            17431520              853.1772
## 2023-03-23             855.45            17920621              848.4660
## 2023-03-24             852.40            17295696              845.4409
## 2023-03-27             848.70            17047675              841.7711
## 2023-03-28             854.75            24469018              847.7717
## 2023-03-29             851.00            34881515              844.0524
plot(ICICIBANK.NS[,"ICICIBANK.NS.Close"], main = "ICICIBANK PRICE MOVEMENT")

Simple Moving Average

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

addSMA(n=20)

addSMA(n=c(20,50,200), col = c("green", "red", "blue"))

Exploring the dataset

NIFTY 50

head(NSEI)
##            NSEI.Open NSEI.High NSEI.Low NSEI.Close NSEI.Volume NSEI.Adjusted
## 2022-04-01  17436.90  17703.70 17422.70   17670.45      291800      17670.45
## 2022-04-04  17809.10  18114.65 17791.40   18053.40      345500      18053.40
## 2022-04-05  18080.60  18095.45 17921.55   17957.40      283500      17957.40
## 2022-04-06  17842.75  17901.00 17779.85   17807.65      328800      17807.65
## 2022-04-07  17723.30  17787.50 17623.70   17639.55      308800      17639.55
## 2022-04-08  17698.15  17842.75 17600.55   17784.35      274400      17784.35
tail(NSEI)
##            NSEI.Open NSEI.High NSEI.Low NSEI.Close NSEI.Volume NSEI.Adjusted
## 2023-03-22  17177.45  17207.25 17107.85   17151.90           0      17151.90
## 2023-03-23  17097.40  17205.40 17045.30   17076.90      219200      17076.90
## 2023-03-24  17076.20  17109.45 16917.35   16945.05      228000      16945.05
## 2023-03-27  16984.30  17091.00 16918.55   16985.70      218400      16985.70
## 2023-03-28  17031.75  17061.75 16913.75   16951.70      238800      16951.70
## 2023-03-29  16977.30  17126.15 16940.60   17080.70      345900      17080.70
plot(NSEI[,"NSEI.Close"],  main = "NIFTY 50 PRICE MOVEMENT")

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

addSMA(n=c(20,50,200), col = c("green", "red", "blue"))

Let us compare two stocks

head(list(ICICIBANK.NS,NSEI))
## [[1]]
##            ICICIBANK.NS.Open ICICIBANK.NS.High ICICIBANK.NS.Low
## 2022-04-01            725.00            738.30           723.10
## 2022-04-04            742.00            749.80           725.00
## 2022-04-05            746.80            755.45           738.00
## 2022-04-06            732.30            744.90           732.10
## 2022-04-07            737.10            757.80           737.10
## 2022-04-08            749.85            756.00           743.60
## 2022-04-11            752.30            765.45           751.65
## 2022-04-12            757.00            768.90           750.25
## 2022-04-13            769.60            772.55           760.70
## 2022-04-18            751.00            762.90           749.20
##        ...                                                     
## 2023-03-16            829.70            831.30           810.30
## 2023-03-17            831.80            838.40           818.65
## 2023-03-20            837.00            837.80           825.00
## 2023-03-21            843.00            854.50           839.05
## 2023-03-22            857.65            862.55           851.00
## 2023-03-23            857.00            867.45           852.80
## 2023-03-24            852.50            860.00           849.15
## 2023-03-27            854.95            856.40           844.05
## 2023-03-28            855.55            858.90           848.20
## 2023-03-29            857.05            862.10           848.00
##            ICICIBANK.NS.Close ICICIBANK.NS.Volume ICICIBANK.NS.Adjusted
## 2022-04-01             736.25            12658848              725.8832
## 2022-04-04             746.60            17192197              736.0874
## 2022-04-05             741.80            12290391              731.3550
## 2022-04-06             741.60            13518142              731.1578
## 2022-04-07             748.55            15087175              738.0100
## 2022-04-08             754.30            15480705              743.6790
## 2022-04-11             759.60            13216391              748.9044
## 2022-04-12             763.85            14020000              753.0945
## 2022-04-13             762.25            16119956              751.5171
## 2022-04-18             757.80            17651530              747.1298
##        ...                                                             
## 2023-03-16             823.90            14987868              817.1736
## 2023-03-17             836.25            11104490              829.4228
## 2023-03-20             836.45            14838651              829.6212
## 2023-03-21             852.40            18755854              845.4409
## 2023-03-22             860.20            17431520              853.1772
## 2023-03-23             855.45            17920621              848.4660
## 2023-03-24             852.40            17295696              845.4409
## 2023-03-27             848.70            17047675              841.7711
## 2023-03-28             854.75            24469018              847.7717
## 2023-03-29             851.00            34881515              844.0524
## 
## [[2]]
##            NSEI.Open NSEI.High NSEI.Low NSEI.Close NSEI.Volume NSEI.Adjusted
## 2022-04-01  17436.90  17703.70 17422.70   17670.45      291800      17670.45
## 2022-04-04  17809.10  18114.65 17791.40   18053.40      345500      18053.40
## 2022-04-05  18080.60  18095.45 17921.55   17957.40      283500      17957.40
## 2022-04-06  17842.75  17901.00 17779.85   17807.65      328800      17807.65
## 2022-04-07  17723.30  17787.50 17623.70   17639.55      308800      17639.55
## 2022-04-08  17698.15  17842.75 17600.55   17784.35      274400      17784.35
## 2022-04-11  17740.90  17779.05 17650.95   17674.95      251700      17674.95
## 2022-04-12  17584.85  17595.30 17442.35   17530.30      266000      17530.30
## 2022-04-13  17599.90  17663.65 17457.40   17475.65      245100      17475.65
## 2022-04-18  17183.45  17237.75 17067.85   17173.65      376100      17173.65
##        ...                                                                  
## 2023-03-16  16994.65  17062.45 16850.15   16985.60      349800      16985.60
## 2023-03-17  17111.80  17145.80 16958.15   17100.05      408100      17100.05
## 2023-03-20  17066.60  17066.60 16828.35   16988.40      241800      16988.40
## 2023-03-21  17060.40  17127.70 17016.00   17107.50      246700      17107.50
## 2023-03-22  17177.45  17207.25 17107.85   17151.90           0      17151.90
## 2023-03-23  17097.40  17205.40 17045.30   17076.90      219200      17076.90
## 2023-03-24  17076.20  17109.45 16917.35   16945.05      228000      16945.05
## 2023-03-27  16984.30  17091.00 16918.55   16985.70      218400      16985.70
## 2023-03-28  17031.75  17061.75 16913.75   16951.70      238800      16951.70
## 2023-03-29  16977.30  17126.15 16940.60   17080.70      345900      17080.70
stocks <- as.xts(data.frame(ICICIBANK.NS=ICICIBANK.NS[, "ICICIBANK.NS.Close"], NSEI=NSEI[,"NSEI.Close"]))
stocks
##            ICICIBANK.NS.Close NSEI.Close
## 2022-04-01             736.25   17670.45
## 2022-04-04             746.60   18053.40
## 2022-04-05             741.80   17957.40
## 2022-04-06             741.60   17807.65
## 2022-04-07             748.55   17639.55
## 2022-04-08             754.30   17784.35
## 2022-04-11             759.60   17674.95
## 2022-04-12             763.85   17530.30
## 2022-04-13             762.25   17475.65
## 2022-04-18             757.80   17173.65
##        ...                              
## 2023-03-16             823.90   16985.60
## 2023-03-17             836.25   17100.05
## 2023-03-20             836.45   16988.40
## 2023-03-21             852.40   17107.50
## 2023-03-22             860.20   17151.90
## 2023-03-23             855.45   17076.90
## 2023-03-24             852.40   16945.05
## 2023-03-27             848.70   16985.70
## 2023-03-28             854.75   16951.70
## 2023-03-29             851.00   17080.70
head(stocks)
##            ICICIBANK.NS.Close NSEI.Close
## 2022-04-01             736.25   17670.45
## 2022-04-04             746.60   18053.40
## 2022-04-05             741.80   17957.40
## 2022-04-06             741.60   17807.65
## 2022-04-07             748.55   17639.55
## 2022-04-08             754.30   17784.35
plot(as.zoo(stocks), screens = 1, lty = 1:3, xlab = "Date", ylab = "Price") 
legend("right", c("ICICIBANK.NS", "NSEI"), lty = 1:3, cex = 0.5)

stock_return <- apply(stocks, 1, function(x) {x / stocks[1,]}) %>% t %>% as.xts
stock_return
##            ICICIBANK.NS.Close NSEI.Close
## 2022-04-01           1.000000  1.0000000
## 2022-04-04           1.014058  1.0216718
## 2022-04-05           1.007538  1.0162390
## 2022-04-06           1.007267  1.0077644
## 2022-04-07           1.016706  0.9982514
## 2022-04-08           1.024516  1.0064458
## 2022-04-11           1.031715  1.0002547
## 2022-04-12           1.037487  0.9920688
## 2022-04-13           1.035314  0.9889760
## 2022-04-18           1.029270  0.9718853
##        ...                              
## 2023-03-16           1.119049  0.9612432
## 2023-03-17           1.135823  0.9677202
## 2023-03-20           1.136095  0.9614017
## 2023-03-21           1.157759  0.9681418
## 2023-03-22           1.168353  0.9706545
## 2023-03-23           1.161902  0.9664101
## 2023-03-24           1.157759  0.9589485
## 2023-03-27           1.152733  0.9612489
## 2023-03-28           1.160951  0.9593247
## 2023-03-29           1.155857  0.9666251
head(stock_return)
##            ICICIBANK.NS.Close NSEI.Close
## 2022-04-01           1.000000  1.0000000
## 2022-04-04           1.014058  1.0216718
## 2022-04-05           1.007538  1.0162390
## 2022-04-06           1.007267  1.0077644
## 2022-04-07           1.016706  0.9982514
## 2022-04-08           1.024516  1.0064458
plot(as.zoo(stock_return), screens = 1, lty = 1:3, xlab = "Date", ylab = "Price") 
legend("topleft", c("ICICIBANK.NS", "NSEI"), lty = 1:3, cex = 0.5)

Making our returns more accurate and insightful perspective on investment returns

stock_change <- stocks %>% log%>% diff
head(stock_change)
##            ICICIBANK.NS.Close   NSEI.Close
## 2022-04-01                 NA           NA
## 2022-04-04       0.0139597988  0.021440346
## 2022-04-05      -0.0064498852 -0.005331746
## 2022-04-06      -0.0002696673 -0.008374146
## 2022-04-07       0.0093280043 -0.009484578
## 2022-04-08       0.0076521651  0.008175251
plot(as.zoo(stock_return), screens = 1, lty = 1:3, xlab = "Date", ylab = "Price") 
legend("topleft", c("ICICIBANK.NS", "NSEI"), lty = 1:3, cex = 0.5)