Method to get previous day date

please share the code to get current and previous market dates

Thanks

Hi @Kiran_Thomas ,

From where would you like to get the current and previous market dates?

Kindly elaborate more on your query, so that it enables us to assist you further.

Regards,
`Akhil

sorry for the inconvenience , i need to fetch current and previous market dates from python build .
this is the current code i am using now which is not proper… ,candle -3 min, NSE , intraday,

    def get_previous_trading_day(self):
    hist_data = self.get_historical_data(instrument)
    market_holidays = ['2023-01-01', '2023-05-01', '2023-12-25']
    hist_data.index = pd.to_datetime(hist_data.index)
    # Convert market holidays to datetime for comparison
    market_holidays_dt = pd.to_datetime(market_holidays)
    current_date = hist_data.index[-1].date()
    # Iterate backwards to find the previous trading day
    for i in range(1, len(hist_data)):
        previous_date = hist_data.index[-(i + 1)].date()
        # Check if previous date is not a weekend and not a holiday
        if previous_date.weekday() < 5 and previous_date not in market_holidays_dt:
            return previous_date
        else:
            self.logger.info(f"Skipping non-trading day: {previous_date}")
            
    prev_day_data = self.get_historical_data(instrument, start=previous_date, end=previous_date)
    prev_day_last_candle_low = prev_day_data['low'].iloc[-1]
    prev_day_last_candle_high = prev_day_data['high'].iloc[-1]

    return prev_day_last_candle_low, prev_day_last_candle_high

Hi @Kiran_Thomas ,

You want to catch the previous date from the historical data dataframe.

From your code, we see that you have already got the hist data:
hist_data = self.get_historical_data(instrument)

A few steps of pandas operations should help you retrieve the unique dates from the dataframe, from which you can extract the previous date.

A simple ChatGPT query may help:

Note:
The solution provided by ChatGPT may not work out-of-the-box; some additional tweaks may be required. But it should help steer you in the right direction.

Hope this helps,
`Akhil

1 Like