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-01-01")
end <- as.Date("2023-11-24")
getSymbols("DLF.NS", src = "yahoo", from = start, to = end)
## [1] "DLF.NS"
head(DLF.NS)
## DLF.NS.Open DLF.NS.High DLF.NS.Low DLF.NS.Close DLF.NS.Volume
## 2021-01-01 232.85 240.45 232.50 238.35 10012441
## 2021-01-04 239.85 245.00 233.20 244.30 17172770
## 2021-01-05 241.95 246.95 239.50 241.75 17130907
## 2021-01-06 241.70 244.40 234.65 241.05 12995777
## 2021-01-07 244.90 249.30 242.60 245.40 16159966
## 2021-01-08 247.90 252.45 245.90 248.30 15045597
## DLF.NS.Adjusted
## 2021-01-01 233.1185
## 2021-01-04 238.9379
## 2021-01-05 236.4439
## 2021-01-06 235.7592
## 2021-01-07 240.0138
## 2021-01-08 242.8501
tail(DLF.NS)
## DLF.NS.Open DLF.NS.High DLF.NS.Low DLF.NS.Close DLF.NS.Volume
## 2023-11-16 621.95 633.30 620.25 630.25 3206517
## 2023-11-17 625.95 632.00 625.10 631.15 2412089
## 2023-11-20 632.00 634.25 623.55 626.25 3290059
## 2023-11-21 627.10 641.20 627.10 634.45 6052465
## 2023-11-22 633.85 635.50 623.50 632.55 2762713
## 2023-11-23 634.00 640.00 633.00 636.15 2516687
## DLF.NS.Adjusted
## 2023-11-16 630.25
## 2023-11-17 631.15
## 2023-11-20 626.25
## 2023-11-21 634.45
## 2023-11-22 632.55
## 2023-11-23 636.15
plot(DLF.NS[,"DLF.NS.Close"], main = "DLF.NS CLOSING PRICE")

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

