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
start <- as.Date("2021-04-01")
end <- as.Date("2023-11-24")
getSymbols("HINDALCO.NS", src = "yahoo", from = start, to=end)
## [1] "HINDALCO.NS"
#Exploring the Dataset
head(HINDALCO.NS)
## HINDALCO.NS.Open HINDALCO.NS.High HINDALCO.NS.Low HINDALCO.NS.Close
## 2021-04-01 329.75 352.0 328.45 350.25
## 2021-04-05 349.90 352.9 338.00 350.65
## 2021-04-06 352.70 358.3 348.50 350.45
## 2021-04-07 350.40 360.7 349.00 352.20
## 2021-04-08 355.90 374.0 354.65 365.20
## 2021-04-09 367.00 370.0 354.70 361.05
## HINDALCO.NS.Volume HINDALCO.NS.Adjusted
## 2021-04-01 25631843 342.4461
## 2021-04-05 14771653 342.8372
## 2021-04-06 11185963 342.6416
## 2021-04-07 12542475 344.3527
## 2021-04-08 28768469 357.0630
## 2021-04-09 16371164 353.0055
tail(HINDALCO.NS)
## HINDALCO.NS.Open HINDALCO.NS.High HINDALCO.NS.Low HINDALCO.NS.Close
## 2023-11-16 504.00 507.45 497.15 504.30
## 2023-11-17 503.05 506.45 496.85 497.75
## 2023-11-20 500.15 505.95 496.55 497.30
## 2023-11-21 502.80 510.95 501.90 506.85
## 2023-11-22 504.90 506.85 498.95 500.25
## 2023-11-23 499.00 503.00 491.65 501.95
## HINDALCO.NS.Volume HINDALCO.NS.Adjusted
## 2023-11-16 4171759 504.30
## 2023-11-17 3595049 497.75
## 2023-11-20 4305637 497.30
## 2023-11-21 7154476 506.85
## 2023-11-22 4337970 500.25
## 2023-11-23 6628259 501.95
plot(HINDALCO.NS[,"HINDALCO.NS.Close"],main="TATASTEEL.NS.Close")
candleChart(HINDALCO.NS,up.col="green",dn.col="red",theme="white")
#Loading data
getSymbols("^NSEI",src= "yahoo", from = start, to= end)
## [1] "NSEI"
head(NSEI)
## NSEI.Open NSEI.High NSEI.Low NSEI.Close NSEI.Volume NSEI.Adjusted
## 2021-04-01 14798.40 14883.20 14692.45 14867.35 445000 14867.35
## 2021-04-05 14837.70 14849.85 14459.50 14637.80 509700 14637.80
## 2021-04-06 14737.00 14779.10 14573.90 14683.50 475300 14683.50
## 2021-04-07 14716.45 14879.80 14649.85 14819.05 0 14819.05
## 2021-04-08 14875.65 14984.15 14821.10 14873.80 514800 14873.80
## 2021-04-09 14882.65 14918.45 14785.65 14834.85 504100 14834.85
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="NSEI.Close")
#Loading both the data together
start <- as.Date("2021-04-01")
end <- as.Date("2023-11-24")
getSymbols(c("HINDALCO.NS","NSEI"),src= "yahoo", from = start, to = end)
## Warning: Unable to import "NSEI".
## HTTP error 404.
## [1] "HINDALCO.NS"
getSymbols(c("HINDALCO.NS","^NSEI"),src= "yahoo", from = start, to = end)
## [1] "HINDALCO.NS" "NSEI"
head(list(HINDALCO.NS,NSEI))
## [[1]]
## HINDALCO.NS.Open HINDALCO.NS.High HINDALCO.NS.Low HINDALCO.NS.Close
## 2021-04-01 329.75 352.00 328.45 350.25
## 2021-04-05 349.90 352.90 338.00 350.65
## 2021-04-06 352.70 358.30 348.50 350.45
## 2021-04-07 350.40 360.70 349.00 352.20
## 2021-04-08 355.90 374.00 354.65 365.20
## 2021-04-09 367.00 370.00 354.70 361.05
## 2021-04-12 352.10 354.00 331.60 336.20
## 2021-04-13 336.20 350.30 336.20 348.70
## 2021-04-15 354.00 366.80 349.75 353.55
## 2021-04-16 356.75 376.50 352.70 370.00
## ...
## 2023-11-09 488.80 488.80 483.15 484.45
## 2023-11-10 482.05 488.25 479.55 481.30
## 2023-11-13 482.00 492.35 481.10 487.50
## 2023-11-15 497.00 513.50 494.20 505.40
## 2023-11-16 504.00 507.45 497.15 504.30
## 2023-11-17 503.05 506.45 496.85 497.75
## 2023-11-20 500.15 505.95 496.55 497.30
## 2023-11-21 502.80 510.95 501.90 506.85
## 2023-11-22 504.90 506.85 498.95 500.25
## 2023-11-23 499.00 503.00 491.65 501.95
## HINDALCO.NS.Volume HINDALCO.NS.Adjusted
## 2021-04-01 25631843 342.4461
## 2021-04-05 14771653 342.8372
## 2021-04-06 11185963 342.6416
## 2021-04-07 12542475 344.3527
## 2021-04-08 28768469 357.0630
## 2021-04-09 16371164 353.0055
## 2021-04-12 16096812 328.7091
## 2021-04-13 12942071 340.9306
## 2021-04-15 21825520 345.6726
## 2021-04-16 28438547 361.7561
## ...
## 2023-11-09 3271496 484.4500
## 2023-11-10 3498064 481.3000
## 2023-11-13 4918873 487.5000
## 2023-11-15 11674265 505.4000
## 2023-11-16 4171759 504.3000
## 2023-11-17 3595049 497.7500
## 2023-11-20 4305637 497.3000
## 2023-11-21 7154476 506.8500
## 2023-11-22 4337970 500.2500
## 2023-11-23 6628259 501.9500
##
## [[2]]
## NSEI.Open NSEI.High NSEI.Low NSEI.Close NSEI.Volume NSEI.Adjusted
## 2021-04-01 14798.40 14883.20 14692.45 14867.35 445000 14867.35
## 2021-04-05 14837.70 14849.85 14459.50 14637.80 509700 14637.80
## 2021-04-06 14737.00 14779.10 14573.90 14683.50 475300 14683.50
## 2021-04-07 14716.45 14879.80 14649.85 14819.05 0 14819.05
## 2021-04-08 14875.65 14984.15 14821.10 14873.80 514800 14873.80
## 2021-04-09 14882.65 14918.45 14785.65 14834.85 504100 14834.85
## 2021-04-12 14644.65 14652.50 14248.70 14310.80 650200 14310.80
## 2021-04-13 14364.90 14528.90 14274.90 14504.80 605100 14504.80
## 2021-04-15 14522.40 14597.55 14353.20 14581.45 568000 14581.45
## 2021-04-16 14599.60 14697.70 14559.00 14617.85 577200 14617.85
## ...
## 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
stocks <- as.xts(data.frame(HINDALCO.NS = HINDALCO.NS[, "HINDALCO.NS.Close"],NSEI = NSEI[, "NSEI.Close"]))
stocks
## HINDALCO.NS.Close NSEI.Close
## 2021-04-01 350.25 14867.35
## 2021-04-05 350.65 14637.80
## 2021-04-06 350.45 14683.50
## 2021-04-07 352.20 14819.05
## 2021-04-08 365.20 14873.80
## 2021-04-09 361.05 14834.85
## 2021-04-12 336.20 14310.80
## 2021-04-13 348.70 14504.80
## 2021-04-15 353.55 14581.45
## 2021-04-16 370.00 14617.85
## ...
## 2023-11-09 484.45 19395.30
## 2023-11-10 481.30 19425.35
## 2023-11-13 487.50 19443.55
## 2023-11-15 505.40 19675.45
## 2023-11-16 504.30 19765.20
## 2023-11-17 497.75 19731.80
## 2023-11-20 497.30 19694.00
## 2023-11-21 506.85 19783.40
## 2023-11-22 500.25 19811.85
## 2023-11-23 501.95 19802.00
head(stocks)
## HINDALCO.NS.Close NSEI.Close
## 2021-04-01 350.25 14867.35
## 2021-04-05 350.65 14637.80
## 2021-04-06 350.45 14683.50
## 2021-04-07 352.20 14819.05
## 2021-04-08 365.20 14873.80
## 2021-04-09 361.05 14834.85
plot(as.zoo(stocks),screens=1, lty=1:2,xlab = "Date",ylab = "Price")
legend("topleft",c("HINDALCO.NS.Close", "NSEI.Close"),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
head(stock_return)
## HINDALCO.NS.Close NSEI.Close
## 2021-04-01 1.000000 1.0000000
## 2021-04-05 1.001142 0.9845601
## 2021-04-06 1.000571 0.9876340
## 2021-04-07 1.005567 0.9967513
## 2021-04-08 1.042684 1.0004338
## 2021-04-09 1.030835 0.9978140
plot(as.zoo(stocks),screens=2, lty=1.3,xlab = "Date",ylab = "Price")
legend("topleft",c("HINDALCO.NS.Close","NSEI.Close"),lty = 1:3, cex=0.5)
stock_change<- stocks %>% log %>% diff
head(stock_change)
## HINDALCO.NS.Close NSEI.Close
## 2021-04-01 NA NA
## 2021-04-05 0.0011413724 -0.015560296
## 2021-04-06 -0.0005704798 0.003117204
## 2021-04-07 0.0049811529 0.009189088
## 2021-04-08 0.0362459514 0.003687761
## 2021-04-09 -0.0114287631 -0.002622147
plot(as.zoo(stock_change),screens=2, lty=1.3,xlab = "Date",ylab = "Price")
legend("topleft",c("HINDALCO.NS.Close","NSEI.Close"),lty = 1:3, cex=0.5)
candleChart(HINDALCO.NS,up.col="green",dn.col="red",theme="white")
addSMA(n=20)
addSMA(n=c(20,50,200), col=c("green","red","blue"))