alator 0.4.1

Library for backtesting investment strategies
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
//! Generates performance stats for backtest

use crate::types::StrategySnapshot;
use itertools::Itertools;
use rotala::clock::Frequency;

/// Output for single backtest run.
#[derive(Clone, Debug)]
pub struct BacktestOutput {
    pub ret: f64,
    pub cagr: f64,
    pub vol: f64,
    pub mdd: f64,
    pub sharpe: f64,
    pub values: Vec<f64>,
    pub returns: Vec<f64>,
    pub dates: Vec<i64>,
    pub cash_flows: Vec<f64>,
    pub first_date: i64,
    pub last_date: i64,
    pub dd_start_date: i64,
    pub dd_end_date: i64,
    pub best_return: f64,
    pub worst_return: f64,
    pub frequency: String,
}

/// Group of functions common to portfolio performance calculations.
struct CalculationAlgos;

impl CalculationAlgos {
    /// Returns a tuple containing (max drawdown, position of drawdown start, end position)
    fn maxdd(values: &[f64]) -> (f64, usize, usize) {
        let mut maxdd = 0.0;
        let mut peak = 0.0;
        let mut peak_pos: usize = 0;
        let mut trough = 0.0;
        let mut trough_pos: usize = 0;
        let mut t2;
        for (pos, t1) in values.iter().enumerate() {
            if t1 > &peak {
                peak = *t1;
                peak_pos = pos;
                trough = peak;
                trough_pos = peak_pos;
            } else if t1 < &trough {
                trough = *t1;
                trough_pos = pos;
                t2 = (trough / peak) - 1.0;
                if t2 < maxdd {
                    maxdd = t2
                }
            }
        }
        (maxdd, peak_pos, trough_pos)
    }

    fn var(values: &[f64]) -> f64 {
        let count = values.len();
        let mean: f64 = values.iter().sum::<f64>() / (count as f64);
        let squared_diffs: Vec<f64> = values
            .iter()
            .map(|ret| ret - mean)
            .map(|diff| diff.powf(2.0))
            .collect_vec();
        let sum_of_diff = squared_diffs.iter().sum::<f64>();
        sum_of_diff / (count as f64)
    }

    fn vol(values: &[f64]) -> f64 {
        //Accepts returns not raw portfolio values
        CalculationAlgos::var(values).sqrt()
    }
}

pub struct PortfolioCalculations;

impl PortfolioCalculations {
    //This calculation returns the annualized return from the cumulative return given the number of
    //daily/monthly/yearly periods.
    //Most calculations of annualized returns start from a per-period, as opposed to cumulative
    //return, so the calculations will be more simple. The reason why we need to make additional
    //calculations with the exponent is because we need to convert from the cumulative return.
    fn annualize_returns(ret: f64, periods: i32, frequency: &Frequency) -> f64 {
        match frequency {
            Frequency::Daily => ((1_f64 + ret).powf(365_f64 / periods as f64)) - 1_f64,
            Frequency::Second => panic!("No performance stats by second"),
            Frequency::Fixed => panic!("No performance stats by fixed"),
        }
    }

    fn annualize_volatility(vol: f64, frequency: &Frequency) -> f64 {
        match frequency {
            Frequency::Daily => (vol) * (252_f64).sqrt(),
            Frequency::Second => panic!("No performance stats by second"),
            Frequency::Fixed => panic!("No performance stats by fixed"),
        }
    }

    fn get_vol(rets: &[f64], freq: &Frequency) -> f64 {
        let vol = CalculationAlgos::vol(rets);
        PortfolioCalculations::annualize_volatility(vol, freq)
    }

    fn get_sharpe(rets: &[f64], log_rets: &[f64], days: i32, freq: &Frequency) -> f64 {
        let vol = PortfolioCalculations::get_vol(rets, freq);
        let ret = PortfolioCalculations::get_cagr(log_rets, days, freq);
        if vol == 0.0 {
            if ret != 0.0 {
                return ret;
            } else {
                return 0.0;
            }
        }
        ret / vol
    }

