alpaca-py-nopandas 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. alpaca/__init__.py +2 -0
  2. alpaca/broker/__init__.py +8 -0
  3. alpaca/broker/client.py +2360 -0
  4. alpaca/broker/enums.py +528 -0
  5. alpaca/broker/models/__init__.py +7 -0
  6. alpaca/broker/models/accounts.py +347 -0
  7. alpaca/broker/models/cip.py +265 -0
  8. alpaca/broker/models/documents.py +159 -0
  9. alpaca/broker/models/funding.py +114 -0
  10. alpaca/broker/models/journals.py +71 -0
  11. alpaca/broker/models/rebalancing.py +80 -0
  12. alpaca/broker/models/trading.py +13 -0
  13. alpaca/broker/requests.py +1135 -0
  14. alpaca/common/__init__.py +6 -0
  15. alpaca/common/constants.py +13 -0
  16. alpaca/common/enums.py +64 -0
  17. alpaca/common/exceptions.py +47 -0
  18. alpaca/common/models.py +21 -0
  19. alpaca/common/requests.py +82 -0
  20. alpaca/common/rest.py +438 -0
  21. alpaca/common/types.py +7 -0
  22. alpaca/common/utils.py +89 -0
  23. alpaca/data/__init__.py +5 -0
  24. alpaca/data/enums.py +184 -0
  25. alpaca/data/historical/__init__.py +13 -0
  26. alpaca/data/historical/corporate_actions.py +76 -0
  27. alpaca/data/historical/crypto.py +299 -0
  28. alpaca/data/historical/news.py +63 -0
  29. alpaca/data/historical/option.py +230 -0
  30. alpaca/data/historical/screener.py +72 -0
  31. alpaca/data/historical/stock.py +226 -0
  32. alpaca/data/historical/utils.py +30 -0
  33. alpaca/data/live/__init__.py +11 -0
  34. alpaca/data/live/crypto.py +168 -0
  35. alpaca/data/live/news.py +62 -0
  36. alpaca/data/live/option.py +88 -0
  37. alpaca/data/live/stock.py +199 -0
  38. alpaca/data/live/websocket.py +390 -0
  39. alpaca/data/mappings.py +84 -0
  40. alpaca/data/models/__init__.py +7 -0
  41. alpaca/data/models/bars.py +83 -0
  42. alpaca/data/models/base.py +45 -0
  43. alpaca/data/models/corporate_actions.py +309 -0
  44. alpaca/data/models/news.py +90 -0
  45. alpaca/data/models/orderbooks.py +59 -0
  46. alpaca/data/models/quotes.py +78 -0
  47. alpaca/data/models/screener.py +68 -0
  48. alpaca/data/models/snapshots.py +132 -0
  49. alpaca/data/models/trades.py +204 -0
  50. alpaca/data/requests.py +580 -0
  51. alpaca/data/timeframe.py +148 -0
  52. alpaca/py.typed +0 -0
  53. alpaca/trading/__init__.py +5 -0
  54. alpaca/trading/client.py +784 -0
  55. alpaca/trading/enums.py +412 -0
  56. alpaca/trading/models.py +697 -0
  57. alpaca/trading/requests.py +604 -0
  58. alpaca/trading/stream.py +225 -0
  59. alpaca_py_nopandas-0.1.0.dist-info/LICENSE +201 -0
  60. alpaca_py_nopandas-0.1.0.dist-info/METADATA +299 -0
  61. alpaca_py_nopandas-0.1.0.dist-info/RECORD +62 -0
  62. alpaca_py_nopandas-0.1.0.dist-info/WHEEL +4 -0
