Introduction

Short run performance analysis using Technical Analysis(SMA) on the selected STOCK and INDEX

We will be fetching the data(historical prices) from the website(Yahoo) on selected stock and Index. this is called Web Scrapping.

Selected Stock- TATAMOTORS

Selected Index- NIFTY50

Data Duration- April 1, 2021 to March 31, 2023

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 start date to fetch the data from yahoo

start <- as.Date("2021-04-01")
end <- as.Date("2023-03-31")

The First dataset- Closing Prices of TATAMOTORS.NS

getSymbols("TATAMOTORS.NS",src="yahoo",from = start, to = end) # SYNATAX is universal --important to do
## [1] "TATAMOTORS.NS"

The Second dataset- Closing Prices of ‘^NSEI- NIFTY50’

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

Exploring the DatasetS

FIRST DATASET

head(TATAMOTORS.NS)
##            TATAMOTORS.NS.Open TATAMOTORS.NS.High TATAMOTORS.NS.Low
## 2021-04-01             306.75             309.85            303.05
## 2021-04-05             306.80             311.70            297.20
## 2021-04-06             306.15             313.80            304.80
## 2021-04-07             306.75             310.65            305.10
## 2021-04-08             307.90             319.80            307.50
## 2021-04-09             313.20             325.00            312.50
##            TATAMOTORS.NS.Close TATAMOTORS.NS.Volume TATAMOTORS.NS.Adjusted
## 2021-04-01              307.75             44088352               306.7938
## 2021-04-05              305.05             66178755               304.1022
## 2021-04-06              307.75             63031783               306.7938
## 2021-04-07              307.80             39073986               306.8437
## 2021-04-08              313.95             62459774               312.9746
## 2021-04-09              318.20             75462572               317.2114
tail(TATAMOTORS.NS)  
##            TATAMOTORS.NS.Open TATAMOTORS.NS.High TATAMOTORS.NS.Low
## 2023-03-22             416.00             418.40            413.75
## 2023-03-23             416.00             422.50            416.00
## 2023-03-24             419.00             420.55            414.80
## 2023-03-27             416.00             418.50            410.25
## 2023-03-28             414.15             414.50            400.45
## 2023-03-29             401.60             410.60            401.60
##            TATAMOTORS.NS.Close TATAMOTORS.NS.Volume TATAMOTORS.NS.Adjusted
## 2023-03-22              416.10              5728054               414.8072
## 2023-03-23              419.15              9817460               417.8477
## 2023-03-24              416.50              6877960               415.2059
## 2023-03-27              412.05              7433933               410.7697
## 2023-03-28              401.60              9553659               400.3522
## 2023-03-29              409.20             10478506               407.9286

SECOND DATASET

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-03-22  17177.45  17207.25 17107.85   17151.90           0      17151.90
## 2023-03-23  17097.40  17205.40 17045.30   17076.90      219200      17076.90
## 2023-03-24  17076.20  17109.45 16917.35   16945.05      228000      16945.05
## 2023-03-27  16984.30  17091.00 16918.55   16985.70      218400      16985.70
## 2023-03-28  17031.75  17061.75 16913.75   16951.70      238800      16951.70
## 2023-03-29  16977.30  17126.15 16940.60   17080.70      345900      17080.70

Let us get a plot and a candlestick chart FOR EACH DATASET

FIRST DATASET

plot(TATAMOTORS.NS[,"TATAMOTORS.NS.Close"],main = "TATAMOTORS Closing Prices",col="brown")

candleChart(TATAMOTORS.NS, up.col="green",dn.col="red",theme ="white") # A CANDLE HAS 'TOP' AND 'BOTTOM' 

#Point to remember: Technical analysis- short run- less than a year to visualise candles properly

SECOND DATASET

plot(NSEI[,"NSEI.Close"],main = "NIFTY50 Closing Prices",col="black")

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

### LET US SHOW THE DATA TOGETHER

