Skip to content

Commit 0deb567

Browse files
author
Shlomi Kushchi
authored
support the new data schema
Trade and Quote are in nanoseconds and all "sym" fields converted to "T"
1 parent 97be1f8 commit 0deb567

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

alpaca_trade_api/entity.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,22 +128,24 @@ def df(self):
128128
self._df = pd.concat(dfs, axis=1)
129129
return self._df
130130

131-
132131
class _Timestamped(object):
133132
def __getattr__(self, key):
134133
if key in self._raw:
135134
val = self._raw[key]
136135
if key == 'timestamp':
137-
if val > 1000000000000000000:
138-
# this is for supporing timestamp represented in nanosecond.
139-
# the alpaca data api uses nanoseconds. polygon uses microseconds.
140-
return pd.Timestamp(val, tz=NY)
141-
return pd.Timestamp(val, tz=NY, unit='ms')
136+
return pd.Timestamp(val, tz=NY, unit=self.unit)
142137
return val
143138
return getattr(super(), key)
144139

145140

146-
class Agg(_Timestamped, Entity):
141+
class _NanoTimestamped(_Timestamped):
142+
unit = 'ms'
143+
144+
145+
class _MicroTimestamped(_Timestamped):
146+
unit = 'ms'
147+
148+
class Agg(_MicroTimestamped, Entity):
147149
pass
148150

149151

@@ -189,11 +191,11 @@ def df(self):
189191
return self._df
190192

191193

192-
class Trade(_Timestamped, Entity):
194+
class Trade(_NanoTimestamped, Entity):
193195
pass
194196

195197

196-
class Quote(_Timestamped, Entity):
198+
class Quote(_NanoTimestamped, Entity):
197199
pass
198200

199201

@@ -251,7 +253,7 @@ def df(self):
251253

252254

253255
trade_mapping = {
254-
"sym": "symbol",
256+
"T": "symbol",
255257
"c": "conditions",
256258
"x": "exchange",
257259
"p": "price",
@@ -272,7 +274,7 @@ def df(self):
272274
}
273275

274276
agg_mapping = {
275-
"sym": "symbol",
277+
"T": "symbol",
276278
"o": "open",
277279
"c": "close",
278280
"h": "high",

0 commit comments

Comments
 (0)