    fn get_maxdd(rets: &[f64]) -> (f64, usize, usize) {
        //Adds N to the runtime, can run faster but it isn't worth the time atm
        let mut values_with_cashflows = vec![100_000.0];
        for i in rets {
            //Because we add one value on creation, we can always unwrap safely
            let last_value = values_with_cashflows.last().unwrap();
            let new_value = last_value * (1.0 + i);
            values_with_cashflows.push(new_value);
        }
        CalculationAlgos::maxdd(&values_with_cashflows)
    }

    fn get_cagr(log_rets: &[f64], days: i32, freq: &Frequency) -> f64 {
        let ret = PortfolioCalculations::get_portfolio_return(log_rets);
        PortfolioCalculations::annualize_returns(ret, days, freq)
    }

    fn get_portfolio_return(log_rets: &[f64]) -> f64 {
        let sum_log_rets: f64 = log_rets.iter().sum();
        sum_log_rets.exp() - 1.0
    }

    fn get_returns(
        portfolio_values: &[f64],
        cash_flows: &[f64],
        inflation: &[f64],
        is_log: bool,
    ) -> Vec<f64> {
        let count = portfolio_values.len();
        let mut rets: Vec<f64> = Vec::new();

        if count.ne(&0) {
            for i in 1..count {
                let end = portfolio_values.get(i).unwrap();
                let start = portfolio_values.get(i - 1).unwrap();

                let cash_flow = cash_flows.get(i).unwrap();

                let gain = end - (start + *cash_flow);
                let capital = start + *cash_flow;

                let inflation_value = inflation.get(i).unwrap();

                let ret: f64 = if capital == 0.0 {
                    0.0
                } else {
                    ((1.0 + (gain / capital)) / (1.0 + *inflation_value)) - 1.0
                };

                if is_log {
                    let log_ret = (1.0 + ret).ln();
                    rets.push(log_ret);
                } else {
                    rets.push(ret);
                }
            }
        }
        rets
    }
}

/// Calculates performance statistics from [`Vec<StrategySnapshot>`].
///
/// Intended to be run after the simulation is completed.
#[derive(Debug, Clone)]
pub struct PerformanceCalculator;

impl PerformanceCalculator {
    pub fn calculate(freq: Frequency, states: Vec<StrategySnapshot>) -> BacktestOutput {
        //Cash flow on [StrategySnapshot] is the sum of cash flows to that date, so we need to
        //calculate the difference in cash flows at each stage.
        let mut cash_flows: Vec<f64> = Vec::new();
        let mut dates: Vec<i64> = Vec::new();
        let mut total_values: Vec<f64> = Vec::new();
        cash_flows.push(0.0);

        for i in 0..states.len() {
            dates.push(*states.get(i).unwrap().date);
            total_values.push(*states.get(i).unwrap().portfolio_value);
            if i != 0 {
                let last = *states.get(i - 1).unwrap().net_cash_flow.clone();
                let curr = *states.get(i).unwrap().net_cash_flow.clone();
                let diff = curr - last;
                cash_flows.push(diff)
            }
        }

        let inflation: Vec<f64> = states.iter().map(|v| v.inflation).collect();

        let returns =
            PortfolioCalculations::get_returns(&total_values, &cash_flows, &inflation, false);

        let log_returns =
            PortfolioCalculations::get_returns(&total_values, &cash_flows, &inflation, true);

        let (mdd, drawdown_start_pos, drawdown_end_pos) =
            PortfolioCalculations::get_maxdd(&returns);
        //This can error but shouldn't because we are querying into the same-length array
        let dd_start_date = dates[drawdown_start_pos];
        let dd_end_date = dates[drawdown_end_pos];

        let best_return = *returns
            .iter()
            .max_by(|x, y| x.partial_cmp(y).unwrap())
            .unwrap();
        let worst_return = *returns
            .iter()
            .min_by(|x, y| x.partial_cmp(y).unwrap())
            .unwrap();

        BacktestOutput {
            ret: PortfolioCalculations::get_portfolio_return(&log_returns),
            cagr: PortfolioCalculations::get_cagr(&log_returns, dates.len() as i32, &freq),
            vol: PortfolioCalculations::get_vol(&returns, &freq),
            mdd,
            sharpe: PortfolioCalculations::get_sharpe(
                &returns,
                &log_returns,
                dates.len() as i32,
                &freq,
            ),
            values: total_values.clone(),
            returns,
            dates: dates.clone(),
            cash_flows,
            first_date: *dates.first().unwrap(),
            last_date: *dates.last().unwrap(),
            dd_start_date,
            dd_end_date,
            best_return,
            worst_return,
            frequency: freq.into(),
        }
    }
}

