use std::collections::HashMap;
use crate::prelude::*;
use crate::reporting::FinancialPeriod;
pub type Counter = u32;
#[derive(PartialEq, Eq, Hash)]
pub enum RatingType {
Buy,
Outperform,
Hold,
Underperform,
Sell,
}
pub struct Ratings {
pub ratings: HashMap<RatingType, Counter>,
pub scale_mark: Option<f32>,
}
pub struct PriceTarget {
pub high: Money,
pub low: Money,
pub average: Money,
pub number_of_analysts: Counter,
}
pub struct EPSConsensus {
pub consensus: Money,
pub number_of_estimates: Counter,
pub fiscal_period: FinancialPeriod,
pub fiscal_end_date: Date,
pub next_report_date: Date,
}
impl Ratings {
pub fn scaled_average(&self) -> f64 {
let (count, total) = self.ratings.iter().fold((0, 0), |(c, t), (k, v)| {
(
c + *v,
t + match *k {
RatingType::Buy => 1,
RatingType::Outperform => 2,
RatingType::Hold => 3,
RatingType::Underperform => 4,
RatingType::Sell => 5,
} * *v,
)
});
f64::from(total) / f64::from(count)
}
}
pub trait Peers {
fn peers(&self, for_symbol: Symbol) -> RequestResult<Symbols>;
}
pub trait AnalystRecommendations {
fn target_price(&self, for_symbol: Symbol) -> RequestResult<Snapshot<PriceTarget>>;
fn consensus_rating(&self, for_symbol: Symbol) -> RequestResult<Vec<Bounded<Ratings>>>;
fn consensus_eps(&self, for_symbol: Symbol) -> RequestResult<Vec<EPSConsensus>>;
}