-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #384 from alpacahq/feature/data-v2
Data v2
- Loading branch information
Showing
13 changed files
with
901 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from .rest import REST # noqa | ||
from .stream import Stream # noqa | ||
from .stream2 import StreamConn # noqa | ||
|
||
__version__ = '0.53.0' | ||
__version__ = '1.0.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from enum import Enum | ||
import pandas as pd | ||
from .entity import Bar, Trade, Quote | ||
|
||
|
||
trade_mapping_v2 = { | ||
"i": "id", | ||
"S": "symbol", | ||
"c": "conditions", | ||
"x": "exchange", | ||
"p": "price", | ||
"s": "size", | ||
"t": "timestamp", | ||
"z": "tape" | ||
} | ||
|
||
quote_mapping_v2 = { | ||
"S": "symbol", | ||
"ax": "ask_exchange", | ||
"ap": "ask_price", | ||
"as": "ask_size", | ||
"bx": "bid_exchange", | ||
"bp": "bid_price", | ||
"bs": "bid_size", | ||
"c": "conditions", | ||
"t": "timestamp", | ||
"z": "tape" | ||
} | ||
|
||
bar_mapping_v2 = { | ||
"S": "symbol", | ||
"o": "open", | ||
"h": "high", | ||
"l": "low", | ||
"c": "close", | ||
"v": "volume", | ||
"t": "timestamp" | ||
} | ||
|
||
|
||
class EntityListType(Enum): | ||
Trade = Trade, trade_mapping_v2 | ||
Quote = Quote, quote_mapping_v2 | ||
Bar = Bar, bar_mapping_v2 | ||
|
||
|
||
class EntityList(list): | ||
def __init__(self, entity_type: EntityListType, raw): | ||
entity = entity_type.value[0] | ||
super().__init__([entity(o) for o in raw]) | ||
self._raw = raw | ||
self.mapping = entity_type.value[1] | ||
|
||
@property | ||
def df(self): | ||
if not hasattr(self, '_df'): | ||
df = pd.DataFrame( | ||
self._raw, | ||
) | ||
|
||
df.columns = [self.mapping[c] for c in df.columns] | ||
if not df.empty: | ||
df.set_index('timestamp', inplace=True) | ||
df.index = pd.DatetimeIndex(df.index) | ||
self._df = df | ||
return self._df | ||
|
||
|
||
class BarsV2(EntityList): | ||
def __init__(self, raw): | ||
super().__init__(EntityListType.Bar, raw) | ||
|
||
|
||
class TradesV2(EntityList): | ||
def __init__(self, raw): | ||
super().__init__(EntityListType.Trade, raw) | ||
|
||
|
||
class QuotesV2(EntityList): | ||
def __init__(self, raw): | ||
super().__init__(EntityListType.Quote, raw) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.