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 - State Bank of India 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 use 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("SBIN.NS", "^NSEI"), src = "yahoo", from = start, to = end)
## [1] "SBIN.NS" "NSEI"

Exploring the dataset

State Bank of India

head(SBIN.NS)
##            SBIN.NS.Open SBIN.NS.High SBIN.NS.Low SBIN.NS.Close SBIN.NS.Volume
## 2022-04-01       491.00       509.75      491.00        508.20       18527226
## 2022-04-04       509.30       514.90      506.00        512.40       16257176
## 2022-04-05       514.40       516.40      507.70        509.40       13602384
## 2022-04-06       507.00       515.20      506.00        513.95       13738275
## 2022-04-07       510.05       519.90      510.05        514.95       19620958
## 2022-04-08       519.50       519.50      512.35        516.10       13572638
##            SBIN.NS.Adjusted
## 2022-04-01         490.8523
## 2022-04-04         494.9090
## 2022-04-05         492.0114
## 2022-04-06         496.4061
## 2022-04-07         497.3719
## 2022-04-08         498.4827
tail(SBIN.NS)
##            SBIN.NS.Open SBIN.NS.High SBIN.NS.Low SBIN.NS.Close SBIN.NS.Volume
## 2023-03-22       522.20       527.00      520.10        521.55       11619538
## 2023-03-23       519.40       521.00      511.50        512.75       22097792
## 2023-03-24       514.50       514.95      503.45        505.35       14416984
## 2023-03-27       507.00       514.15      501.55        510.15       15674218
## 2023-03-28       511.45       511.75      503.85        508.10       15833599
## 2023-03-29       509.00       518.00      506.30        516.50       24203789
##            SBIN.NS.Adjusted
## 2023-03-22         511.6082
## 2023-03-23         502.9759
## 2023-03-24         495.7170
## 2023-03-27         500.4255
## 2023-03-28         498.4146
## 2023-03-29         506.6544
plot(SBIN.NS[,"SBIN.NS.Close"], main = "SBI PRICE MOVEMENT")

Simple Moving Average

candleChart(SBIN.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(SBIN.NS,NSEI))
## [[1]]
##            SBIN.NS.Open SBIN.NS.High SBIN.NS.Low SBIN.NS.Close SBIN.NS.Volume
## 2022-04-01       491.00       509.75      491.00        508.20       18527226
## 2022-04-04       509.30       514.90      506.00        512.40       16257176
## 2022-04-05       514.40       516.40      507.70        509.40       13602384
## 2022-04-06       507.00       515.20      506.00        513.95       13738275
## 2022-04-07       510.05       519.90      510.05        514.95       19620958
## 2022-04-08       519.50       519.50      512.35        516.10       13572638
## 2022-04-11       513.50       522.00      513.20        514.90       12370288
## 2022-04-12       512.75       516.70      504.35        512.85       14460680
## 2022-04-13       514.30       520.90      513.75        517.70       15440525
## 2022-04-18       512.00       515.00      504.00        509.50       14492183
##        ...                                                                   
## 2023-03-16       518.40       529.35      514.30        524.50       20040008
## 2023-03-17       528.65       531.60      520.10        529.80       18787379
## 2023-03-20       526.60       526.60      515.00        519.20       14275315
## 2023-03-21       522.00       523.45      516.45        521.85       29485180
## 2023-03-22       522.20       527.00      520.10        521.55       11619538
## 2023-03-23       519.40       521.00      511.50        512.75       22097792
## 2023-03-24       514.50       514.95      503.45        505.35       14416984
## 2023-03-27       507.00       514.15      501.55        510.15       15674218
## 2023-03-28       511.45       511.75      503.85        508.10       15833599
## 2023-03-29       509.00       518.00      506.30        516.50       24203789
##            SBIN.NS.Adjusted
## 2022-04-01         490.8523
## 2022-04-04         494.9090
## 2022-04-05         492.0114
## 2022-04-06         496.4061
## 2022-04-07         497.3719
## 2022-04-08         498.4827
## 2022-04-11         497.3237
## 2022-04-12         495.3436
## 2022-04-13         500.0281
## 2022-04-18         492.1080
##        ...                 
## 2023-03-16         514.5020
## 2023-03-17         519.7009
## 2023-03-20         509.3030
## 2023-03-21         511.9024
## 2023-03-22         511.6082
## 2023-03-23         502.9759
## 2023-03-24         495.7170
## 2023-03-27         500.4255
## 2023-03-28         498.4146
## 2023-03-29         506.6544
## 
## [[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(SBIN.NS=SBIN.NS[, "SBIN.NS.Close"], NSEI=NSEI[,"NSEI.Close"]))
stocks
##            SBIN.NS.Close NSEI.Close
## 2022-04-01        508.20   17670.45
## 2022-04-04        512.40   18053.40
## 2022-04-05        509.40   17957.40
## 2022-04-06        513.95   17807.65
## 2022-04-07        514.95   17639.55
## 2022-04-08        516.10   17784.35
## 2022-04-11        514.90   17674.95
## 2022-04-12        512.85   17530.30
## 2022-04-13        517.70   17475.65
## 2022-04-18        509.50   17173.65
##        ...                         
## 2023-03-16        524.50   16985.60
## 2023-03-17        529.80   17100.05
## 2023-03-20        519.20   16988.40
## 2023-03-21        521.85   17107.50
## 2023-03-22        521.55   17151.90
## 2023-03-23        512.75   17076.90
## 2023-03-24        505.35   16945.05
## 2023-03-27        510.15   16985.70
## 2023-03-28        508.10   16951.70
## 2023-03-29        516.50   17080.70
head(stocks)
##            SBIN.NS.Close NSEI.Close
## 2022-04-01        508.20   17670.45
## 2022-04-04        512.40   18053.40
## 2022-04-05        509.40   17957.40
## 2022-04-06        513.95   17807.65
## 2022-04-07        514.95   17639.55
## 2022-04-08        516.10   17784.35
plot(as.zoo(stocks), screens = 1, lty = 1:3, xlab = "Date", ylab = "Price") 
legend("right", c("SBIN.NS", "NSEI"), lty = 1:3, cex = 0.5)

