finance_query/models/corporate/
recommendation_trend.rs1use serde::{Deserialize, Serialize};
6
7#[derive(Default, Debug, Clone, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase")]
10#[non_exhaustive]
11pub struct RecommendationTrend {
12 #[serde(default)]
14 pub trend: Vec<RecommendationPeriod>,
15
16 #[serde(default)]
18 pub max_age: Option<i64>,
19}
20
21#[derive(Debug, Clone, Serialize, Deserialize)]
23#[serde(rename_all = "camelCase")]
24pub struct RecommendationPeriod {
25 #[serde(default)]
27 pub period: Option<String>,
28
29 #[serde(default)]
31 pub strong_buy: Option<i32>,
32
33 #[serde(default)]
35 pub buy: Option<i32>,
36
37 #[serde(default)]
39 pub hold: Option<i32>,
40
41 #[serde(default)]
43 pub sell: Option<i32>,
44
45 #[serde(default)]
47 pub strong_sell: Option<i32>,
48}
49
50impl RecommendationPeriod {
51 pub fn total(&self) -> i32 {
53 self.strong_buy.unwrap_or(0)
54 + self.buy.unwrap_or(0)
55 + self.hold.unwrap_or(0)
56 + self.sell.unwrap_or(0)
57 + self.strong_sell.unwrap_or(0)
58 }
59
60 pub fn average_score(&self) -> Option<f64> {
62 let total = self.total();
63 if total == 0 {
64 return None;
65 }
66
67 let score = (self.strong_buy.unwrap_or(0) as f64 * 1.0)
68 + (self.buy.unwrap_or(0) as f64 * 2.0)
69 + (self.hold.unwrap_or(0) as f64 * 3.0)
70 + (self.sell.unwrap_or(0) as f64 * 4.0)
71 + (self.strong_sell.unwrap_or(0) as f64 * 5.0);
72
73 Some(score / total as f64)
74 }
75}