Analyzing TATA Steel and JSW Steel stock performance using Python
Steel is a versatile and widely used material known for its strength, durability, and a wide range of applications. It is primarily an alloy of iron and carbon, with small amounts of other elements often added to enhance its properties. The exact composition and characteristics of steel can vary, and there are many different types of steel with various properties and uses.
Steel is a crucial material in modern society and plays a significant role in infrastructure, manufacturing, and everyday life. Its versatility and durability make it a fundamental component of various industries. steel is a foundational industry that influences economic growth, job creation, and various sectors in the economy. Its importance extends beyond construction and manufacturing, as it plays a critical role in infrastructure development and international trade, making it an essential component of a nation’s economic well-being.
The steel industry in India is one of the largest and most important sectors in the country’s economy. It has witnessed significant growth and transformation over the years, making India one of the world’s top steel producers. Some of the prominent steel companies in India include Tata Steel, Steel Authority of India Limited (SAIL), JSW Steel, Jindal Steel and Power Limited (JSPL), and Essar Steel (now part of ArcelorMittal Nippon Steel India).
For our analysis, we will look at the stock performance of TATA Steel and JSW Steel. The stock data for NIFTY 50 is also taken for comparing and analysis in terms of benchmark. The stock data for both the companies and NIFTY 50 is taken from Yahoo finance from January 2018 to October 2023.
Before performing the analysis following steps were taken to prepare the data for analysis:
- The required python libraries like Numpy, Pandas, Matplotlib, Scipy and Seaborn were imported.
- The data was read into the dataframe with company name using read_csv method.
- The date column in the data obtained from Yahoo finance was changed to datetime for pandas library to recognize it as date and set as the index column.
- Names of the stock were stored in a list to be used further in the code
- The separate dataframes containing the stock data for both the stocks were concatenated to form one dataframe containing all the data.
All the above steps are shown below.
The concatenated data can be seen above containing data for JSW Steel, TATA Steel and NIFTY 50 in one dataframe.
Let us visualize the movement of the closing price as well as the 15 day moving averages for the stocks over the time period using the code below.
The graph above shows the movement of the adjusted closing price of the two stocks. It can be seen that both follow a similar pattern in terms of price movement. A dip in the price can be seen around the start of 2020 due to Covid lockdown and spread of the virus. But then after that, the price can be seen rising continuously till the starting months of 2021. After the starting months of 2021, the price increases slower but is also more volatile for both the companies. Overall, there is an increasing trend for the entire time period for both the companies.
We can also look at the moving averages, which helps in smoothening the daily fluctuations, for both the companies to look at the general trend using the code shown below.
The graph above shows the 15 day moving averages for the stocks of both companies.
To look at relative movement of prices for both the companies, let us now look at the normalized returns which is obtained by dividing the adjusted closing price for each day for a stock by the adjusted price on the first day of the time period. The calculation in Python is shown below.
The graph above shows the relative movement of prices for the two companies. the starting point for the both the graph is 1 and then their movement can be seen with respect to each other. Normalized returns are a useful tool for comparing assets with different price levels.
Moving forward, let us now calculate daily returns for both the companies. Daily returns informs us of the percent gain (or loss) if we bought the stock on one day and then sold it the next day. The calculations for the daily returns are shown below.
The returns are calculated as the daily percent changes in the price. These returns are also called simple or arithmetic returns. The first value of the return is NaN (Not a Number) because there was no value before it. Using daily returns several important calculations can be made like average annul returns, daily volatility, average annual volatility.
Another way of calculating returns is by using logarithm, also called log returns. Log returns are generally used for analysing an individual stock, not a portfolio of stocks. Since, we are analyzing stocks individually, we will be using log returns.
Log returns can be obtained from simple returns as well as separately. Here, we used simple returns to calculate log returns by taking logarithm of 1 + r, where r is simple returns.
Let us look at the correlation between returns of both stocks with each other and the NIFTY 50 index which we have used as benchmark. The correlation is calculated by using the .corr() method to create a correlation matrix. Correlation is a statistical measure that quantifies the degree to which two variables are related. It can take on values between -1 and 1 with 1 being the highest correlation and -1 showing a negative correlation
In the matrix above, the stocks have a 1 correlation with itself since their returns will be fully correlated with itself. The correlation matrix is visualized using a heatmap. The color scale shows the highest correlation are in red whereas the lowest are in blue. It can be seen that TATA and JSW have the highest correlation with each other (0.75 in value). A high positive correlation (0.7 to 0.99) suggests that the returns of the two assets tend to move in the same direction most of the time. We also find that JSW has a higher correlation with the NIFTY 50 than TATA.
Let us look more closely the high correlation of returns between JSW and TATA using a scatter plot as shown below.
The scatter plot above shows the relationship between the returns of JSW and TATA Steel. The x-axis has the returns for TATA whereas the y-axis has the returns for JSW Steel. In the plot, some linear relationship can be seen between their returns.
Now, let us look at the performance of the two companies in relation to the NIFTY 50 benchmark. For this, we perform a regression analysis of the returns of the two companies with the returns of the NIFTY 50 benchmark. The code shown below describes the regression analysis of returns of TATA Steel & NIFTY 50 and the returns of JSW Steel and NIFTY 50.
The code above shows the regression and its results. To perform regression analysis stats module was imported from scipy library in python. The linregress function is used to perform the regression and its results are shown in the output cell below the input cell. The function accepts two arguments, x and y.
It is important to keep in mind the x and y values or independent and dependent variable. In our case, we want to look at the movement of returns of TATA and JSW Steel against the returns of NIFTY 50 so NIFTY 50 returns is the independent variable , or x, and the returns of the companies is dependent variable, or y.
Let us look at each regression more closely.
The code above shows the regression between returns of NIFTY 50 and TATA. NIFTY 50 returns are taken as independent variable x and TATA returns are the dependent variable y. The cell below the input cell shows the result of the regression using several metrics. The slope of the regression gives the beta value of the returns of TATA. The intercept gives us the alpha value and the rvalue gives us the correlation coefficient. The returns of TATA Steel have a beta value of 1.23, alpha of around 0.000022, correlation coefficient of 0.59 (which we calculated using the .corr() method)
The beta value helps us quantify the relationship between a security and the overall market represented here by the NIFTY 50. It also helps us in quantifying how risky is the specific asset with respect to the market.
Similarly, the returns of JSW Steel have a beta value of 1.28, alpha of around 0.000026, correlation coefficient of 0.64. JSW Steel has a higher beta value, alpha value and correlation coefficient as compared to TATA Steel.
Let us now look at the annual returns and volatility of the two compaies along with the NIFTY 50
The above code shows the annual returns and volatility for the entire time period. It can be seen that JSW Steel has a higher return than TATA despite being at almost same level of volatility. The NIFTY 50 benchmark has the lowest risk but also has the lowest volatility of around 18%.
A good understanding of the returns and risk for a stock can be given by the Sharpe ratio. It is used to calculate the risk-adjusted return of an investment. The Sharpe ratio gives the return delivered per unit of risk taken. Therefore, an investment with a higher Sharpe Ratio means greater returns. Mathematically, Sharpe ratio can be calculated by the difference between the return of the stock or investment and the return that can be earned from a risk-free investment, divided by the fund’s standard deviation.
Let us calculate the Sharpe ratio for both stocks and the NIFTY 50 for the period between 31 October 2020 and 31 October 2023. Before calcualting the Sharpe, we will need the annual risk and returns for the stocks for this period. This is done in Python by using the code below.
The mean annual returns calculated during the period is stored in the annual_ret dataframe and the annual standard deviation is stored in the annual_vol (short for annual volatility) dataframe for both the companies and the NIFTY 50 index. It can be seen that TATA has the highest returns as well as the highest volatility or risk. We have obtained the returns and volatility or risk for the period under consideration. For calculating the Sharpe ratio, we have taken the yield of 10 year Indian Government bonds, which is 7.5 %, as the risk free rate. We can now calculate the Sharpe ratio using these values which is shown in the image below.
The above calculations show the Sharpe ratio for the two stocks and the NIFTY 50 index. It can be seen that for the period under consideration TATA Steel has the highest Sharpe ratio followed by JSW Steel and the NIFTY 50 benchmark.
Although Sharpe ratio is an important metric, it has some limitation. One of the limitations of the Sharpe ratio is that it is based on standard deviation. A higher standard deviation will bring down the Sharpe ratio. But what we need to be mindful of is that the standard deviation takes into account all types of returns, i.e., both negative and positive deviations from average or mean. Therefore, even if the positive deviations are higher, the standard deviation could be higher, which may portray the stock or investment to be risky while actually, that may not be the case.
This brings us to the end of the analysis. We looked at various ways of analyzing stock performance using various metrics. The analysis done is focused on analyzing and comparing stock performance of two companies in the same industry. This analysis can be further expanded to companies in different industry or different assets as well. In the next project, we will look at analyzing a portfolio of securities.