Skip to content

Commit a2659f7

Browse files
authored
Merge pull request #18 from PhantomResearch/timestamp-fix
Enhance Polygon WebSocket client and add conversion function for equi…
2 parents 92cc8ad + 652775a commit a2659f7

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

phantom_core/polygon.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ def get_backfill_ohlcv(
249249
return ohlcv
250250

251251

252-
def get_polygon_websocket_client(tickers: list[str], api_key: str | None = None) -> PolygonWebSocketClient:
252+
def get_polygon_websocket_client(tickers: list[str], api_key: str | None = None) -> PolygonWebSocketClient: # TODO: check for usage; but probably delete
253253
"""
254254
Get Polygon WebSocket client. Subscribes to 1-minute aggs for the provided tickers.
255255
@@ -286,6 +286,22 @@ def get_last_price_cached(ticker: str) -> float:
286286

287287

288288
def convert_pg_agg(pg_agg: EquityAgg) -> OHLCVAgg:
289+
"""
290+
Convert a Polygon EquityAgg object to a phantom OHLCVAgg object.
291+
292+
This function transforms the data model from Polygon's format to phantom's internal
293+
representation. The main use case is when receiving realtime data streamed from
294+
Polygon via websocket.
295+
296+
Args:
297+
pg_agg (EquityAgg): The Polygon equity aggregate object to convert.
298+
299+
Returns:
300+
OHLCVAgg: A phantom OHLCVAgg object containing the same data.
301+
302+
Raises:
303+
ValueError: If the event type is not supported.
304+
"""
289305
data = pg_agg.__dict__
290306

291307
ticker = Ticker(data['symbol'].upper())
@@ -298,12 +314,10 @@ def convert_pg_agg(pg_agg: EquityAgg) -> OHLCVAgg:
298314
start_ts = pd.to_datetime(data['start_timestamp'], unit='ms')\
299315
.tz_localize('UTC')\
300316
.tz_convert('US/Eastern')\
301-
.tz_localize(None)\
302317
.to_pydatetime()
303318
end_ts = pd.to_datetime(data['end_timestamp'], unit='ms')\
304319
.tz_localize('UTC')\
305320
.tz_convert('US/Eastern')\
306-
.tz_localize(None)\
307321
.to_pydatetime()
308322

309323
volume = data['volume'] / 10 # TODO: needs verification

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "phantom-core"
3-
version = "0.0.17"
3+
version = "0.0.18"
44
description = "Phantom Core"
55
authors = ["Adam Lineberry <ablineberry@gmail.com>"]
66
license = "MIT"

0 commit comments

Comments
 (0)