R Markdown

#Shortrun performance analysis using on the selected stocks #We will be fetching the data (Historical prices)from the (Yahoo) on selected stocks. #Selected stocks ,NESTLE IND , NIFTY 50

#List of pacakges

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 the data from yahoo

start <- as.Date("2021-01-01")
end <- as.Date("2023-03-31")
getSymbols("NESTLEIND.NS", src = "yahoo", from=start, to=end)
## [1] "NESTLEIND.NS"

#Exploring the Dataset

head(NESTLEIND.NS)
##            NESTLEIND.NS.Open NESTLEIND.NS.High NESTLEIND.NS.Low
## 2021-01-01          18389.70             18520         18306.05
## 2021-01-04          18495.00             18630         18329.75
## 2021-01-05          18343.50             18620         18278.05
## 2021-01-06          18510.05             18600         18430.00
## 2021-01-07          18599.00             18615         18050.10
## 2021-01-08          18227.30             18350         18000.00
##            NESTLEIND.NS.Close NESTLEIND.NS.Volume NESTLEIND.NS.Adjusted
## 2021-01-01           18450.70               50286              17788.47
## 2021-01-04           18377.95               62035              17718.33
## 2021-01-05           18558.25               83673              17892.16
## 2021-01-06           18515.25               57706              17850.70
## 2021-01-07           18127.30              120605              17476.68
## 2021-01-08           18306.25              151635              17649.21
tail(NESTLEIND.NS)
##            NESTLEIND.NS.Open NESTLEIND.NS.High NESTLEIND.NS.Low
## 2023-03-22          19009.00          19009.00         18698.70
## 2023-03-23          18819.70          19169.90         18710.15
## 2023-03-24          19005.05          19100.00         18888.00
## 2023-03-27          19000.00          19127.80         18815.05
## 2023-03-28          18900.00          19019.95         18850.00
## 2023-03-29          18930.00          19125.00         18876.20
##            NESTLEIND.NS.Close NESTLEIND.NS.Volume NESTLEIND.NS.Adjusted
## 2023-03-22           18819.70               28248              18617.76
## 2023-03-23           19044.45               56685              18840.10
## 2023-03-24           18953.10               26299              18749.73
## 2023-03-27           18891.00               35854              18688.29
## 2023-03-28           18946.00               35481              18742.71
## 2023-03-29           19052.95               48606              18848.51
plot(NESTLEIND.NS[,"NESTLEIND.NS.Close"], main = "NESTLEIND.NS")

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

#Let us get to more stock in addition to NESTLE IND AND NIFTY 50

getSymbols(c("NESTLEIND.NS","^NSEI"),src = "yahoo", from = start ,to=end)
## [1] "NESTLEIND.NS" "NSEI"

#let us get the multiple stock into our study

Stocks <- as.xts(data.frame(NESTLEIND.NS=NESTLEIND.NS[,"NESTLEIND.NS.Close"],NSEI = NSEI[,"NSEI.Close"]))
Stocks                            
##            NESTLEIND.NS.Close NSEI.Close
## 2021-01-01           18450.70   14018.50
## 2021-01-04           18377.95   14132.90
## 2021-01-05           18558.25   14199.50
## 2021-01-06           18515.25   14146.25
## 2021-01-07           18127.30   14137.35
## 2021-01-08           18306.25   14347.25
## 2021-01-11           18391.45   14484.75
## 2021-01-12           17999.10   14563.45
## 2021-01-13           18019.30   14564.85
## 2021-01-14           18132.60   14595.60
##        ...                              
## 2023-03-16           18450.65   16985.60
## 2023-03-17           18858.15   17100.05
## 2023-03-20           18937.85   16988.40
## 2023-03-21           18927.40   17107.50
## 2023-03-22           18819.70   17151.90
## 2023-03-23           19044.45   17076.90
## 2023-03-24           18953.10   16945.05
## 2023-03-27           18891.00   16985.70
## 2023-03-28           18946.00   16951.70
## 2023-03-29           19052.95   17080.70
head(Stocks)
##            NESTLEIND.NS.Close NSEI.Close
## 2021-01-01           18450.70   14018.50
## 2021-01-04           18377.95   14132.90
## 2021-01-05           18558.25   14199.50
## 2021-01-06           18515.25   14146.25
## 2021-01-07           18127.30   14137.35
## 2021-01-08           18306.25   14347.25
plot(as.zoo(Stocks),screens = 1,lty = 1:3,xlab = "Date",ylab = "price")

#Let us calculate returns

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)
##            NESTLEIND.NS.Close NSEI.Close
## 2021-01-01          1.0000000   1.000000
## 2021-01-04          0.9960571   1.008161
## 2021-01-05          1.0058291   1.012912
## 2021-01-06          1.0034986   1.009113
## 2021-01-07          0.9824723   1.008478
## 2021-01-08          0.9921711   1.023451
plot(as.zoo(Stock_return), screens = 2, lty = 1:2, xlab = "Date", ylab = "Price")
legend("topleft", c("NESTLEIND.NS","NSEI"), lty = 1:2, cex = 0.5)

#making our returns more accurate and insghtful on perspective on investment return

Stock_change <- Stocks %>% log%>% diff
head(Stock_change)
##            NESTLEIND.NS.Close    NSEI.Close
## 2021-01-01                 NA            NA
## 2021-01-04       -0.003950734  0.0081275545
## 2021-01-05        0.009762900  0.0047013124
## 2021-01-06       -0.002319717 -0.0037571814
## 2021-01-07       -0.021175585 -0.0006293676
## 2021-01-08        0.009823399  0.0147380825
plot(as.zoo(Stock_change),screens = 2,lty = 1:3,xlab = "Date",ylab = "price")
legend("left",c("NESTLEIND.NS","NSEI"),lty = 1:3, cex = 0.5)

#simple moving avareage

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

addSMA(n=20)

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