Skip to content

Commit

Permalink
support the new data schema
Browse files Browse the repository at this point in the history
Trade and Quote are in nanoseconds
and all "sym" fields converted to "T"
  • Loading branch information
Shlomi Kushchi authored May 5, 2020
1 parent 97be1f8 commit 0deb567
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions alpaca_trade_api/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,24 @@ def df(self):
self._df = pd.concat(dfs, axis=1)
return self._df


class _Timestamped(object):
def __getattr__(self, key):
if key in self._raw:
val = self._raw[key]
if key == 'timestamp':
if val > 1000000000000000000:
# this is for supporing timestamp represented in nanosecond.
# the alpaca data api uses nanoseconds. polygon uses microseconds.
return pd.Timestamp(val, tz=NY)
return pd.Timestamp(val, tz=NY, unit='ms')
return pd.Timestamp(val, tz=NY, unit=self.unit)
return val
return getattr(super(), key)


class Agg(_Timestamped, Entity):
class _NanoTimestamped(_Timestamped):
unit = 'ms'


class _MicroTimestamped(_Timestamped):
unit = 'ms'

class Agg(_MicroTimestamped, Entity):
pass


Expand Down Expand Up @@ -189,11 +191,11 @@ def df(self):
return self._df


class Trade(_Timestamped, Entity):
class Trade(_NanoTimestamped, Entity):
pass


class Quote(_Timestamped, Entity):
class Quote(_NanoTimestamped, Entity):
pass


Expand Down Expand Up @@ -251,7 +253,7 @@ def df(self):


trade_mapping = {
"sym": "symbol",
"T": "symbol",
"c": "conditions",
"x": "exchange",
"p": "price",
Expand All @@ -272,7 +274,7 @@ def df(self):
}

agg_mapping = {
"sym": "symbol",
"T": "symbol",
"o": "open",
"c": "close",
"h": "high",
Expand Down

0 comments on commit 0deb567

Please sign in to comment.