chapaty 1.0.0

High-performance backtesting and financial simulation framework for trading strategies and reinforcement learning agents. Async-first, Gym-like API in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
use std::{fs, path::Path};

use polars::{
    frame::DataFrame,
    io::cloud::CloudOptions,
    prelude::{
        CsvWriterOptions, IntoLazy, LazyFrame, ParquetWriteOptions, PlPath, SchemaRef, SinkOptions,
        SinkTarget,
    },
};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use strum::{Display, EnumIter, EnumString, IntoStaticStr};
use tonic::async_trait;

use crate::{
    error::{ChapatyError, ChapatyResult, DataError, IoError, SystemError},
    report::polars_ext::{DataFrameExt, LazyFrameExt},
};

// ================================================================================================
// Traits
// ================================================================================================

/// Defines a common interface for all Report types (Journal, TradeStats, etc.).
pub trait Report {
    /// Access the underlying DataFrame (Immutable).
    fn as_df(&self) -> &DataFrame;

    /// Access the underlying DataFrame (Mutable).
    fn as_df_mut(&mut self) -> &mut DataFrame;
}

pub trait ReportName {
    fn base_name(&self) -> String;

    fn filename(&self, ext: FileExtension) -> String {
        format!("{}.{}", self.base_name(), ext)
    }
}

pub trait ToSchema {
    /// Returns the canonical schema for this report type.
    fn to_schema() -> SchemaRef;
}

pub trait AsFormattedLazyFrame {
    fn as_formatted_lf(&self) -> LazyFrame;
}

pub trait ToJson {
    /// Serializes the report to a generic JSON Value.
    /// Returns a `Value::Array` containing row objects.
    fn to_json(&self) -> ChapatyResult<serde_json::Value>;
}

pub trait ToCsv {
    /// Writes the report to a CSV file in the target directory.
    ///
    /// # Formatting
    /// - Applies human-readable formatting to Duration columns (e.g. "2d 1h").
    /// - Uses the canonical schema defined in `ToSchema`.
    ///
    /// # Arguments
    /// - `dir`: Target directory. Created if it doesn't exist.
    /// - `opts`: CSV writing options (delimiter, headers, etc.).
    ///
    /// # Side Effects
    /// - Creates the directory if missing.
    /// - Overwrites the file if it exists.
    fn to_csv(
        &self,
        dir: impl AsRef<Path>,
        opts: Option<&CsvWriterOptions>,
        sink_opts: Option<&SinkOptions>,
    ) -> ChapatyResult<()>;
}

pub trait ToParquet {
    fn to_parquet(
        &self,
        dir: impl AsRef<Path>,
        opts: Option<&ParquetWriteOptions>,
        sink_opts: Option<&SinkOptions>,
    ) -> ChapatyResult<()>;
}

#[async_trait]
pub trait ToCloudCsv {
    /// Streams the report to the specified Cloud URI as a CSV.
    ///
    /// # Performance
    /// - Uses Polars' **Streaming Engine**: Data is formatted and uploaded in chunks.
    /// - Memory Efficient: Does not materialize the full dataset in RAM.
    /// - Non-Blocking: Offloads the entire execution graph to a blocking thread.
    ///
    /// # Arguments
    /// * `uri` - The full bucket URI (e.g., `gs://bucket/path.csv`).
    /// * `opts` - CSV formatting options.
    /// * `cloud_opts` - Credentials/Region config.
    /// * `sink_opts` - Sink config.
    async fn stream_csv(
        &self,
        uri: &str,
        opts: Option<&CsvWriterOptions>,
        cloud_opts: Option<&CloudOptions>,
        sink_opts: Option<&SinkOptions>,
    ) -> ChapatyResult<()>;
}

#[async_trait]
pub trait ToCloudParquet {
    async fn stream_parquet(
        &self,
        uri: &str,
        opts: Option<&ParquetWriteOptions>,
        cloud_opts: Option<&CloudOptions>,
        sink_opts: Option<&SinkOptions>,
    ) -> ChapatyResult<()>;
}

// ================================================================================================
// Blanket Implementations
// ================================================================================================

impl<T> AsFormattedLazyFrame for T
where
    T: Report + ToSchema,
{
    fn as_formatted_lf(&self) -> LazyFrame {
        self.as_df()
            .clone()
            .lazy()
            .with_human_durations(T::to_schema())
    }
}

impl<T> ToJson for T
where
    T: Report + ToSchema,
{
    fn to_json(&self) -> ChapatyResult<serde_json::Value> {
        let rows = self
            .as_formatted_lf()
            .collect()
            .map_err(|e| ChapatyError::Data(DataError::DataFrame(e.to_string())))?
            .to_json_rows()?;
        Ok(Value::Array(rows.into_iter().map(Value::Object).collect()))
    }
}

