diff --git a/.gitignore b/.gitignore index ec901759..f1438c84 100644 --- a/.gitignore +++ b/.gitignore @@ -131,3 +131,6 @@ dmypy.json #IDE configs .idea .vscode/ + +# Cloudflare stuff +Cloudflare_CA.pem diff --git a/alpaca/__init__.py b/alpaca/__init__.py index 61fb31ca..ea370a8e 100644 --- a/alpaca/__init__.py +++ b/alpaca/__init__.py @@ -1 +1 @@ -__version__ = "0.10.0" +__version__ = "0.12.0" diff --git a/alpaca/common/websocket.py b/alpaca/common/websocket.py index 95fa4c9d..fc94f948 100644 --- a/alpaca/common/websocket.py +++ b/alpaca/common/websocket.py @@ -163,9 +163,9 @@ def _cast(self, msg_type: str, msg: Dict) -> Union[BaseModel, RawData]: """ result = msg if not self._raw_data: - # convert msgpack timestamp to nanoseconds + if "t" in msg: - msg["t"] = msg["t"].seconds * int(1e9) + msg["t"].nanoseconds + msg["t"] = msg["t"].to_datetime() if "S" not in msg: return msg @@ -443,6 +443,8 @@ def run(self) -> None: except KeyboardInterrupt: print("keyboard interrupt, bye") pass + finally: + self.stop() def stop(self) -> None: """Stops the websocket connection.""" diff --git a/poetry.lock b/poetry.lock index d5076b7f..bebd3c0a 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1040,7 +1040,7 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "1.1" python-versions = "^3.8.0" -content-hash = "b704d08dd3759ecc36d8a76668d6a669c615de5e220efddbbc51119357f4be97" +content-hash = "69726c8c3975fdab8f0cbd7b6a0edeea1457edb13fcbe689f96adb67489e0cdb" [metadata.files] alabaster = [ diff --git a/pyproject.toml b/pyproject.toml index cae8a56e..64872579 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "alpaca-py" -version = "0.11.0" +version = "0.12.0" description = "The Official Python SDK for Alpaca APIs" authors = [ "Rahul Chowdhury ", @@ -18,7 +18,7 @@ include = [ "alpaca/py.typed" ] python = "^3.8.0" requests = "^2.30.0" pydantic = "^2.0.3" -pandas = "^2.0.0" +pandas = ">=1.5.3" msgpack = "^1.0.3" websockets = "^11.0.3" sseclient-py = "^1.7.2" diff --git a/tests/data/test_websockets.py b/tests/data/test_websockets.py index b38c33fb..7b2b4edc 100644 --- a/tests/data/test_websockets.py +++ b/tests/data/test_websockets.py @@ -1,4 +1,5 @@ import pytest +from msgpack.ext import Timestamp from alpaca.common.websocket import BaseStream from alpaca.data.enums import Exchange @@ -6,28 +7,25 @@ @pytest.fixture -def ws_client(): - client = BaseStream("endpoint", "key-id", "secret-key") - return client +def ws_client() -> BaseStream: + """Socket client fixture with pydantic models as output.""" + return BaseStream("endpoint", "key-id", "secret-key") @pytest.fixture -def raw_ws_client(): - raw_client = BaseStream("endpoint", "key-id", "secret-key", raw_data=True) - return raw_client +def raw_ws_client() -> BaseStream: + """Socket client fixture with raw data output.""" + return BaseStream("endpoint", "key-id", "secret-key", raw_data=True) @pytest.fixture -def timestamp(): - class MockTimestamp: - def __init__(self, _seconds, _nanoseconds): - self.seconds = _seconds - self.nanoseconds = _nanoseconds +def timestamp() -> Timestamp: + """Msgpack mock timestamp.""" + return Timestamp(seconds=10, nanoseconds=10) - return MockTimestamp(0, 0) - -def test_cast(ws_client, raw_ws_client, timestamp): +def test_cast(ws_client: BaseStream, raw_ws_client: BaseStream, timestamp: Timestamp): + """Test the value error in case there's a different timestamp type.""" # Bar bar_msg_type = "b" bar_msg_dict = {