Skip to content

Commit 591f87d

Browse files
authored
fix next_market_open for fx and metal (#39)
* fix get_next_market_open logic for fx and metal * bump * add tests
1 parent 9c6e207 commit 591f87d

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

pythclient/calendar.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def get_next_market_open(asset_type: str, dt: datetime.datetime) -> int:
9696
)
9797
next_market_open += datetime.timedelta(days=1)
9898
elif asset_type in ["fx", "metal"]:
99-
if time < FX_METAL_OPEN_CLOSE_TIME:
99+
if dt.weekday() == 6 and time < FX_METAL_OPEN_CLOSE_TIME:
100100
next_market_open = dt.replace(
101101
hour=FX_METAL_OPEN_CLOSE_TIME.hour,
102102
minute=FX_METAL_OPEN_CLOSE_TIME.minute,
@@ -112,7 +112,6 @@ def get_next_market_open(asset_type: str, dt: datetime.datetime) -> int:
112112
)
113113
while is_market_open(asset_type, next_market_open):
114114
next_market_open += datetime.timedelta(days=1)
115-
116115
else:
117116
return None
118117

@@ -148,12 +147,16 @@ def get_next_market_close(asset_type: str, dt: datetime.datetime) -> int:
148147
next_market_open = get_next_market_open(
149148
asset_type, dt + datetime.timedelta(days=1)
150149
)
151-
next_market_close = datetime.datetime.fromtimestamp(next_market_open).astimezone(NY_TZ).replace(
150+
next_market_close = (
151+
datetime.datetime.fromtimestamp(next_market_open)
152+
.astimezone(NY_TZ)
153+
.replace(
152154
hour=EQUITY_CLOSE.hour,
153155
minute=EQUITY_CLOSE.minute,
154156
second=0,
155157
microsecond=0,
156158
)
159+
)
157160
else:
158161
next_market_close = dt.replace(
159162
hour=EQUITY_CLOSE.hour,

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setup(
99
name='pythclient',
10-
version='0.1.11',
10+
version='0.1.12',
1111
packages=['pythclient'],
1212
author='Pyth Developers',
1313
author_email='[email protected]',

tests/test_calendar.py

+29-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
EQUITY_EARLY_CLOSE_CLOSE_FRI_2023_11_24_14 = datetime.datetime(2023, 11, 24, 14, 0, 0, tzinfo=NY_TZ)
1717

1818
# Define constants for fx & metal market
19-
FX_METAL_OPEN_WED_2023_6_21_22 = datetime.datetime(2023, 6, 21, 22, 0, 0, tzinfo=NY_TZ)
19+
FX_METAL_OPEN_WED_2023_6_21_21 = datetime.datetime(2023, 6, 21, 21, 0, 0, tzinfo=NY_TZ)
20+
FX_METAL_OPEN_WED_2023_6_21_23 = datetime.datetime(2023, 6, 21, 23, 0, 0, tzinfo=NY_TZ)
2021
FX_METAL_CLOSE_SUN_2023_6_18_16 = datetime.datetime(2023, 6, 18, 16, 0, 0, tzinfo=NY_TZ)
2122
FX_METAL_HOLIDAY_SUN_2023_1_1 = datetime.datetime(2023, 1, 1, tzinfo=NY_TZ)
2223

@@ -51,8 +52,8 @@ def test_is_market_open():
5152

5253
# fx & metal
5354
# weekday, within fx & metal market hours
54-
assert is_market_open("fx", FX_METAL_OPEN_WED_2023_6_21_22) == True
55-
assert is_market_open("metal", FX_METAL_OPEN_WED_2023_6_21_22) == True
55+
assert is_market_open("fx", FX_METAL_OPEN_WED_2023_6_21_21) == True
56+
assert is_market_open("metal", FX_METAL_OPEN_WED_2023_6_21_21) == True
5657

5758
# weekday, out of fx & metal market hours
5859
assert is_market_open("fx", FX_METAL_CLOSE_SUN_2023_6_18_16) == False
@@ -102,13 +103,22 @@ def test_get_next_market_open():
102103
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 11, 27, 9, 30, 0, tzinfo=NY_TZ))
103104
)
104105

105-
# fx & metal within market hours
106+
# fx & metal within market hours (before 10pm UTC)
106107
assert (
107-
get_next_market_open("fx", FX_METAL_OPEN_WED_2023_6_21_22)
108+
get_next_market_open("fx", FX_METAL_OPEN_WED_2023_6_21_21)
108109
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 25, 17, 0, 0, tzinfo=NY_TZ))
109110
)
110111
assert (
111-
get_next_market_open("metal", FX_METAL_OPEN_WED_2023_6_21_22)
112+
get_next_market_open("metal", FX_METAL_OPEN_WED_2023_6_21_21)
113+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 25, 17, 0, 0, tzinfo=NY_TZ))
114+
)
115+
# fx & metal within market hours (after 10pm UTC)
116+
assert (
117+
get_next_market_open("fx", FX_METAL_OPEN_WED_2023_6_21_23)
118+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 25, 17, 0, 0, tzinfo=NY_TZ))
119+
)
120+
assert (
121+
get_next_market_open("metal", FX_METAL_OPEN_WED_2023_6_21_23)
112122
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 25, 17, 0, 0, tzinfo=NY_TZ))
113123
)
114124

@@ -172,13 +182,23 @@ def test_get_next_market_close():
172182
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 11, 27, 16, 0, 0, tzinfo=NY_TZ))
173183
)
174184

175-
# fx & metal within market hours
185+
# fx & metal within market hours (before 10pm UTC)
186+
assert (
187+
get_next_market_close("fx", FX_METAL_OPEN_WED_2023_6_21_21)
188+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ))
189+
)
190+
assert (
191+
get_next_market_close("metal", FX_METAL_OPEN_WED_2023_6_21_21)
192+
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ))
193+
)
194+
195+
# fx & metal within market hours (after 10pm UTC)
176196
assert (
177-
get_next_market_close("fx", FX_METAL_OPEN_WED_2023_6_21_22)
197+
get_next_market_close("fx", FX_METAL_OPEN_WED_2023_6_21_23)
178198
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ))
179199
)
180200
assert (
181-
get_next_market_close("metal", FX_METAL_OPEN_WED_2023_6_21_22)
201+
get_next_market_close("metal", FX_METAL_OPEN_WED_2023_6_21_23)
182202
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ))
183203
)
184204

0 commit comments

Comments
 (0)