#[cfg(test)]
mod tests {
    use rotala::clock::Clock;
    use rotala::exchange::uist::UistV1;
    use rotala::input::penelope::PenelopeBuilder;

    use crate::broker::uist::UistBroker;
    use crate::broker::uist::UistBrokerBuilder;
    use crate::broker::BrokerCost;
    use crate::perf::StrategySnapshot;
    use crate::strategy::staticweight::StaticWeightStrategyBuilder;
    use crate::strategy::{History, Strategy};
    use crate::types::PortfolioAllocation;

    use super::Frequency;
    use super::PerformanceCalculator;
    use super::PortfolioCalculations;

    fn setup() -> (UistBroker, Clock) {
        let mut source_builder = PenelopeBuilder::new();
        source_builder.add_quote(101.0, 102.0, 100, "ABC");
        source_builder.add_quote(102.0, 103.0, 101, "ABC");
        source_builder.add_quote(97.0, 98.0, 102, "ABC");
        source_builder.add_quote(105.0, 106.0, 103, "ABC");

        source_builder.add_quote(501.0, 502.0, 100, "BCD");
        source_builder.add_quote(503.0, 504.0, 101, "BCD");
        source_builder.add_quote(498.0, 499.0, 102, "BCD");
        source_builder.add_quote(495.0, 496.0, 103, "BCD");

        let (price_source, clock) =
            source_builder.build_with_frequency(rotala::clock::Frequency::Second);
        let uist = UistV1::new(clock.clone(), price_source, "FAKE");

        let brkr = UistBrokerBuilder::new()
            .with_trade_costs(vec![BrokerCost::PctOfValue(0.01)])
            .with_exchange(uist)
            .build();

        (brkr, clock)
    }

    #[test]
    fn test_that_annualizations_calculate_correctly() {
        assert_eq!(
            (PortfolioCalculations::annualize_returns(0.29, 252, &Frequency::Daily) * 100.0)
                .round(),
            45.0
        );
        assert_eq!(
            (PortfolioCalculations::annualize_returns(0.05, 126, &Frequency::Daily) * 100.0)
                .round(),
            15.0
        );

        assert_eq!(
            (PortfolioCalculations::annualize_volatility(0.01, &Frequency::Daily) * 100.0).round(),
            16.0
        );
    }

    #[test]
    fn test_that_portfolio_calculates_performance_accurately() {
        let (brkr, clock) = setup();
        //We use less than 100% because some bugs become possible when you are allocating the full
        //portfolio which perturb the order of operations leading to different perf outputs.
        let mut target_weights = PortfolioAllocation::new();
        target_weights.insert("ABC", 0.4);
        target_weights.insert("BCD", 0.4);

        let mut strat = StaticWeightStrategyBuilder::new()
            .with_brkr(brkr)
            .with_weights(target_weights)
            .with_clock(clock.clone())
            .default();

        strat.init(&100_000.0);

        strat.update();

        strat.update();

        strat.update();

        let output = strat.get_history();
        println!("{:?}", output);
        let perf = PerformanceCalculator::calculate(Frequency::Daily, output);

        let portfolio_return = perf.ret;
        //We need to round up to cmp properly
        let to_comp = (portfolio_return * 1000.0).round();
        println!("{:?}", to_comp);
        assert_eq!(to_comp, 5.0);
    }

