Skip to content

Commit

Permalink
Merge pull request #198 from alpacahq/agg-timestamp
Browse files Browse the repository at this point in the history
Add an alias for timestamp for streaming Agg
  • Loading branch information
umitanuki authored May 6, 2020
2 parents b342fef + cda7611 commit 6e397ef
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
15 changes: 10 additions & 5 deletions alpaca_trade_api/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -288,4 +290,7 @@ def df(self):
"e": "end",
"vw": "vwap",
"av": "totalvolume",

# this is extra alias in the client side
"t": "timestamp",
}
2 changes: 2 additions & 0 deletions alpaca_trade_api/stream2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6e397ef

Please sign in to comment.