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 - Maruti Suzuki India 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("MARUTI.NS", "^NSEI"), src = "yahoo", from = start, to = end)
## [1] "MARUTI.NS" "NSEI"
Exploring the dataset
MARUTI SUZUKI INDIA LIMITED
head(MARUTI.NS)
## MARUTI.NS.Open MARUTI.NS.High MARUTI.NS.Low MARUTI.NS.Close
## 2022-04-01 7574.00 7729.0 7517.35 7700.05
## 2022-04-04 7650.00 7794.0 7631.65 7774.80
## 2022-04-05 7781.00 7900.0 7730.00 7761.85
## 2022-04-06 7739.00 7849.0 7675.00 7744.20
## 2022-04-07 7659.75 7748.0 7621.05 7640.50
## 2022-04-08 7708.80 7730.5 7515.40 7557.95
## MARUTI.NS.Volume MARUTI.NS.Adjusted
## 2022-04-01 449386 7578.368
## 2022-04-04 491029 7651.938
## 2022-04-05 488132 7639.192
## 2022-04-06 393628 7621.821
## 2022-04-07 458566 7519.760
## 2022-04-08 849846 7438.515
tail(MARUTI.NS)
## MARUTI.NS.Open MARUTI.NS.High MARUTI.NS.Low MARUTI.NS.Close
## 2023-03-22 8236.00 8289.6 8217.00 8252.40
## 2023-03-23 8264.00 8375.0 8232.60 8353.20
## 2023-03-24 8353.20 8386.5 8211.00 8242.90
## 2023-03-27 8250.00 8316.8 8229.55 8278.35
## 2023-03-28 8305.90 8355.6 8140.00 8208.80
## 2023-03-29 8249.85 8319.1 8187.50 8295.30
## MARUTI.NS.Volume MARUTI.NS.Adjusted
## 2023-03-22 218120 8175.465
## 2023-03-23 540553 8275.325
## 2023-03-24 391840 8166.054
## 2023-03-27 364326 8201.173
## 2023-03-28 360349 8132.271
## 2023-03-29 339851 8217.965
plot(MARUTI.NS[,"MARUTI.NS.Close"], main = "MARUTI PRICE MOVEMENT")

Simple Moving Average
candleChart(MARUTI.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(MARUTI.NS,NSEI))
## [[1]]
## MARUTI.NS.Open MARUTI.NS.High MARUTI.NS.Low MARUTI.NS.Close
## 2022-04-01 7574.00 7729.00 7517.35 7700.05
## 2022-04-04 7650.00 7794.00 7631.65 7774.80
## 2022-04-05 7781.00 7900.00 7730.00 7761.85
## 2022-04-06 7739.00 7849.00 7675.00 7744.20
## 2022-04-07 7659.75 7748.00 7621.05 7640.50
## 2022-04-08 7708.80 7730.50 7515.40 7557.95
## 2022-04-11 7614.85 7616.55 7485.00 7565.40
## 2022-04-12 7540.00 7711.50 7500.00 7618.35
## 2022-04-13 7618.35 7704.95 7440.00 7469.50
## 2022-04-18 7374.50 7599.00 7355.00 7573.50
## ...
## 2023-03-16 8460.10 8533.40 8411.00 8437.45
## 2023-03-17 8503.00 8510.95 8293.55 8316.85
## 2023-03-20 8316.85 8316.85 8130.00 8219.60
## 2023-03-21 8245.00 8275.00 8215.00 8228.40
## 2023-03-22 8236.00 8289.60 8217.00 8252.40
## 2023-03-23 8264.00 8375.00 8232.60 8353.20
## 2023-03-24 8353.20 8386.50 8211.00 8242.90
## 2023-03-27 8250.00 8316.80 8229.55 8278.35
## 2023-03-28 8305.90 8355.60 8140.00 8208.80
## 2023-03-29 8249.85 8319.10 8187.50 8295.30
## MARUTI.NS.Volume MARUTI.NS.Adjusted
## 2022-04-01 449386 7578.368
## 2022-04-04 491029 7651.938
## 2022-04-05 488132 7639.192
## 2022-04-06 393628 7621.821
## 2022-04-07 458566 7519.760
## 2022-04-08 849846 7438.515
## 2022-04-11 496254 7445.846
## 2022-04-12 598047 7497.959
## 2022-04-13 527991 7351.462
## 2022-04-18 551783 7453.818
## ...
## 2023-03-16 340261 8358.790
## 2023-03-17 471602 8239.313
## 2023-03-20 488199 8142.970
## 2023-03-21 287497 8151.689
## 2023-03-22 218120 8175.465
## 2023-03-23 540553 8275.325
## 2023-03-24 391840 8166.054
## 2023-03-27 364326 8201.173
## 2023-03-28 360349 8132.271
## 2023-03-29 339851 8217.965
##
## [[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(MARUTI.NS=MARUTI.NS[, "MARUTI.NS.Close"], NSEI=NSEI[,"NSEI.Close"]))
stocks
## MARUTI.NS.Close NSEI.Close
## 2022-04-01 7700.05 17670.45
## 2022-04-04 7774.80 18053.40
## 2022-04-05 7761.85 17957.40
## 2022-04-06 7744.20 17807.65
## 2022-04-07 7640.50 17639.55
## 2022-04-08 7557.95 17784.35
## 2022-04-11 7565.40 17674.95
## 2022-04-12 7618.35 17530.30
## 2022-04-13 7469.50 17475.65
## 2022-04-18 7573.50 17173.65
## ...
## 2023-03-16 8437.45 16985.60
## 2023-03-17 8316.85 17100.05
## 2023-03-20 8219.60 16988.40
## 2023-03-21 8228.40 17107.50
## 2023-03-22 8252.40 17151.90
## 2023-03-23 8353.20 17076.90
## 2023-03-24 8242.90 16945.05
## 2023-03-27 8278.35 16985.70
## 2023-03-28 8208.80 16951.70
## 2023-03-29 8295.30 17080.70
head(stocks)
## MARUTI.NS.Close NSEI.Close
## 2022-04-01 7700.05 17670.45
## 2022-04-04 7774.80 18053.40
## 2022-04-05 7761.85 17957.40
## 2022-04-06 7744.20 17807.65
## 2022-04-07 7640.50 17639.55
## 2022-04-08 7557.95 17784.35
plot(as.zoo(stocks), screens = 1, lty = 1:3, xlab = "Date", ylab = "Price")
legend("right", c("MARUTI.NS", "NSEI"), lty = 1:3, cex = 0.5)

