Short - Run performance analysis Using Technical Analysis on the VBL

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.0
## ✔ 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

set start date and end date to fetch the data from the yahoo

start <- as.Date("2023-01-01")
end <- as.Date("2023-11-24")
getSymbols("VBL.NS", src = "yahoo", from=start, to=end)
## [1] "VBL.NS"

Exploring the Dataset

head(VBL.NS)
##            VBL.NS.Open VBL.NS.High VBL.NS.Low VBL.NS.Close VBL.NS.Volume
## 2023-01-02     669.725     678.750    659.000      666.550       1680232
## 2023-01-03     666.550     669.850    650.550      653.350       1565120
## 2023-01-04     656.750     664.250    650.000      654.275       1579838
## 2023-01-05     656.975     665.450    635.500      654.425       3505460
## 2023-01-06     657.475     659.800    638.625      650.450       2836868
## 2023-01-09     655.875     662.275    648.525      654.375       2064148
##            VBL.NS.Adjusted
## 2023-01-02        664.6436
## 2023-01-03        651.4813
## 2023-01-04        652.4037
## 2023-01-05        652.5532
## 2023-01-06        648.5897
## 2023-01-09        652.5034
tail(VBL.NS)
##            VBL.NS.Open VBL.NS.High VBL.NS.Low VBL.NS.Close VBL.NS.Volume
## 2023-11-16     1016.50     1051.00    1015.60      1044.55       3074266
## 2023-11-17     1044.55     1047.30    1021.55      1043.45       1843610
## 2023-11-20     1042.00     1060.35    1028.00      1047.35       1052356
## 2023-11-21     1054.95     1057.30    1022.75      1037.80       2046220
## 2023-11-22     1040.35     1041.00    1016.25      1031.35       1370191
## 2023-11-23     1035.15     1075.00    1025.00      1054.60       2439087
##            VBL.NS.Adjusted
## 2023-11-16         1044.55
## 2023-11-17         1043.45
## 2023-11-20         1047.35
## 2023-11-21         1037.80
## 2023-11-22         1031.35
## 2023-11-23         1054.60
plot(VBL.NS[,"VBL.NS.Close"], main= "VBL CLOSING PRICE MOVEMENT")

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

Added the NIFTY 50 Index

getSymbols(c("VBL.NS", "^NSEI"), src = "yahoo", from=start, to=end)
## [1] "VBL.NS" "NSEI"
head(list(VBL.NS,NSEI))
## [[1]]
##            VBL.NS.Open VBL.NS.High VBL.NS.Low VBL.NS.Close VBL.NS.Volume
## 2023-01-02     669.725     678.750    659.000      666.550       1680232
## 2023-01-03     666.550     669.850    650.550      653.350       1565120
## 2023-01-04     656.750     664.250    650.000      654.275       1579838
## 2023-01-05     656.975     665.450    635.500      654.425       3505460
## 2023-01-06     657.475     659.800    638.625      650.450       2836868
## 2023-01-09     655.875     662.275    648.525      654.375       2064148
## 2023-01-10     655.800     656.500    639.500      643.650       1764800
## 2023-01-11     647.000     647.100    607.550      612.525       4534312
## 2023-01-12     614.975     614.975    585.500      593.550       4813534
## 2023-01-13     597.725     597.725    561.325      567.875       7276072
##        ...                                                              
## 2023-11-09    1015.000    1015.000    996.700     1002.550       1256722
## 2023-11-10    1003.800    1014.250    993.700     1009.050       1218861
## 2023-11-13    1012.000    1016.250   1002.350     1008.750       1299695
## 2023-11-15    1013.000    1019.000    998.000     1014.200       2353248
## 2023-11-16    1016.500    1051.000   1015.600     1044.550       3074266
## 2023-11-17    1044.550    1047.300   1021.550     1043.450       1843610
## 2023-11-20    1042.000    1060.350   1028.000     1047.350       1052356
## 2023-11-21    1054.950    1057.300   1022.750     1037.800       2046220
## 2023-11-22    1040.350    1041.000   1016.250     1031.350       1370191
## 2023-11-23    1035.150    1075.000   1025.000     1054.600       2439087
##            VBL.NS.Adjusted
## 2023-01-02        664.6436
## 2023-01-03        651.4813
## 2023-01-04        652.4037
## 2023-01-05        652.5532
## 2023-01-06        648.5896
## 2023-01-09        652.5034
## 2023-01-10        641.8091
## 2023-01-11        610.7731
## 2023-01-12        591.8524
## 2023-01-13        566.2508
##        ...                
## 2023-11-09       1002.5500
## 2023-11-10       1009.0500
## 2023-11-13       1008.7500
## 2023-11-15       1014.2000
## 2023-11-16       1044.5500
## 2023-11-17       1043.4500
## 2023-11-20       1047.3500
## 2023-11-21       1037.8000
## 2023-11-22       1031.3500
## 2023-11-23       1054.6000
## 
## [[2]]
##            NSEI.Open NSEI.High NSEI.Low NSEI.Close NSEI.Volume NSEI.Adjusted
## 2023-01-02  18131.70  18215.15 18086.50   18197.45      256100      18197.45
## 2023-01-03  18163.20  18251.95 18149.80   18232.55      208700      18232.55
## 2023-01-04  18230.65  18243.00 18020.60   18042.95      235200      18042.95
## 2023-01-05  18101.95  18120.30 17892.60   17992.15      269900      17992.15
## 2023-01-06  18008.05  18047.40 17795.55   17859.45      238200      17859.45
## 2023-01-09  17952.55  18141.40 17936.15   18101.20      257200      18101.20
## 2023-01-10  18121.30  18127.60 17856.00   17914.15      283300      17914.15
## 2023-01-11  17924.25  17976.35 17824.35   17895.70      259900      17895.70
## 2023-01-12  17920.85  17945.80 17761.65   17858.20      227800      17858.20
## 2023-01-13  17867.50  17999.35 17774.25   17956.60      256700      17956.60
##        ...                                                                  
## 2023-11-09  19457.40  19463.90 19378.35   19395.30      208400      19395.30
## 2023-11-10  19351.85  19451.30 19329.45   19425.35      152200      19425.35
## 2023-11-13  19486.75  19494.40 19414.75   19443.55      189300      19443.55
## 2023-11-15  19651.40  19693.20 19579.65   19675.45      291500      19675.45
## 2023-11-16  19674.70  19875.25 19627.00   19765.20      282700      19765.20
## 2023-11-17  19674.75  19806.00 19667.45   19731.80      236800      19731.80
## 2023-11-20  19731.15  19756.45 19670.50   19694.00      173800      19694.00
## 2023-11-21  19770.90  19829.10 19754.05   19783.40      195000      19783.40
## 2023-11-22  19784.00  19825.55 19703.85   19811.85      184500      19811.85
## 2023-11-23  19828.45  19875.15 19786.75   19802.00      170000      19802.00

