finalytics 0.9.0

A rust library for financial data analysis
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
use crate::analytics::performance::TickerPerformanceStats;
use crate::data::yahoo::config::TickerSummaryStats;
use crate::prelude::{
    Column as OhlcvColumn, StatementFrequency, StatementType, TickerData, TickerPerformance,
    Tickers,
};
use futures::future::join_all;
use indicatif::{ProgressBar, ProgressStyle};
use polars::prelude::*;
use std::error::Error;

macro_rules! fetch_all {
    ($tickers:expr, $method:ident, $idx:expr $(, $param:expr)*) => {{
        let mut futures = Vec::new();
        let tickers = $tickers.clone();
        let total_tickers = tickers.len();
        let pb = ProgressBar::new(total_tickers as u64);
        pb.set_style(
            ProgressStyle::default_bar()
                .template("{msg} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} ({eta})")?
                .progress_chars("#>-"),
        );

        for ticker in tickers.into_iter() {
            let ticker = ticker.clone();
            let fut = tokio::task::spawn(async move {
                let result = ticker.$method($($param), *).await;
                match result {
                    Ok(mut df) => {
                        let symbol_series = Series::new("symbol".into(), vec![ticker.ticker.clone(); df.height()]);
                        if df.width() > $idx {
                            let _ = df.insert_column($idx, symbol_series);
                            Ok(df)
                        } else {
                            eprintln!("No Data for {}", &ticker.ticker);
                            Err(format!("No Data for {}", &ticker.ticker))
                        }
                    }
                    Err(e) => {
                        eprintln!("Error Fetching Data for {}: {}", &ticker.ticker, e);
                        Err(format!("Error Fetching Data for {}: {}", &ticker.ticker, e))
                    }
                }
            });

            futures.push(fut);
        }

        let results = join_all(futures).await;
        let mut joint_df = DataFrame::default();

        for result in results {
            match result {
                Ok(Ok(df)) => {
                    match joint_df.vstack(&df) {
                        Ok(jdf) => joint_df = jdf,
                        Err(e) => eprintln!("Unable to stack {:?}: {}", &df, e),
                    }
                }
                Ok(Err(_)) => continue,
                Err(e) => eprintln!("Error in task: {}", e),
            }
        }

        pb.finish_with_message(format!("Done"));
        Ok(joint_df)
    }};
}

pub trait TickersData {
    fn get_chart(&self) -> impl std::future::Future<Output = Result<DataFrame, Box<dyn Error>>>;
    fn get_news(&self) -> impl std::future::Future<Output = Result<DataFrame, Box<dyn Error>>>;
    fn get_financials(
        &self,
        statement_type: StatementType,
        frequency: StatementFrequency,
        formatted: Option<bool>,
    ) -> impl std::future::Future<Output = Result<DataFrame, Box<dyn Error>>>;
    fn get_ticker_stats(
        &self,
    ) -> impl std::future::Future<Output = Result<DataFrame, Box<dyn Error>>>;
    fn get_options(&self) -> impl std::future::Future<Output = Result<DataFrame, Box<dyn Error>>>;
    fn returns(&self) -> impl std::future::Future<Output = Result<DataFrame, Box<dyn Error>>>;
    fn performance_stats(
        &self,
    ) -> impl std::future::Future<Output = Result<DataFrame, Box<dyn Error>>>;
}

impl TickersData for Tickers {
    /// Fetch the OHLCV Data for all tickers in the Tickers Struct
    async fn get_chart(&self) -> Result<DataFrame, Box<dyn Error>> {
        fetch_all!(self.tickers.clone(), get_chart, 1)
    }

    /// Fetch the Historical News Headlines for all tickers in the Tickers Struct
    async fn get_news(&self) -> Result<DataFrame, Box<dyn Error>> {
        fetch_all!(self.tickers.clone(), get_news, 1)
    }