stock_return <- apply(stocks, 1, function(x) {x / stocks[1,]}) %>% t %>% as.xts
stock_return
## MARUTI.NS.Close NSEI.Close
## 2022-04-01 1.0000000 1.0000000
## 2022-04-04 1.0097077 1.0216718
## 2022-04-05 1.0080260 1.0162390
## 2022-04-06 1.0057338 1.0077644
## 2022-04-07 0.9922663 0.9982514
## 2022-04-08 0.9815456 1.0064458
## 2022-04-11 0.9825131 1.0002547
## 2022-04-12 0.9893897 0.9920688
## 2022-04-13 0.9700587 0.9889760
## 2022-04-18 0.9835651 0.9718853
## ...
## 2023-03-16 1.0957657 0.9612432
## 2023-03-17 1.0801034 0.9677202
## 2023-03-20 1.0674736 0.9614017
## 2023-03-21 1.0686165 0.9681418
## 2023-03-22 1.0717334 0.9706545
## 2023-03-23 1.0848242 0.9664101
## 2023-03-24 1.0704996 0.9589485
## 2023-03-27 1.0751034 0.9612489
## 2023-03-28 1.0660710 0.9593247
## 2023-03-29 1.0773047 0.9666251
head(stock_return)
## MARUTI.NS.Close NSEI.Close
## 2022-04-01 1.0000000 1.0000000
## 2022-04-04 1.0097077 1.0216718
## 2022-04-05 1.0080260 1.0162390
## 2022-04-06 1.0057338 1.0077644
## 2022-04-07 0.9922663 0.9982514
## 2022-04-08 0.9815456 1.0064458
plot(as.zoo(stock_return), screens = 1, lty = 1:3, xlab = "Date", ylab = "Price")
legend("topleft", c("MARUTI.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)
## MARUTI.NS.Close NSEI.Close
## 2022-04-01 NA NA
## 2022-04-04 0.009660912 0.021440346
## 2022-04-05 -0.001666989 -0.005331746
## 2022-04-06 -0.002276519 -0.008374146
## 2022-04-07 -0.013481155 -0.009484578
## 2022-04-08 -0.010863031 0.008175251
plot(as.zoo(stock_return), screens = 1, lty = 1:3, xlab = "Date", ylab = "Price")
legend("topleft", c("MARUTI.NS", "NSEI"), lty = 1:3, cex = 0.5)
