|
| 1 | +from .base import BaseClient |
| 2 | +from typing import Optional, Any, Dict, List, Union |
| 3 | +from .models import ( |
| 4 | + Snapshot, |
| 5 | + Direction, |
| 6 | + OptionContractSnapshot, |
| 7 | + SnapshotMarketType, |
| 8 | + SnapshotTickerFullBook, |
| 9 | +) |
| 10 | +from urllib3 import HTTPResponse |
| 11 | + |
| 12 | + |
| 13 | +class SnapshotClient(BaseClient): |
| 14 | + def get_snapshot_all( |
| 15 | + self, |
| 16 | + market_type: Optional[Union[str, SnapshotMarketType]] = "stocks", |
| 17 | + tickers: Optional[Union[str, List[str]]] = None, |
| 18 | + params: Optional[Dict[str, Any]] = None, |
| 19 | + raw: bool = False, |
| 20 | + ) -> Union[List[Snapshot], HTTPResponse]: |
| 21 | + """ |
| 22 | + Get the most up-to-date market data for all traded stock symbols. |
| 23 | +
|
| 24 | + Note: Snapshot data is cleared at 12am EST and gets populated as data is received from the exchanges. This can happen as early as 4am EST. |
| 25 | +
|
| 26 | + :param market_type: Which market to get a snapshot of. |
| 27 | + :param tickers: A comma separated list of tickers to get snapshots for. |
| 28 | + :return: List of Snapshots |
| 29 | + """ |
| 30 | + url = f"/v2/snapshot/locale/us/markets/{market_type}/tickers" |
| 31 | + if type(tickers) is list: |
| 32 | + tickers = ",".join(tickers) |
| 33 | + return self._get( |
| 34 | + path=url, |
| 35 | + params=self._get_params(self.get_snapshot_all, locals()), |
| 36 | + deserializer=Snapshot.from_dict, |
| 37 | + raw=raw, |
| 38 | + ) |
| 39 | + |
| 40 | + def get_snapshot_direction( |
| 41 | + self, |
| 42 | + direction: Union[str, Direction], |
| 43 | + market_type: Optional[Union[str, SnapshotMarketType]] = "stocks", |
| 44 | + params: Optional[Dict[str, Any]] = None, |
| 45 | + raw: bool = False, |
| 46 | + ) -> Union[List[Snapshot], HTTPResponse]: |
| 47 | + """ |
| 48 | + Get the most up-to-date market data for the current top 20 gainers or losers of the day in the stocks/equities markets. |
| 49 | +
|
| 50 | + Top gainers are those tickers whose price has increased by the highest percentage since the previous day's close. Top losers are those tickers whose price has decreased by the highest percentage since the previous day's close. |
| 51 | +
|
| 52 | + Note: Snapshot data is cleared at 12am EST and gets populated as data is received from the exchanges. |
| 53 | +
|
| 54 | + :param market_type: Which market to get a snapshot of. |
| 55 | + :param direction: The direction ("gainers" or "losers") |
| 56 | + :return: List of Snapshots |
| 57 | + """ |
| 58 | + url = f"/v2/snapshot/locale/us/markets/{market_type}/{direction}" |
| 59 | + return self._get( |
| 60 | + path=url, |
| 61 | + params=self._get_params(self.get_snapshot_direction, locals()), |
| 62 | + result_key="tickers", |
| 63 | + deserializer=Snapshot.from_dict, |
| 64 | + raw=raw, |
| 65 | + ) |
| 66 | + |
| 67 | + def get_snapshot_ticker( |
| 68 | + self, |
| 69 | + ticker: str, |
| 70 | + market_type: Optional[Union[str, SnapshotMarketType]] = "stocks", |
| 71 | + params: Optional[Dict[str, Any]] = None, |
| 72 | + raw: bool = False, |
| 73 | + ) -> Union[Snapshot, HTTPResponse]: |
| 74 | + """ |
| 75 | + Get the most up-to-date market data for all traded stock symbols. |
| 76 | +
|
| 77 | + Note: Snapshot data is cleared at 12am EST and gets populated as data is received from the exchanges. This can happen as early as 4am EST. |
| 78 | +
|
| 79 | + :param market_type: Which market to get a snapshot of. |
| 80 | + :param ticker: The ticker symbol. |
| 81 | + :return: List of Snapshots |
| 82 | + """ |
| 83 | + url = f"/v2/snapshot/locale/us/markets/{market_type}/tickers/{ticker}" |
| 84 | + return self._get( |
| 85 | + path=url, |
| 86 | + params=self._get_params(self.get_snapshot_ticker, locals()), |
| 87 | + result_key="ticker", |
| 88 | + deserializer=Snapshot.from_dict, |
| 89 | + raw=raw, |
| 90 | + ) |
| 91 | + |
| 92 | + def get_snapshot_option( |
| 93 | + self, |
| 94 | + underlying_asset: str, |
| 95 | + option_contract: str, |
| 96 | + params: Optional[Dict[str, Any]] = None, |
| 97 | + raw: bool = False, |
| 98 | + ) -> Union[OptionContractSnapshot, HTTPResponse]: |
| 99 | + """ |
| 100 | + Get the snapshot of an option contract for a stock equity. |
| 101 | +
|
| 102 | + :param underlying_asset: The underlying ticker symbol of the option contract. |
| 103 | + :param option_contract: The option contract identifier. |
| 104 | + :return: List of Snapshots |
| 105 | + """ |
| 106 | + url = f"/v2/snapshot/options/{underlying_asset}/{option_contract}" |
| 107 | + return self._get( |
| 108 | + path=url, |
| 109 | + params=self._get_params(self.get_snapshot_option, locals()), |
| 110 | + result_key="results", |
| 111 | + deserializer=OptionContractSnapshot.from_dict, |
| 112 | + raw=raw, |
| 113 | + ) |
| 114 | + |
| 115 | + def get_snapshot_crypto_book( |
| 116 | + self, |
| 117 | + ticker: str, |
| 118 | + params: Optional[Dict[str, Any]] = None, |
| 119 | + raw: bool = False, |
| 120 | + ) -> Union[SnapshotTickerFullBook, HTTPResponse]: |
| 121 | + """ |
| 122 | + Get the current level 2 book of a single ticker. This is the combined book from all of the exchanges. |
| 123 | +
|
| 124 | + Note: Snapshot data is cleared at 12am EST and gets populated as data is received from the exchanges. |
| 125 | +
|
| 126 | + :param ticker: The ticker symbol. |
| 127 | + :return: List of Snapshots |
| 128 | + """ |
| 129 | + url = f" /v2/snapshot/locale/global/markets/crypto/tickers/{ticker}/book" |
| 130 | + return self._get( |
| 131 | + path=url, |
| 132 | + params=self._get_params(self.get_snapshot_crypto_book, locals()), |
| 133 | + result_key="data", |
| 134 | + deserializer=SnapshotTickerFullBook.from_dict, |
| 135 | + raw=raw, |
| 136 | + ) |
0 commit comments