示例#1
0
def us_market_holidays(years) -> list:
    """get US market holidays"""
    if isinstance(years, int):
        years = [
            years,
        ]
    # https://www.nyse.com/markets/hours-calendars
    marketHolidays = [
        "Martin Luther King Jr. Day",
        "Washington's Birthday",
        "Memorial Day",
        "Independence Day",
        "Labor Day",
        "Thanksgiving",
        "Christmas Day",
    ]
    #   http://www.maa.clell.de/StarDate/publ_holidays.html
    goodFridays = {
        2010: "2010-04-02",
        2011: "2011-04-22",
        2012: "2012-04-06",
        2013: "2013-03-29",
        2014: "2014-04-18",
        2015: "2015-04-03",
        2016: "2016-03-25",
        2017: "2017-04-14",
        2018: "2018-03-30",
        2019: "2019-04-19",
        2020: "2020-04-10",
        2021: "2021-04-02",
        2022: "2022-04-15",
        2023: "2023-04-07",
        2024: "2024-03-29",
        2025: "2025-04-18",
        2026: "2026-04-03",
        2027: "2027-03-26",
        2028: "2028-04-14",
        2029: "2029-03-30",
        2030: "2030-04-19",
    }
    marketHolidays_and_obsrvd = marketHolidays + [
        holiday + " (Observed)" for holiday in marketHolidays
    ]
    allHolidays = holidaysUS(years=years)
    validHolidays = []
    for date in list(allHolidays):
        if allHolidays[date] in marketHolidays_and_obsrvd:
            validHolidays.append(date)
    for year in years:
        new_Year = datetime.strptime(f"{year}-01-01", "%Y-%m-%d")
        if new_Year.weekday() != 5:  # ignore saturday
            validHolidays.append(new_Year.date())
        if new_Year.weekday() == 6:  # add monday for Sunday
            validHolidays.append(new_Year.date() + timedelta(1))
    for year in years:
        validHolidays.append(
            datetime.strptime(goodFridays[year], "%Y-%m-%d").date())
    return validHolidays
示例#2
0
def get_last_time_market_was_open(dt):
    # Check if it is a weekend
    if dt.date().weekday() > 4:
        dt = get_last_time_market_was_open(dt - timedelta(hours=24))

    # Check if it is a holiday
    if dt.strftime('%Y-%m-%d') in holidaysUS():
        dt = get_last_time_market_was_open(dt - timedelta(hours=24))

    dt = dt.replace(hour=21, minute=0, second=0)

    return dt
示例#3
0
def afterHours(now=None):
    # checks if the market is open(only used in autonomous trading bot)
    tz = timezone('Asia/Kolkata')
    us_holidays = holidaysUS()
    if not now:
        now = datetime.now(tz)
    openTime = Time(hour=9, minute=30, second=0)
    closeTime = Time(hour=16, minute=0, second=0)
    # If a holiday
    if now.strftime('%Y-%m-%d') in us_holidays:
        return True
    # If before 0930 or after 1600
    if (now.time() < openTime) or (now.time() > closeTime):
        return True
    # If it's a weekend
    if now.date().weekday() > 4:
        return True
    return False