Skip to content

Commit

Permalink
Change default side to "left"
Browse files Browse the repository at this point in the history
Changes default calendar side to "left" for all calendars (from "right"
for 24h calendars and "both" for all others).
  • Loading branch information
maread99 committed Jun 9, 2022
1 parent fbb651c commit 19358b2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
13 changes: 2 additions & 11 deletions exchange_calendars/exchange_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class ExchangeCalendar(ABC):
Last calendar session will be `end`, if `end` is a session, or last
session before `end`.
side : default: "both" ("left" for 24 hour calendars)
side : default: "left"
Define which of session open/close and break start/end should
be treated as a trading minute:
"left" - treat session open and break_start as trading minutes,
Expand Down Expand Up @@ -287,9 +287,8 @@ def __init__(
self,
start: Date | None = None,
end: Date | None = None,
side: Literal["left", "right", "both", "neither"] | None = None,
side: Literal["left", "right", "both", "neither"] = "left",
):
side = side if side is not None else self.default_side()
if side not in self.valid_sides():
raise ValueError(
f"`side` must be in {self.valid_sides()} although received as {side}."
Expand Down Expand Up @@ -702,14 +701,6 @@ def valid_sides(cls) -> list[str]:
else:
return ["both", "left", "right", "neither"]

@classmethod
def default_side(cls) -> Literal["right", "both"]:
"""Default `side` option."""
if cls.close_times == cls.open_times:
return "right"
else:
return "both"

@property
def side(self) -> Literal["left", "right", "both", "neither"]:
"""Side on which sessions are closed.
Expand Down
3 changes: 1 addition & 2 deletions tests/test_calendar_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,7 @@ def answers(self) -> abc.Iterator[dict[str, Answers]]:
"""Dict of answers for tested calendars, key as name, value as Answers."""
d = {}
for name in self.calendar_names:
cal_cls = calendar_utils._default_calendar_factories[name]
d[name] = Answers(name, cal_cls.default_side())
d[name] = Answers(name, side="left")
return d

@pytest.fixture(scope="class")
Expand Down
29 changes: 16 additions & 13 deletions tests/test_exchange_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import pytest
import pytz
from pytz import UTC
from toolz import concat

from exchange_calendars import errors
from exchange_calendars.calendar_utils import (
Expand Down Expand Up @@ -109,18 +108,23 @@ def test_force_registration(self, dispatcher, dummy_cal_type):


def test_default_calendars():
"""Test dispatcher and calendar default values."""
dispatcher = ExchangeCalendarDispatcher(
calendars={},
calendar_factories=_default_calendar_factories,
aliases=_default_calendar_aliases,
)
# These are ordered aliases first, so that we can deregister the
# canonical factories when we're done with them, and we'll be done with
# them after they've been used by all aliases and by canonical name.
for name in concat([_default_calendar_aliases, _default_calendar_factories]):
assert (
dispatcher.get_calendar(name) is not None
), f"get_calendar({name}) returned None"
for alias in _default_calendar_aliases:
cal = dispatcher.get_calendar(alias)
assert cal is not None
dispatcher.deregister_calendar(alias)

for name, cal_cls in _default_calendar_factories.items():
cal = dispatcher.get_calendar(name)
assert cal is not None
assert cal.side == "left"
assert cal.first_session >= cal_cls.default_start()
assert cal.last_session <= cal_cls.default_end()
dispatcher.deregister_calendar(name)


Expand Down Expand Up @@ -179,8 +183,10 @@ def get_csv(name: str) -> pd.DataFrame:
parse_dates=[0, 1, 2, 3, 4],
infer_datetime_format=True,
)
# Necessary for csv saved prior to v4.0
if df.index.tz is not None:
df.index = df.index.tz_convert(None)
# Necessary for csv saved prior to v4.0
for col in df:
if df[col].dt.tz is None:
df[col] = df[col].dt.tz_localize(UTC)
Expand Down Expand Up @@ -1927,12 +1933,9 @@ def has_24h_session(self, name) -> abc.Iterator[bool]:
yield (df.close == df.open.shift(-1)).any()

@pytest.fixture(scope="class")
def default_side(self, has_24h_session) -> abc.Iterator[str]:
def default_side(self) -> abc.Iterator[str]:
"""Default calendar side."""
if has_24h_session:
yield "left"
else:
yield "both"
yield "left"

@pytest.fixture(scope="class")
def sides(self, has_24h_session) -> abc.Iterator[list[str]]:
Expand Down

0 comments on commit 19358b2

Please sign in to comment.