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.3 ✔ 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("2023-01-01")
end <- as.Date("2023-11-24")
getSymbols("HINDUNILVR.NS", src = "yahoo", from = start, to = end)
## [1] "HINDUNILVR.NS"
head(HINDUNILVR.NS)
## HINDUNILVR.NS.Open HINDUNILVR.NS.High HINDUNILVR.NS.Low
## 2023-01-02 2573.90 2580.05 2538.05
## 2023-01-03 2550.05 2562.00 2535.00
## 2023-01-04 2550.50 2558.40 2527.05
## 2023-01-05 2537.55 2591.95 2537.50
## 2023-01-06 2580.00 2611.90 2574.90
## 2023-01-09 2589.35 2629.45 2570.00
## HINDUNILVR.NS.Close HINDUNILVR.NS.Volume HINDUNILVR.NS.Adjusted
## 2023-01-02 2558.40 681703 2519.198
## 2023-01-03 2542.05 821843 2503.098
## 2023-01-04 2536.10 1672649 2497.240
## 2023-01-05 2583.70 2008493 2544.110
## 2023-01-06 2576.45 991782 2536.971
## 2023-01-09 2624.05 1267078 2583.842
tail(HINDUNILVR.NS)
## HINDUNILVR.NS.Open HINDUNILVR.NS.High HINDUNILVR.NS.Low
## 2023-11-16 2480.20 2505.80 2475.35
## 2023-11-17 2465.00 2533.50 2465.00
## 2023-11-20 2534.45 2534.50 2500.00
## 2023-11-21 2505.00 2517.00 2498.00
## 2023-11-22 2507.75 2524.25 2504.00
## 2023-11-23 2506.20 2527.75 2506.20
## HINDUNILVR.NS.Close HINDUNILVR.NS.Volume HINDUNILVR.NS.Adjusted
## 2023-11-16 2491.20 1420578 2491.20
## 2023-11-17 2528.80 1756043 2528.80
## 2023-11-20 2506.45 944231 2506.45
## 2023-11-21 2505.25 1088806 2505.25
## 2023-11-22 2521.90 816610 2521.90
## 2023-11-23 2519.90 1023670 2519.90
plot(HINDUNILVR.NS[,"HINDUNILVR.NS.Close"], main = "HINDUNILVR.NS CLOSING PRICE")

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

