use crate::constants::{Interval, TimeRange};
use crate::error::Result;
use crate::models::chart::Chart;
domain_handle! {
pub struct ForexPair {
from, from,
to, to,
}
cache: crate::models::forex::ForexQuote, chart
}
impl ForexPair {
pub async fn quote(&self) -> Result<crate::models::forex::ForexQuote> {
fetch_via_two!(
self,
from,
to,
FOREX,
fetch_forex_quote,
crate::models::forex::ForexQuote
)
}
pub async fn chart(&self, interval: Interval, range: TimeRange) -> Result<Chart> {
fetch_chart_via!(self, chart_symbol(&self.from, &self.to), interval, range)
}
pub async fn history(&self, range: TimeRange) -> Result<Chart> {
self.chart(range.default_interval(), range).await
}
}
impl_chartable_analytics!(ForexPair, crate::risk::TradingCalendar::Forex);
fn chart_symbol(from: &str, to: &str) -> String {
format!("{from}{to}=X")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn chart_symbol_uses_yahoo_fx_convention() {
assert_eq!(chart_symbol("USD", "EUR"), "USDEUR=X");
assert_eq!(chart_symbol("GBP", "JPY"), "GBPJPY=X");
}
}