ig_client/utils/
parsing.rs

1use crate::presentation::order::Status;
2use pretty_simple_display::{DebugPretty, DisplaySimple};
3use regex::Regex;
4use serde::{Deserialize, Deserializer, Serialize};
5use tracing::warn;
6
7/// Structure to represent the parsed option information from an instrument name
8#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, PartialEq)]
9pub struct ParsedOptionInfo {
10    /// Name of the underlying asset (e.g., "US Tech 100")
11    pub asset_name: String,
12    /// Strike price of the option (e.g., "19200")
13    pub strike: Option<String>,
14    /// Type of the option: CALL or PUT
15    pub option_type: Option<String>,
16}
17
18/// Structure to represent the parsed market data with additional information
19#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
20pub struct ParsedMarketData {
21    /// Unique identifier for the market (EPIC code)
22    pub epic: String,
23    /// Full name of the financial instrument
24    pub instrument_name: String,
25    /// Expiry date of the instrument (if applicable)
26    pub expiry: String,
27    /// Name of the underlying asset
28    pub asset_name: String,
29    /// Strike price for options
30    pub strike: Option<String>,
31    /// Type of option (e.g., 'CALL' or 'PUT')
32    pub option_type: Option<String>,
33}
34
35impl ParsedMarketData {
36    /// Checks if the current financial instrument is a call option.
37    ///
38    /// A call option is a financial derivative that gives the holder the right (but not the obligation)
39    /// to buy an underlying asset at a specified price within a specified time period. This method checks
40    /// whether the instrument represented by this instance is a call option by inspecting the `instrument_name`
41    /// field.
42    ///
43    /// # Returns
44    ///
45    /// * `true` if the instrument's name contains the substring `"CALL"`, indicating it is a call option.
46    /// * `false` otherwise.
47    ///
48    pub fn is_call(&self) -> bool {
49        self.instrument_name.contains("CALL")
50    }
51
52    /// Checks if the financial instrument is a "PUT" option.
53    ///
54    /// This method examines the `instrument_name` field of the struct to determine
55    /// if it contains the substring "PUT". If the substring is found, the method
56    /// returns `true`, indicating that the instrument is categorized as a "PUT" option.
57    /// Otherwise, it returns `false`.
58    ///
59    /// # Returns
60    /// * `true` - If `instrument_name` contains the substring "PUT".
61    /// * `false` - If `instrument_name` does not contain the substring "PUT".
62    ///
63    pub fn is_put(&self) -> bool {
64        self.instrument_name.contains("PUT")
65    }
66}
67
68/// Normalize text by removing accents and standardizing names
69///
70/// This function converts accented characters to their non-accented equivalents
71/// and standardizes certain names (e.g., "Japan" in different languages)
72pub fn normalize_text(text: &str) -> String {
73    // Special case for Japan in Spanish
74    if text.contains("Japón") {
75        return text.replace("Japón", "Japan");
76    }
77
78    let mut result = String::with_capacity(text.len());
79    for c in text.chars() {
80        match c {
81            'á' | 'à' | 'ä' | 'â' | 'ã' => result.push('a'),
82            'é' | 'è' | 'ë' | 'ê' => result.push('e'),
83            'í' | 'ì' | 'ï' | 'î' => result.push('i'),
84            'ó' | 'ò' | 'ö' | 'ô' | 'õ' => result.push('o'),
85            'ú' | 'ù' | 'ü' | 'û' => result.push('u'),
86            'ñ' => result.push('n'),
87            'ç' => result.push('c'),
88            'Á' | 'À' | 'Ä' | 'Â' | 'Ã' => result.push('A'),
89            'É' | 'È' | 'Ë' | 'Ê' => result.push('E'),
90            'Í' | 'Ì' | 'Ï' | 'Î' => result.push('I'),
91            'Ó' | 'Ò' | 'Ö' | 'Ô' | 'Õ' => result.push('O'),
92            'Ú' | 'Ù' | 'Ü' | 'Û' => result.push('U'),
93            'Ñ' => result.push('N'),
94            'Ç' => result.push('C'),
95            _ => result.push(c),
96        }
97    }
98    result
99}
100
101/// Parse the instrument name to extract asset name, strike price, and option type
102///
103/// # Examples
104///
105/// ```
106/// use ig_client::utils::parsing::parse_instrument_name;
107///
108/// let info = parse_instrument_name("US Tech 100 19200 CALL ($1)");
109/// assert_eq!(info.asset_name, "US Tech 100");
110/// assert_eq!(info.strike, Some("19200".to_string()));
111/// assert_eq!(info.option_type, Some("CALL".to_string()));
112///
113/// let info = parse_instrument_name("Germany 40");
114/// assert_eq!(info.asset_name, "Germany 40");
115/// assert_eq!(info.strike, None);
116/// assert_eq!(info.option_type, None);
117/// ```
118pub fn parse_instrument_name(instrument_name: &str) -> ParsedOptionInfo {
119    // Create regex patterns for different instrument name formats
120    // Lazy initialization of regex patterns
121    lazy_static::lazy_static! {
122        // Pattern for standard options like "US Tech 100 19200 CALL ($1)"
123        static ref OPTION_PATTERN: Regex = Regex::new(r"^(.*?)\s+(\d+(?:\.\d+)?)\s+(CALL|PUT)(?:\s+\(.*?\))?$").unwrap();
124
125        // Pattern for options with decimal strikes like "Volatility Index 10.5 PUT ($1)"
126        static ref DECIMAL_OPTION_PATTERN: Regex = Regex::new(r"^(.*?)\s+(\d+\.\d+)\s+(CALL|PUT)(?:\s+\(.*?\))?$").unwrap();
127
128        // Pattern for options with no space between parenthesis and strike like "Weekly Germany 40 (Wed)27500 PUT"
129        static ref SPECIAL_OPTION_PATTERN: Regex = Regex::new(r"^(.*?)\s+\(([^)]+)\)(\d+)\s+(CALL|PUT)(?:\s+\(.*?\))?$").unwrap();
130
131        // Pattern for options with incomplete parenthesis like "Weekly USDJPY 12950 CALL (Y100"
132        static ref INCOMPLETE_PAREN_PATTERN: Regex = Regex::new(r"^(.*?)\s+(\d+(?:\.\d+)?)\s+(CALL|PUT)\s+\([^)]*$").unwrap();
133
134        // Pattern for other instruments that don't follow the option pattern
135        static ref GENERIC_PATTERN: Regex = Regex::new(r"^(.*?)(?:\s+\(.*?\))?$").unwrap();
136
137        // Pattern to clean up asset names
138        static ref DAILY_WEEKLY_PATTERN: Regex = Regex::new(r"^(Daily|Weekly)\s+(.*?)$").unwrap();
139        static ref END_OF_MONTH_PATTERN: Regex = Regex::new(r"^(End of Month)\s+(.*?)$").unwrap();
140        static ref QUARTERLY_PATTERN: Regex = Regex::new(r"^(Quarterly)\s+(.*?)$").unwrap();
141        static ref MONTHLY_PATTERN: Regex = Regex::new(r"^(Monthly)\s+(.*?)$").unwrap();
142        static ref SUFFIX_PATTERN: Regex = Regex::new(r"^(.*?)\s+\(.*?\)$").unwrap();
143    }
144
145    // Helper function to clean up asset names
146    fn clean_asset_name(asset_name: &str) -> String {
147        // First normalize the text to remove accents
148        let normalized_name = normalize_text(asset_name);
149
150        // Remove prefixes like "Daily", "Weekly", etc.
151        let asset_name = if let Some(captures) = DAILY_WEEKLY_PATTERN.captures(&normalized_name) {
152            captures.get(2).unwrap().as_str().trim()
153        } else if let Some(captures) = END_OF_MONTH_PATTERN.captures(&normalized_name) {
154            captures.get(2).unwrap().as_str().trim()
155        } else if let Some(captures) = QUARTERLY_PATTERN.captures(&normalized_name) {
156            captures.get(2).unwrap().as_str().trim()
157        } else if let Some(captures) = MONTHLY_PATTERN.captures(&normalized_name) {
158            captures.get(2).unwrap().as_str().trim()
159        } else {
160            &normalized_name
161        };
162
163        // Remove suffixes like "(End of Month)", etc.
164        let asset_name = if let Some(captures) = SUFFIX_PATTERN.captures(asset_name) {
165            captures.get(1).unwrap().as_str().trim()
166        } else {
167            asset_name
168        };
169
170        asset_name.to_string()
171    }
172
173    if let Some(captures) = OPTION_PATTERN.captures(instrument_name) {
174        // This is an option with strike and type
175        let asset_name = captures.get(1).unwrap().as_str().trim();
176        ParsedOptionInfo {
177            asset_name: clean_asset_name(asset_name),
178            strike: Some(captures.get(2).unwrap().as_str().to_string()),
179            option_type: Some(captures.get(3).unwrap().as_str().to_string()),
180        }
181    } else if let Some(captures) = SPECIAL_OPTION_PATTERN.captures(instrument_name) {
182        // This is a special case like "Weekly Germany 40 (Wed)27500 PUT"
183        let base_name = captures.get(1).unwrap().as_str().trim();
184        ParsedOptionInfo {
185            asset_name: clean_asset_name(base_name),
186            strike: Some(captures.get(3).unwrap().as_str().to_string()),
187            option_type: Some(captures.get(4).unwrap().as_str().to_string()),
188        }
189    } else if let Some(captures) = INCOMPLETE_PAREN_PATTERN.captures(instrument_name) {
190        // This is a case with incomplete parenthesis like "Weekly USDJPY 12950 CALL (Y100"
191        let asset_name = captures.get(1).unwrap().as_str().trim();
192        ParsedOptionInfo {
193            asset_name: clean_asset_name(asset_name),
194            strike: Some(captures.get(2).unwrap().as_str().to_string()),
195            option_type: Some(captures.get(3).unwrap().as_str().to_string()),
196        }
197    } else if let Some(captures) = DECIMAL_OPTION_PATTERN.captures(instrument_name) {
198        // This is an option with decimal strike
199        let asset_name = captures.get(1).unwrap().as_str().trim();
200        ParsedOptionInfo {
201            asset_name: clean_asset_name(asset_name),
202            strike: Some(captures.get(2).unwrap().as_str().to_string()),
203            option_type: Some(captures.get(3).unwrap().as_str().to_string()),
204        }
205    } else if let Some(captures) = GENERIC_PATTERN.captures(instrument_name) {
206        // This is a generic instrument without strike or type
207        let asset_name = captures.get(1).unwrap().as_str().trim();
208        ParsedOptionInfo {
209            asset_name: clean_asset_name(asset_name),
210            strike: None,
211            option_type: None,
212        }
213    } else {
214        // Fallback for any other format
215        warn!("Could not parse instrument name: {}", instrument_name);
216        ParsedOptionInfo {
217            asset_name: instrument_name.to_string(),
218            strike: None,
219            option_type: None,
220        }
221    }
222}
223
224/// Helper function to deserialize null values as empty vectors
225pub fn deserialize_null_as_empty_vec<'de, D, T>(deserializer: D) -> Result<Vec<T>, D::Error>
226where
227    D: serde::Deserializer<'de>,
228    T: serde::Deserialize<'de>,
229{
230    let opt = Option::deserialize(deserializer)?;
231    Ok(opt.unwrap_or_default())
232}
233
234/// Helper function to deserialize a nullable status field
235/// When the status is null in the JSON, we default to Open status
236pub fn deserialize_nullable_status<'de, D>(deserializer: D) -> Result<Status, D::Error>
237where
238    D: Deserializer<'de>,
239{
240    let opt = Option::deserialize(deserializer)?;
241    Ok(opt.unwrap_or(Status::Open))
242}
243
244#[cfg(test)]
245mod tests {
246    use super::*;
247
248    #[test]
249    fn test_parse_instrument_name_standard_option() {
250        let info = parse_instrument_name("US Tech 100 19200 CALL ($1)");
251        assert_eq!(info.asset_name, "US Tech 100");
252        assert_eq!(info.strike, Some("19200".to_string()));
253        assert_eq!(info.option_type, Some("CALL".to_string()));
254    }
255
256    #[test]
257    fn test_parse_instrument_name_decimal_strike() {
258        let info = parse_instrument_name("Volatility Index 10.5 PUT ($1)");
259        assert_eq!(info.asset_name, "Volatility Index");
260        assert_eq!(info.strike, Some("10.5".to_string()));
261        assert_eq!(info.option_type, Some("PUT".to_string()));
262    }
263
264    #[test]
265    fn test_parse_instrument_name_no_option() {
266        let info = parse_instrument_name("Germany 40");
267        assert_eq!(info.asset_name, "Germany 40");
268        assert_eq!(info.strike, None);
269        assert_eq!(info.option_type, None);
270    }
271
272    #[test]
273    fn test_parse_instrument_name_with_parenthesis() {
274        let info = parse_instrument_name("US 500 (Mini)");
275        assert_eq!(info.asset_name, "US 500");
276        assert_eq!(info.strike, None);
277        assert_eq!(info.option_type, None);
278    }
279
280    #[test]
281    fn test_parse_instrument_name_special_format() {
282        let info = parse_instrument_name("Weekly Germany 40 (Wed)27500 PUT");
283        assert_eq!(info.asset_name, "Germany 40");
284        assert_eq!(info.strike, Some("27500".to_string()));
285        assert_eq!(info.option_type, Some("PUT".to_string()));
286    }
287
288    #[test]
289    fn test_parse_instrument_name_daily_prefix() {
290        let info = parse_instrument_name("Daily Germany 40 24225 CALL");
291        assert_eq!(info.asset_name, "Germany 40");
292        assert_eq!(info.strike, Some("24225".to_string()));
293        assert_eq!(info.option_type, Some("CALL".to_string()));
294    }
295
296    #[test]
297    fn test_parse_instrument_name_weekly_prefix() {
298        let info = parse_instrument_name("Weekly US Tech 100 19200 CALL");
299        assert_eq!(info.asset_name, "US Tech 100");
300        assert_eq!(info.strike, Some("19200".to_string()));
301        assert_eq!(info.option_type, Some("CALL".to_string()));
302    }
303
304    #[test]
305    fn test_parse_instrument_name_end_of_month_prefix() {
306        let info = parse_instrument_name("End of Month EU Stocks 50 4575 PUT");
307        assert_eq!(info.asset_name, "EU Stocks 50");
308        assert_eq!(info.strike, Some("4575".to_string()));
309        assert_eq!(info.option_type, Some("PUT".to_string()));
310    }
311
312    #[test]
313    fn test_parse_instrument_name_end_of_month_suffix() {
314        let info = parse_instrument_name("US 500 (End of Month) 3200 PUT");
315        assert_eq!(info.asset_name, "US 500");
316        assert_eq!(info.strike, Some("3200".to_string()));
317        assert_eq!(info.option_type, Some("PUT".to_string()));
318    }
319
320    #[test]
321    fn test_parse_instrument_name_quarterly_prefix() {
322        let info = parse_instrument_name("Quarterly GBPUSD 10000 PUT ($1)");
323        assert_eq!(info.asset_name, "GBPUSD");
324        assert_eq!(info.strike, Some("10000".to_string()));
325        assert_eq!(info.option_type, Some("PUT".to_string()));
326    }
327
328    #[test]
329    fn test_parse_instrument_name_weekly_with_day() {
330        let info = parse_instrument_name("Weekly Germany 40 (Mon) 18500 PUT");
331        assert_eq!(info.asset_name, "Germany 40");
332        assert_eq!(info.strike, Some("18500".to_string()));
333        assert_eq!(info.option_type, Some("PUT".to_string()));
334    }
335
336    #[test]
337    fn test_parse_instrument_name_incomplete_parenthesis() {
338        let info = parse_instrument_name("Weekly USDJPY 12950 CALL (Y100");
339        assert_eq!(info.asset_name, "USDJPY");
340        assert_eq!(info.strike, Some("12950".to_string()));
341        assert_eq!(info.option_type, Some("CALL".to_string()));
342    }
343
344    #[test]
345    fn test_parse_instrument_name_with_accents() {
346        let info = parse_instrument_name("Japón 225 18500 CALL");
347        assert_eq!(info.asset_name, "Japan 225");
348        assert_eq!(info.strike, Some("18500".to_string()));
349        assert_eq!(info.option_type, Some("CALL".to_string()));
350    }
351}