Skip to content

Commit

Permalink
market open window method
Browse files Browse the repository at this point in the history
  • Loading branch information
rcholic committed Jul 16, 2024
1 parent 6435f29 commit 5bcb382
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
22 changes: 17 additions & 5 deletions cschwabpy/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
"""models folder."""
from datetime import datetime, date
from dateutil import parser
from dataclasses import dataclass
from pydantic import BaseModel, ConfigDict, Field
from typing import MutableMapping, Mapping, MutableSet, Any, List, Optional
from typing import MutableMapping, Mapping, MutableSet, Any, List, Tuple, Optional
from enum import Enum
import cschwabpy.util as util
import pandas as pd
import pytz

us_eastern_timezone = pytz.timezone("US/Eastern")

OptionChain_Headers = [
"underlying_price",
Expand Down Expand Up @@ -58,8 +62,8 @@ def __handle_item(self, item: Any) -> Any:
return result
elif isinstance(item, Enum):
return item.value
elif isinstance(item, datetime):
return str(item) # because datetime object is not JSON serializable
elif isinstance(item, datetime) or isinstance(item, date):
return str(item) # because datetime/date object is not JSON serializable
else:
return item

Expand Down Expand Up @@ -173,8 +177,16 @@ class MarketType(str, Enum):


class MarketHours(JSONSerializableBaseModel):
start: datetime
end: datetime
start: str
end: str

def open_window(
self, timezone: pytz.BaseTzInfo = us_eastern_timezone
) -> Tuple[datetime, datetime]:
"""Returns the market open window (tuple) in datetime format, defaulted to US eastern timezone.."""
start_time = parser.parse(self.start).astimezone(timezone)
end_time = parser.parse(self.end).astimezone(timezone)
return start_time, end_time


class SessionHours(JSONSerializableBaseModel):
Expand Down
22 changes: 18 additions & 4 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
OptionContract,
OptionContractType,
OptionContractStrategy,
Market,
MarketHours,
MarketHourInfo,
MarketType,
OptionMarket,
EquityMarket,
)
Expand Down Expand Up @@ -76,15 +78,19 @@ async def test_market_hours(httpx_mock: HTTPXMock) -> None:
}
market_hour = MarketHours(**market_hours_json)
assert market_hour is not None
assert market_hour.start.year == 2022
assert market_hour.start.month == 4
assert market_hour.start.day == 14
start_time, end_time = market_hour.open_window()
assert start_time.year == 2022
assert start_time.month == 4
assert start_time.day == 14
assert end_time.day == 14

market_hours_json2 = market_hour.to_json()
print("market_hours_json2: ", market_hours_json2)
restored_mareket_hours2 = MarketHours(**market_hours_json2)
assert restored_mareket_hours2 is not None
assert restored_mareket_hours2.start.year == 2022
start_time2, end_time2 = restored_mareket_hours2.open_window()
assert start_time2.year == 2022
assert end_time2.hour == 16

all_market_json = get_mock_response()["all_market_resp"]

Expand All @@ -94,6 +100,14 @@ async def test_market_hours(httpx_mock: HTTPXMock) -> None:
assert all_market.equity.EQ.sessionHours.regularMarket is not None
assert all_market.equity.EQ.sessionHours.preMarket is not None

equity_market = all_market.equity.EQ
assert equity_market.marketType == MarketType.Equity
equity_market_json = equity_market.to_json()
print("equity_market_json: ", equity_market_json)
restored_equity_market = Market(**equity_market_json)
assert restored_equity_market is not None
assert restored_equity_market.isOpen

mocked_token = mock_tokens()
mock_response = {
**all_market_json,
Expand Down

0 comments on commit 5bcb382

Please sign in to comment.