Calculating Daily Drift of Stocks Using Python on Indian Stocks

Daily drift is often reflective of an underlying momentum within the market.  

Daily Drift refers to the average rate at which stock prices tend to move over time, indicating a sort of inherent tendency in their price trajectory. It is calculated by analyzing the mean of daily returns over a specified period.

Significance of Daily Drift

  • A positive daily drift implies a general upward momentum, suggesting a favorable market sentiment towards the particular stock or sector. 
  • Conversely, a negative drift might indicate an unfavorable sentiment. 

Understanding the daily drift can provide traders and investors with a subtle, yet potentially impactful, insight into the prevailing market sentiment.

Calculating Daily Drift:

The formula to calculate Daily Drift is given by:

$$ \text{Daily Drift} = \frac{1}{N} \sum_{i=1}^{N} (\frac{P_{i} – P_{i-1}}{P_{i-1}}) $$

where:

  • \( P_{i} \) is the closing price of the stock on day \( i \).
  • \( P_{i-1} \) is the closing price of the stock on the previous day.
  • \( N \) is the total number of days in the period being analyzed.

The computation of daily drift is often done by analyzing the daily returns of a stock. Daily returns are calculated by comparing the price of a stock at the close of the market on each day with the closing price on the previous day. The mean of these daily returns over a particular period represents the daily drift.

Various factors contribute to daily drift, including trading volume, market liquidity, and the inherent bias of market participants. Understanding the daily drift of a stock can provide insights into its subtle price movements over time, independent of significant market events. By monitoring the daily drift, investors can get a sense of the intrinsic momentum of a stock or a market segment. This insight can be a valuable addition to the toolkit of metrics used for making informed investment decisions.

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 Drift

				
					from nsepython import *

def get_daily_drift(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 drift (mean of daily returns)
    daily_drift = daily_returns.mean()
    
    return daily_drift

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

daily_drift = get_daily_drift(symbol, series, start_date, end_date)

print(f'Daily Drift: {daily_drift}')

				
			

Output:

				
					Daily Drift: -4.4751398615004874e-05

				
			

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 Drift

    Calculate the daily drift by finding the mean of the daily returns. This mean represents the average daily price movement 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_drift function to obtain the daily drift of the specified stock.

This script provides a straightforward method to compute the daily drift, allowing investors and analysts to better understand the inherent price movements of stocks over time.

Other Ways to Calculate Daily Drift

Calculating the daily drift of stock prices involves statistical analysis and can be approached in a few different ways depending on the specifics of the analysis being performed. Here’s a consolidated explanation based on the information gathered:

  • Log Returns Method:
    • One common method to calculate daily drift involves using log returns. Firstly, you’d compute the log returns of the stock prices, which is typically done by taking the natural logarithm of the ratio of successive prices.
    • Once you have the log returns, find the mean of these log returns. This mean value represents the daily drift of the stock prices over the period of analysis.
  • Historical Drift Estimation:
    • Another method involves estimating the historical drift, where the drift term (\(\mu\)) can be recovered from the formula \(\mu = \alpha + \frac{1}{2} \sigma^2\), where \(\alpha\) is a constant and \(\sigma^2\) is the variance of the returns over a specified period.
  • Bid-Ask Spread Consideration:
    • The difference between the bid and ask prices in day-to-day trading can also explain the drift in stock prices, especially over longer periods following significant market events like earnings reports.
  • Volatility-Based Approach:
    • In a volatility-based approach, gather daily stock prices, compute the mean of these prices, find the deviation of each day’s price from the mean, square these deviations, and sum them up. While this method is more related to calculating volatility, understanding the volatility can also give insights into the daily drift over time.

These methods represent different approaches to analyzing daily drift, each with its own set of assumptions and interpretations. The choice of method could depend on the specific context and the available data. For a more precise and tailored analysis, especially in a professional or academic setting, consulting with a financial analyst or a statistician might be advisable.

Join The Conversation?

Post a comment

Leave a Comment

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

×Close