getSymbols( c("HINDUNILVR.NS", "^NSEI"), src = "yahoo", from = start, to = end)
## [1] "HINDUNILVR.NS" "NSEI"
head(list(HINDUNILVR.NS, NSEI))
## [[1]]
## HINDUNILVR.NS.Open HINDUNILVR.NS.High HINDUNILVR.NS.Low
## 2023-01-02 2573.90 2580.05 2538.05
## 2023-01-03 2550.05 2562.00 2535.00
## 2023-01-04 2550.50 2558.40 2527.05
## 2023-01-05 2537.55 2591.95 2537.50
## 2023-01-06 2580.00 2611.90 2574.90
## 2023-01-09 2589.35 2629.45 2570.00
## 2023-01-10 2624.05 2653.35 2615.10
## 2023-01-11 2644.00 2647.85 2588.10
## 2023-01-12 2586.10 2602.60 2572.75
## 2023-01-13 2590.20 2639.25 2566.65
## ...
## 2023-11-09 2520.05 2522.00 2473.05
## 2023-11-10 2477.00 2488.60 2465.35
## 2023-11-13 2477.20 2487.20 2471.00
## 2023-11-15 2501.90 2503.05 2476.00
## 2023-11-16 2480.20 2505.80 2475.35
## 2023-11-17 2465.00 2533.50 2465.00
## 2023-11-20 2534.45 2534.50 2500.00
## 2023-11-21 2505.00 2517.00 2498.00
## 2023-11-22 2507.75 2524.25 2504.00
## 2023-11-23 2506.20 2527.75 2506.20
## HINDUNILVR.NS.Close HINDUNILVR.NS.Volume HINDUNILVR.NS.Adjusted
## 2023-01-02 2558.40 681703 2519.198
## 2023-01-03 2542.05 821843 2503.098
## 2023-01-04 2536.10 1672649 2497.239
## 2023-01-05 2583.70 2008493 2544.110
## 2023-01-06 2576.45 991782 2536.971
## 2023-01-09 2624.05 1267078 2583.842
## 2023-01-10 2643.85 1413088 2603.338
## 2023-01-11 2593.25 1471687 2553.514
## 2023-01-12 2590.05 1703829 2550.363
## 2023-01-13 2624.00 1348015 2583.792
## ...
## 2023-11-09 2476.70 1823094 2476.700
## 2023-11-10 2486.55 771062 2486.550
## 2023-11-13 2478.15 760809 2478.150
## 2023-11-15 2486.40 1457029 2486.400
## 2023-11-16 2491.20 1420578 2491.200
## 2023-11-17 2528.80 1756043 2528.800
## 2023-11-20 2506.45 944231 2506.450
## 2023-11-21 2505.25 1088806 2505.250
## 2023-11-22 2521.90 816610 2521.900
## 2023-11-23 2519.90 1023670 2519.900
##
## [[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
stocks <- as.xts(data.frame(HINDUNILVR.NS = HINDUNILVR.NS[,"HINDUNILVR.NS.Close"],NSEI = NSEI[,"NSEI.Close"]))
stocks
## HINDUNILVR.NS.Close NSEI.Close
## 2023-01-02 2558.40 18197.45
## 2023-01-03 2542.05 18232.55
## 2023-01-04 2536.10 18042.95
## 2023-01-05 2583.70 17992.15
## 2023-01-06 2576.45 17859.45
## 2023-01-09 2624.05 18101.20
## 2023-01-10 2643.85 17914.15
## 2023-01-11 2593.25 17895.70
## 2023-01-12 2590.05 17858.20
## 2023-01-13 2624.00 17956.60
## ...
## 2023-11-09 2476.70 19395.30
## 2023-11-10 2486.55 19425.35
## 2023-11-13 2478.15 19443.55
## 2023-11-15 2486.40 19675.45
## 2023-11-16 2491.20 19765.20
## 2023-11-17 2528.80 19731.80
## 2023-11-20 2506.45 19694.00
## 2023-11-21 2505.25 19783.40
## 2023-11-22 2521.90 19811.85
## 2023-11-23 2519.90 19802.00
plot(as.zoo(stocks), screens = 1, lty = 1:2, xlab = "Date", ylab = "Price")

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
## HINDUNILVR.NS.Close NSEI.Close
## 2023-01-02 1.0000000 1.0000000
## 2023-01-03 0.9936093 1.0019289
## 2023-01-04 0.9912837 0.9915098
## 2023-01-05 1.0098890 0.9887183
## 2023-01-06 1.0070552 0.9814260
## 2023-01-09 1.0256606 0.9947108
## 2023-01-10 1.0333999 0.9844319
## 2023-01-11 1.0136218 0.9834180
## 2023-01-12 1.0123711 0.9813573
## 2023-01-13 1.0256411 0.9867647
## ...
## 2023-11-09 0.9680660 1.0658252
## 2023-11-10 0.9719161 1.0674765
## 2023-11-13 0.9686327 1.0684767
## 2023-11-15 0.9718574 1.0812202
## 2023-11-16 0.9737336 1.0861522
## 2023-11-17 0.9884303 1.0843168
## 2023-11-20 0.9796944 1.0822396
## 2023-11-21 0.9792253 1.0871524
## 2023-11-22 0.9857333 1.0887158
## 2023-11-23 0.9849515 1.0881745
plot(as.zoo(stock_return), screens = 2, lty = 1:2, xlab = "Date", ylab = "Price")
legend("topleft", c("HINDUNILVR.NS","NSEI"), lty = 1:2, cex = 0.5)

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

addSMA(n=20)

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