# Stock Market Analysis with R programming language

Updated: Sep 18, 2020

**This blog covers the fall and recovery of Indian Stock Market during this pandemic period. This analysis is done by using R programming language to study the related dataset**

__Click here to view this article on Medium__

Case :

The stock market of India fell by 41.2% to be precise during the period between January to April. At the starting of this year most of the companies were at it's peak and turned down to be 52 week low (considered as lowest ever in the year). Now, the stock market started recovering and the gain percentage in an average is 43% but still there are pending recovery values which need to be retrieved. This analysis is taken sector wise and is compared with the NSE (National Stock Exchange) indices movement. All these data and scrutiny (observation) is between Jan-01-2020 to Jun-09-2020. This analysis also reveals how the stocks and index performed during this period and which are the best and worst recovered companies.

R as a Analysis Tool

R programming language is considered as one of the powerful language for Data Science and it is also very accessible for users. Software used for coding R is RStudio and there are also other platforms to code R which you can prefer. This language is widely used by Statisticians and Data miners for data analysis and for developing statistical software. Let's see how to do Stock Analysis with R :

1. Importing dataset using **Import Dataset **:

2. Read the dataset file which you have imported :

3. Coding for our required Analysis (I have extracted the data from NSE Website, the above one is just an example, now I'm coding with a different dataset)

This is an example with Wipro :

```
library(dplyr)
#Examining the lowest value of wipro
> min(wipro_index$Close)
162.35
#Examining the highest value of wipro
> max(wipro_index$Close)
257.2
#Calculating fall value of wipro
> max(wipro_index$Close) - min(wipro_index$Close)
94.85
#Calculating fall percentage of wipro
> 94.85 / max(wipro_index$Close) * 100
36.88
#Examining the recpvery of wipro
> max(wipro_may$'Close Price')
226.45
#Calculating recovery value of wipro
> max(wipro_may$'Close Price') - min(wipro_index$Close)
64.1
#Calculating recovery percentage of wipro
> 64.1 / min(wipro_index$Close) * 100
39.48
#Calculating pending recovery value of wipro
> max(wipro_index$Close) - max(wipro_may$'Close Price')
30.75
#Calculating pending recovery percentage of wipro
> 30.75 / max(wipro_index$Close) * 100
11.96
#Summarizing our analysis of wipro
> wipro_index %>%
summarize(high = max(wipro_index$Close) ,
low = min(wipro_index$Close) ,
fall_value = max(wipro_index$Close) - min(wipro_index$Close) ,
fall_percentage = 94.85 / max(wipro_index$Close) * 100 ,
recovery = max(wipro_may$`Close Price`) ,
recovery_value = max(wipro_may$`Close Price`) - min(wipro_index$Close) ,
recovery_percentage = 64.10 / min(wipro_index$Close) * 100 ,
pending_recovery = max(wipro_index$Close) - max(wipro_may$`Close Price`) ,
pending_percentage = 30.75 / max(wipro_index$Close) * 100 )
#Output
high low fall_value fall_percentage recovery recovery_value
257.2 162.35 94.85 36.88 226.45 64.1
recovery_percentage pending_recovery pending_percentage 39.4826 30.75 11.96
```

From this coding we can generate our specified result which we want form the dataset. After getting our data from R we can use Excel for a neat tabulation for our Analysis. In this program I have used functions including : "min" ,"max" , "summarize".

min - "min" function returns the minimum value of a vector or column.

max - It returns the highest or the maximum value of a vector or column.

summarize - The "summarize" function reduces a data frame into a summary of just one vector or value.

** ** **Tabulation of our Data **

Graph comparison of Values

Data visualization is very important in the financial world especially for Analysis. Instead of creating a tabulation, a simple graph or any way of representation is easy to understand and it is very useful for comparison too. In this blog I've compared the values of companies using line graph for easy understanding.

1. Nifty 50

From this graph we can understand that Nifty 50 index was at it's peak on 14-Jan-2020 and had a fall price of 7,610.250 on 23-Mar-2020. We can also observe that the price of 12,362.30 came down to 7,610.250 in just 68 days which is 38.44% from it's peak value. The next thing we can observe from this graph is the recovery value of Nifty 50. The stock values of Nifty 50 zoomed from 7,610 to 10,167 in only 78 days and had a jump percentage of 33.6 %. The last thing about this graph is that, even though the prices spiked in 78 days it couldn't make up to it's peak price of 12,362. So ,we can conclude that the pending recovery value of Nifty 50 is still 2,195 and needs to shine up for a percentage of 21.59%.

2. Wipro and ITC (Indian Tobacco Company)

This is a graph comparing stock prices of two companies ,Wipro and ITC. We can observe that the movement of both the companies are relatively similar during this period. The stock price of Wipro on January was 257.2, had a fall of 94.85 and reached a stock price of 162. The percentage of fall value is 36.88 %. Soon it recovered on June and zoomed to a price of 226 and had a recovery rate of

39.4%. When we compare the peak price of Wipro with the recovery price ,the pending recovery value is 30.75 and has to rise 13.58%. Coming to ITC ,it's peak price was 243.25 and declined to a price of 147.25 during this period. This company had a fall value of 96 and declined by 39.47%. The stock prices are appreciated to 200.55 between March and June, the recovery rate was 36.20%. In case of pending recovery value, Wipro has a less pending recovery rate when compared to ITC which is at 21.29%. We can say that Wipro performed well during this period as it has less pending recovery rate when compared to ITC.

3. Tata Motors and Gail

This graph compares the stock prices of two companies Tata Motors and Gail. We can notice that the peak price of these two companies differ but in case of declined price they are similar. The peak price of Tata Motors was 200.35 and the declined price is 65.30. From these prices we can observe that Tata Motors has phenomenal fall value and rate. The fall value of Tata motors is 135.05 and declined percentage is 64.41%. Inspite of having drastic depreciation in prices Tata Motors recovered in a very short span of time. It zoomed to a price of 115.45 from a declined price of 65.30. The recovery rate is 76.80%. But still the pending recovery value is 84.90 with a rate of 73.54%. Coming to Gail the price movements are not so phenomenal when compared to Tata Motors. The peak price was 131.65 and the declined price is 69.40 which is more similar to Tata Motors. The fall value and rate of Gail is 62.25 and 42.28% respectively. The recovery price is 104.75 which can be considered as a good combe back when compared to Tata Motors. The pending recovery value of Gail is 26.90 with a percentage of 25.68%. This graph comparison clearly reveals that the price movements of Tata Motors during this period was very volatile.

4. Reliance , L & T and Asian Paints

This graph comprises the stock prices of three companies namely Reliance, L & T and Asian Paints. The stock price of Reliance was it's peak value of 1,581 and had a price depreciation to 884.05. But the recovery rate was phenomenally high which is higher than it's peak price. The recovery price is 1,581.70, a increase rate of 0.7% from it's peak price. Obviously there is no pending recovery rate as the price exceeds from it's peak value. The highest stock price of L & T was 1,370.20. The fall price and value of L & T is 707.90 and 662.30 respectively. The down rate of L & T is 48.34%. The recovery price of L & T is 961.35 and the rate is 35.80. The pending recovery value of L & T is 408.85. From this we can say that, the performance of L & T is poor when compared to L & T. Coming to Asian Paints, considering all the peak prices Asian Paints is top among that. The peak price of Asian Paints was 1,893.70 and the declined price is 1,498.45. The recovery price is 1,716.55, from this we can note that the pending value and rate of Asian Paints would be lower than L & T. The pending recovery value and rate of Asian Paints are 177.15 and 10.32%. Like this we can predict the values easily in charts or any types of representations without doing any calculations.

5. SBI and Sun Pharma

This line chart represents the stock prices of SBI and Sun Pharma. The price movements of SBI is very volatile during this period as we can observe from the chart. The peak price of SBI was 339.3 and had a staggering price decline of 175.5. The fall percentage of SBI is 48.28%. The recovery rate of SBI is only 7.01% which is considered as the lowest of all companies. The pending recovery value is 151.50 with a phenomenal pending recovery rate of 80.67%. The next following company is Sun Pharma whose performance is good when compared to SBI. The peak price of Sun Pharma was 489.9 and had a price depreciation of 324.5 with a fall value and rate 165.40 and 33.76% respectively. Sun Pharma has an exceptionally good recovery price and rate. The recovery price of Sun Pharma is 500.75 which is greater than it's peak price of 489.9 with a rate of 2.17%. It is obvious that Sun Pharma has no pending recovery value. The last company is Airtel. It is considered as the most profitable company in this whole dataset. The peak price of Airtel was 565 and had a price depreciation to 404.05. The fall value and percentage are 160.95 and 28.49% respectively. However having a fall percentage of 28.49%, Airtel has a surprising recovery price of 598.8 which is higher than it's peak value with increase in rate of 5.64%. From this we can conclude that the performance Sun Pharma and Airtel is far beter than SBI.

Summarizing the Best and Worst Performances

After all these analysis, in this pandemic period some companies have performed exceptionally well and some or not. So, I've picked three best performing companies and three worst performing companies from this analysis.

Best Performing Companies :

Reliance

Sun Pharma

Airtel

The reasons for picking out these companies is that they have phenomenal recovery rate and no pending recovery value. The price movements were also not too volatile, so these companies might turn out to be good investment options.

Worst Performing Companies :

SBI

Tata Motors

L & T

These companies performed well in the previous year but during this pandemic the price movements of this three companies were very volatile and also they have a phenomenal pending recovery value and rate. So, it is not advisable to invest in these companies during this pandemic period.

Data science not only comes under computer science but also in the financial analysis. Data Science is very important in business, economics etc. In economics, using data science we can do our own demand analysis series which plays a key role in Economic planning for making future projection of demand, sales, prices, cost allocation etc. Coming to business, data science has a huge impact in it as the complexity of business goes on. One of the important application of data science in business is decision-making. Not only in economics and business there are many fields where data science is used and it is also considered as one of the trending topics to explore.

The below link is an interesting online basic data science course offered by Harvard University:

https://www.edx.org/course/data-science-r-basics