Nifty Crossed 22000. How many days it took to Cross all the Round Numbers: Python Tutorial

Nifty today crossed 22000+ levels with a fresh all-time high. Have you ever calculated how many trading days it took for Nifty to move from 21000 to 22000 levels? Did you know that Nifty took 38 days to move from 21000 levels to 22000 levels? In this Python tutorial, we will be computing the duration it took for the Nifty to transition between two consecutive round numbers starting from 1000 to 22000 levels.

Python Code to Fetch the Dates When the Round Number Milestones crossed along with the time taken

Prerequisites

• Basic understanding of Python.
• Pandas library installed (`pip install pandas`).
``````import pandas as pd

# URL of the CSV file
csv_url = 'https://raw.githubusercontent.com/marketcalls/data/main/NIFTY_daily_data.csv'

# Load the CSV file from the URL

# Convert the date column to datetime for easier calculations
data['date'] = pd.to_datetime(data['date'])

# Generate the milestones list using a for loop
milestones = [i * 1000 for i in range(1, 23)]  # Generates 1000, 2000, ..., 22000

# Function to find the first date a milestone is reached and days to reach next milestone
def find_milestone_dates(data, column):
milestone_data = []
last_milestone_date = None

for milestone in milestones:
# Finding the first occurrence where the milestone was crossed
milestone_record = data[data[column] >= milestone].iloc[0]
milestone_date = milestone_record['date']

# Calculating days taken from the last milestone
if last_milestone_date is not None:
days_taken = (milestone_date - last_milestone_date).days
else:
days_taken = None  # For the first milestone

milestone_data.append({
'Milestone': milestone,
'First Date Crossed': milestone_date,
'Days Taken': days_taken
})

# Updating the last milestone date
last_milestone_date = milestone_date

milestone_df = pd.DataFrame(milestone_data)
# Convert 'Days Taken' to integer, handling NaN values
milestone_df['Days Taken'] = milestone_df['Days Taken'].astype('Int64')

return milestone_df

# Analysis using the 'high' column
milestone_data_high = find_milestone_dates(data, 'high')

# Display the results for the 'high' column analysis
print("Analysis using the 'high' column:")
print(milestone_data_high)
``````

Python Output

``````Analysis using the 'high' column:
Milestone First Date Crossed  Days Taken
0        1000         1992-03-09        <NA>
1        2000         2004-01-09        4323
2        3000         2006-01-30         752
3        4000         2006-12-01         305
4        5000         2007-09-27         300
5        6000         2007-11-01          35
6        7000         2014-05-12        2384
7        8000         2014-09-01         112
8        9000         2015-03-03         183
9       10000         2017-07-25         875
10      11000         2018-01-23         182
11      12000         2019-05-23         485
12      13000         2020-11-24         551
13      14000         2020-12-31          37
14      15000         2021-02-05          36
15      16000         2021-08-03         179
16      17000         2021-08-31          28
17      18000         2021-10-11          41
18      19000         2023-06-28         625
19      20000         2023-09-11          75
20      21000         2023-12-08          88
21      22000         2024-01-15          38``````

[Course] Designing a Stock Market Trading Dashboard App using…

Designing a Stock Market App using Python is a hands-on course that guides you through the development of a functional stock market application. Over...

Exploring the Essential Python Libraries for Data Analytics

Python has emerged as a powerhouse due to its versatility, ease of use, and extensive library support. Whether you're manipulating data, visualizing trends, performing...