Skip to main content

longbridge/alert/
types.rs

1#![allow(missing_docs)]
2
3use rust_decimal::Decimal;
4use serde::{Deserialize, Serialize};
5
6use crate::utils::counter::deserialize_counter_id_as_symbol;
7
8/// Response for [`crate::AlertContext::list`]
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct AlertList {
11    /// Alert groups per security
12    pub lists: Vec<AlertSymbolGroup>,
13}
14
15/// Alert items for one security
16#[derive(Debug, Clone, Serialize, Deserialize)]
17pub struct AlertSymbolGroup {
18    /// Security symbol
19    #[serde(
20        rename = "counter_id",
21        deserialize_with = "deserialize_counter_id_as_symbol"
22    )]
23    pub symbol: String,
24    /// Ticker code (without market)
25    pub code: String,
26    /// Market, e.g. `"HK"`
27    pub market: String,
28    /// Security name
29    pub name: String,
30    /// Latest price
31    #[serde(with = "crate::serde_utils::decimal_opt_str_is_none")]
32    pub price: Option<Decimal>,
33    /// Day change amount
34    #[serde(with = "crate::serde_utils::decimal_opt_str_is_none")]
35    pub chg: Option<Decimal>,
36    /// Day change percentage
37    #[serde(with = "crate::serde_utils::decimal_opt_str_is_none")]
38    pub p_chg: Option<Decimal>,
39    /// Product type (may be empty)
40    #[serde(default)]
41    pub product: String,
42    /// Alert items
43    pub indicators: Vec<AlertItem>,
44}
45
46/// One price alert
47#[derive(Debug, Clone, Serialize, Deserialize)]
48pub struct AlertItem {
49    /// Alert ID
50    pub id: String,
51    /// Condition: "1"=price_rise, "2"=price_fall, "3"=pct_rise, "4"=pct_fall
52    pub indicator_id: String,
53    /// Whether the alert is active
54    pub enabled: bool,
55    /// Frequency: 1=daily, 2=every_time, 3=once
56    pub frequency: i32,
57    /// Scope
58    pub scope: i32,
59    /// Display text, e.g. "价格涨到 600"
60    pub text: String,
61    /// Trigger state flags
62    #[serde(default)]
63    pub state: Vec<i32>,
64    /// Trigger value: `{"price":"500"}` or `{"chg":"5"}`
65    pub value_map: serde_json::Value,
66}
67
68/// Alert condition
69#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
70pub enum AlertCondition {
71    /// Price rises above threshold
72    #[serde(rename = "1")]
73    PriceRise = 1,
74    /// Price falls below threshold
75    #[serde(rename = "2")]
76    PriceFall = 2,
77    /// Percentage rise above threshold
78    #[serde(rename = "3")]
79    PercentRise = 3,
80    /// Percentage fall below threshold
81    #[serde(rename = "4")]
82    PercentFall = 4,
83}
84
85/// Alert trigger frequency
86#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
87pub enum AlertFrequency {
88    /// Trigger once per day
89    #[serde(rename = "1")]
90    Daily = 1,
91    /// Trigger every time condition is met
92    #[serde(rename = "2")]
93    EveryTime = 2,
94    /// Trigger only once
95    #[serde(rename = "3")]
96    Once = 3,
97}