impl<T> ToCsv for T
where
    T: Report + ReportName + ToSchema,
{
    fn to_csv(
        &self,
        dir: impl AsRef<Path>,
        opts: Option<&CsvWriterOptions>,
        sink_opts: Option<&SinkOptions>,
    ) -> ChapatyResult<()> {
        let dir = dir.as_ref();
        let file_path = dir.join(self.filename(FileExtension::Csv));

        if !dir.exists() {
            fs::create_dir_all(dir).map_err(|e| {
                IoError::FileSystem(format!(
                    "Failed to create directory {}: {}",
                    dir.display(),
                    e
                ))
            })?;
        }

        let uri = file_path.to_str().ok_or_else(|| {
            IoError::FileSystem(format!(
                "Path contains invalid UTF-8 characters: {}",
                file_path.display()
            ))
        })?;
        let target = SinkTarget::Path(PlPath::new(uri));
        let options = opts.cloned().unwrap_or_default();
        let sink_opts = sink_opts.cloned().unwrap_or_default();

        let lf = self.as_formatted_lf();

        let sink_plan = lf
            .sink_csv(target, options, None, sink_opts)
            .map_err(|e| DataError::DataFrame(format!("Failed to build CSV sink plan: {e}")))?;

        let _ = sink_plan.collect().map_err(|e| {
            DataError::DataFrame(format!(
                "Failed to write CSV to '{}': {e}",
                file_path.display()
            ))
        })?;

        Ok(())
    }
}

#[async_trait]
impl<T> ToCloudCsv for T
where
    T: Report + ToSchema + Sync + Send,
{
    async fn stream_csv(
        &self,
        uri: &str,
        opts: Option<&CsvWriterOptions>,
        cloud_opts: Option<&CloudOptions>,
        sink_opts: Option<&SinkOptions>,
    ) -> ChapatyResult<()> {
        let lf = self.as_formatted_lf();
        let target = SinkTarget::Path(PlPath::new(uri));
        let options = opts.cloned().unwrap_or_default();
        let cloud_options = cloud_opts.cloned();
        let sink_opts = sink_opts.cloned().unwrap_or_default();

        tokio::task::spawn_blocking(move || {
            let sink_plan = lf
                .sink_csv(target, options, cloud_options, sink_opts)
                .map_err(|e| DataError::DataFrame(format!("Failed to build sink plan: {e}")))?;

            let _ = sink_plan
                .collect()
                .map_err(|e| DataError::DataFrame(format!("Streaming CSV upload failed: {e}")))?;

            Ok(())
        })
        .await
        .map_err(|e| SystemError::Generic(format!("Task Join Error: {e}")))?
    }
}

impl<T> ToParquet for T
where
    T: Report + ReportName + ToSchema,
{
    fn to_parquet(
        &self,
        dir: impl AsRef<Path>,
        opts: Option<&ParquetWriteOptions>,
        sink_opts: Option<&SinkOptions>,
    ) -> ChapatyResult<()> {
        let dir = dir.as_ref();
        let file_path = dir.join(self.filename(FileExtension::Parquet));

        if !dir.exists() {
            fs::create_dir_all(dir).map_err(|e| {
                IoError::FileSystem(format!(
                    "Failed to create directory {}: {}",
                    dir.display(),
                    e
                ))
            })?;
        }

        let uri = file_path.to_str().ok_or_else(|| {
            IoError::FileSystem(format!(
                "Path contains invalid UTF-8 characters: {}",
                file_path.display()
            ))
        })?;
        let target = SinkTarget::Path(PlPath::new(uri));
        let options = opts.cloned().unwrap_or_default();
        let sink_opts = sink_opts.cloned().unwrap_or_default();

        let lf = self.as_df().clone().lazy();

        let sink_plan = lf
            .sink_parquet(target, options, None, sink_opts)
            .map_err(|e| DataError::DataFrame(format!("Failed to build Parquet sink plan: {e}")))?;

        let _ = sink_plan.collect().map_err(|e| {
            DataError::DataFrame(format!(
                "Failed to write Parquet to '{}': {e}",
                file_path.display()
            ))
        })?;

        Ok(())
    }
}

#[async_trait]
impl<T> ToCloudParquet for T
where
    T: Report + ToSchema + Sync + Send,
{
    async fn stream_parquet(
        &self,
        uri: &str,
        opts: Option<&ParquetWriteOptions>,
        cloud_opts: Option<&CloudOptions>,
        sink_opts: Option<&SinkOptions>,
    ) -> ChapatyResult<()> {
        let df = self.as_df().clone();
        let target = SinkTarget::Path(PlPath::new(uri));
        let options = opts.cloned().unwrap_or_default();
        let cloud_options = cloud_opts.cloned();
        let sink_opts = sink_opts.cloned().unwrap_or_default();

        tokio::task::spawn_blocking(move || {
            let lf = df.lazy();

            let sink_plan = lf
                .with_new_streaming(true)
                .sink_parquet(target, options, cloud_options, sink_opts)
                .map_err(|e| {
                    DataError::DataFrame(format!("Failed to build Parquet sink plan: {e}"))
                })?;

            let _ = sink_plan.collect().map_err(|e| {
                DataError::DataFrame(format!("Streaming Parquet upload failed: {e}"))
            })?;

            Ok(())
        })
        .await
        .map_err(|e| SystemError::Generic(format!("Task Join Error: {e}")))?
    }
}

/// Generates a base name dynamically based on the presence of grouping columns.
///
/// # Logic
/// 1. Scans the DataFrame column names.
/// 2. Filters for columns starting with `__` (the `GroupCol` prefix).
/// 3. Strips the prefix to get clean names (e.g., `__symbol` -> `symbol`).
/// 4. Joins them to form a prefix for the file.
///
/// # Example
/// - No groups: `cumulative_returns`
/// - Grouped by Symbol: `symbol_cumulative_returns`
/// - Grouped by Symbol & Year: `symbol_entry_year_cumulative_returns`
pub(crate) fn generate_dynamic_base_name(df: &DataFrame, base_name: &str) -> String {
    let group_keys = df
        .get_column_names()
        .iter()
        .filter_map(|name| {
            if name.starts_with("__") {
                Some(name.strip_prefix("__").unwrap_or(name))
            } else {
                None
            }
        })
        .collect::<Vec<_>>();

    if group_keys.is_empty() {
        base_name.to_string()
    } else {
        let prefix = group_keys.join("_");
        format!("{}_{}", prefix, base_name)
    }
}

#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    Serialize,
    Deserialize,
    EnumString,
    Display,
    EnumIter,
    IntoStaticStr,
)]
#[strum(serialize_all = "lowercase")]
pub enum FileExtension {
    Csv,
    Parquet,
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use polars::{
        df,
        prelude::{LazyCsvReader, LazyFileListReader},
    };

    use crate::{data::common::RiskMetricsConfig, report::journal::Journal};

    use super::*;

    #[test]
    fn test_generate_dynamic_base_name() {
        // 1. Case: No Groups (Plain)
        let df_plain = df![
            "pnl" => &[100.0],
            "count" => &[5]
        ]
        .expect("Failed to create plain DF");

        let name_plain = generate_dynamic_base_name(&df_plain, "stats");
        assert_eq!(name_plain, "stats");

        // 2. Case: Single Group (Symbol)
        // We simulate the "__" prefix that your GroupedJournal logic adds
        let df_symbol = df![
            "__symbol" => &["BTC"],
            "pnl" => &[100.0]
        ]
        .expect("Failed to create symbol DF");

        let name_symbol = generate_dynamic_base_name(&df_symbol, "stats");
        assert_eq!(name_symbol, "symbol_stats");

        // 3. Case: Multi Group (Symbol + Year)
        // Order matters in the output name, driven by column order in DF
        let df_multi = df![
            "__symbol" => &["BTC"],
            "__entry_year" => &[2023],
            "pnl" => &[100.0]
        ]
        .expect("Failed to create multi DF");

        let name_multi = generate_dynamic_base_name(&df_multi, "stats");
        assert_eq!(name_multi, "symbol_entry_year_stats");
    }

    #[test]
    fn test_to_json_rows() {
        let manifest_dir = env!("CARGO_MANIFEST_DIR");
        let pb = PathBuf::from(manifest_dir).join("tests/fixtures/report/input/journal.csv");
        let path = PlPath::new(
            pb.as_os_str()
                .to_str()
                .expect("failed to convert input file path to string"),
        );

        let schema = Journal::to_schema();
        let df = LazyCsvReader::new(path)
            .with_has_header(true)
            .with_schema(Some(schema.clone()))
            .with_try_parse_dates(true)
            .finish()
            .expect("failed to create LazyFrame from CSV")
            .collect()
            .expect("failed to collect DataFrame from LazyFrame");

        let journal = Journal::new(df, RiskMetricsConfig::default())
            .expect("failed to create Journal from DataFrame");

        let have = journal
            .to_json()
            .expect("failed to serialize journal to JSON");

        let expected_path =
            PathBuf::from(manifest_dir).join("tests/fixtures/report/expected/journal.json");
        let want =
            std::fs::read_to_string(&expected_path).expect("failed to read expected JSON file");
        let want_value: serde_json::Value =
            serde_json::from_str(&want).expect("failed to parse expected JSON file");

        assert_eq!(have, want_value, "JSON output does not match expected");
    }
}