conor10 / examples

Example code from my blog posts
18 stars 8 forks source link

incorrect dates? #2

Open carstenf opened 2 years ago

carstenf commented 2 years ago

Beautiful pice of code!

I'm downloading the vix data from https://www.cboe.com/us/futures/market_statistics/historical_data/ and one needs to know the expiring date to be able to get the file:

urlStr = 'https://www.cboe.com/us/futures/market_statistics/historical_data/products/csv/VX/2014-03-18' urllib.request.urlretrieve(urlStr)

I found only three issues between 2014 and today: 2014-03-19 -> should be 2014-03-18 2019-03-20 -> should be 2020-03-18 2022-03-16 -> should be 2022-03-15

I check regarding valid business days and all days are valid.

end = start = '2014-03-18' len(nyse.valid_days(start_date=start, end_date=end))

some idea how to fix it?

carstenf commented 2 years ago

looks like I found a solution, you might try this:

import pandas_market_calendars as mcal

nyse = mcal.get_calendar('NYSE')
def check_if_business_day(day):
    end =  start = day
    return len(nyse.valid_days(start_date=start, end_date=end))>0

than inside: def get_expiry_date_for_month(curr_date):

........

    # TODO: Incorporate check that it's a trading day, if so move the 3rd
    # Friday back by one day before subtracting

    if check_if_business_day(third_friday_next_month) == False:
        third_friday_next_month = third_friday_next_month - one_day   

    return third_friday_next_month - thirty_days