finance_query/domains/
crypto.rs1use crate::constants::{Interval, TimeRange};
6use crate::error::Result;
7use crate::models::chart::Chart;
8
9domain_handle! {
10 pub struct CryptoCoin { id, id }
14 cache: crate::models::crypto::CryptoQuote, chart
15}
16
17impl CryptoCoin {
18 pub async fn quote(&self, vs_currency: &str) -> Result<crate::models::crypto::CryptoQuote> {
20 fetch_via_with!(
21 self,
22 id,
23 CRYPTO,
24 fetch_crypto_quote,
25 vs_currency,
26 crate::models::crypto::CryptoQuote
27 )
28 }
29
30 pub async fn chart(
39 &self,
40 vs_currency: &str,
41 interval: Interval,
42 range: TimeRange,
43 ) -> Result<Chart> {
44 let symbol = chart_symbol(self.id(), vs_currency);
45 fetch_chart_via!(self, symbol, interval, range)
46 }
47
48 pub async fn history(&self, vs_currency: &str, range: TimeRange) -> Result<Chart> {
51 self.chart(vs_currency, range.default_interval(), range)
52 .await
53 }
54
55 #[cfg(feature = "indicators")]
58 pub async fn indicators(
59 &self,
60 vs_currency: &str,
61 interval: Interval,
62 range: TimeRange,
63 ) -> Result<crate::indicators::IndicatorsSummary> {
64 let chart = self.chart(vs_currency, interval, range).await?;
65 Ok(crate::indicators::summary::calculate_indicators(
66 &chart.candles,
67 ))
68 }
69
70 #[cfg(feature = "indicators")]
72 pub async fn indicator(
73 &self,
74 indicator: crate::indicators::Indicator,
75 vs_currency: &str,
76 interval: Interval,
77 range: TimeRange,
78 ) -> Result<crate::indicators::IndicatorResult> {
79 let chart = self.chart(vs_currency, interval, range).await?;
80 Ok(crate::indicators::compute_indicator(indicator, &chart)?)
81 }
82
83 #[cfg(feature = "risk")]
86 pub async fn risk(
87 &self,
88 vs_currency: &str,
89 interval: Interval,
90 range: TimeRange,
91 ) -> Result<crate::risk::RiskSummary> {
92 let chart = self.chart(vs_currency, interval, range).await?;
93 Ok(crate::risk::compute_risk_summary_with_periods(
94 &chart.candles,
95 None,
96 crate::risk::periods_per_year(interval, crate::risk::TradingCalendar::Crypto),
97 ))
98 }
99}
100
101fn chart_symbol(id: &str, vs_currency: &str) -> String {
104 format!("{}-{}", id.to_uppercase(), vs_currency.to_uppercase())
105}
106
107#[cfg(test)]
108mod tests {
109 use super::*;
110
111 #[test]
112 fn chart_symbol_uppercases_ticker_and_vs() {
113 assert_eq!(chart_symbol("BTC", "USD"), "BTC-USD");
114 assert_eq!(chart_symbol("eth", "eur"), "ETH-EUR");
115 }
116}