head(list(TATAMOTORS.NS,NSEI))
## [[1]]
##            TATAMOTORS.NS.Open TATAMOTORS.NS.High TATAMOTORS.NS.Low
## 2021-04-01             306.75             309.85            303.05
## 2021-04-05             306.80             311.70            297.20
## 2021-04-06             306.15             313.80            304.80
## 2021-04-07             306.75             310.65            305.10
## 2021-04-08             307.90             319.80            307.50
## 2021-04-09             313.20             325.00            312.50
## 2021-04-12             307.85             307.85            282.90
## 2021-04-13             287.25             304.55            280.55
## 2021-04-15             303.00             307.55            296.50
## 2021-04-16             302.00             315.00            300.15
##        ...                                                        
## 2023-03-16             413.30             418.50            405.00
## 2023-03-17             419.00             423.45            414.55
## 2023-03-20             415.60             416.95            405.50
## 2023-03-21             412.85             413.90            407.70
## 2023-03-22             416.00             418.40            413.75
## 2023-03-23             416.00             422.50            416.00
## 2023-03-24             419.00             420.55            414.80
## 2023-03-27             416.00             418.50            410.25
## 2023-03-28             414.15             414.50            400.45
## 2023-03-29             401.60             410.60            401.60
##            TATAMOTORS.NS.Close TATAMOTORS.NS.Volume TATAMOTORS.NS.Adjusted
## 2021-04-01              307.75             44088352               306.7938
## 2021-04-05              305.05             66178755               304.1022
## 2021-04-06              307.75             63031783               306.7938
## 2021-04-07              307.80             39073986               306.8437
## 2021-04-08              313.95             62459774               312.9746
## 2021-04-09              318.20             75462572               317.2114
## 2021-04-12              286.55            105342538               285.6597
## 2021-04-13              302.85            116874853               301.9091
## 2021-04-15              303.50             75262749               302.5570
## 2021-04-16              310.00             75330709               309.0368
##        ...                                                                
## 2023-03-16              415.70             11854885               414.4084
## 2023-03-17              419.00              8658701               417.6982
## 2023-03-20              410.75             10075022               409.4738
## 2023-03-21              412.55              7391950               411.2682
## 2023-03-22              416.10              5728054               414.8072
## 2023-03-23              419.15              9817460               417.8477
## 2023-03-24              416.50              6877960               415.2059
## 2023-03-27              412.05              7433933               410.7697
## 2023-03-28              401.60              9553659               400.3522
## 2023-03-29              409.20             10478506               407.9286
## 
## [[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-03-16  16994.65  17062.45 16850.15   16985.60      349800      16985.60
## 2023-03-17  17111.80  17145.80 16958.15   17100.05      408100      17100.05
## 2023-03-20  17066.60  17066.60 16828.35   16988.40      241800      16988.40
## 2023-03-21  17060.40  17127.70 17016.00   17107.50      246700      17107.50
## 2023-03-22  17177.45  17207.25 17107.85   17151.90           0      17151.90
## 2023-03-23  17097.40  17205.40 17045.30   17076.90      219200      17076.90
## 2023-03-24  17076.20  17109.45 16917.35   16945.05      228000      16945.05
## 2023-03-27  16984.30  17091.00 16918.55   16985.70      218400      16985.70
## 2023-03-28  17031.75  17061.75 16913.75   16951.70      238800      16951.70
## 2023-03-29  16977.30  17126.15 16940.60   17080.70      345900      17080.70

Let us get the stock AND Index closing prices in one tabular form

stocks<-as.xts(data.frame(TATAMOTORS.NS=TATAMOTORS.NS[,"TATAMOTORS.NS.Close"],NSEI=NSEI[,"NSEI.Close"]))
stocks
##            TATAMOTORS.NS.Close NSEI.Close
## 2021-04-01              307.75   14867.35
## 2021-04-05              305.05   14637.80
## 2021-04-06              307.75   14683.50
## 2021-04-07              307.80   14819.05
## 2021-04-08              313.95   14873.80
## 2021-04-09              318.20   14834.85
## 2021-04-12              286.55   14310.80
## 2021-04-13              302.85   14504.80
## 2021-04-15              303.50   14581.45
## 2021-04-16              310.00   14617.85
##        ...                               
## 2023-03-16              415.70   16985.60
## 2023-03-17              419.00   17100.05
## 2023-03-20              410.75   16988.40
## 2023-03-21              412.55   17107.50
## 2023-03-22              416.10   17151.90
## 2023-03-23              419.15   17076.90
## 2023-03-24              416.50   16945.05
## 2023-03-27              412.05   16985.70
## 2023-03-28              401.60   16951.70
## 2023-03-29              409.20   17080.70
head(stocks)
##            TATAMOTORS.NS.Close NSEI.Close
## 2021-04-01              307.75   14867.35
## 2021-04-05              305.05   14637.80
## 2021-04-06              307.75   14683.50
## 2021-04-07              307.80   14819.05
## 2021-04-08              313.95   14873.80
## 2021-04-09              318.20   14834.85
plot(as.zoo(stocks),screens=1,lty=1:2,xlab="Date",ylab="Price")

