#[derive(Debug, Clone)]
pub struct PolygonUrls {
pub rest_base: &'static str,
pub ws_realtime: &'static str,
pub ws_delayed: &'static str,
}
impl PolygonUrls {
pub const MAINNET: Self = Self {
rest_base: "https://api.massive.com",
ws_realtime: "wss://socket.massive.com/stocks",
ws_delayed: "wss://delayed.massive.com/stocks",
};
pub fn rest_url(&self) -> &str {
self.rest_base
}
pub fn ws_url(&self, is_realtime: bool) -> &str {
if is_realtime {
self.ws_realtime
} else {
self.ws_delayed
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[allow(dead_code)]
#[allow(clippy::upper_case_acronyms)]
pub enum PolygonEndpoint {
Tickers,
TickerDetails,
TickerTypes,
Aggregates, PreviousClose, GroupedDaily,
SingleSnapshot, AllSnapshot, UnifiedSnapshot,
Trades, LastTrade, Quotes, LastQuote,
SMA, EMA, MACD, RSI,
Dividends, Splits, FinancialRatios,
MarketStatus, MarketHolidays,
News,
OptionsContracts,
OptionsChain,
IndicesSnapshot,
ForexQuote,
ForexAggregates,
CryptoSnapshot,
ReferenceConditions,
ReferenceExchanges,
}
impl PolygonEndpoint {
pub fn path(&self) -> &'static str {
match self {
Self::Tickers => "/v3/reference/tickers",
Self::TickerDetails => "/v3/reference/tickers/{ticker}",
Self::TickerTypes => "/v3/reference/tickers/types",
Self::Aggregates => "/v2/aggs/ticker/{ticker}/range/{multiplier}/{timespan}/{from}/{to}",
Self::PreviousClose => "/v2/aggs/ticker/{ticker}/prev",
Self::GroupedDaily => "/v2/aggs/grouped/locale/us/market/stocks/{date}",
Self::SingleSnapshot => "/v2/snapshot/locale/us/markets/stocks/tickers/{ticker}",
Self::AllSnapshot => "/v2/snapshot/locale/us/markets/stocks/tickers",
Self::UnifiedSnapshot => "/v3/snapshot",
Self::Trades => "/v3/trades/{ticker}",
Self::LastTrade => "/v2/last/trade/{ticker}",
Self::Quotes => "/v3/quotes/{ticker}",
Self::LastQuote => "/v2/last/nbbo/{ticker}",
Self::SMA => "/v1/indicators/sma/{ticker}",
Self::EMA => "/v1/indicators/ema/{ticker}",
Self::MACD => "/v1/indicators/macd/{ticker}",
Self::RSI => "/v1/indicators/rsi/{ticker}",
Self::Dividends => "/vX/reference/dividends",
Self::Splits => "/vX/reference/splits",
Self::FinancialRatios => "/vX/reference/financials",
Self::MarketStatus => "/v1/marketstatus/now",
Self::MarketHolidays => "/v1/marketstatus/upcoming",
Self::News => "/v2/reference/news",
Self::OptionsContracts => "/v3/reference/options/contracts",
Self::OptionsChain => "/v3/snapshot/options/{underlyingAsset}",
Self::IndicesSnapshot => "/v3/snapshot/indices",
Self::ForexQuote => "/v1/last_quote/currencies/{from}/{to}",
Self::ForexAggregates => "/v2/aggs/ticker/{ticker}/range/{multiplier}/{timespan}/{from}/{to}",
Self::CryptoSnapshot => "/v2/snapshot/locale/global/markets/crypto/tickers",
Self::ReferenceConditions => "/v3/reference/conditions",
Self::ReferenceExchanges => "/v3/reference/exchanges",
}
}
pub fn _requires_auth(&self) -> bool {
true
}
pub fn _method(&self) -> &'static str {
"GET" }
}
pub fn format_symbol(symbol: &str) -> String {
symbol.to_uppercase()
}
pub fn map_timespan(interval: &str) -> &'static str {
match interval {
"1m" | "3m" | "5m" | "15m" | "30m" => "minute",
"1h" | "2h" | "4h" | "6h" | "8h" | "12h" => "hour",
"1d" => "day",
"1w" => "week",
"1M" => "month",
_ => "day", }
}
pub fn extract_multiplier(interval: &str) -> u32 {
interval.chars()
.take_while(|c| c.is_numeric())
.collect::<String>()
.parse()
.unwrap_or(1)
}