    #[test]
    fn test_that_returns_with_cash_flow_correct() {
        //Each period has a 10% return starting from the last period value + the value of the cash
        //flow. Adding the cash flow at the start is the most conservative calculation and should
        //reflect how operations are ordered in the client.

        let snap0 = StrategySnapshot {
            date: 100.into(),
            portfolio_value: 100.0.into(),
            net_cash_flow: 0.0.into(),
            inflation: 0.0,
        };
        let snap1 = StrategySnapshot {
            date: 101.into(),
            portfolio_value: 121.0.into(),
            net_cash_flow: 10.0.into(),
            inflation: 0.0,
        };
        let snap2 = StrategySnapshot {
            date: 102.into(),
            portfolio_value: 126.9.into(),
            net_cash_flow: 30.0.into(),
            inflation: 0.0,
        };
        let snap3 = StrategySnapshot {
            date: 103.into(),
            portfolio_value: 150.59.into(),
            net_cash_flow: 40.0.into(),
            inflation: 0.0,
        };
        let with_cash_flows = vec![snap0, snap1, snap2, snap3];

        let snap3 = StrategySnapshot {
            date: 100.into(),
            portfolio_value: 100.0.into(),
            net_cash_flow: 0.0.into(),
            inflation: 0.0,
        };
        let snap4 = StrategySnapshot {
            date: 101.into(),
            portfolio_value: 110.0.into(),
            net_cash_flow: 0.0.into(),
            inflation: 0.0,
        };
        let snap5 = StrategySnapshot {
            date: 102.into(),
            portfolio_value: 99.0.into(),
            net_cash_flow: 0.0.into(),
            inflation: 0.0,
        };
        let snap6 = StrategySnapshot {
            date: 103.into(),
            portfolio_value: 108.9.into(),
            net_cash_flow: 0.0.into(),
            inflation: 0.0,
        };
        let without_cash_flows = vec![snap3, snap4, snap5, snap6];

        let perf0 = PerformanceCalculator::calculate(Frequency::Daily, with_cash_flows);
        let perf1 = PerformanceCalculator::calculate(Frequency::Daily, without_cash_flows);

        let ret0 = f64::round(perf0.ret * 100.0);
        let ret1 = f64::round(perf1.ret * 100.0);

        println!("{:?}", perf0);
        println!("{:?}", perf1);
        assert_eq!(ret0, ret1);
    }

    #[test]
    fn test_that_returns_with_inflation_correct() {
        let snap1 = StrategySnapshot {
            date: 100.into(),
            portfolio_value: 100.0.into(),
            net_cash_flow: 0.0.into(),
            inflation: 0.0,
        };
        let snap2 = StrategySnapshot {
            date: 101.into(),
            portfolio_value: 110.0.into(),
            net_cash_flow: 0.0.into(),
            inflation: 0.10,
        };
        let snap3 = StrategySnapshot {
            date: 102.into(),
            portfolio_value: 121.0.into(),
            net_cash_flow: 0.0.into(),
            inflation: 0.10,
        };

        let with_inflation = vec![snap1, snap2, snap3];

        let perf = PerformanceCalculator::calculate(Frequency::Daily, with_inflation);

        dbg!(&perf.returns);
        assert!(perf.returns == vec![0.0, 0.0])
    }

    #[test]
    fn test_that_perf_completes_with_zeros() {
        let snap1 = StrategySnapshot {
            date: 100.into(),
            portfolio_value: 0.0.into(),
            net_cash_flow: 0.0.into(),
            inflation: 0.0,
        };
        let snap2 = StrategySnapshot {
            date: 101.into(),
            portfolio_value: 0.0.into(),
            net_cash_flow: 0.0.into(),
            inflation: 0.0,
        };
        let snap3 = StrategySnapshot {
            date: 102.into(),
            portfolio_value: 0.0.into(),
            net_cash_flow: 0.0.into(),
            inflation: 0.0,
        };

        let with_zeros = vec![snap1, snap2, snap3];

        let perf = PerformanceCalculator::calculate(Frequency::Daily, with_zeros);

        dbg!(&perf.returns);
        assert!(perf.returns == vec![0.0, 0.0])
    }

    #[test]
    fn test_that_perf_orders_best_and_worst() {
        let snap1 = StrategySnapshot {
            date: 100.into(),
            portfolio_value: 110.0.into(),
            net_cash_flow: 0.0.into(),
            inflation: 0.0,
        };
        let snap2 = StrategySnapshot {
            date: 101.into(),
            portfolio_value: 90.0.into(),
            net_cash_flow: 0.0.into(),
            inflation: 0.0,
        };
        let snap3 = StrategySnapshot {
            date: 102.into(),
            portfolio_value: 110.0.into(),
            net_cash_flow: 0.0.into(),
            inflation: 0.0,
        };

        let snaps = vec![snap1, snap2, snap3];

        let perf = PerformanceCalculator::calculate(Frequency::Daily, snaps);
        assert!(perf.best_return > perf.worst_return);
    }
}