#shortrun performance analysis using Technical Ananlysison the selected stocks #we will be fetching data from (historical prices) from the website (yahoo) on selected stocks. #selected stocks - tatasteels, jindalsteels and jsw steels
#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 set start date and end date to fetch data from yahoo
start <- as.Date("2023-06-01")
end <- as.Date("2023-11-24")
getSymbols("TATASTEEL.NS", SRC = "yahoo", from =start, to =end)
## [1] "TATASTEEL.NS"
#Exploring the dataset
head(TATASTEEL.NS)
## TATASTEEL.NS.Open TATASTEEL.NS.High TATASTEEL.NS.Low
## 2023-06-01 105.90 106.70 105.80
## 2023-06-02 106.50 108.30 106.50
## 2023-06-05 108.65 109.90 107.80
## 2023-06-06 109.75 109.85 108.30
## 2023-06-07 109.50 111.75 108.90
## 2023-06-08 111.50 113.00 110.95
## TATASTEEL.NS.Close TATASTEEL.NS.Volume TATASTEEL.NS.Adjusted
## 2023-06-01 105.95 21211538 102.6013
## 2023-06-02 108.00 46686933 104.5865
## 2023-06-05 109.40 39642539 105.9422
## 2023-06-06 109.00 26751372 105.5549
## 2023-06-07 111.50 45157197 107.9759
## 2023-06-08 111.15 46380294 107.6369
tail(TATASTEEL.NS)
## TATASTEEL.NS.Open TATASTEEL.NS.High TATASTEEL.NS.Low
## 2023-11-16 124.00 125.25 123.00
## 2023-11-17 124.70 125.75 124.30
## 2023-11-20 125.05 125.65 124.05
## 2023-11-21 125.05 126.40 124.95
## 2023-11-22 126.40 126.90 125.50
## 2023-11-23 126.35 127.40 125.90
## TATASTEEL.NS.Close TATASTEEL.NS.Volume TATASTEEL.NS.Adjusted
## 2023-11-16 124.70 30836413 124.70
## 2023-11-17 125.20 23319439 125.20
## 2023-11-20 124.35 18780290 124.35
## 2023-11-21 126.25 29643312 126.25
## 2023-11-22 126.10 19588063 126.10
## 2023-11-23 126.70 30736851 126.70
plot(TATASTEEL.NS[,"TATASTEEL.NS.Close"], main = "TATASTEEL CLOSING PRICE MOVEMENT")
candleChart(TATASTEEL.NS, up.col="green", dn.col="red", theme="white")
#Let us set start date and end date to fetch data from yahoo
getSymbols("^NSEI", SRC = "yahoo", from =start, to =end)
## [1] "NSEI"
#Exploring the dataset
head(NSEI)
## NSEI.Open NSEI.High NSEI.Low NSEI.Close NSEI.Volume NSEI.Adjusted
## 2023-06-01 18579.40 18580.30 18464.55 18487.75 265600 18487.75
## 2023-06-02 18550.85 18573.70 18478.40 18534.10 270900 18534.10
## 2023-06-05 18612.00 18640.15 18582.80 18593.85 256100 18593.85
## 2023-06-06 18600.80 18622.75 18531.60 18599.00 220800 18599.00
## 2023-06-07 18665.60 18738.95 18636.00 18726.40 260100 18726.40
## 2023-06-08 18725.35 18777.90 18615.60 18634.55 286500 18634.55
tail(NSEI)
## NSEI.Open NSEI.High NSEI.Low NSEI.Close NSEI.Volume NSEI.Adjusted
## 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
plot(NSEI[,"NSEI.Close"], main = "NIFTY 50 CLOSING PRICE MOVEMENT")
candleChart(NSEI, up.col="green", dn.col="red", theme="white")
# Let us compare two stocks
getSymbols(c("TATASTEEL.NS", "^NSEI"), src = "yahoo", from =start, to =end)
## [1] "TATASTEEL.NS" "NSEI"
head(list(TATASTEEL.NS, NSEI))
## [[1]]
## TATASTEEL.NS.Open TATASTEEL.NS.High TATASTEEL.NS.Low
## 2023-06-01 105.90 106.70 105.80
## 2023-06-02 106.50 108.30 106.50
## 2023-06-05 108.65 109.90 107.80
## 2023-06-06 109.75 109.85 108.30
## 2023-06-07 109.50 111.75 108.90
## 2023-06-08 111.50 113.00 110.95
## 2023-06-09 111.25 111.65 108.65
## 2023-06-12 109.15 110.60 109.15
## 2023-06-13 110.00 111.85 109.65
## 2023-06-14 111.50 114.00 111.40
## ...
## 2023-11-09 119.20 120.50 118.85
## 2023-11-10 119.50 120.45 118.80
## 2023-11-13 120.85 121.25 119.90
## 2023-11-15 121.70 124.40 121.50
## 2023-11-16 124.00 125.25 123.00
## 2023-11-17 124.70 125.75 124.30
## 2023-11-20 125.05 125.65 124.05
## 2023-11-21 125.05 126.40 124.95
## 2023-11-22 126.40 126.90 125.50
## 2023-11-23 126.35 127.40 125.90
## TATASTEEL.NS.Close TATASTEEL.NS.Volume TATASTEEL.NS.Adjusted
## 2023-06-01 105.95 21211538 102.6013
## 2023-06-02 108.00 46686933 104.5865
## 2023-06-05 109.40 39642539 105.9422
## 2023-06-06 109.00 26751372 105.5549
## 2023-06-07 111.50 45157197 107.9759
## 2023-06-08 111.15 46380294 107.6369
## 2023-06-09 108.90 36392617 105.4580
## 2023-06-12 109.50 23472313 106.0391
## 2023-06-13 111.15 30018212 107.6369
## 2023-06-14 113.80 66696433 110.2032
## ...
## 2023-11-09 119.65 22979112 119.6500
## 2023-11-10 120.05 23584510 120.0500
## 2023-11-13 121.00 17951775 121.0000
## 2023-11-15 124.05 38344393 124.0500
## 2023-11-16 124.70 30836413 124.7000
## 2023-11-17 125.20 23319439 125.2000
## 2023-11-20 124.35 18780290 124.3500
## 2023-11-21 126.25 29643312 126.2500
## 2023-11-22 126.10 19588063 126.1000
## 2023-11-23 126.70 30736851 126.7000
##
## [[2]]
## NSEI.Open NSEI.High NSEI.Low NSEI.Close NSEI.Volume NSEI.Adjusted
## 2023-06-01 18579.40 18580.30 18464.55 18487.75 265600 18487.75
## 2023-06-02 18550.85 18573.70 18478.40 18534.10 270900 18534.10
## 2023-06-05 18612.00 18640.15 18582.80 18593.85 256100 18593.85
## 2023-06-06 18600.80 18622.75 18531.60 18599.00 220800 18599.00
## 2023-06-07 18665.60 18738.95 18636.00 18726.40 260100 18726.40
## 2023-06-08 18725.35 18777.90 18615.60 18634.55 286500 18634.55
## 2023-06-09 18655.90 18676.65 18555.40 18563.40 221800 18563.40
## 2023-06-12 18595.05 18633.60 18559.75 18601.50 179500 18601.50
## 2023-06-13 18631.80 18728.90 18631.80 18716.15 233200 18716.15
## 2023-06-14 18744.60 18769.70 18690.00 18755.90 261400 18755.90
## ...
## 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 multiple stocks into our study
stocks <- as.xts(data.frame(TATASTEEL.NS=TATASTEEL.NS[,"TATASTEEL.NS.Close"],NSEI=NSEI[,"NSEI.Close"]))
stocks
## TATASTEEL.NS.Close NSEI.Close
## 2023-06-01 105.95 18487.75
## 2023-06-02 108.00 18534.10
## 2023-06-05 109.40 18593.85
## 2023-06-06 109.00 18599.00
## 2023-06-07 111.50 18726.40
## 2023-06-08 111.15 18634.55
## 2023-06-09 108.90 18563.40
## 2023-06-12 109.50 18601.50
## 2023-06-13 111.15 18716.15
## 2023-06-14 113.80 18755.90
## ...
## 2023-11-09 119.65 19395.30
## 2023-11-10 120.05 19425.35
## 2023-11-13 121.00 19443.55
## 2023-11-15 124.05 19675.45
## 2023-11-16 124.70 19765.20
## 2023-11-17 125.20 19731.80
## 2023-11-20 124.35 19694.00
## 2023-11-21 126.25 19783.40
## 2023-11-22 126.10 19811.85
## 2023-11-23 126.70 19802.00
head(stocks)
## TATASTEEL.NS.Close NSEI.Close
## 2023-06-01 105.95 18487.75
## 2023-06-02 108.00 18534.10
## 2023-06-05 109.40 18593.85
## 2023-06-06 109.00 18599.00
## 2023-06-07 111.50 18726.40
## 2023-06-08 111.15 18634.55
plot(as.zoo(stocks), screens=1, lty=1:2, xlab="Date", ylab="Price")
legend("left", c("TATASTEEL.NS", "NSIE"), lty= 1:2, cex=0.5)
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)
## TATASTEEL.NS.Close NSEI.Close
## 2023-06-01 1.000000 1.000000
## 2023-06-02 1.019349 1.002507
## 2023-06-05 1.032563 1.005739
## 2023-06-06 1.028787 1.006017
## 2023-06-07 1.052383 1.012909
## 2023-06-08 1.049080 1.007940
## 2023-06-09 1.027843 1.004092
## 2023-06-12 1.033506 1.006153
## 2023-06-13 1.049080 1.012354
## 2023-06-14 1.074092 1.014504
## ...
## 2023-11-09 1.129306 1.049089
## 2023-11-10 1.133082 1.050715
## 2023-11-13 1.142048 1.051699
## 2023-11-15 1.170835 1.064242
## 2023-11-16 1.176970 1.069097
## 2023-11-17 1.181689 1.067291
## 2023-11-20 1.173667 1.065246
## 2023-11-21 1.191600 1.070082
## 2023-11-22 1.190184 1.071620
## 2023-11-23 1.195847 1.071088
head(stock_return)
## TATASTEEL.NS.Close NSEI.Close
## 2023-06-01 1.000000 1.000000
## 2023-06-02 1.019349 1.002507
## 2023-06-05 1.032563 1.005739
## 2023-06-06 1.028787 1.006017
## 2023-06-07 1.052383 1.012909
## 2023-06-08 1.049080 1.007940
#Making ourreturns more accurate and insightful perpsectives on investment returns
stock_change <- stocks %>% log%>% diff
head(stock_change)
## TATASTEEL.NS.Close NSEI.Close
## 2023-06-01 NA NA
## 2023-06-02 0.019163971 0.002503907
## 2023-06-05 0.012879677 0.003218602
## 2023-06-06 -0.003663022 0.000276956
## 2023-06-07 0.022676709 0.006826498
## 2023-06-08 -0.003143937 -0.004916887
plot(as.zoo(stock_change),screens=2, lty=1:2, xlab="Date", ylab="Price")
legend("left", c("TATASTEEL.NS", "NSEI"), lty= 1:2, cex=0.5)
#simple moving average
candleChart(TATASTEEL.NS, up.col="green", dn.col="red", theme="white")
addSMA(n=20)
addSMA(n=c(20,50,120), col = c("green", "red","blue"))