Skip to main content

data_preprocess/
models.rs

1use chrono::NaiveDateTime;
2use serde::{Deserialize, Serialize};
3
4use crate::error::{DataError, Result};
5
6/// Query parameters for tick view/query commands.
7pub struct QueryOpts {
8    pub exchange: String,
9    pub symbol: String,
10    pub from: Option<NaiveDateTime>,
11    pub to: Option<NaiveDateTime>,
12    pub limit: usize,
13    pub tail: bool,
14    pub descending: bool,
15}
16
17/// Query parameters for bar view/query commands.
18pub struct BarQueryOpts {
19    pub exchange: String,
20    pub symbol: String,
21    pub timeframe: String,
22    pub from: Option<NaiveDateTime>,
23    pub to: Option<NaiveDateTime>,
24    pub limit: usize,
25    pub tail: bool,
26    pub descending: bool,
27}
28
29/// Supported bar timeframes.
30#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
31pub enum Timeframe {
32    M1,
33    M3,
34    M5,
35    M15,
36    M30,
37    H1,
38    H4,
39    D1,
40    W1,
41    MN1,
42}
43
44impl Timeframe {
45    /// Parse from CLI string. Accepts "1m","M1","3m","M3", etc.
46    pub fn parse(s: &str) -> Result<Self> {
47        match s.to_lowercase().as_str() {
48            "1m" | "m1" => Ok(Self::M1),
49            "3m" | "m3" => Ok(Self::M3),
50            "5m" | "m5" => Ok(Self::M5),
51            "15m" | "m15" => Ok(Self::M15),
52            "30m" | "m30" => Ok(Self::M30),
53            "1h" | "h1" => Ok(Self::H1),
54            "4h" | "h4" => Ok(Self::H4),
55            "1d" | "d1" => Ok(Self::D1),
56            "1w" | "w1" => Ok(Self::W1),
57            "1mn" | "mn1" | "1m0" | "mn" => Ok(Self::MN1),
58            _ => Err(DataError::InvalidTimeframe(s.to_string())),
59        }
60    }
61
62    /// Canonical short label for storage: "1m", "3m", "5m", ...
63    pub fn as_str(&self) -> &'static str {
64        match self {
65            Self::M1 => "1m",
66            Self::M3 => "3m",
67            Self::M5 => "5m",
68            Self::M15 => "15m",
69            Self::M30 => "30m",
70            Self::H1 => "1h",
71            Self::H4 => "4h",
72            Self::D1 => "1d",
73            Self::W1 => "1w",
74            Self::MN1 => "1M",
75        }
76    }
77}
78
79impl std::fmt::Display for Timeframe {
80    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81        f.write_str(self.as_str())
82    }
83}
84
85/// A single tick (bid/ask/last at a point in time).
86#[derive(Debug, Clone, Serialize, Deserialize)]
87pub struct Tick {
88    pub exchange: String,
89    pub symbol: String,
90    pub ts: NaiveDateTime,
91    pub bid: Option<f64>,
92    pub ask: Option<f64>,
93    pub last: Option<f64>,
94    pub volume: Option<f64>,
95    pub flags: Option<i32>,
96}
97
98/// A single OHLCV bar.
99#[derive(Debug, Clone, Serialize, Deserialize)]
100pub struct Bar {
101    pub exchange: String,
102    pub symbol: String,
103    pub timeframe: Timeframe,
104    pub ts: NaiveDateTime,
105    pub open: f64,
106    pub high: f64,
107    pub low: f64,
108    pub close: f64,
109    pub tick_vol: i64,
110    pub volume: i64,
111    pub spread: i32,
112}
113
114/// Summary row returned by stats queries.
115#[derive(Debug)]
116pub struct StatRow {
117    pub exchange: String,
118    pub symbol: String,
119    pub data_type: String,
120    pub count: u64,
121    pub ts_min: NaiveDateTime,
122    pub ts_max: NaiveDateTime,
123}
124
125/// Result of an import operation.
126#[derive(Debug)]
127pub struct ImportResult {
128    pub file: String,
129    pub exchange: String,
130    pub symbol: String,
131    pub rows_parsed: usize,
132    pub rows_inserted: usize,
133    pub rows_skipped: usize,
134    pub elapsed: std::time::Duration,
135}