Let us calculate the 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
stock_return
##            TATAMOTORS.NS.Close NSEI.Close
## 2021-04-01           1.0000000  1.0000000
## 2021-04-05           0.9912266  0.9845601
## 2021-04-06           1.0000000  0.9876340
## 2021-04-07           1.0001624  0.9967513
## 2021-04-08           1.0201463  1.0004338
## 2021-04-09           1.0339562  0.9978140
## 2021-04-12           0.9311129  0.9625656
## 2021-04-13           0.9840780  0.9756144
## 2021-04-15           0.9861901  0.9807700
## 2021-04-16           1.0073111  0.9832183
##        ...                               
## 2023-03-16           1.3507718  1.1424766
## 2023-03-17           1.3614947  1.1501748
## 2023-03-20           1.3346872  1.1426650
## 2023-03-21           1.3405361  1.1506758
## 2023-03-22           1.3520715  1.1536623
## 2023-03-23           1.3619821  1.1486177
## 2023-03-24           1.3533712  1.1397493
## 2023-03-27           1.3389114  1.1424833
## 2023-03-28           1.3049553  1.1401964
## 2023-03-29           1.3296507  1.1488732
head(stock_return)
##            TATAMOTORS.NS.Close NSEI.Close
## 2021-04-01           1.0000000  1.0000000
## 2021-04-05           0.9912266  0.9845601
## 2021-04-06           1.0000000  0.9876340
## 2021-04-07           1.0001624  0.9967513
## 2021-04-08           1.0201463  1.0004338
## 2021-04-09           1.0339562  0.9978140
plot(as.zoo(stock_return),screens=1,lty=1:2,xlab="Date",ylab="Price")
legend("topleft",c("TATAMOTORS.NS","NSEI"),lty=1:2, cex=0.5)

Here, it can be observed that the the volatility is more for TATAMOTORS returns as compared to NIFTY50 returns.

Making our returns more accurate and insightful perspective on investment returns

Using log returns –to get the real impact

stock_change<- stocks %>% log %>% diff # This gives the relative closing price change.
stock_change
##            TATAMOTORS.NS.Close    NSEI.Close
## 2021-04-01                  NA            NA
## 2021-04-05       -0.0088121075 -0.0155602965
## 2021-04-06        0.0088121075  0.0031172037
## 2021-04-07        0.0001624167  0.0091890877
## 2021-04-08        0.0197835947  0.0036877607
## 2021-04-09        0.0134463778 -0.0026221466
## 2021-04-12       -0.1047671559 -0.0359646328
## 2021-04-13        0.0553246552  0.0134651323
## 2021-04-15        0.0021439569  0.0052705707
## 2021-04-16        0.0211906870  0.0024931716
##        ...                                  
## 2023-03-16        0.0105194272  0.0007921149
## 2023-03-17        0.0079070443  0.0067155299
## 2023-03-20       -0.0198861630 -0.0065506520
## 2023-03-21        0.0043726240  0.0069861826
## 2023-03-22        0.0085682497  0.0025920136
## 2023-03-23        0.0073032059 -0.0043822815
## 2023-03-24       -0.0063423749 -0.0077508941
## 2023-03-27       -0.0107417900  0.0023959657
## 2023-03-28       -0.0256880880 -0.0020036899
## 2023-03-29        0.0187474803  0.0075810477
head(stock_change)
##            TATAMOTORS.NS.Close   NSEI.Close
## 2021-04-01                  NA           NA
## 2021-04-05       -0.0088121075 -0.015560296
## 2021-04-06        0.0088121075  0.003117204
## 2021-04-07        0.0001624167  0.009189088
## 2021-04-08        0.0197835947  0.003687761
## 2021-04-09        0.0134463778 -0.002622147
plot(as.zoo(stock_change),screens=1,lty=1:2,xlab="Date",ylab="Price",col= c("red","blue"))
legend("topleft", c("TATAMOTORS.NS","NSEI"),lty=1:2, cex=0.5)

Simple Moving Averages

For TATAMOTORS

candleChart(TATAMOTORS.NS, up.col="green",dn.col="red",theme ="beige")

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

Let us see the visuals for ‘6 months at a time’ for TATMOTORS in chronological order in the given duration of study.

candleChart(TATAMOTORS.NS, up.col="green",dn.col="red",theme ="beige",subset= "2021-04-01/2021-09-30")

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

candleChart(TATAMOTORS.NS, up.col="green",dn.col="red",theme ="beige",subset= "2021-10-01/2022-03-31")

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

candleChart(TATAMOTORS.NS, up.col="green",dn.col="red",theme ="beige",subset= "2022-04-01/2022-09-30")

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

candleChart(TATAMOTORS.NS, up.col="green",dn.col="red",theme ="beige",subset= "2022-10-01/2023-03-31")

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

FOR NIFTY50

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

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

Let us see the visuals for ‘6 months at a time’ for NIFTY50 in chronological order in the given duration of study.

candleChart(NSEI, up.col="green",dn.col="red",theme ="white",subset= "2021-04-01/2021-09-30")

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

candleChart(NSEI, up.col="green",dn.col="red",theme ="white",subset= "2021-10-01/2022-03-31")

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

candleChart(NSEI, up.col="green",dn.col="red",theme ="white",subset= "2022-04-01/2022-09-30")

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

candleChart(NSEI, up.col="green",dn.col="red",theme ="white",subset= "2022-10-01/2023-03-31")

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

Conclusion