Analyzing Stock Using R

Author

Rajasree Konda

Published

November 21, 2022

Introduction

Insfosys Limited is the first Indian Tech company listed on Nasdaq in the year 1999 and shifted to NYSE in the year 2012. On the other hand Wipro Limited got its stock listed on NYSE in the year 2000. The mere fact that these two were only the top indian tech companies on NYSE since ages and because of their performance many investors believe to hold the shares. An in-dept analysis should be done before purchasing , holding or selling a stock based on speculation,

I implemented my working knowledge on stock market and R to analyze the stocks . Also i need to make it clear that i dont have any experience in stock market nor do i am an expert in this field, this project is just an experiment to improve my learning on Language R and Stock market.

Before you load the data u need few packages to be installed. All the packages that i used for this project are frequently used in quantitative finance.

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(plotly)
Loading required package: ggplot2

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(ggplot2)
library(magrittr)
library(broom)

Loading stock data

I had used Quantmod to load stock data from yahoo finance

stock_list <- c("WIT", "INFY")
start_date <- Sys.Date()-365
end_date <- Sys.Date()
master_df <- NULL
for (idx in seq(length(stock_list))){
  stock_index = stock_list[idx]
  getSymbols(stock_index, verbose = TRUE, src = "yahoo", 
             from=start_date,to=end_date)
  temp_df = as.data.frame(get(stock_index))
  temp_df$Date = row.names(temp_df)
  temp_df$Index = stock_index
  row.names(temp_df) = NULL
  colnames(temp_df) = c("Open", "High", "Low", "Close", 
                        "Volume", "Adjusted", "Date", "Index")
  temp_df = temp_df[c("Date", "Index", "Open", "High", 
                      "Low", "Close", "Volume", "Adjusted")]
  master_df = rbind(master_df, temp_df)
}
downloading  WIT .....

done.
downloading  INFY .....

done.

OHLC Charts

An OHLC chart is a type of bar chart that shows open, high, low, and closing prices for each period. OHLC charts are useful since they show the four major data points over a period, with the closing price being considered the most important by many traders.

generally, the colors are green and red for OHLC charts but here i used blue and red.

first chart shows infosys prices and the next one shows wipro prices.

df <- data.frame(Date=index(INFY),coredata(INFY))
df <- tail(df, 30)

i <- list(line = list(color = '0000ff'))
d <- list(line = list(color = '#FF0000'))


fig <- df %>% plot_ly(x = ~Date, type="ohlc",
                      open = ~INFY.Open, close = ~INFY.Close,
                      high = ~INFY.High, low = ~INFY.Low,
                      increasing = i, decreasing = d) 
fig <- fig %>% layout(title = "Infosys OHLC Chart")

fig
df <- data.frame(Date=index(WIT),coredata(WIT))
df <- tail(df, 30)

i <- list(line = list(color = '0000ff'))
d <- list(line = list(color = '#FF0000'))

fig <- df %>% plot_ly(x = ~Date, type="ohlc",
                      open = ~WIT.Open, close = ~WIT.Close,
                      high = ~WIT.High, low = ~WIT.Low,
                      increasing = i, decreasing = d) 
fig <- fig %>% layout(title = "Wipro OHLC Chart")

fig

Comparing Closing prices of stocks

I need more analysis on closing prices of stock at this moment and for that i compared closing prices of the stocks using zoo and plot.

Using plot i had created a graph of closing prices of both the companies individually

first one represents infosys closing prices and the next one represents wipro closing prices.

plot(INFY$"INFY.Close", main = "Closing Prices of infosys limited stocks")

plot(WIT$"WIT.Close", main = "Closing Prices of wipro limted stocks")

Now i made a single chart to compare the closing prices, my main idea here is to create a xts object for closing prices and calling it as stocks.

stocks <- as.xts(data.frame(infosys = INFY$"INFY.Close",wipro = WIT$"WIT.Close"))
head(stocks)
           INFY.Close WIT.Close
2021-11-22      22.77      8.72
2021-11-23      22.89      8.75
2021-11-24      22.61      8.73
2021-11-26      22.20      8.38
2021-11-29      22.62      8.56
2021-11-30      22.58      8.54
tail(stocks)
           INFY.Close WIT.Close
2022-11-11      19.75      5.05
2022-11-14      19.55      4.99
2022-11-15      19.76      4.99
2022-11-16      19.69      4.98
2022-11-17      19.48      4.94
2022-11-18      19.47      4.91

Using plot and zoo created a graph for stocks.

plot(as.zoo(stocks[,c("WIT.Close")]),screens = 1,lty = c(1,3), col = c("blue"),xlab = "Months",ylab = "Price")
par(new =TRUE)
plot(as.zoo(stocks$"INFY.Close"),screens = 1,lty = 5, col = "yellow",xlab = "",ylab = "", xaxt = "n", yaxt = "n")
axis(4)
mtext("Price", side = 4, line = 3)
legend("topleft",c("WIT(left)","INFY(right)"),lty = c(1,3,5),cex = 0.5)

The infosys stock was trading at 25.31 USD at the beginning of the year. Since then, INFY shares have decreased by 23.1 percent and is now trading at 19.47 USD. Whereas Wipro stock was trading at 9.76 USD at the beginning of 2022. Since then, WIT shares have decreased by 49.7 percent and is now trading at 4.91 USD. Also, infosys is 8.2 percent upside the stock price where wipro is 2.5 percent downside.

Comparing the numbers and analysis, i still think i can surely hold infosys stock than the other.

Reference:

https://plotly.com/r/ohlc-charts/

cran/quantmod/man/getSymbols

quarto.org/docs/computations

colorPaletteCheatsheet

xtscheatsheet