Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return market hours in timestamp instead of iso string #36

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions pythclient/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def is_market_open(asset_type: str, dt: datetime.datetime) -> bool:
return True


def get_next_market_open(asset_type: str, dt: datetime.datetime) -> str:
def get_next_market_open(asset_type: str, dt: datetime.datetime) -> int:
# make sure time is in NY timezone
dt = dt.astimezone(NY_TZ)
time = dt.time()
Expand Down Expand Up @@ -119,10 +119,10 @@ def get_next_market_open(asset_type: str, dt: datetime.datetime) -> str:
while not is_market_open(asset_type, next_market_open):
next_market_open += datetime.timedelta(days=1)

return next_market_open.astimezone(UTC_TZ).strftime("%Y-%m-%dT%H:%M:%S") + "Z"
return int(next_market_open.timestamp())


def get_next_market_close(asset_type: str, dt: datetime.datetime) -> str:
def get_next_market_close(asset_type: str, dt: datetime.datetime) -> int:
# make sure time is in NY timezone
dt = dt.astimezone(NY_TZ)
time = dt.time()
Expand All @@ -148,16 +148,12 @@ def get_next_market_close(asset_type: str, dt: datetime.datetime) -> str:
next_market_open = get_next_market_open(
asset_type, dt + datetime.timedelta(days=1)
)
next_market_close = (
datetime.datetime.fromisoformat(next_market_open.replace("Z", "+00:00"))
.astimezone(NY_TZ)
.replace(
next_market_close = datetime.datetime.fromtimestamp(next_market_open).astimezone(NY_TZ).replace(
hour=EQUITY_CLOSE.hour,
minute=EQUITY_CLOSE.minute,
second=0,
microsecond=0,
)
)
else:
next_market_close = dt.replace(
hour=EQUITY_CLOSE.hour,
Expand Down Expand Up @@ -189,4 +185,4 @@ def get_next_market_close(asset_type: str, dt: datetime.datetime) -> str:
else: # crypto markets never close
return None

return next_market_close.astimezone(UTC_TZ).strftime("%Y-%m-%dT%H:%M:%S") + "Z"
return int(next_market_close.timestamp())
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='pythclient',
version='0.1.8',
version='0.1.9',
packages=['pythclient'],
author='Pyth Developers',
author_email='[email protected]',
Expand Down
56 changes: 30 additions & 26 deletions tests/test_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@
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"

def format_datetime_to_unix_timestamp(dt: datetime.datetime):
# Convert the datetime object to a Unix timestamp in UTC
timestamp = dt.astimezone(UTC_TZ).timestamp()
unix_timestamp_utc = int(timestamp)
return unix_timestamp_utc

def test_is_market_open():
# equity
Expand Down Expand Up @@ -67,65 +71,65 @@ def test_get_next_market_open():
# equity within market hours
assert (
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))
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 22, 9, 30, 0, tzinfo=NY_TZ))
)

# equity out of market hours
assert (
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))
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 22, 9, 30, 0, tzinfo=NY_TZ))
)

# equity weekend
assert (
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))
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 12, 9, 30, 0, tzinfo=NY_TZ))
)

# equity holiday
assert (
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))
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 20, 9, 30, 0, tzinfo=NY_TZ))
)

# equity early close holiday
assert (
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))
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 11, 27, 9, 30, 0, tzinfo=NY_TZ))
)
assert (
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))
== format_datetime_to_unix_timestamp(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_WED_2023_6_21_22)
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 25, 17, 0, 0, tzinfo=NY_TZ))
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 25, 17, 0, 0, tzinfo=NY_TZ))
)
assert (
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))
== format_datetime_to_unix_timestamp(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_SUN_2023_6_18_16)
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 18, 17, 0, 0, tzinfo=NY_TZ))
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 18, 17, 0, 0, tzinfo=NY_TZ))
)
assert (
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))
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 18, 17, 0, 0, tzinfo=NY_TZ))
)

# fx & metal holiday
assert (
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))
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 1, 2, 17, 0, 0, tzinfo=NY_TZ))
)
assert (
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))
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 1, 2, 17, 0, 0, tzinfo=NY_TZ))
)

# crypto
Expand All @@ -137,65 +141,65 @@ def test_get_next_market_close():
# equity within market hours
assert (
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))
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 21, 16, 0, 0, tzinfo=NY_TZ))
)

# equity out of market hours
assert (
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))
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 22, 16, 0, 0, tzinfo=NY_TZ))
)

# equity weekend
assert (
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))
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 12, 16, 0, 0, tzinfo=NY_TZ))
)

# equity holiday
assert (
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))
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 20, 16, 0, 0, tzinfo=NY_TZ))
)

# equity early close holiday
assert (
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))
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 11, 24, 13, 0, 0, tzinfo=NY_TZ))
)
assert (
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))
== format_datetime_to_unix_timestamp(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_WED_2023_6_21_22)
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ))
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ))
)
assert (
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))
== format_datetime_to_unix_timestamp(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_SUN_2023_6_18_16)
== format_datetime_to_utc_iso_string(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ))
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ))
)
assert (
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))
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 6, 23, 17, 0, 0, tzinfo=NY_TZ))
)

# fx & metal holiday
assert (
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))
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 1, 6, 17, 0, 0, tzinfo=NY_TZ))
)
assert (
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))
== format_datetime_to_unix_timestamp(datetime.datetime(2023, 1, 6, 17, 0, 0, tzinfo=NY_TZ))
)

# crypto
Expand Down