@@ -0,0 +1,63 @@
1
+ from typing import Optional, Union
2
+
3
+ from alpaca.common.enums import BaseURL
4
+ from alpaca.common.rest import RESTClient
5
+ from alpaca.common.types import RawData
6
+ from alpaca.data.models.news import NewsSet
7
+ from alpaca.data.requests import NewsRequest
8
+
9
+
10
+ class NewsClient(RESTClient):
11
+ """
12
+ The REST client for interacting with Alpaca News API endpoints.
13
+
14
+ """
15
+
16
+ def __init__(
17
+ self,
18
+ api_key: Optional[str] = None,
19
+ secret_key: Optional[str] = None,
20
+ oauth_token: Optional[str] = None,
21
+ use_basic_auth: bool = False,
22
+ raw_data: bool = False,
23
+ url_override: Optional[str] = None,
24
+ ) -> None:
25
+ """
26
+ Instantiates a Historical Data Client.
27
+
28
+ Args:
29
+ api_key (Optional[str], optional): Alpaca API key. Defaults to None.
30
+ secret_key (Optional[str], optional): Alpaca API secret key. Defaults to None.
31
+ oauth_token (Optional[str]): The oauth token if authenticating via OAuth. Defaults to None.
32
+ use_basic_auth (bool, optional): If true, API requests will use basic authorization headers.
33
+ raw_data (bool, optional): If true, API responses will not be wrapped and raw responses will be returned from
34
+ methods. Defaults to False. This has not been implemented yet.
35
+ url_override (Optional[str], optional): If specified allows you to override the base url the client points
36
+ to for proxy/testing.
37
+ """
38
+ super().__init__(
39
+ api_key=api_key,
40
+ secret_key=secret_key,
41
+ oauth_token=oauth_token,
42
+ use_basic_auth=use_basic_auth,
43
+ api_version="v1beta1",
44
+ base_url=url_override if url_override is not None else BaseURL.DATA,
45
+ sandbox=False,
46
+ raw_data=raw_data,
47
+ )
48
+
49
+ def get_news(self, request_params: NewsRequest) -> Union[RawData, NewsSet]:
50
+ """Returns news data
51
+
52
+ Args:
53
+ request_params (NewsRequest): The request params to filter the news data"""
54
+ raw_news = self._get_marketdata(
55
+ path=f"/news",
56
+ params=request_params.to_request_fields(),
57
+ page_limit=50,
58
+ page_size=50,
59
+ )
60
+ if self._use_raw_data:
61
+ return raw_news
62
+
63
+ return NewsSet(raw_news)
@@ -0,0 +1,230 @@
1
+ from enum import Enum
2
+ from typing import Dict, Optional, Union
3
+
4
+ from alpaca.common.enums import BaseURL
5
+ from alpaca.common.rest import RESTClient
6
+ from alpaca.common.types import RawData
7
+ from alpaca.data.historical.utils import (
8
+ parse_obj_as_symbol_dict,
9
+ )
10
+ from alpaca.data.models.bars import BarSet
11
+ from alpaca.data.models.quotes import Quote
12
+ from alpaca.data.models.snapshots import OptionsSnapshot
13
+ from alpaca.data.models.trades import Trade, TradeSet
14
+ from alpaca.data.requests import (
15
+ OptionBarsRequest,
16
+ OptionChainRequest,
17
+ OptionLatestQuoteRequest,
18
+ OptionLatestTradeRequest,
19
+ OptionSnapshotRequest,
20
+ OptionTradesRequest,
21
+ )
22
+
23
+
24
+ class OptionHistoricalDataClient(RESTClient):
25
+ """
26
+ The REST client for interacting with Alpaca Market Data API option data endpoints.
27
+
28
+ Learn more on https://docs.alpaca.markets/docs/about-market-data-api
29
+ """
30
+
31
+ def __init__(
32
+ self,
33
+ api_key: Optional[str] = None,
34
+ secret_key: Optional[str] = None,
35
+ oauth_token: Optional[str] = None,
36
+ use_basic_auth: bool = False,
37
+ raw_data: bool = False,
38
+ url_override: Optional[str] = None,
39
+ sandbox: bool = False,
40
+ ) -> None:
41
+ """
42
+ Instantiates a Historical Data Client.
43
+
44
+ Args:
45
+ api_key (Optional[str], optional): Alpaca API key. Defaults to None.
46
+ secret_key (Optional[str], optional): Alpaca API secret key. Defaults to None.
47
+ oauth_token (Optional[str]): The oauth token if authenticating via OAuth. Defaults to None.
48
+ use_basic_auth (bool, optional): If true, API requests will use basic authorization headers. Set to true if using
49
+ broker api sandbox credentials
50
+ raw_data (bool, optional): If true, API responses will not be wrapped and raw responses will be returned from
51
+ methods. Defaults to False. This has not been implemented yet.
52
+ url_override (Optional[str], optional): If specified allows you to override the base url the client points
53
+ to for proxy/testing.
54
+ sandbox (bool): True if using sandbox mode. Defaults to False.
55
+ """
56
+
57
+ base_url = (
58
+ url_override
59
+ if url_override is not None
60
+ else BaseURL.DATA_SANDBOX if sandbox else BaseURL.DATA
61
+ )
62
+
63
+ super().__init__(
64
+ api_key=api_key,
65
+ secret_key=secret_key,
66
+ oauth_token=oauth_token,
67
+ use_basic_auth=use_basic_auth,
68
+ api_version="v1beta1",
69
+ base_url=base_url,
70
+ sandbox=sandbox,
71
+ raw_data=raw_data,
72
+ )
73
+
74
+ def get_option_bars(
75
+ self, request_params: OptionBarsRequest
76
+ ) -> Union[BarSet, RawData]:
77
+ """Returns bar data for an option contract or list of option contracts over a given
78
+ time period and timeframe.
79
+
80
+ Args:
81
+ request_params (OptionBarsRequest): The request object for retrieving option bar data.
82
+
83
+ Returns:
84
+ Union[BarSet, RawData]: The bar data either in raw or wrapped form
85
+ """
86
+
87
+ # paginated get request for market data api
88
+ raw_bars = self._get_marketdata(
89
+ path=f"/options/bars",
90
+ params=request_params.to_request_fields(),
91
+ page_size=10_000,
92
+ )
93
+
94
+ if self._use_raw_data:
95
+ return raw_bars
96
+
97
+ return BarSet(raw_bars)
98
+
99
+ def get_option_exchange_codes(self) -> RawData:
100
+ """Returns the mapping between the option exchange codes and the corresponding exchanges names.
101
+
102
+ Args:
103
+ None
104
+
105
+ Returns:
106
+ RawData: The mapping between the option exchange codes and the corresponding exchanges names.
107
+ """
108
+ path = "/options/meta/exchanges"
109
+ raw_exchange_code = self.get(
110
+ path=path,
111
+ api_version=self._api_version,
112
+ )
113
+
114
+ return raw_exchange_code
115
+
116
+ def get_option_latest_quote(
117
+ self, request_params: OptionLatestQuoteRequest
118
+ ) -> Union[Dict[str, Quote], RawData]:
119
+ """Retrieves the latest quote for an option symbol or list of option symbols.
120
+
121
+ Args:
122
+ request_params (OptionLatestQuoteRequest): The request object for retrieving the latest quote data.
123
+
124
+ Returns:
125
+ Union[Dict[str, Quote], RawData]: The latest quote in raw or wrapped format
126
+ """
127
+ raw_latest_quotes = self._get_marketdata(
128
+ path=f"/options/quotes/latest",
129
+ params=request_params.to_request_fields(),
130
+ )
131
+
132
+ if self._use_raw_data:
133
+ return raw_latest_quotes
134
+
135
+ return parse_obj_as_symbol_dict(Quote, raw_latest_quotes)
136
+
137
+ def get_option_latest_trade(
138
+ self, request_params: OptionLatestTradeRequest
139
+ ) -> Union[Dict[str, Trade], RawData]:
140
+ """Retrieves the latest trade for an option symbol or list of option symbols.
141
+
142
+ Args:
143
+ request_params (OptionLatestTradeRequest): The request object for retrieving the latest trade data.
144
+
145
+ Returns:
146
+ Union[Dict[str, Trade], RawData]: The latest trade in raw or wrapped format
147
+ """
148
+ raw_latest_trades = self._get_marketdata(
149
+ path=f"/options/trades/latest",
150
+ params=request_params.to_request_fields(),
151
+ )
152
+
153
+ if self._use_raw_data:
154
+ return raw_latest_trades
155
+
156
+ return parse_obj_as_symbol_dict(Trade, raw_latest_trades)
157
+
158
+ def get_option_trades(
159
+ self, request_params: OptionTradesRequest
160
+ ) -> Union[TradeSet, RawData]:
161
+ """The historical option trades API provides trade data for a list of contract symbols between the specified dates up to 7 days ago.
162
+
163
+ Args:
164
+ request_params (OptionTradesRequest): The request object for retrieving option trade data.
165
+
166
+ Returns:
167
+ Union[TradeSet, RawData]: The trade data either in raw or wrapped form
168
+ """
169
+ raw_trades = self._get_marketdata(
170
+ path=f"/options/trades",
171
+ params=request_params.to_request_fields(),
172
+ page_size=10_000,
173
+ )
174
+
175
+ if self._use_raw_data:
176
+ return raw_trades
177
+
178
+ return TradeSet(raw_trades)
179
+
180
+ def get_option_snapshot(
181
+ self, request_params: OptionSnapshotRequest
182
+ ) -> Union[Dict[str, OptionsSnapshot], RawData]:
183
+ """Returns snapshots of queried symbols. OptionsSnapshot contain latest trade,
184
+ latest quote, implied volatility, and greeks for the queried symbols.
185
+
186
+ Args:
187
+ request_params (OptionSnapshotRequest): The request object for retrieving snapshot data.
188
+
189
+ Returns:
190
+ Union[Dict[str, OptionsSnapshot], RawData]: The snapshot data either in raw or wrapped form
191
+ """
192
+ raw_snapshots = self._get_marketdata(
193
+ path=f"/options/snapshots",
194
+ params=request_params.to_request_fields(),
195
+ page_limit=1000,
196
+ page_size=1000,
197
+ )
198
+
199
+ if self._use_raw_data:
200
+ return raw_snapshots
201
+
202
+ return parse_obj_as_symbol_dict(OptionsSnapshot, raw_snapshots)
203
+
204
+ def get_option_chain(
205
+ self, request_params: OptionChainRequest
206
+ ) -> Union[Dict[str, OptionsSnapshot], RawData]:
207
+ """The option chain endpoint for underlying symbol provides the latest trade, latest quote,
208
+ implied volatility, and greeks for each contract symbol of the underlying symbol.
209
+
210
+ Args:
211
+ request_params (OptionChainRequest): The request object for retrieving snapshot data.
212
+
213
+ Returns:
214
+ Union[Dict[str, OptionsSnapshot], RawData]: The snapshot data either in raw or wrapped form
215
+ """
216
+
217
+ params = request_params.to_request_fields()
218
+ del params["underlying_symbol"]
219
+
220
+ raw_snapshots = self._get_marketdata(
221
+ path=f"/options/snapshots/{request_params.underlying_symbol}",
222
+ params=params,
223
+ page_limit=1000,
224
+ page_size=1000,
225
+ )
226
+
227
+ if self._use_raw_data:
228
+ return raw_snapshots
229
+
230
+ return parse_obj_as_symbol_dict(OptionsSnapshot, raw_snapshots)
@@ -0,0 +1,72 @@
1
+ from typing import Optional, Union
2
+
3
+ from alpaca.common.enums import BaseURL
4
+ from alpaca.common.rest import RESTClient
5
+ from alpaca.common.types import RawData
6
+ from alpaca.data.models.screener import MostActives, Movers
7
+ from alpaca.data.requests import MarketMoversRequest, MostActivesRequest
8
+
9
+
10
+ class ScreenerClient(RESTClient):
11
+ """
12
+ The REST client for interacting with Alpaca Screener API endpoints.
13
+
14
+ Learn more on https://docs.alpaca.markets/reference/mostactives
15
+ """
16
+
17
+ def __init__(
18
+ self,
19
+ api_key: Optional[str] = None,
20
+ secret_key: Optional[str] = None,
21
+ oauth_token: Optional[str] = None,
22
+ use_basic_auth: bool = False,
23
+ raw_data: bool = False,
24
+ url_override: Optional[str] = None,
25
+ ) -> None:
26
+ """
27
+ Instantiates a Historical Data Client.
28
+
29
+ Args:
30
+ api_key (Optional[str], optional): Alpaca API key. Defaults to None.
31
+ secret_key (Optional[str], optional): Alpaca API secret key. Defaults to None.
32
+ oauth_token (Optional[str]): The oauth token if authenticating via OAuth. Defaults to None.
33
+ use_basic_auth (bool, optional): If true, API requests will use basic authorization headers.
34
+ raw_data (bool, optional): If true, API responses will not be wrapped and raw responses will be returned from
35
+ methods. Defaults to False. This has not been implemented yet.
36
+ url_override (Optional[str], optional): If specified allows you to override the base url the client points
37
+ to for proxy/testing.
38
+ """
39
+ super().__init__(
40
+ api_key=api_key,
41
+ secret_key=secret_key,
42
+ oauth_token=oauth_token,
43
+ use_basic_auth=use_basic_auth,
44
+ api_version="v1beta1",
45
+ base_url=url_override if url_override is not None else BaseURL.DATA,
46
+ sandbox=False,
47
+ raw_data=raw_data,
48
+ )
49
+
50
+ def get_most_actives(
51
+ self, request_params: MostActivesRequest
52
+ ) -> Union[RawData, MostActives]:
53
+ """Returns most active stocks."""
54
+ response = self.get(
55
+ path="/screener/stocks/most-actives",
56
+ data=request_params.to_request_fields(),
57
+ )
58
+ if self._use_raw_data:
59
+ return response
60
+ return MostActives(**response)
61
+
62
+ def get_market_movers(
63
+ self, request_params: MarketMoversRequest
64
+ ) -> Union[RawData, Movers]:
65
+ """Return market movers."""
66
+ response = self.get(
67
+ path=f"/screener/{request_params.market_type.lower()}/movers",
68
+ data=request_params.model_dump(exclude={"market_type"}),
69
+ )
70
+ if self._use_raw_data:
71
+ return response
72
+ return Movers(**response)
@@ -0,0 +1,226 @@
1
+ from collections import defaultdict
2
+ from enum import Enum
3
+ from typing import Dict, List, Optional, Union
4
+
5
+ from alpaca.common.constants import DATA_V2_MAX_LIMIT
6
+ from alpaca.common.enums import BaseURL
7
+ from alpaca.common.rest import RESTClient
8
+ from alpaca.common.types import RawData
9
+ from alpaca.data import Bar, Quote, Snapshot, Trade
10
+ from alpaca.data.historical.utils import (
11
+ parse_obj_as_symbol_dict,
12
+ )
13
+ from alpaca.data.models import BarSet, QuoteSet, TradeSet
14
+ from alpaca.data.requests import (
15
+ StockBarsRequest,
16
+ StockLatestBarRequest,
17
+ StockLatestQuoteRequest,
18
+ StockLatestTradeRequest,
19
+ StockQuotesRequest,
20
+ StockSnapshotRequest,
21
+ StockTradesRequest,
22
+ )
23
+
24
+
25
+ class StockHistoricalDataClient(RESTClient):
26
+ """
27
+ The REST client for interacting with Alpaca Market Data API stock data endpoints.
28
+
29
+ Learn more on https://alpaca.markets/docs/market-data/
30
+ """
31
+
32
+ def __init__(
33
+ self,
34
+ api_key: Optional[str] = None,
35
+ secret_key: Optional[str] = None,
36
+ oauth_token: Optional[str] = None,
37
+ use_basic_auth: bool = False,
38
+ raw_data: bool = False,
39
+ url_override: Optional[str] = None,
40
+ sandbox: bool = False,
41
+ ) -> None:
42
+ """
43
+ Instantiates a Historical Data Client.
44
+
45
+ Args:
46
+ api_key (Optional[str], optional): Alpaca API key. Defaults to None.
47
+ secret_key (Optional[str], optional): Alpaca API secret key. Defaults to None.
48
+ oauth_token (Optional[str]): The oauth token if authenticating via OAuth. Defaults to None.
49
+ use_basic_auth (bool, optional): If true, API requests will use basic authorization headers. Set to true if using
50
+ broker api sandbox credentials
51
+ raw_data (bool, optional): If true, API responses will not be wrapped and raw responses will be returned from
52
+ methods. Defaults to False. This has not been implemented yet.
53
+ url_override (Optional[str], optional): If specified allows you to override the base url the client points
54
+ to for proxy/testing.
55
+ sandbox (bool): True if using sandbox mode. Defaults to False.
56
+ """
57
+
58
+ base_url = (
59
+ url_override
60
+ if url_override is not None
61
+ else BaseURL.DATA_SANDBOX if sandbox else BaseURL.DATA
62
+ )
63
+
64
+ super().__init__(
65
+ api_key=api_key,
66
+ secret_key=secret_key,
67
+ oauth_token=oauth_token,
68
+ use_basic_auth=use_basic_auth,
69
+ api_version="v2",
70
+ base_url=base_url,
71
+ sandbox=sandbox,
72
+ raw_data=raw_data,
73
+ )
74
+
75
+ def get_stock_bars(
76
+ self, request_params: StockBarsRequest
77
+ ) -> Union[BarSet, RawData]:
78
+ """Returns bar data for an equity or list of equities over a given
79
+ time period and timeframe.
80
+
81
+ Args:
82
+ request_params (GetStockBarsRequest): The request object for retrieving stock bar data.
83
+
84
+ Returns:
85
+ Union[BarSet, RawData]: The bar data either in raw or wrapped form
86
+ """
87
+ raw_bars = self._get_marketdata(
88
+ path="/stocks/bars",
89
+ params=request_params.to_request_fields(),
90
+ page_size=10_000,
91
+ )
92
+
93
+ if self._use_raw_data:
94
+ return raw_bars
95
+
96
+ return BarSet(raw_bars)
97
+
98
+ def get_stock_quotes(
99
+ self, request_params: StockQuotesRequest
100
+ ) -> Union[QuoteSet, RawData]:
101
+ """Returns level 1 quote data over a given time period for a security or list of securities.
102
+
103
+ Args:
104
+ request_params (GetStockQuotesRequest): The request object for retrieving stock quote data.
105
+
106
+ Returns:
107
+ Union[QuoteSet, RawData]: The quote data either in raw or wrapped form
108
+ """
109
+ raw_quotes = self._get_marketdata(
110
+ path="/stocks/quotes",
111
+ params=request_params.to_request_fields(),
112
+ page_size=10_000,
113
+ )
114
+
115
+ if self._use_raw_data:
116
+ return raw_quotes
117
+
118
+ return QuoteSet(raw_quotes)
119
+
120
+ def get_stock_trades(
121
+ self, request_params: StockTradesRequest
122
+ ) -> Union[TradeSet, RawData]:
123
+ """Returns the price and sales history over a given time period for a security or list of securities.
124
+
125
+ Args:
126
+ request_params (GetStockTradesRequest): The request object for retrieving stock trade data.
127
+
128
+ Returns:
129
+ Union[TradeSet, RawData]: The trade data either in raw or wrapped form
130
+ """
131
+ raw_trades = self._get_marketdata(
132
+ path="/stocks/trades",
133
+ params=request_params.to_request_fields(),
134
+ page_size=10_000,
135
+ )
136
+
137
+ if self._use_raw_data:
138
+ return raw_trades
139
+
140
+ return TradeSet(raw_trades)
141
+
142
+ def get_stock_latest_trade(
143
+ self, request_params: StockLatestTradeRequest
144
+ ) -> Union[Dict[str, Trade], RawData]:
145
+ """Retrieves the latest trade for an equity symbol or list of equities.
146
+
147
+ Args:
148
+ request_params (StockLatestTradeRequest): The request object for retrieving the latest trade data.
149
+
150
+ Returns:
151
+ Union[Dict[str, Trade], RawData]: The latest trade in raw or wrapped format
152
+ """
153
+ raw_latest_trades = self._get_marketdata(
154
+ path="/stocks/trades/latest",
155
+ params=request_params.to_request_fields(),
156
+ )
157
+
158
+ if self._use_raw_data:
159
+ return raw_latest_trades
160
+
161
+ return parse_obj_as_symbol_dict(Trade, raw_latest_trades)
162
+
163
+ def get_stock_latest_quote(
164
+ self, request_params: StockLatestQuoteRequest
165
+ ) -> Union[Dict[str, Quote], RawData]:
166
+ """Retrieves the latest quote for an equity symbol or list of equity symbols.
167
+
168
+ Args:
169
+ request_params (StockLatestQuoteRequest): The request object for retrieving the latest quote data.
170
+
171
+ Returns:
172
+ Union[Dict[str, Quote], RawData]: The latest quote in raw or wrapped format
173
+ """
174
+ raw_latest_quotes = self._get_marketdata(
175
+ path="/stocks/quotes/latest",
176
+ params=request_params.to_request_fields(),
177
+ )
178
+
179
+ if self._use_raw_data:
180
+ return raw_latest_quotes
181
+
182
+ return parse_obj_as_symbol_dict(Quote, raw_latest_quotes)
183
+
184
+ def get_stock_latest_bar(
185
+ self, request_params: StockLatestBarRequest
186
+ ) -> Union[Dict[str, Bar], RawData]:
187
+ """Retrieves the latest minute bar for an equity symbol or list of equity symbols.
188
+
189
+ Args:
190
+ request_params (StockLatestBarRequest): The request object for retrieving the latest bar data.
191
+
192
+ Returns:
193
+ Union[Dict[str, Bar], RawData]: The latest minute bar in raw or wrapped format
194
+ """
195
+ raw_latest_bars = self._get_marketdata(
196
+ path="/stocks/bars/latest",
197
+ params=request_params.to_request_fields(),
198
+ )
199
+
200
+ if self._use_raw_data:
201
+ return raw_latest_bars
202
+
203
+ return parse_obj_as_symbol_dict(Bar, raw_latest_bars)
204
+
205
+ def get_stock_snapshot(
206
+ self, request_params: StockSnapshotRequest
207
+ ) -> Union[Dict[str, Snapshot], RawData]:
208
+ """Returns snapshots of queried symbols. Snapshots contain latest trade, latest quote, latest minute bar,
209
+ latest daily bar and previous daily bar data for the queried symbols.
210
+
211
+ Args:
212
+ request_params (StockSnapshotRequest): The request object for retrieving snapshot data.
213
+
214
+ Returns:
215
+ Union[SnapshotSet, RawData]: The snapshot data either in raw or wrapped form
216
+ """
217
+ raw_snapshots = self._get_marketdata(
218
+ path="/stocks/snapshots",
219
+ params=request_params.to_request_fields(),
220
+ no_sub_key=True,
221
+ )
222
+
223
+ if self._use_raw_data:
224
+ return raw_snapshots
225
+
226
+ return parse_obj_as_symbol_dict(Snapshot, raw_snapshots)
@@ -0,0 +1,30 @@
1
+ from collections import defaultdict
2
+ from typing import Dict, Type
3
+
4
+ from alpaca.common import HTTPResult, RawData
5
+
6
+ """
7
+ These functions were created and put in this file to handle all of the edge cases
8
+ for parsing data that exist in the market data API.
9
+
10
+ v2/stocks and v1beta2/crypto
11
+ """
12
+
13
+
14
+ def parse_obj_as_symbol_dict(model: Type, raw_data: RawData) -> Dict[str, Type]:
15
+ """
16
+ Parses raw_data into a dictionary where the keys are the string valued symbols and the values are the
17
+ parsed data into the model.
18
+
19
+ Args:
20
+ model (Type): The model we want to parse the data into
21
+ raw_data (RawData): The raw data from the API
22
+
23
+ Returns:
24
+ Dict[str, Type]: The symbol keyed dictionary of parsed data
25
+ """
26
+ if raw_data is None:
27
+ return {}
28
+ return {
29
+ k: model(symbol=k, raw_data=v) for k, v in raw_data.items() if v is not None
30
+ }
@@ -0,0 +1,11 @@
1
+ from alpaca.data.live.crypto import CryptoDataStream
2
+ from alpaca.data.live.news import NewsDataStream
3
+ from alpaca.data.live.option import OptionDataStream
4
+ from alpaca.data.live.stock import StockDataStream
5
+
6
+ __all__ = [
7
+ "CryptoDataStream",
8
+ "NewsDataStream",
9
+ "OptionDataStream",
10
+ "StockDataStream",
11
+ ]