getSymbols( c("DLF.NS", "^NSEI"), src = "yahoo", from = start, to = end)
## [1] "DLF.NS" "NSEI"
head(list(DLF.NS, NSEI))
## [[1]]
## DLF.NS.Open DLF.NS.High DLF.NS.Low DLF.NS.Close DLF.NS.Volume
## 2021-01-01 232.85 240.45 232.50 238.35 10012441
## 2021-01-04 239.85 245.00 233.20 244.30 17172770
## 2021-01-05 241.95 246.95 239.50 241.75 17130907
## 2021-01-06 241.70 244.40 234.65 241.05 12995777
## 2021-01-07 244.90 249.30 242.60 245.40 16159966
## 2021-01-08 247.90 252.45 245.90 248.30 15045597
## 2021-01-11 249.00 251.00 243.50 246.95 13389039
## 2021-01-12 245.20 278.35 245.00 267.55 55656650
## 2021-01-13 271.80 292.75 268.25 280.45 53194247
## 2021-01-14 281.50 285.80 277.10 281.60 18052699
## ...
## 2023-11-09 597.90 605.60 596.95 600.70 5403880
## 2023-11-10 598.90 607.90 595.10 606.05 3378490
## 2023-11-13 611.40 611.40 604.35 608.40 1898672
## 2023-11-15 614.10 624.45 612.05 622.10 3886884
## 2023-11-16 621.95 633.30 620.25 630.25 3206517
## 2023-11-17 625.95 632.00 625.10 631.15 2412089
## 2023-11-20 632.00 634.25 623.55 626.25 3290059
## 2023-11-21 627.10 641.20 627.10 634.45 6052465
## 2023-11-22 633.85 635.50 623.50 632.55 2762713
## 2023-11-23 634.00 640.00 633.00 636.15 2516687
## DLF.NS.Adjusted
## 2021-01-01 233.1185
## 2021-01-04 238.9379
## 2021-01-05 236.4439
## 2021-01-06 235.7592
## 2021-01-07 240.0138
## 2021-01-08 242.8501
## 2021-01-11 241.5298
## 2021-01-12 261.6776
## 2021-01-13 274.2945
## 2021-01-14 275.4192
## ...
## 2023-11-09 600.7000
## 2023-11-10 606.0500
## 2023-11-13 608.4000
## 2023-11-15 622.1000
## 2023-11-16 630.2500
## 2023-11-17 631.1500
## 2023-11-20 626.2500
## 2023-11-21 634.4500
## 2023-11-22 632.5500
## 2023-11-23 636.1500
##
## [[2]]
## NSEI.Open NSEI.High NSEI.Low NSEI.Close NSEI.Volume NSEI.Adjusted
## 2021-01-01 13996.10 14049.85 13991.35 14018.50 358100 14018.50
## 2021-01-04 14104.35 14147.95 13953.75 14132.90 495000 14132.90
## 2021-01-05 14075.15 14215.60 14048.15 14199.50 492500 14199.50
## 2021-01-06 14240.95 14244.15 14039.90 14146.25 632300 14146.25
## 2021-01-07 14253.75 14256.25 14123.10 14137.35 559200 14137.35
## 2021-01-08 14258.40 14367.30 14221.65 14347.25 613500 14347.25
## 2021-01-11 14474.05 14498.20 14383.10 14484.75 672900 14484.75
## 2021-01-12 14473.80 14590.65 14432.85 14563.45 929600 14563.45
## 2021-01-13 14639.80 14653.35 14435.70 14564.85 874000 14564.85
## 2021-01-14 14550.05 14617.80 14471.50 14595.60 620200 14595.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(DLF.NS = DLF.NS[,"DLF.NS.Close"],NSEI = NSEI[,"NSEI.Close"]))
stocks
## DLF.NS.Close NSEI.Close
## 2021-01-01 238.35 14018.50
## 2021-01-04 244.30 14132.90
## 2021-01-05 241.75 14199.50
## 2021-01-06 241.05 14146.25
## 2021-01-07 245.40 14137.35
## 2021-01-08 248.30 14347.25
## 2021-01-11 246.95 14484.75
## 2021-01-12 267.55 14563.45
## 2021-01-13 280.45 14564.85
## 2021-01-14 281.60 14595.60
## ...
## 2023-11-09 600.70 19395.30
## 2023-11-10 606.05 19425.35
## 2023-11-13 608.40 19443.55
## 2023-11-15 622.10 19675.45
## 2023-11-16 630.25 19765.20
## 2023-11-17 631.15 19731.80
## 2023-11-20 626.25 19694.00
## 2023-11-21 634.45 19783.40
## 2023-11-22 632.55 19811.85
## 2023-11-23 636.15 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
## DLF.NS.Close NSEI.Close
## 2021-01-01 1.000000 1.000000
## 2021-01-04 1.024963 1.008161
## 2021-01-05 1.014265 1.012912
## 2021-01-06 1.011328 1.009113
## 2021-01-07 1.029578 1.008478
## 2021-01-08 1.041745 1.023451
## 2021-01-11 1.036081 1.033260
## 2021-01-12 1.122509 1.038874
## 2021-01-13 1.176631 1.038973
## 2021-01-14 1.181456 1.041167
## ...
## 2023-11-09 2.520243 1.383550
## 2023-11-10 2.542689 1.385694
## 2023-11-13 2.552549 1.386992
## 2023-11-15 2.610027 1.403535
## 2023-11-16 2.644221 1.409937
## 2023-11-17 2.647997 1.407554
## 2023-11-20 2.627439 1.404858
## 2023-11-21 2.661842 1.411235
## 2023-11-22 2.653870 1.413265
## 2023-11-23 2.668974 1.412562
plot(as.zoo(stock_return), screens = 2, lty = 1:2, xlab = "Date", ylab = "Price")
legend("topleft", c("DLF.NS","NSEI"), lty = 1:2, cex = 0.5)

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

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