# Stock price analysis with Python

#### Contents

Hello readers! We all know how the Stock Market works. A share is the small part of the property of the company. The company's share price reflects the net evaluation of the company and also gives a small idea of ​​its performance.. These shares are traded on exchanges and their prices are constantly changing due to their demand and supply in the market.. If a stock has high demand and low supply, namely, more people want to buy it and fewer people are willing to sell it, then the price of the stock will rise and similarly if the stock has low demand and high supply, which means more people are willing to sell it, but fewer people are willing to buy it and its prices go down.

The sudden increase in demand for stocks can be due to a number of reasons, including positive news about the company or a company announcement. After a period of time in which the demand for the shares disappears, their prices slowly decline as the investor loses interest in them. These rising and falling stock prices are an iterative and repeated process. This volatility in stocks makes investors nervous when investing in a company. Then, to understand the risk associated with it, there must be a proper analysis of the shares before buying them. In this article, we would try to explore only the tip of the iceberg for stock market analysis, since technical value analysis is a vast field. This blog can be the starting point in this industry.

The tool is not important for analysis, can be done in any statistical software like Python, R o Excel, but for the sake of this article, we are demonstrating parsing in python.

## Contents

• Libraries used
• Description of data
• Exploratory analysis
• Scattered plot matrix
• Moving averages
• Percentage increase in the value of shares
• Conclution

## Libraries used

The following are the libraries that must be installed beforehand and can be easily downloaded with the help of the pip function. the following is a brief description of the name of the library and its application.

 Library Application Yahoo Finance To download stock data Pandas To handle data frameworks in Python Numpy Numeric Python Matplotlib Plot charts
```import pandas as pd

import datetime

import numpy as np

import matplotlib.pyplot as plt

from pandas.plotting import scatter_matrix

!pip install yfinance

import yfinance as yf

%matplotlib inline```

## Description of data:

We have downloaded daily stock price data using Yahoo Finance API functionality. It is a data capture of five years of openness, maximum, minimum, closure and volume.

• Opened: The price of the stock when the market opens in the morning.
• Closing: the price of the stock when the market closed overnight.
• Alto: highest price the stock reached on that day
• Low: lowest price the stock is trading that day
• Volume: the total number of shares traded that day.

Here, we will take the example of three companies, TCS, Infosys and Wipro, who are industry leaders in IT service delivery.

```start = "2014-01-01"
end = '2019-1-01'

### Exploratory analysis

```Tcs['Open'].plot(label="TCS", figsize = (15,7))
infy['Open'].plot(label = "Infosys")
wipro['Open'].plot(label="Wipro")
plt.title('Stock Prices of TCS, Infosys and Wipro')```

The graph above is the representation of open stock prices for these three companies through a line chart leveraging the matplotlib library in Python. The chart clearly shows that Wipro's prices are more when compared to two other companies., but we are not interested in the absolute prices of these companies, but we wanted to understand how these actions fluctuate over time..

```Tcs['Volume'].plot(label="TCS", figsize = (15,7))
infy['Volume'].plot(label = "Infosys")
wipro['Volume'].plot(label="Wipro")
plt.legend()```

The chart shows the volume traded by these companies, which clearly shows that Infosys shares are traded more compared to other IT stocks.

```#Market Capitalisation
tcs['MarktCap'] = tcs['Open'] * Tcs['Volume']
infy['MarktCap'] = infy['Open'] * infy['Volume']
wipro['MarktCap'] = wipro['Open'] * wipro['Volume']
Tcs['MarktCap'].plot(label="TCS", figsize = (15,7))
infy['MarktCap'].plot(label="Infosys")
wipro['MarktCap'].plot(label="Wipro")
plt.title('Market Cap')
plt.legend()```

Only volume or stock prices do not provide a cross-company comparison. In this case, we have drawn a volume chart * Share price to better compare companies. As we can clearly see in the graph, Wipro seems to be trading on a higher side.

## Moving averages

As we know, stock prices are very volatile and prices change rapidly over time. To observe any trend or pattern we can take the help of an average of 200 days of 50 days

```Tcs['MA50'] = tcs['Open'].rolling(50).mean()
Tcs['MA200'] = tcs['Open'].rolling(200).mean()
Tcs['Open'].plot(figsize = (15,7))
Tcs['MA50'].plot()
Tcs['MA200'].plot()```

## Scattered plot matrix

```data = pd.concat([Tcs['Open'],infy['Open'],wipro['Open']],axis = 1)
data.columns = ['TCSOpen','InfosysOpen','WiproOpen']
scatter_matrix(data, figsize = (8,8), hist_kwds= {'bins':250})```

The graph above is the combination of histograms for each company and a subsequent scatter plot that takes the shares of two companies at once.. From the chart, we can clearly deduce that Wipro's actions vaguely show a linear correlation with Infosys.

## Percentage increase in the value of shares

A percentage increase in the value of the stock is the change in stocks compared to the previous day. The higher the value, either positive or negative, the more volatile the stock will be.

```#Volatility
tcs['returns'] = (Tcs['Close']/Tcs['Close'].shift(1)) -1
infy['returns'] = (infy['Close']/infy['Close'].shift(1))-1
wipro['returns'] = (wipro['Close']/wipro['Close'].shift(1)) - 1
Tcs['returns'].hist(bins = 100, label="TCS", alpha = 0.5, figsize = (15,7))
infy['returns'].hist(bins = 100, label="Infosysy", alpha = 0.5)
wipro['returns'].hist(bins = 100, label="Wipro", alpha = 0.5)
plt.legend()```

It is clear from the graph that the percentage increase in the TCS share price histogram is the widest, indicating that TCS shares are the most volatile among the three companies compared.

## Conclution

The above analysis can be used to understand the short-term and long-term behavior of a stock.. A decision support system can be created for which stocks to choose from the industry for low risk low profit or high risk high profit, depending on the risk apatite of the investor.

Feel free to connect through Linkedin: