Calculating Daily Volatility of Stocks Using Python on Indian Stocks

In the financial markets, having a clear understanding of the risks and movements associated with stocks is essential for making informed investment decisions. A key metric in this arena is ‘Daily Volatility,’ which measures the variation in a stock’s price over a single trading day. 

This measure is crucial for investors and traders to gauge the level of uncertainty associated with a particular stock or market segment.

Understanding Daily Volatility:

The formula to calculate Daily Volatility is:

$$ \text{Daily Volatility} = \sqrt{\frac{1}{N-1} \sum_{i=1}^{N} (r_i – \bar{r})^2} $$

where:

  • \( r_i \) is the daily return on day \( i \).
  • \( \bar{r} \) is the mean of daily returns over the period.
  • \( N \) is the total number of days in the period being analyzed.
Daily returns (\( r_i \)) are calculated by comparing the closing price of a stock on a given day with its closing price on the previous day.

The formula for daily volatility, in terms of standard deviation of daily returns, is:

$$ \text{Daily Volatility} = \text{Std}(r) $$

Here, \( \text{Std}(r) \) represents the standard deviation of daily returns \( r \), measuring the variation in daily returns from their average, indicating the daily volatility.

Significance of Daily Volatility:

Daily Volatility is a statistical measure that indicates the degree to which a stock’s price fluctuates within a trading day.

  • High daily volatility signifies greater uncertainty and risk.
  • Low volatility suggests a level of stability in the stock’s price movements.

The measure of daily volatility serves as a beacon for investors and traders to assess the risk entailed in holding a particular stock. It offers a glimpse into the possible price fluctuations a stock could exhibit, thereby aiding in making informed decisions based on one’s risk tolerance and investment horizon.

Daily volatility finds its use in various applications, including portfolio management, risk assessment, and derivative pricing. By understanding the daily volatility, investors can better position their portfolios to align with their risk profiles, and traders can devise strategies to capitalize on price fluctuations. 

Calculating Daily Volatility:

The journey to computing daily volatility begins with the extraction of historical stock prices, followed by the calculation of daily returns. Daily returns are obtained by comparing the closing price of a stock on a given day with its closing price on the preceding day.

The standard deviation of these daily returns over a specified period then represents the daily volatility, providing a statistical measure of price dispersion.

Prerequisites:

This python code patch is written for NSEPython Library first time. 

Ensure you have the nsepython library installed in your Python environment. If not, install it using pip:

				
					pip install nsepython

				
			

Script for Calculating Daily Volatility

				
					from nsepython import *

def get_daily_volatility(symbol, series, start_date, end_date):
    # Fetch historical stock prices
    historical_data = equity_history(symbol, series, start_date, end_date)
    
    # Calculate daily returns
    daily_returns = historical_data['CH_CLOSING_PRICE'].pct_change().dropna()
    
    # Calculate daily volatility (standard deviation of daily returns)
    daily_volatility = daily_returns.std()
    
    return daily_volatility

# Usage:
symbol = "SBIN"
series = "EQ"
start_date = "01-01-2021"
end_date = "31-12-2021"

daily_volatility = get_daily_volatility(symbol, series, start_date, end_date)

print(f'Daily Volatility: {daily_volatility}')

				
			

Output:

				
					Daily Volatility: 0.03267783901207512
				
			

Explanation:

  1. Importing Necessary Library

    Import the nsepython library, which provides functions to fetch historical stock price data from the National Stock Exchange (NSE) of India.

  2. Defining the Function

    Define a function get_daily_drift with parameters for the stock symbol, series type, start date, and end date.

  3. Fetching Historical Data

    Utilize the equity_history function from nsepython to fetch historical stock prices for the specified symbol and date range.

  4. Calculating Daily Returns

    Compute daily returns by finding the percentage change in the closing price from the previous day using the pct_change() method.

  5. Calculating Daily Volatility
    Calculate the daily volatility by finding the standard deviation of the daily returns using the std() method. This standard deviation represents the average daily price fluctuation of the stock over the specified period.

  6. Usage:
    Provide the necessary inputs (symbol, series, start date, and end date) and call the get_daily_volatility function to obtain the daily volatility of the specified stock.

This script offers a method to compute the daily volatility, aiding investors and analysts in evaluating the inherent price risks associated with stocks over time.

Join The Conversation?

Post a comment

Leave a Comment

Your email address will not be published. Required fields are marked *

×Close