use crate::constants::{Interval, TimeRange};
use crate::error::Result;
use crate::models::chart::Chart;
domain_handle! {
pub struct CryptoCoin { id, id }
cache: crate::models::crypto::CryptoQuote, chart
}
impl CryptoCoin {
pub async fn quote(&self, vs_currency: &str) -> Result<crate::models::crypto::CryptoQuote> {
fetch_via_with!(
self,
id,
CRYPTO,
fetch_crypto_quote,
vs_currency,
crate::models::crypto::CryptoQuote
)
}
pub async fn chart(
&self,
vs_currency: &str,
interval: Interval,
range: TimeRange,
) -> Result<Chart> {
let symbol = chart_symbol(self.id(), vs_currency);
fetch_chart_via!(self, symbol, interval, range)
}
pub async fn history(&self, vs_currency: &str, range: TimeRange) -> Result<Chart> {
self.chart(vs_currency, range.default_interval(), range)
.await
}
#[cfg(feature = "indicators")]
pub async fn indicators(
&self,
vs_currency: &str,
interval: Interval,
range: TimeRange,
) -> Result<crate::indicators::IndicatorsSummary> {
let chart = self.chart(vs_currency, interval, range).await?;
Ok(crate::indicators::summary::calculate_indicators(
&chart.candles,
))
}
#[cfg(feature = "indicators")]
pub async fn indicator(
&self,
indicator: crate::indicators::Indicator,
vs_currency: &str,
interval: Interval,
range: TimeRange,
) -> Result<crate::indicators::IndicatorResult> {
let chart = self.chart(vs_currency, interval, range).await?;
Ok(crate::indicators::compute_indicator(indicator, &chart)?)
}
#[cfg(feature = "risk")]
pub async fn risk(
&self,
vs_currency: &str,
interval: Interval,
range: TimeRange,
) -> Result<crate::risk::RiskSummary> {
let chart = self.chart(vs_currency, interval, range).await?;
Ok(crate::risk::compute_risk_summary_with_periods(
&chart.candles,
None,
crate::risk::periods_per_year(interval, crate::risk::TradingCalendar::Crypto),
))
}
}
fn chart_symbol(id: &str, vs_currency: &str) -> String {
format!("{}-{}", id.to_uppercase(), vs_currency.to_uppercase())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn chart_symbol_uppercases_ticker_and_vs() {
assert_eq!(chart_symbol("BTC", "USD"), "BTC-USD");
assert_eq!(chart_symbol("eth", "eur"), "ETH-EUR");
}
}