    /// Fetch the Financials for all tickers in the Tickers Struct
    async fn get_financials(
        &self,
        statement_type: StatementType,
        frequency: StatementFrequency,
        formatted: Option<bool>,
    ) -> Result<DataFrame, Box<dyn Error>> {
        fetch_all!(
            self.tickers.clone(),
            get_financials,
            1,
            statement_type,
            frequency,
            formatted
        )
    }

    /// Fetch the Ticker Summary Stats Data for all tickers in the Tickers' Struct
    async fn get_ticker_stats(&self) -> Result<DataFrame, Box<dyn Error>> {
        let mut futures = Vec::new();
        let total_tickers = self.tickers.len();
        let pb = ProgressBar::new(total_tickers as u64);
        pb.set_style(
            ProgressStyle::default_bar()
                .template("{msg} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} ({eta})")?
                .progress_chars("#>-"),
        );

        for ticker in self.tickers.clone().into_iter() {
            let fut = tokio::task::spawn(async move {
                match ticker.get_ticker_stats().await {
                    Ok(stats) => Ok((ticker.ticker.clone(), stats)),
                    Err(e) => {
                        eprintln!("Error Fetching Ticker Stats for {}: {}", &ticker.ticker, e);
                        Err(format!(
                            "Error Fetching Ticker Stats for {}: {}",
                            &ticker.ticker, e
                        ))
                    }
                }
            });

            futures.push(fut);
        }

        let results = join_all(futures).await;
        let mut all_stats: Vec<(String, TickerSummaryStats)> = Vec::new();

        for result in results {
            match result {
                Ok(Ok(stats)) => {
                    all_stats.push(stats);
                }
                Ok(Err(_)) => continue,
                Err(e) => eprintln!("Error in task: {e}"),
            }
        }

        let mut formatted_dfs = Vec::new();

        for (ticker, stats) in all_stats {
            let mut fmt_df = stats.to_dataframe()?;
            fmt_df.rename("Value", ticker.as_str().into())?;
            formatted_dfs.push(fmt_df);
        }

        let combine = |dfs: Vec<DataFrame>| -> Result<DataFrame, Box<dyn Error>> {
            let mut combined = dfs.first().ok_or("No data")?.clone();
            for df in dfs.iter().skip(1) {
                combined = combined.left_join(df, ["Metric"], ["Metric"])?;
            }
            Ok(combined)
        };

        let mut stats = combine(formatted_dfs)?;
        let columns = stats
            .column("Metric")?
            .str()?
            .into_no_null_iter()
            .map(|x| x.to_string())
            .collect::<Vec<String>>();
        stats = stats.drop("Metric")?;
        let column_names = stats
            .get_column_names()
            .iter()
            .map(|&name| name.to_string())
            .collect::<Vec<String>>();
        let symbols = Series::new("Symbol".into(), column_names);
        let mut stats_df = stats.transpose(None, None)?;
        stats_df.set_column_names(&columns)?;
        let _ = stats_df.insert_column(0, symbols)?;

        // Drop columns where all values are empty strings
        let columns_to_drop: Vec<String> = stats_df
            .get_column_names()
            .iter()
            .filter(|&&name| name != "Symbol")
            .filter_map(|&col_name| {
                let col = stats_df.column(col_name).ok()?;
                let utf8 = col.str().ok()?;
                if utf8.into_no_null_iter().all(|val| val.trim().is_empty()) {
                    Some(col_name.to_string())
                } else {
                    None
                }
            })
            .collect();

        for col_name in columns_to_drop {
            stats_df = stats_df.drop(&col_name)?;
        }

        pb.finish_with_message("Done");

        Ok(stats_df)
    }

    /// Fetch the Options Chain Data for all tickers in the Tickers Struct
    async fn get_options(&self) -> Result<DataFrame, Box<dyn Error>> {
        let mut futures = Vec::new();
        let total_tickers = self.tickers.len();
        let pb = ProgressBar::new(total_tickers as u64);
        pb.set_style(
            ProgressStyle::default_bar()
                .template("{msg} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} ({eta})")?
                .progress_chars("#>-"),
        );

        for ticker in self.tickers.clone().into_iter() {
            let fut = tokio::task::spawn(async move {
                match ticker.get_options().await {
                    Ok(options) => {
                        let mut df = options.chain;
                        let symbol_series =
                            Series::new("symbol".into(), vec![ticker.ticker.clone(); df.height()]);
                        if df.width() > 3 {
                            let _ = df.insert_column(3, symbol_series);
                            Ok(df)
                        } else {
                            eprintln!("No Options Data for {}", &ticker.ticker);
                            Err(format!("No Options Data for {}", &ticker.ticker))
                        }
                    }
                    Err(e) => {
                        eprintln!("Error Fetching Options Data for {}: {}", &ticker.ticker, e);
                        Err(format!(
                            "Error Fetching Options Data for {}: {}",
                            &ticker.ticker, e
                        ))
                    }
                }
            });

            futures.push(fut);
        }

        let results = join_all(futures).await;
        let mut joint_df = DataFrame::default();

        for result in results {
            match result {
                Ok(Ok(df)) => {
                    joint_df = joint_df.vstack(&df)?;
                }
                Ok(Err(_)) => continue,
                Err(e) => eprintln!("Error in task: {e}"),
            }
        }

        pb.finish_with_message("Done");

        Ok(joint_df)
    }

    /// Compute the Returns for all tickers in the Tickers Struct.
    ///
    /// Aligns all assets at the price level (inner join on timestamp) and then
    /// computes percentage returns from the aligned prices. This ensures every
    /// asset has a return for every common trading date without fabricating data.
    async fn returns(&self) -> Result<DataFrame, Box<dyn Error>> {
        let mut futures = Vec::new();
        let total_tickers = self.tickers.len();
        let pb = ProgressBar::new(total_tickers as u64);
        pb.set_style(
            ProgressStyle::default_bar()
                .template("{msg} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} ({eta})")?
                .progress_chars("#>-"),
        );

        // Fetch prices for each ticker concurrently
        for ticker in self.tickers.clone().into_iter() {
            let fut = tokio::task::spawn(async move {
                match ticker.get_chart().await {
                    Ok(chart) => {
                        let df = DataFrame::new(vec![
                            chart.column("timestamp").unwrap().clone(),
                            chart
                                .column(OhlcvColumn::AdjClose.as_str())
                                .unwrap()
                                .clone()
                                .with_name(ticker.ticker.as_str().into()),
                        ]);
                        match df {
                            Ok(df) => Ok((ticker.ticker.clone(), df)),
                            Err(_) => {
                                eprintln!("No Price Data for {}", &ticker.ticker);
                                Err(format!("No Price Data for {}", &ticker.ticker))
                            }
                        }
                    }
                    Err(e) => {
                        eprintln!("No Price Data for {}: {}", &ticker.ticker, e);
                        Err(format!("No Price Data for {}: {}", &ticker.ticker, e))
                    }
                }
            });

            futures.push(fut);
        }

        let results = join_all(futures).await;
        let mut price_frames: Vec<(String, DataFrame)> = Vec::new();

        for result in results {
            match result {
                Ok(Ok(item)) => price_frames.push(item),
                Ok(Err(_)) => continue,
                Err(e) => eprintln!("Error in task: {e}"),
            }
        }

        pb.finish_with_message("Done");

        if price_frames.is_empty() {
            return Err("No price data available for any ticker".into());
        }

        // Full outer join all price frames on timestamp (keep all dates)
        let mut joined = price_frames
            .into_iter()
            .map(|(_, df)| df)
            .reduce(|acc, df| {
                acc.join(
                    &df,
                    ["timestamp"],
                    ["timestamp"],
                    JoinArgs::new(JoinType::Full).with_coalesce(JoinCoalesce::CoalesceColumns),
                    None,
                )
                .map_err(|e| format!("Failed to join price frames: {e}"))
                .unwrap_or(acc)
            })
            .ok_or("No price data to join")?;

        // Sort chronologically, then forward-fill and backward-fill prices
        joined = joined.sort(
            ["timestamp"],
            SortMultipleOptions::new().with_order_descending(false),
        )?;
        joined = joined.fill_null(FillNullStrategy::Forward(None))?;
        joined = joined.fill_null(FillNullStrategy::Backward(None))?;

        // Compute returns from aligned prices
        let ticker_symbols: Vec<String> = self.tickers.iter().map(|t| t.ticker.clone()).collect();
        let mut returns_cols: Vec<polars::prelude::Column> = Vec::new();

        // Build date strings (skip the first — no return for it)
        let dates = joined
            .column("timestamp")?
            .datetime()?
            .into_no_null_iter()
            .map(|x| {
                chrono::DateTime::from_timestamp_millis(x)
                    .unwrap_or_default()
                    .naive_local()
                    .to_string()
            })
            .collect::<Vec<String>>();
        let dates = &dates[1..];
        returns_cols.push(polars::prelude::Column::new("timestamp".into(), dates));

        for sym in &ticker_symbols {
            let prices = joined.column(sym)?.f64()?.to_vec();
            let rets: Vec<f64> = prices
                .windows(2)
                .map(|w| match (w[0], w[1]) {
                    (Some(prev), Some(curr)) if prev.abs() > 1e-12 => (curr - prev) / prev,
                    _ => 0.0,
                })
                .collect();
            returns_cols.push(polars::prelude::Column::new(sym.as_str().into(), &rets));
        }

        let joint_df = DataFrame::new(returns_cols)?;

        Ok(joint_df)
    }

    /// Fetch the performance statistics for all tickers in the Tickers Struct
    async fn performance_stats(&self) -> Result<DataFrame, Box<dyn Error>> {
        let mut futures = Vec::new();
        let total_tickers = self.tickers.len();
        let pb = ProgressBar::new(total_tickers as u64);
        pb.set_style(
            ProgressStyle::default_bar()
                .template("{msg} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} ({eta})")?
                .progress_chars("#>-"),
        );

        for ticker in self.tickers.clone().into_iter() {
            let fut = tokio::task::spawn(async move {
                match ticker.performance_stats().await {
                    Ok(stats) => Ok(stats),
                    Err(e) => {
                        eprintln!("No Returns Data for {}: {}", &ticker.ticker, e);
                        Err(format!("No Returns Data for {}: {}", &ticker.ticker, e))
                    }
                }
            });

            futures.push(fut);
        }

        let results = join_all(futures).await;
        let mut all_stats: Vec<TickerPerformanceStats> = Vec::new();

        for result in results {
            match result {
                Ok(Ok(stats)) => {
                    all_stats.push(stats);
                }
                Ok(Err(_)) => continue,
                Err(e) => eprintln!("Error in task: {e}"),
            }
        }

        let mut ticker_symbols: Vec<String> = vec![];
        let mut numeric_fields: Vec<Vec<f64>> = vec![vec![]; 16];

        for stat in &all_stats {
            ticker_symbols.push(stat.ticker_symbol.clone());
            numeric_fields[0].push(stat.performance_stats.daily_return);
            numeric_fields[1].push(stat.performance_stats.daily_volatility);
            numeric_fields[2].push(stat.performance_stats.cumulative_return);
            numeric_fields[3].push(stat.performance_stats.annualized_return);
            numeric_fields[4].push(stat.performance_stats.annualized_volatility);
            numeric_fields[5].push(stat.performance_stats.alpha.unwrap_or(f64::NAN));
            numeric_fields[6].push(stat.performance_stats.beta.unwrap_or(f64::NAN));
            numeric_fields[7].push(stat.performance_stats.sharpe_ratio);
            numeric_fields[8].push(stat.performance_stats.sortino_ratio);
            numeric_fields[9].push(stat.performance_stats.active_return.unwrap_or(f64::NAN));
            numeric_fields[10].push(stat.performance_stats.active_risk.unwrap_or(f64::NAN));
            numeric_fields[11].push(stat.performance_stats.information_ratio.unwrap_or(f64::NAN));
            numeric_fields[12].push(stat.performance_stats.calmar_ratio);
            numeric_fields[13].push(stat.performance_stats.maximum_drawdown);
            numeric_fields[14].push(stat.performance_stats.value_at_risk);
            numeric_fields[15].push(stat.performance_stats.expected_shortfall);
        }

        let df = DataFrame::new(vec![
            Column::new("Symbol".into(), ticker_symbols),
            Column::new(
                "Daily Return".into(),
                numeric_fields[0]
                    .iter()
                    .map(|x| x.to_string())
                    .collect::<Vec<String>>(),
            ),
            Column::new(
                "Daily Volatility".into(),
                numeric_fields[1]
                    .iter()
                    .map(|x| x.to_string())
                    .collect::<Vec<String>>(),
            ),
            Column::new(
                "Cumulative Return".into(),
                numeric_fields[2]
                    .iter()
                    .map(|x| x.to_string())
                    .collect::<Vec<String>>(),
            ),
            Column::new(
                "Annualized Return".into(),
                numeric_fields[3]
                    .iter()
                    .map(|x| x.to_string())
                    .collect::<Vec<String>>(),
            ),
            Column::new(
                "Annualized Volatility".into(),
                numeric_fields[4]
                    .iter()
                    .map(|x| x.to_string())
                    .collect::<Vec<String>>(),
            ),
            Column::new(
                "Alpha".into(),
                numeric_fields[5]
                    .iter()
                    .map(|x| x.to_string())
                    .collect::<Vec<String>>(),
            ),
            Column::new(
                "Beta".into(),
                numeric_fields[6]
                    .iter()
                    .map(|x| x.to_string())
                    .collect::<Vec<String>>(),
            ),
            Column::new(
                "Sharpe Ratio".into(),
                numeric_fields[7]
                    .iter()
                    .map(|x| x.to_string())
                    .collect::<Vec<String>>(),
            ),
            Column::new(
                "Sortino Ratio".into(),
                numeric_fields[8]
                    .iter()
                    .map(|x| x.to_string())
                    .collect::<Vec<String>>(),
            ),
            Column::new(
                "Active Return".into(),
                numeric_fields[9]
                    .iter()
                    .map(|x| x.to_string())
                    .collect::<Vec<String>>(),
            ),
            Column::new(
                "Active Risk".into(),
                numeric_fields[10]
                    .iter()
                    .map(|x| x.to_string())
                    .collect::<Vec<String>>(),
            ),
            Column::new(
                "Information Ratio".into(),
                numeric_fields[11]
                    .iter()
                    .map(|x| x.to_string())
                    .collect::<Vec<String>>(),
            ),
            Column::new(
                "Calmar Ratio".into(),
                numeric_fields[12]
                    .iter()
                    .map(|x| x.to_string())
                    .collect::<Vec<String>>(),
            ),
            Column::new(
                "Maximum Drawdown".into(),
                numeric_fields[13]
                    .iter()
                    .map(|x| x.to_string())
                    .collect::<Vec<String>>(),
            ),
            Column::new(
                "Value at Risk".into(),
                numeric_fields[14]
                    .iter()
                    .map(|x| x.to_string())
                    .collect::<Vec<String>>(),
            ),
            Column::new(
                "Expected Shortfall".into(),
                numeric_fields[15]
                    .iter()
                    .map(|x| x.to_string())
                    .collect::<Vec<String>>(),
            ),
        ])?;

        pb.finish_with_message("Done");

        Ok(df)
    }
}