Skip to content

Commit fdff423

Browse files
authored
fix fx bug and add 2024 calendar (#45)
* fix fx bug and add 2024 calendar * use 17 as close time for test case instead
1 parent d657170 commit fdff423

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

pythclient/calendar.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,22 @@
2222
datetime.datetime(2022, 9, 4, tzinfo=NY_TZ).date(),
2323
datetime.datetime(2023, 11, 23, tzinfo=NY_TZ).date(),
2424
datetime.datetime(2023, 12, 25, tzinfo=NY_TZ).date(),
25+
datetime.datetime(2024, 1, 1, tzinfo=NY_TZ).date(),
26+
datetime.datetime(2024, 1, 15, tzinfo=NY_TZ).date(),
27+
datetime.datetime(2024, 2, 19, tzinfo=NY_TZ).date(),
28+
datetime.datetime(2024, 3, 29, tzinfo=NY_TZ).date(),
29+
datetime.datetime(2024, 5, 27, tzinfo=NY_TZ).date(),
30+
datetime.datetime(2024, 6, 19, tzinfo=NY_TZ).date(),
31+
datetime.datetime(2024, 7, 4, tzinfo=NY_TZ).date(),
32+
datetime.datetime(2024, 9, 2, tzinfo=NY_TZ).date(),
33+
datetime.datetime(2024, 11, 28, tzinfo=NY_TZ).date(),
34+
datetime.datetime(2024, 12, 25, tzinfo=NY_TZ).date(),
2535
]
2636
NYSE_EARLY_HOLIDAYS = [
2737
datetime.datetime(2023, 7, 3, tzinfo=NY_TZ).date(),
2838
datetime.datetime(2023, 11, 24, tzinfo=NY_TZ).date(),
39+
datetime.datetime(2024, 7, 3, tzinfo=NY_TZ).date(),
40+
datetime.datetime(2024, 11, 29, tzinfo=NY_TZ).date(),
2941
]
3042

3143
FX_METAL_OPEN_CLOSE_TIME = datetime.time(17, 0, 0, tzinfo=NY_TZ)
@@ -35,6 +47,8 @@
3547
FX_METAL_HOLIDAYS = [
3648
datetime.datetime(2023, 1, 1, tzinfo=NY_TZ).date(),
3749
datetime.datetime(2023, 12, 25, tzinfo=NY_TZ).date(),
50+
datetime.datetime(2024, 1, 1, tzinfo=NY_TZ).date(),
51+
datetime.datetime(2024, 12, 25, tzinfo=NY_TZ).date(),
3852
]
3953

4054
RATES_OPEN = datetime.time(8, 0, 0, tzinfo=NY_TZ)
@@ -60,7 +74,7 @@ def is_market_open(asset_type: str, dt: datetime.datetime) -> bool:
6074
return False
6175

6276
if asset_type in ["fx", "metal"]:
63-
if date in FX_METAL_HOLIDAYS:
77+
if date in FX_METAL_HOLIDAYS and time < FX_METAL_OPEN_CLOSE_TIME:
6478
return False
6579
# On Friday the market is closed after 5pm
6680
if day == 4 and time >= FX_METAL_OPEN_CLOSE_TIME:
@@ -71,6 +85,13 @@ def is_market_open(asset_type: str, dt: datetime.datetime) -> bool:
7185
# On Sunday the market is closed before 5pm
7286
if day == 6 and time < FX_METAL_OPEN_CLOSE_TIME:
7387
return False
88+
# On Sunday the market is closed after 5pm if the next day is a holiday
89+
if (
90+
day == 6
91+
and time >= FX_METAL_OPEN_CLOSE_TIME
92+
and (date + datetime.timedelta(days=1) in FX_METAL_HOLIDAYS)
93+
):
94+
return False
7495

7596
return True
7697

tests/test_calendar.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
FX_METAL_OPEN_WED_2023_6_21_23 = datetime.datetime(2023, 6, 21, 23, 0, 0, tzinfo=NY_TZ)
2323
FX_METAL_CLOSE_SUN_2023_6_18_16 = datetime.datetime(2023, 6, 18, 16, 0, 0, tzinfo=NY_TZ)
2424
FX_METAL_HOLIDAY_SUN_2023_1_1 = datetime.datetime(2023, 1, 1, tzinfo=NY_TZ)
25+
FX_METAL_HOLIDAY_SUN_2023_12_24_17 = datetime.datetime(2023, 12, 24, 17, 0, 0, tzinfo=NY_TZ)
26+
FX_METAL_HOLIDAY_SUN_2023_12_31_17 = datetime.datetime(2023, 12, 31, 17, 0, 0, tzinfo=NY_TZ)
2527

2628
# Define constants for rates market
2729
RATES_OPEN_WED_2023_6_21_12 = datetime.datetime(2023, 6, 21, 8, 0, 0, tzinfo=NY_TZ)
@@ -75,6 +77,12 @@ def test_is_market_open():
7577
assert is_market_open("fx", FX_METAL_HOLIDAY_SUN_2023_1_1) == False
7678
assert is_market_open("metal", FX_METAL_HOLIDAY_SUN_2023_1_1) == False
7779

80+
# fx & metal out of market hours on Sunday Dec 24 2023 after 10pm UTC
81+
assert is_market_open("fx", FX_METAL_HOLIDAY_SUN_2023_12_24_17) == False
82+
83+
# fx & metal out of market hours on Sunday Dec 31 2023 after 10pm UTC
84+
assert is_market_open("fx", FX_METAL_HOLIDAY_SUN_2023_12_31_17) == False
85+
7886
# rates
7987
# weekday, within rates market hours
8088
assert is_market_open("rates", RATES_OPEN_WED_2023_6_21_12) == True
@@ -167,14 +175,24 @@ def test_get_next_market_open():
167175
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 18, 17, 0, 0, tzinfo=NY_TZ))
168176
)
169177

178+
# fx & metal out of market hours on Sunday Dec 24 2024 after 10pm UTC
179+
assert (
180+
get_next_market_open("fx", FX_METAL_HOLIDAY_SUN_2023_12_24_17)
181+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 12, 25, 17, 0, 0, tzinfo=NY_TZ))
182+
)
183+
assert (
184+
get_next_market_open("metal", FX_METAL_HOLIDAY_SUN_2023_12_24_17)
185+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 12, 25, 17, 0, 0, tzinfo=NY_TZ))
186+
)
187+
170188
# fx & metal holiday
171189
assert (
172190
get_next_market_open("fx", FX_METAL_HOLIDAY_SUN_2023_1_1)
173-
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 1, 2, 17, 0, 0, tzinfo=NY_TZ))
191+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 1, 1, 17, 0, 0, tzinfo=NY_TZ))
174192
)
175193
assert (
176194
get_next_market_open("metal", FX_METAL_HOLIDAY_SUN_2023_1_1)
177-
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 1, 2, 17, 0, 0, tzinfo=NY_TZ))
195+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 1, 1, 17, 0, 0, tzinfo=NY_TZ))
178196
)
179197

180198
# rates within market hours

0 commit comments

Comments
 (0)