Created
June 7, 2022 21:04
-
-
Save Kazanskyi/dcd0e786efd0d949ed34af4055cd09f9 to your computer and use it in GitHub Desktop.
Manually set up days of S&P drop for more than 15% and count number of days after the recent drop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
from datetime import date | |
import pandas as pd | |
import numpy as np | |
def get_dates(): | |
last_date = date.today() | |
historical_days = 1450 | |
historical_date = last_date-datetime.timedelta(days=historical_days) | |
dates_df=pd.DataFrame() | |
dates_df["date"] = pd.date_range(start=historical_date, end=last_date) | |
dates_df["last_crisis_day"] = np.nan | |
dates_df.loc[(dates_df['date'] == pd.Timestamp(2018, 12, 21)) | (dates_df['date'] == pd.Timestamp(2020, 3, 13)) | (dates_df['date'] == pd.Timestamp(2022, 4, 29)), 'last_crisis_day'] = dates_df['date'] | |
dates_df.sort_values(by = 'date', axis = 0, ascending = True, inplace = True) | |
dates_df.ffill(axis = 0, inplace = True) | |
dates_df.sort_values(by = 'date', axis = 0, ascending = False, inplace = True) | |
dates_df["days_after_crisis"] = dates_df["date"] - dates_df["last_crisis_day"] | |
dates_df.set_index(["date"], inplace = True) | |
dates_df['days_after_crisis'] = pd.to_numeric(dates_df['days_after_crisis'].dt.days, downcast='integer') | |
dates_df.drop(["last_crisis_day"], axis = 1, inplace = True) | |
return dates_df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment