Python Calculation of Basic Stock Metrics
Any stock can be described using descriptive statistics.
An introduction
A few metrics will be computed in Python in this blog post that provide a brief overview of a specific stock. In researching stocks, a number of factors may influence stock performance; these specific indicators can help you capture a stock's initial movement and compare it to others.
Defining metrics - an overview
Averaging is the sum of all prices divided by the total number of prices available in a period of time.
The simple rate of return. Basically, you subtract the starting value from the current value and divide it by the starting value. Multiplying a final value by 100 will give you a percentage return.
Daily Returns. Changes in price from one day to the next, expressed as a percentage.
Standard Deviation. A measure of the degree to which the values within a set are spread out. Standard deviations are higher when there is a greater range of possible outcomes. In principle, the standard deviation measures how much the values deviate from the mean; the lower the standard deviation, the more similar measurements cluster together.
Minimum Value. Your set's lowest value. A price, volume, or other metric could be used here.
Maximum Value. In your set, this value was the highest.
Simple Moving Average. It's calculated using the average of the closing prices of n recent periods, where n is how many periods we're considering. Using the moving average, you can determine the current trend of a stock by measuring the volatility of the market. Long-term trends can be shifted by changes in short-term moving averages (for example, those of five and twenty days).
Correlation. A linear relationship between two variables is measured by the strength and direction (positive or negative) of the relationship. In most cases, correlation is calculated using Pearson's r. Correlations that are positive represent a positive linear relationship, while correlations that are negative represent a negative linear relationship. Linear correlation is not present if the value is 0. Linear correlations are stronger when the value is close to 1 or -1.
Beta. In relation to a benchmark index, the standard deviation of a stock's historical price movement. Stocks are ranked according to their deviation from the index beta, which is 1.0. Stocks with betas below 1 are less volatile than indices, while stocks with betas above 1 are more volatile.
Technical Examples + Data Download
The data for SPY, the SPDR S&P 500 ETF, will be obtained through yfinance. In addition to accessing financial data from Yahoo Finance, yfinance is an open source library.
Using pip install, let's install yfinance package first.
Yfinance library can be imported after the package is installed.
Now let's download SPY data between January 1, 2018 and September 16, 2022, and check the first five rows.
Average Price Calculation
On the 'Close' column, we will run the mean() function
Simple Rate of Return Calculation
In order to calculate a simple return for the period, we need to use the following formula:
Three weeks later, you sold one share of SPY for USD315 after you bought it at USD300. Three weeks would result in a 5% return.
Our data will now be used to calculate the SPY return.
Returns Calculated Daily
The Daily Return will be calculated by adding a new column 'Daily Return' and calculating the following:
Since pct_change() does not return percentage values, the formula is multiplied by 100. Due to the fact that there are no rows before the first, the first row is also NaN.
The Standard Deviation Calculator
To calculate the standard deviation, we will use .std().
Is this number significant?
Taking into account the normal distribution of the data points. Normal distributions demonstrate that values close to the mean (the average price) occur more frequently than values further from the mean.
The standard deviation from the mean is one standard deviation 68% of the time and two standard deviations 95% of the time in a normal distribution.
There are a range of closing prices between USD 393.27 and USD 446.53 within the first standard deviation (26.63).
Calculating minimum and maximum values
The .min() and .max() functions will be used.
Simple Moving Average Calculation
SMA involves adding up the most recent data points and dividing them by the time period. The Series.rolling() method is used to compute SMA. Using the 'window' argument, we can specify any number of periods. Then plot it on a graph for a 5 and 20-day window.
Plotly library will be used for the graph.
Correlation Calculation
In order to calculate correlation, at least two different stock datasets must be used. An ETF that tracks Treasury bonds over 20 years, TLT, will be compared to SPY.
Correlation can be calculated using the .corr() function:
A correlation of 0.8 indicates a strong positive relationship. It is particularly interesting because historically TLT and SPY have been negatively correlated or have been weakly positively correlated.
Beta Calculation
ETFs tracking 500 companies are called index ETFs. Our objective is to calculate the beta of JPMorgan Chase & Co. (JPM) compared with SPY. Obtain the standard deviations of the returns of the two instruments by downloading JPM data and computing the correlation between them. JPM's beta can be calculated by plugging the values into the following equation:
Since we have daily returns for SPY, let's calculate those for JPM. To avoid getting a percentage, let's not multiply it by 100.
Now let's see how the returns correlate.
Next, we will look at the standard deviations of both instruments' returns.
It's finally time for JPM's beta release.
JPM stock is theoretically less volatile than SPY by 12% (1-0.88).
Thank you for taking the time to read this tutorial. I hope you enjoyed it. I appreciate your time.
Comments
Post a Comment