From cda76115ec305e8f85a0490273d29e61762c9313 Mon Sep 17 00:00:00 2001 From: Hitoshi Harada Date: Wed, 6 May 2020 13:13:17 -0700 Subject: [PATCH] Add an alias for timestamp for streaming Agg --- alpaca_trade_api/entity.py | 15 ++++++++++----- alpaca_trade_api/stream2.py | 2 ++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/alpaca_trade_api/entity.py b/alpaca_trade_api/entity.py index 97f9c491..0788e444 100644 --- a/alpaca_trade_api/entity.py +++ b/alpaca_trade_api/entity.py @@ -130,25 +130,27 @@ def df(self): class _Timestamped(object): + _tskeys = ('timestamp',) + def __getattr__(self, key): if key in self._raw: val = self._raw[key] - if key == 'timestamp': - return pd.Timestamp(val, tz=NY, unit=self.unit) + if key in self._tskeys: + return pd.Timestamp(val, tz=NY, unit=self._unit) return val return getattr(super(), key) class _NanoTimestamped(_Timestamped): - unit = 'ns' + _unit = 'ns' class _MilliTimestamped(_Timestamped): - unit = 'ms' + _unit = 'ms' class Agg(_MilliTimestamped, Entity): - pass + _tskeys = ('timestamp', 'start', 'end') class Aggs(list): @@ -288,4 +290,7 @@ def df(self): "e": "end", "vw": "vwap", "av": "totalvolume", + + # this is extra alias in the client side + "t": "timestamp", } diff --git a/alpaca_trade_api/stream2.py b/alpaca_trade_api/stream2.py index e9a79660..9208846e 100644 --- a/alpaca_trade_api/stream2.py +++ b/alpaca_trade_api/stream2.py @@ -131,6 +131,8 @@ def _cast(self, channel, msg): return Quote({quote_mapping[k]: v for k, v in msg.items() if k in quote_mapping}) if channel.startswith('A.') or channel.startswith('AM.'): + # to be compatible with REST Agg + msg['t'] = msg['s'] return Agg({agg_mapping[k]: v for k, v in msg.items() if k in agg_mapping}) return Entity(msg)