stock_return <- apply(stocks, 1, function(x) {x / stocks[1,]}) %>% t %>% as.xts
stock_return
##            SBIN.NS.Close NSEI.Close
## 2022-04-01     1.0000000  1.0000000
## 2022-04-04     1.0082645  1.0216718
## 2022-04-05     1.0023612  1.0162390
## 2022-04-06     1.0113144  1.0077644
## 2022-04-07     1.0132822  0.9982514
## 2022-04-08     1.0155450  1.0064458
## 2022-04-11     1.0131838  1.0002547
## 2022-04-12     1.0091499  0.9920688
## 2022-04-13     1.0186934  0.9889760
## 2022-04-18     1.0025580  0.9718853
##        ...                         
## 2023-03-16     1.0320740  0.9612432
## 2023-03-17     1.0425029  0.9677202
## 2023-03-20     1.0216450  0.9614017
## 2023-03-21     1.0268594  0.9681418
## 2023-03-22     1.0262691  0.9706545
## 2023-03-23     1.0089531  0.9664101
## 2023-03-24     0.9943920  0.9589485
## 2023-03-27     1.0038370  0.9612489
## 2023-03-28     0.9998032  0.9593247
## 2023-03-29     1.0163321  0.9666251
head(stock_return)
##            SBIN.NS.Close NSEI.Close
## 2022-04-01      1.000000  1.0000000
## 2022-04-04      1.008264  1.0216718
## 2022-04-05      1.002361  1.0162390
## 2022-04-06      1.011314  1.0077644
## 2022-04-07      1.013282  0.9982514
## 2022-04-08      1.015545  1.0064458
plot(as.zoo(stock_return), screens = 1, lty = 1:3, xlab = "Date", ylab = "Price") 
legend("topleft", c("SBIN.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)
##            SBIN.NS.Close   NSEI.Close
## 2022-04-01            NA           NA
## 2022-04-04   0.008230523  0.021440346
## 2022-04-05  -0.005872067 -0.005331746
## 2022-04-06   0.008892458 -0.008374146
## 2022-04-07   0.001943824 -0.009484578
## 2022-04-08   0.002230666  0.008175251
plot(as.zoo(stock_return), screens = 1, lty = 1:3, xlab = "Date", ylab = "Price") 
legend("topleft", c("SBIN.NS", "NSEI"), lty = 1:3, cex = 0.5)