Corporate Actions like Splits, Bonus and Rights affect stock prices and they in-turn affect analysis. So, for analysis you need to apply adjustment factors to historical OHLC (Open, High, Low, Close) data.

Understanding Corporate Actions & Adjustments

  • Stock Splits: If a company announces a split (e.g., 2-for-1), the historical prices should be adjusted.
  • Bonus Issues: Free shares issued to existing shareholders, requiring adjustments.
  • Rights Issues: New shares offered at a discounted price to existing shareholders.

Note: For all the above even volumes should be adjusted if you are using it.

The formula to calculate the adjustment factor is:

On any given day a normal stock(non-FnO) can hit 20%, if the circuit limit allows. So, in the code, an event is detected only if Close value > 20%

def detect_adjustment_factors(df):
    df = df.sort_index(ascending=True)  # Ensure data is sorted chronologically
    
    # Calculate adjustment factor based on price jumps
    df["Adj Factor"] = df["Close"].shift(1) / df["Close"]

    # Detect stock splits (where ratio is a whole number like 2, 3, 5, etc.)
    df["Split Ratio"] = df["Adj Factor"].apply(lambda x: round(x) if x > 1.2 else None)
    return df

def apply_adjustments(df):
    df = detect_adjustment_factors(df)

    # Initialize cumulative adjustment factor (starting from 1)
    df["Cumulative Factor"] = 1

    # Apply adjustment factor retroactively
    split_events = df.dropna(subset=["Split Ratio"])
    for date, row in split_events.iterrows():
        split_ratio = row["Split Ratio"]
        
        # Apply the split ratio to all previous data
        df.loc[df.index < date, "Cumulative Factor"] *= split_ratio

    # Adjust past OHLC prices using the cumulative factor
    df["Adj Open"] = df["Open"] / df["Cumulative Factor"]
    df["Adj High"] = df["High"] / df["Cumulative Factor"]
    df["Adj Low"] = df["Low"] / df["Cumulative Factor"]
    df["Adj Close"] = df["Close"] / df["Cumulative Factor"]

    return df

Generating Test Data from existing data

df_test = df_kitex.loc[(df_kitex.index >= '2025-01-14') & (df_kitex.index <= '2025-01-30')].copy()

# Converting existing dataframe column data to list
date_list      = df_test.index.tolist()
open_list      = df_test['Open'].tolist()
high_list      = df_test['High'].tolist()
low_list       = df_test['Low'].tolist()
close_list     = df_test['Close'].tolist()
prevclose_list = df_test['prevclose'].tolist()

# Simulating a split/bonus. Dividing price by 2 after index 7
for idx, item in enumerate(open_list):
    if idx >= 7:
        open_list[idx] = item / 2
        high_list[idx] = item / 2
        low_list[idx] = item / 2
        close_list[idx] = item / 2
        prevclose_list[idx] = item / 2

# Example usage
data = {
    "Date": date_list,
    "Open": open_list,
    "High": high_list,
    "Low": low_list,
    "Close": close_list,
    "prevclose": prevclose_list
}

df = pd.DataFrame(data)
df["Date"] = pd.to_datetime(df["Date"])
df.set_index("Date", inplace=True)

This is how the data now looks like, you observe the highlighted data and price drops.

Applying Adjustments

df_adjusted = apply_adjustments(df)

df_adjusted['adj_pChng'] = (df_adjusted['Adj Close'].pct_change()) * 100
df_adjusted['pChng'] = (df_adjusted['Close'].pct_change()) * 100

df_adjusted

In the below image you can see the Split Ratio and Cumulative Factor and also observe the pChng and adj_pChng have same values, so theoretically it works.

Retaining the essentials columns

df = df_adjusted[['Adj Open', 'Adj High', 'Adj Low', 'Adj Close', 'pChng']].copy()

cols = {'Adj Open':'Open', 'Adj High':'High', 'Adj Low':'Low', 'Adj Close':'Close'}
df.rename(columns=cols,inplace=True)

df.reset_index(inplace=True)

That’s all for now