Let us get the stock and Index into our study

stocks <- as.xts(data.frame(VBL.NS=VBL.NS[,"VBL.NS.Close"], NSEI=NSEI[,"NSEI.Close"]))
stocks
##            VBL.NS.Close NSEI.Close
## 2023-01-02      666.550   18197.45
## 2023-01-03      653.350   18232.55
## 2023-01-04      654.275   18042.95
## 2023-01-05      654.425   17992.15
## 2023-01-06      650.450   17859.45
## 2023-01-09      654.375   18101.20
## 2023-01-10      643.650   17914.15
## 2023-01-11      612.525   17895.70
## 2023-01-12      593.550   17858.20
## 2023-01-13      567.875   17956.60
##        ...                        
## 2023-11-09     1002.550   19395.30
## 2023-11-10     1009.050   19425.35
## 2023-11-13     1008.750   19443.55
## 2023-11-15     1014.200   19675.45
## 2023-11-16     1044.550   19765.20
## 2023-11-17     1043.450   19731.80
## 2023-11-20     1047.350   19694.00
## 2023-11-21     1037.800   19783.40
## 2023-11-22     1031.350   19811.85
## 2023-11-23     1054.600   19802.00
head(stocks)
##            VBL.NS.Close NSEI.Close
## 2023-01-02      666.550   18197.45
## 2023-01-03      653.350   18232.55
## 2023-01-04      654.275   18042.95
## 2023-01-05      654.425   17992.15
## 2023-01-06      650.450   17859.45
## 2023-01-09      654.375   18101.20
plot(as.zoo(stocks), screens = 1, lty = 1:3, xlab="Date", ylab="Price")
legend("right", c("VBL.NS", "NSEI"), lty = 1:3, cex = 0.5)

Let us calculate the return

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
head(stock_return)
##            VBL.NS.Close NSEI.Close
## 2023-01-02    1.0000000  1.0000000
## 2023-01-03    0.9801965  1.0019289
## 2023-01-04    0.9815843  0.9915098
## 2023-01-05    0.9818093  0.9887183
## 2023-01-06    0.9758458  0.9814260
## 2023-01-09    0.9817343  0.9947108
plot(as.zoo(stock_return), screens = 1, lty = 1:3, xlab="Date", ylab="Price")
legend("left", c("VBL.NS", "NSEI"), lty = 1:3, cex = 0.5)

Making are return more accurate and insightful on investment return

stock_change <- stocks %>% log%>% diff
head(stock_change)
##             VBL.NS.Close   NSEI.Close
## 2023-01-02            NA           NA
## 2023-01-03 -0.0200022012  0.001927070
## 2023-01-04  0.0014148536 -0.010453518
## 2023-01-05  0.0002291792 -0.002819410
## 2023-01-06 -0.0060925190 -0.007402837
## 2023-01-09  0.0060161318  0.013445455
plot(as.zoo(stock_change), screens = 1, lty = 1:3, xlab="Date", ylab="Price")
legend("top", c("VBL.NS", "NSEI"), lty = 1:3, cex = 0.5)

Simple Moving average

candleChart(VBL.NS, up.col="green", dn.col="red", theme = "white", subset = "2023-01-01/")

addSMA(n=20)

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