diff --git a/setup.py b/setup.py index 79b29c1..73c5d18 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ setup( name='pythclient', - version='0.1.7', + version='0.1.8', packages=['pythclient'], author='Pyth Developers', author_email='contact@pyth.network', diff --git a/tests/test_calendar.py b/tests/test_calendar.py index f9bf1a9..602c049 100644 --- a/tests/test_calendar.py +++ b/tests/test_calendar.py @@ -1,361 +1,203 @@ import datetime from zoneinfo import ZoneInfo -import pytest - -from pythclient.calendar import ( - get_next_market_close, - get_next_market_open, - is_market_open, -) +from pythclient.calendar import (get_next_market_close, get_next_market_open, + is_market_open) NY_TZ = ZoneInfo("America/New_York") UTC_TZ = ZoneInfo("UTC") +# Define constants for equity market +EQUITY_OPEN_WED_2023_6_21_12 = datetime.datetime(2023, 6, 21, 12, 0, 0, tzinfo=NY_TZ) +EQUITY_CLOSE_WED_2023_6_21_17 = datetime.datetime(2023, 6, 21, 17, 0, 0, tzinfo=NY_TZ) +EQUITY_CLOSE_SAT_2023_6_10_17 = datetime.datetime(2023, 6, 10, 17, 0, 0, tzinfo=NY_TZ) +EQUITY_HOLIDAY_MON_2023_6_19 = datetime.datetime(2023, 6, 19, tzinfo=NY_TZ) +EQUITY_EARLY_CLOSE_OPEN_FRI_2023_11_24_14 = datetime.datetime(2023, 11, 24, 11, 0, 0, tzinfo=NY_TZ) +EQUITY_EARLY_CLOSE_CLOSE_FRI_2023_11_24_14 = datetime.datetime(2023, 11, 24, 14, 0, 0, tzinfo=NY_TZ) -@pytest.fixture -def equity_open_weekday_datetime(): - # A weekday, within equity market hours - return datetime.datetime(2023, 6, 21, 12, 0, 0, tzinfo=NY_TZ) - - -@pytest.fixture -def equity_close_weekday_datetime(): - # A weekday, out of equity market hours - return datetime.datetime(2023, 6, 21, 17, 0, 0, tzinfo=NY_TZ) - - -@pytest.fixture -def equity_close_weekend_datetime(): - # A weekend, out of equity market hours - return datetime.datetime(2023, 6, 10, 17, 0, 0, tzinfo=NY_TZ) - - -@pytest.fixture -def equity_holiday_datetime(): - # A weekday, NYSE holiday - return datetime.datetime(2023, 6, 19, tzinfo=NY_TZ) - - -@pytest.fixture -def equity_early_holiday_open_datetime(): - # A weekday, NYSE early close holiday - return datetime.datetime(2023, 11, 24, 11, 0, 0, tzinfo=NY_TZ) - - -@pytest.fixture -def equity_early_holiday_close_datetime(): - # A weekday, NYSE early close holiday - return datetime.datetime(2023, 11, 24, 14, 0, 0, tzinfo=NY_TZ) - +# Define constants for fx & metal market +FX_METAL_OPEN_WED_2023_6_21_22 = datetime.datetime(2023, 6, 21, 22, 0, 0, tzinfo=NY_TZ) +FX_METAL_CLOSE_SUN_2023_6_18_16 = datetime.datetime(2023, 6, 18, 16, 0, 0, tzinfo=NY_TZ) +FX_METAL_HOLIDAY_SUN_2023_1_1 = datetime.datetime(2023, 1, 1, tzinfo=NY_TZ) -@pytest.fixture -def fx_metal_open_weekday_datetime(): - # A weekday, within fx & metal market hours - return datetime.datetime(2023, 6, 21, 22, 0, 0, tzinfo=NY_TZ) +# Define constants for cryptocurrency market +CRYPTO_OPEN_WED_2023_6_21_12 = datetime.datetime(2023, 6, 21, 12, 0, 0, tzinfo=NY_TZ) +CRYPTO_OPEN_SUN_2023_6_18_12 = datetime.datetime(2023, 6, 18, 12, 0, 0, tzinfo=NY_TZ) +def format_datetime_to_utc_iso_string(dt: datetime.datetime): + return dt.astimezone(UTC_TZ).strftime("%Y-%m-%dT%H:%M:%S") + "Z" -@pytest.fixture -def fx_metal_close_weekend_datetime(): - # A weekend, out of fx & metal market hours - return datetime.datetime(2023, 6, 18, 16, 0, 0, tzinfo=NY_TZ) - - -@pytest.fixture -def fx_metal_holiday_datetime(): - # CBOE holiday - return datetime.datetime(2023, 1, 1, tzinfo=NY_TZ) - - -@pytest.fixture -def crypto_open_weekday_datetime(): - return datetime.datetime(2023, 6, 21, 12, 0, 0, tzinfo=NY_TZ) - - -@pytest.fixture -def crypto_open_weekend_datetime(): - return datetime.datetime(2023, 6, 18, 12, 0, 0, tzinfo=NY_TZ) - - -def test_is_market_open( - equity_open_weekday_datetime, - equity_close_weekday_datetime, - equity_close_weekend_datetime, - equity_holiday_datetime, - equity_early_holiday_open_datetime, - equity_early_holiday_close_datetime, - fx_metal_open_weekday_datetime, - fx_metal_close_weekend_datetime, - fx_metal_holiday_datetime, - crypto_open_weekday_datetime, - crypto_open_weekend_datetime, -): +def test_is_market_open(): # equity # weekday, within equity market hours - assert is_market_open("equity", equity_open_weekday_datetime) == True + assert is_market_open("equity", EQUITY_OPEN_WED_2023_6_21_12) == True # weekday, out of equity market hours - assert is_market_open("equity", equity_close_weekday_datetime) == False + assert is_market_open("equity", EQUITY_CLOSE_WED_2023_6_21_17) == False # weekend, out of equity market hours - assert is_market_open("equity", equity_close_weekend_datetime) == False + assert is_market_open("equity", EQUITY_CLOSE_SAT_2023_6_10_17) == False # weekday, NYSE holiday - assert is_market_open("equity", equity_holiday_datetime) == False + assert is_market_open("equity", EQUITY_HOLIDAY_MON_2023_6_19) == False # weekday, NYSE early close holiday - assert is_market_open("equity", equity_early_holiday_open_datetime) == True - assert is_market_open("equity", equity_early_holiday_close_datetime) == False + assert is_market_open("equity", EQUITY_EARLY_CLOSE_OPEN_FRI_2023_11_24_14) == True + assert is_market_open("equity", EQUITY_EARLY_CLOSE_CLOSE_FRI_2023_11_24_14) == False # fx & metal # weekday, within fx & metal market hours - assert is_market_open("fx", fx_metal_open_weekday_datetime) == True - assert is_market_open("metal", fx_metal_open_weekday_datetime) == True + assert is_market_open("fx", FX_METAL_OPEN_WED_2023_6_21_22) == True + assert is_market_open("metal", FX_METAL_OPEN_WED_2023_6_21_22) == True # weekday, out of fx & metal market hours - assert is_market_open("fx", fx_metal_close_weekend_datetime) == False - assert is_market_open("metal", fx_metal_close_weekend_datetime) == False + assert is_market_open("fx", FX_METAL_CLOSE_SUN_2023_6_18_16) == False + assert is_market_open("metal", FX_METAL_CLOSE_SUN_2023_6_18_16) == False # fx & metal holiday - assert is_market_open("fx", fx_metal_holiday_datetime) == False - assert is_market_open("metal", fx_metal_holiday_datetime) == False + assert is_market_open("fx", FX_METAL_HOLIDAY_SUN_2023_1_1) == False + assert is_market_open("metal", FX_METAL_HOLIDAY_SUN_2023_1_1) == False # crypto - assert is_market_open("crypto", crypto_open_weekday_datetime) == True - assert is_market_open("crypto", crypto_open_weekend_datetime) == True - - -def test_get_next_market_open( - equity_open_weekday_datetime, - equity_close_weekday_datetime, - equity_close_weekend_datetime, - equity_holiday_datetime, - equity_early_holiday_open_datetime, - equity_early_holiday_close_datetime, - fx_metal_open_weekday_datetime, - fx_metal_close_weekend_datetime, - fx_metal_holiday_datetime, - crypto_open_weekday_datetime, - crypto_open_weekend_datetime, -): + assert is_market_open("crypto", CRYPTO_OPEN_WED_2023_6_21_12) == True + assert is_market_open("crypto", CRYPTO_OPEN_SUN_2023_6_18_12) == True + + +def test_get_next_market_open(): # equity within market hours assert ( - get_next_market_open("equity", equity_open_weekday_datetime) - == datetime.datetime(2023, 6, 22, 9, 30, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_open("equity", EQUITY_OPEN_WED_2023_6_21_12) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 22, 9, 30, 0, tzinfo=NY_TZ)) ) # equity out of market hours assert ( - get_next_market_open("equity", equity_close_weekday_datetime) - == datetime.datetime(2023, 6, 22, 9, 30, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_open("equity", EQUITY_CLOSE_WED_2023_6_21_17) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 22, 9, 30, 0, tzinfo=NY_TZ)) ) # equity weekend assert ( - get_next_market_open("equity", equity_close_weekend_datetime) - == datetime.datetime(2023, 6, 12, 9, 30, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_open("equity", EQUITY_CLOSE_SAT_2023_6_10_17) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 12, 9, 30, 0, tzinfo=NY_TZ)) ) # equity holiday assert ( - get_next_market_open("equity", equity_holiday_datetime) - == datetime.datetime(2023, 6, 20, 9, 30, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_open("equity", EQUITY_HOLIDAY_MON_2023_6_19) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 20, 9, 30, 0, tzinfo=NY_TZ)) ) # equity early close holiday assert ( - get_next_market_open("equity", equity_early_holiday_open_datetime) - == datetime.datetime(2023, 11, 27, 9, 30, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_open("equity", EQUITY_EARLY_CLOSE_OPEN_FRI_2023_11_24_14) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 11, 27, 9, 30, 0, tzinfo=NY_TZ)) ) assert ( - get_next_market_open("equity", equity_early_holiday_close_datetime) - == datetime.datetime(2023, 11, 27, 9, 30, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_open("equity", EQUITY_EARLY_CLOSE_CLOSE_FRI_2023_11_24_14) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 11, 27, 9, 30, 0, tzinfo=NY_TZ)) ) # fx & metal within market hours assert ( - get_next_market_open("fx", fx_metal_open_weekday_datetime) - == datetime.datetime(2023, 6, 25, 17, 0, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_open("fx", FX_METAL_OPEN_WED_2023_6_21_22) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 25, 17, 0, 0, tzinfo=NY_TZ)) ) assert ( - get_next_market_open("metal", fx_metal_open_weekday_datetime) - == datetime.datetime(2023, 6, 25, 17, 0, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_open("metal", FX_METAL_OPEN_WED_2023_6_21_22) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 25, 17, 0, 0, tzinfo=NY_TZ)) ) # fx & metal out of market hours assert ( - get_next_market_open("fx", fx_metal_close_weekend_datetime) - == datetime.datetime(2023, 6, 18, 17, 0, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_open("fx", FX_METAL_CLOSE_SUN_2023_6_18_16) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 18, 17, 0, 0, tzinfo=NY_TZ)) ) assert ( - get_next_market_open("metal", fx_metal_close_weekend_datetime) - == datetime.datetime(2023, 6, 18, 17, 0, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_open("metal", FX_METAL_CLOSE_SUN_2023_6_18_16) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 18, 17, 0, 0, tzinfo=NY_TZ)) ) # fx & metal holiday assert ( - get_next_market_open("fx", fx_metal_holiday_datetime) - == datetime.datetime(2023, 1, 2, 17, 0, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_open("fx", FX_METAL_HOLIDAY_SUN_2023_1_1) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 1, 2, 17, 0, 0, tzinfo=NY_TZ)) ) assert ( - get_next_market_open("metal", fx_metal_holiday_datetime) - == datetime.datetime(2023, 1, 2, 17, 0, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_open("metal", FX_METAL_HOLIDAY_SUN_2023_1_1) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 1, 2, 17, 0, 0, tzinfo=NY_TZ)) ) # crypto - assert get_next_market_open("crypto", crypto_open_weekday_datetime) == None - assert get_next_market_open("crypto", crypto_open_weekend_datetime) == None - - -def test_get_next_market_close( - equity_open_weekday_datetime, - equity_close_weekday_datetime, - equity_close_weekend_datetime, - equity_holiday_datetime, - equity_early_holiday_open_datetime, - equity_early_holiday_close_datetime, - fx_metal_open_weekday_datetime, - fx_metal_close_weekend_datetime, - fx_metal_holiday_datetime, - crypto_open_weekday_datetime, - crypto_open_weekend_datetime, -): + assert get_next_market_open("crypto", CRYPTO_OPEN_WED_2023_6_21_12) == None + assert get_next_market_open("crypto", CRYPTO_OPEN_SUN_2023_6_18_12) == None + + +def test_get_next_market_close(): # equity within market hours assert ( - get_next_market_close("equity", equity_open_weekday_datetime) - == datetime.datetime(2023, 6, 21, 16, 0, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_close("equity", EQUITY_OPEN_WED_2023_6_21_12) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 21, 16, 0, 0, tzinfo=NY_TZ)) ) # equity out of market hours assert ( - get_next_market_close("equity", equity_close_weekday_datetime) - == datetime.datetime(2023, 6, 22, 16, 0, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_close("equity", EQUITY_CLOSE_WED_2023_6_21_17) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 22, 16, 0, 0, tzinfo=NY_TZ)) ) # equity weekend assert ( - get_next_market_close("equity", equity_close_weekend_datetime) - == datetime.datetime(2023, 6, 12, 16, 0, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_close("equity", EQUITY_CLOSE_SAT_2023_6_10_17) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 12, 16, 0, 0, tzinfo=NY_TZ)) ) # equity holiday assert ( - get_next_market_close("equity", equity_holiday_datetime) - == datetime.datetime(2023, 6, 20, 16, 0, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_close("equity", EQUITY_HOLIDAY_MON_2023_6_19) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 20, 16, 0, 0, tzinfo=NY_TZ)) ) # equity early close holiday assert ( - get_next_market_close("equity", equity_early_holiday_open_datetime) - == datetime.datetime(2023, 11, 24, 13, 0, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_close("equity", EQUITY_EARLY_CLOSE_OPEN_FRI_2023_11_24_14) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 11, 24, 13, 0, 0, tzinfo=NY_TZ)) ) assert ( - get_next_market_close("equity", equity_early_holiday_close_datetime) - == datetime.datetime(2023, 11, 27, 16, 0, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_close("equity", EQUITY_EARLY_CLOSE_CLOSE_FRI_2023_11_24_14) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 11, 27, 16, 0, 0, tzinfo=NY_TZ)) ) # fx & metal within market hours assert ( - get_next_market_close("fx", fx_metal_open_weekday_datetime) - == datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_close("fx", FX_METAL_OPEN_WED_2023_6_21_22) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ)) ) assert ( - get_next_market_close("metal", fx_metal_open_weekday_datetime) - == datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_close("metal", FX_METAL_OPEN_WED_2023_6_21_22) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ)) ) # fx & metal out of market hours assert ( - get_next_market_close("fx", fx_metal_close_weekend_datetime) - == datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_close("fx", FX_METAL_CLOSE_SUN_2023_6_18_16) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ)) ) assert ( - get_next_market_close("metal", fx_metal_close_weekend_datetime) - == datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_close("metal", FX_METAL_CLOSE_SUN_2023_6_18_16) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ)) ) # fx & metal holiday assert ( - get_next_market_close("fx", fx_metal_holiday_datetime) - == datetime.datetime(2023, 1, 6, 17, 0, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_close("fx", FX_METAL_HOLIDAY_SUN_2023_1_1) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 1, 6, 17, 0, 0, tzinfo=NY_TZ)) ) assert ( - get_next_market_close("metal", fx_metal_holiday_datetime) - == datetime.datetime(2023, 1, 6, 17, 0, 0, tzinfo=NY_TZ) - .astimezone(UTC_TZ) - .strftime("%Y-%m-%dT%H:%M:%S") - + "Z" + get_next_market_close("metal", FX_METAL_HOLIDAY_SUN_2023_1_1) + == format_datetime_to_utc_iso_string(datetime.datetime(2023, 1, 6, 17, 0, 0, tzinfo=NY_TZ)) ) # crypto - assert get_next_market_close("crypto", crypto_open_weekday_datetime) == None - assert get_next_market_close("crypto", crypto_open_weekend_datetime) == None + assert get_next_market_close("crypto", CRYPTO_OPEN_WED_2023_6_21_12) == None + assert get_next_market_close("crypto", CRYPTO_OPEN_SUN_2023_6_18_12) == None