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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
use crate::api::rest::limits::MAX_HISTORICAL_PRICES;
use crate::api::rest::validation::{
bounded_decimal_field, decimal_field, positive_field, strictly_positive_field, symbol_field,
time_frame_field,
};
use crate::utils::ChainError;
use optionstratlib::simulation::WalkType;
use optionstratlib::utils::TimeFrame;
use positive::pos_or_panic;
use rust_decimal::prelude::ToPrimitive;
use serde::{Deserialize, Serialize};
use std::fmt;
use utoipa::ToSchema;
/// Represents address binding options for the server
#[derive(Debug, Clone, Copy, Default)]
pub enum ListenOn {
/// Binds to all network interfaces (0.0.0.0)
All,
/// Binds only to localhost (127.0.0.1)
#[default]
Localhost,
}
impl ListenOn {
/// Converts the enum variant to its corresponding IP address string
pub fn as_str(&self) -> &'static str {
match self {
ListenOn::All => "0.0.0.0",
ListenOn::Localhost => "127.0.0.1",
}
}
}
impl From<ListenOn> for String {
fn from(listen_on: ListenOn) -> Self {
listen_on.as_str().to_string()
}
}
impl fmt::Display for ListenOn {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, PartialOrd, ToSchema)]
pub enum ApiTimeFrame {
/// 1-microsecond data.
Microsecond,
/// 1-millisecond data.
Millisecond,
/// 1-second data.
Second,
/// 1-minute data.
Minute,
/// 1-hour data.
Hour,
/// Daily data.
Day,
/// Weekly data.
Week,
/// Monthly data.
Month,
/// Quarterly data.
Quarter,
/// Yearly data.
Year,
/// Custom periods per year.
Custom(f64),
}
impl From<TimeFrame> for ApiTimeFrame {
fn from(value: TimeFrame) -> Self {
match value {
TimeFrame::Microsecond => ApiTimeFrame::Microsecond,
TimeFrame::Millisecond => ApiTimeFrame::Millisecond,
TimeFrame::Second => ApiTimeFrame::Second,
TimeFrame::Minute => ApiTimeFrame::Minute,
TimeFrame::Hour => ApiTimeFrame::Hour,
TimeFrame::Day => ApiTimeFrame::Day,
TimeFrame::Week => ApiTimeFrame::Week,
TimeFrame::Month => ApiTimeFrame::Month,
TimeFrame::Quarter => ApiTimeFrame::Quarter,
TimeFrame::Year => ApiTimeFrame::Year,
TimeFrame::Custom(value) => ApiTimeFrame::Custom(value.to_f64()),
}
}
}
impl From<ApiTimeFrame> for TimeFrame {
fn from(value: ApiTimeFrame) -> Self {
match value {
ApiTimeFrame::Microsecond => TimeFrame::Microsecond,
ApiTimeFrame::Millisecond => TimeFrame::Millisecond,
ApiTimeFrame::Second => TimeFrame::Second,
ApiTimeFrame::Minute => TimeFrame::Minute,
ApiTimeFrame::Hour => TimeFrame::Hour,
ApiTimeFrame::Day => TimeFrame::Day,
ApiTimeFrame::Week => TimeFrame::Week,
ApiTimeFrame::Month => TimeFrame::Month,
ApiTimeFrame::Quarter => TimeFrame::Quarter,
ApiTimeFrame::Year => TimeFrame::Year,
ApiTimeFrame::Custom(value) => TimeFrame::Custom(pos_or_panic!(value)),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, ToSchema)]
pub enum ApiWalkType {
/// Standard Brownian motion (normal increments)
Brownian {
/// Time step size (fraction of year: daily=1/365, weekly=1/52, etc.)
dt: f64,
/// Drift parameter (expected return or growth rate)
drift: f64,
/// Volatility parameter (annualized standard deviation)
volatility: f64,
},
/// Geometric Brownian motion (log-normal increments)
GeometricBrownian {
/// Time step size (fraction of year: daily=1/365, weekly=1/52, etc.)
dt: f64,
/// Drift parameter (expected return or growth rate)
drift: f64,
/// Volatility parameter (annualized standard deviation)
volatility: f64,
},
/// Log-Returns model (simulates directly log-returns instead of prices)
LogReturns {
/// Time step size (fraction of year: daily=1/365, weekly=1/52, etc.)
dt: f64,
/// Expected return (mean of log returns)
expected_return: f64,
/// Volatility parameter (annualized standard deviation of log returns)
volatility: f64,
/// Optional autocorrelation parameter (-1 to 1)
autocorrelation: Option<f64>,
},
/// Mean-reverting process (Ornstein-Uhlenbeck)
MeanReverting {
/// Time step size (fraction of year: daily=1/365, weekly=1/52, etc.)
dt: f64,
/// Volatility parameter (annualized standard deviation)
volatility: f64,
/// Mean reversion speed (rate at which process reverts to mean)
speed: f64,
/// Long-term mean (equilibrium level)
mean: f64,
},
/// Jump diffusion process (normal increments with occasional jumps)
JumpDiffusion {
/// Time step size (fraction of year: daily=1/365, weekly=1/52, etc.)
dt: f64,
/// Drift parameter (expected return of continuous part)
drift: f64,
/// Volatility parameter (annualized standard deviation of continuous part)
volatility: f64,
/// Jump intensity (annual frequency of jumps)
intensity: f64,
/// Jump size mean (average jump magnitude)
jump_mean: f64,
/// Jump size volatility (standard deviation of jump size)
jump_volatility: f64,
},
/// GARCH process (time-varying volatility)
Garch {
/// Time step size (fraction of year: daily=1/365, weekly=1/52, etc.)
dt: f64,
/// Drift parameter (expected return)
drift: f64,
/// Initial volatility parameter (starting volatility level)
volatility: f64,
/// GARCH alpha parameter (impact of past observations)
alpha: f64,
/// GARCH beta parameter (persistence of volatility)
beta: f64,
},
/// Heston model (stochastic volatility)
Heston {
/// Time step size (fraction of year: daily=1/365, weekly=1/52, etc.)
dt: f64,
/// Drift parameter (expected return)
drift: f64,
/// Initial volatility parameter (starting volatility level)
volatility: f64,
/// Mean reversion speed of volatility
kappa: f64,
/// Long-term variance (equilibrium level of variance)
theta: f64,
/// Volatility of volatility (standard deviation of variance process)
xi: f64,
/// Correlation between price and volatility processes
rho: f64,
},
/// Custom process defined by a function
Custom {
/// Time step size (fraction of year: daily=1/365, weekly=1/52, etc.)
dt: f64,
/// Drift parameter (expected change)
drift: f64,
/// Volatility parameter (may be interpreted differently based on custom implementation)
volatility: f64,
/// Volatility of Volatility parameter (annualized standard deviation)
vov: f64,
/// Mean reversion speed (rate at which process reverts to mean)
vol_speed: f64,
/// Long-term mean (equilibrium level)
vol_mean: f64,
},
/// Telegraph process (two-state regime switching model)
Telegraph {
/// Time step size (fraction of year: daily=1/365, weekly=1/52, etc.)
dt: f64,
/// Drift parameter (expected return)
drift: f64,
/// Base volatility parameter (annualized standard deviation)
volatility: f64,
/// Transition rate from state -1 to +1 (intensity of upward regime changes)
lambda_up: f64,
/// Transition rate from state +1 to -1 (intensity of downward regime changes)
lambda_down: f64,
/// Optional volatility multiplier for the +1 state (default: 1.0)
vol_multiplier_up: Option<f64>,
/// Optional volatility multiplier for the -1 state (default: 1.0)
vol_multiplier_down: Option<f64>,
},
/// Represents historical price data for a given timeframe.
///
/// This encapsulates the historical price data, including the timeframe
/// over which the data was collected and a vector of positive price values.
/// It is typically used to store and process historical market data for
/// financial analysis and simulation purposes.
///
/// # Fields
///
/// * `timeframe`: The `TimeFrame` over which the historical data is relevant.
/// * `prices`: A `Vec` of `Positive` values representing the historical prices.
Historical {
/// The timeframe of the historical data.
timeframe: ApiTimeFrame,
/// The vector of positive price values.
prices: Vec<f64>,
/// Represents an optional `symbol` as a `String`.
///
/// This field can store the symbol related to an object, entity, or data structure.
/// If no symbol is provided, the value will be `None`.
///
symbol: Option<String>,
},
}
impl From<WalkType> for ApiWalkType {
fn from(value: WalkType) -> Self {
match value {
WalkType::Brownian {
dt,
drift,
volatility,
} => ApiWalkType::Brownian {
dt: dt.to_f64(),
drift: drift.to_f64().unwrap_or(0.0),
volatility: volatility.to_f64(),
},
WalkType::GeometricBrownian {
dt,
drift,
volatility,
} => ApiWalkType::GeometricBrownian {
dt: dt.to_f64(),
drift: drift.to_f64().unwrap_or(0.0),
volatility: volatility.to_f64(),
},
WalkType::LogReturns {
dt,
expected_return,
volatility,
autocorrelation,
} => ApiWalkType::LogReturns {
dt: dt.to_f64(),
expected_return: expected_return.to_f64().unwrap_or(0.0),
volatility: volatility.to_f64(),
autocorrelation: autocorrelation.map(|ac| ac.to_f64().unwrap_or(0.0)),
},
WalkType::MeanReverting {
dt,
volatility,
speed,
mean,
} => ApiWalkType::MeanReverting {
dt: dt.to_f64(),
volatility: volatility.to_f64(),
speed: speed.to_f64(),
mean: mean.to_f64(),
},
WalkType::JumpDiffusion {
dt,
drift,
volatility,
intensity,
jump_mean,
jump_volatility,
} => ApiWalkType::JumpDiffusion {
dt: dt.to_f64(),
drift: drift.to_f64().unwrap_or(0.0),
volatility: volatility.to_f64(),
intensity: intensity.to_f64(),
jump_mean: jump_mean.to_f64().unwrap_or(0.0),
jump_volatility: jump_volatility.to_f64(),
},
WalkType::Garch {
dt,
drift,
volatility,
alpha,
beta,
} => ApiWalkType::Garch {
dt: dt.to_f64(),
drift: drift.to_f64().unwrap_or(0.0),
volatility: volatility.to_f64(),
alpha: alpha.to_f64(),
beta: beta.to_f64(),
},
WalkType::Heston {
dt,
drift,
volatility,
kappa,
theta,
xi,
rho,
} => ApiWalkType::Heston {
dt: dt.to_f64(),
drift: drift.to_f64().unwrap_or(0.0),
volatility: volatility.to_f64(),
kappa: kappa.to_f64(),
theta: theta.to_f64(),
xi: xi.to_f64(),
rho: rho.to_f64().unwrap_or(0.0),
},
WalkType::Custom {
dt,
drift,
volatility,
vov,
vol_speed,
vol_mean,
} => ApiWalkType::Custom {
dt: dt.to_f64(),
drift: drift.to_f64().unwrap_or(0.0),
volatility: volatility.to_f64(),
vov: vov.to_f64(),
vol_speed: vol_speed.to_f64(),
vol_mean: vol_mean.to_f64(),
},
WalkType::Telegraph {
dt,
drift,
volatility,
lambda_up,
lambda_down,
vol_multiplier_up,
vol_multiplier_down,
} => ApiWalkType::Telegraph {
dt: dt.to_f64(),
drift: drift.to_f64().unwrap_or(0.0),
volatility: volatility.to_f64(),
lambda_up: lambda_up.to_f64(),
lambda_down: lambda_down.to_f64(),
vol_multiplier_up: vol_multiplier_up.map(|v| v.to_f64()),
vol_multiplier_down: vol_multiplier_down.map(|v| v.to_f64()),
},
WalkType::Historical {
timeframe,
prices,
symbol,
} => ApiWalkType::Historical {
timeframe: timeframe.into(),
prices: prices.iter().map(|p| p.to_f64()).collect(),
symbol,
},
}
}
}
impl TryFrom<ApiWalkType> for WalkType {
type Error = ChainError;
/// Validates a client-supplied [`ApiWalkType`] and converts it into the domain
/// [`WalkType`]. Every raw `f64` field is checked before conversion: `dt` must be
/// strictly positive, volatilities and other `Positive` fields must be finite and
/// non-negative, `Decimal` fields must be finite, an `autocorrelation` (when present)
/// must lie in `[-1, 1]`, and a `Historical` price series must be non-negative and no
/// longer than [`MAX_HISTORICAL_PRICES`]. Invalid input yields a
/// [`ChainError::Validation`] naming the offending field instead of panicking.
///
/// # Errors
///
/// Returns [`ChainError::Validation`] on the first field that fails validation.
fn try_from(value: ApiWalkType) -> Result<Self, Self::Error> {
let walk = match value {
ApiWalkType::Brownian {
dt,
drift,
volatility,
} => WalkType::Brownian {
dt: strictly_positive_field("dt", dt)?,
drift: decimal_field("drift", drift)?,
volatility: positive_field("volatility", volatility)?,
},
ApiWalkType::GeometricBrownian {
dt,
drift,
volatility,
} => WalkType::GeometricBrownian {
dt: strictly_positive_field("dt", dt)?,
drift: decimal_field("drift", drift)?,
volatility: positive_field("volatility", volatility)?,
},
ApiWalkType::LogReturns {
dt,
expected_return,
volatility,
autocorrelation,
} => WalkType::LogReturns {
dt: strictly_positive_field("dt", dt)?,
expected_return: decimal_field("expected_return", expected_return)?,
volatility: positive_field("volatility", volatility)?,
autocorrelation: autocorrelation
.map(|ac| bounded_decimal_field("autocorrelation", ac, -1.0, 1.0))
.transpose()?,
},
ApiWalkType::MeanReverting {
dt,
volatility,
speed,
mean,
} => WalkType::MeanReverting {
dt: strictly_positive_field("dt", dt)?,
volatility: positive_field("volatility", volatility)?,
speed: positive_field("speed", speed)?,
mean: positive_field("mean", mean)?,
},
ApiWalkType::JumpDiffusion {
dt,
drift,
volatility,
intensity,
jump_mean,
jump_volatility,
} => WalkType::JumpDiffusion {
dt: strictly_positive_field("dt", dt)?,
drift: decimal_field("drift", drift)?,
volatility: positive_field("volatility", volatility)?,
intensity: positive_field("intensity", intensity)?,
jump_mean: decimal_field("jump_mean", jump_mean)?,
jump_volatility: positive_field("jump_volatility", jump_volatility)?,
},
ApiWalkType::Garch {
dt,
drift,
volatility,
alpha,
beta,
} => {
let alpha_p = positive_field("alpha", alpha)?;
let beta_p = positive_field("beta", beta)?;
// GARCH(1,1) stationarity: alpha + beta < 1. Rejecting here
// keeps invalid model domains from being stored and failing
// later inside the simulation.
if alpha + beta >= 1.0 {
return Err(ChainError::Validation {
field: "alpha".to_string(),
reason: format!(
"alpha + beta must be < 1 for GARCH stationarity, got {}",
alpha + beta
),
});
}
WalkType::Garch {
dt: strictly_positive_field("dt", dt)?,
drift: decimal_field("drift", drift)?,
volatility: positive_field("volatility", volatility)?,
alpha: alpha_p,
beta: beta_p,
}
}
ApiWalkType::Heston {
dt,
drift,
volatility,
kappa,
theta,
xi,
rho,
} => WalkType::Heston {
dt: strictly_positive_field("dt", dt)?,
drift: decimal_field("drift", drift)?,
volatility: positive_field("volatility", volatility)?,
kappa: positive_field("kappa", kappa)?,
theta: positive_field("theta", theta)?,
xi: positive_field("xi", xi)?,
rho: bounded_decimal_field("rho", rho, -1.0, 1.0)?,
},
ApiWalkType::Custom {
dt,
drift,
volatility,
vov,
vol_speed,
vol_mean,
} => WalkType::Custom {
dt: strictly_positive_field("dt", dt)?,
drift: decimal_field("drift", drift)?,
volatility: positive_field("volatility", volatility)?,
vov: positive_field("vov", vov)?,
vol_speed: positive_field("vol_speed", vol_speed)?,
vol_mean: positive_field("vol_mean", vol_mean)?,
},
ApiWalkType::Telegraph {
dt,
drift,
volatility,
lambda_up,
lambda_down,
vol_multiplier_up,
vol_multiplier_down,
} => WalkType::Telegraph {
dt: strictly_positive_field("dt", dt)?,
drift: decimal_field("drift", drift)?,
volatility: positive_field("volatility", volatility)?,
lambda_up: positive_field("lambda_up", lambda_up)?,
lambda_down: positive_field("lambda_down", lambda_down)?,
vol_multiplier_up: vol_multiplier_up
.map(|v| positive_field("vol_multiplier_up", v))
.transpose()?,
vol_multiplier_down: vol_multiplier_down
.map(|v| positive_field("vol_multiplier_down", v))
.transpose()?,
},
ApiWalkType::Historical {
timeframe,
prices,
symbol,
} => {
if prices.len() > *MAX_HISTORICAL_PRICES {
return Err(ChainError::Validation {
field: "prices".to_string(),
reason: format!(
"must not exceed {} entries, got {}",
*MAX_HISTORICAL_PRICES,
prices.len()
),
});
}
if let Some(ref sym) = symbol {
symbol_field("method.symbol", sym)?;
}
let mut converted = Vec::with_capacity(prices.len());
for price in prices {
converted.push(strictly_positive_field("prices", price)?);
}
WalkType::Historical {
timeframe: time_frame_field("method.timeframe", timeframe)?,
prices: converted,
symbol,
}
}
};
Ok(walk)
}
}
impl fmt::Display for ApiWalkType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Serialize to a JSON string
let json_str = serde_json::to_string(self).map_err(|_| fmt::Error)?;
write!(f, "{}", json_str)
}
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct SessionId {
#[serde(rename = "sessionid")]
pub(crate) session_id: String,
}
impl fmt::Display for SessionId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.session_id)
}
}
/// Test suite for ApiTimeFrame conversions and serialization
#[cfg(test)]
mod api_timeframe_tests {
use super::*;
use serde_json::{from_str, to_string};
/// Test conversion from TimeFrame to ApiTimeFrame
#[test]
fn test_timeframe_to_api_timeframe_conversion() {
let test_cases = vec![
(TimeFrame::Microsecond, ApiTimeFrame::Microsecond),
(TimeFrame::Millisecond, ApiTimeFrame::Millisecond),
(TimeFrame::Second, ApiTimeFrame::Second),
(TimeFrame::Minute, ApiTimeFrame::Minute),
(TimeFrame::Hour, ApiTimeFrame::Hour),
(TimeFrame::Day, ApiTimeFrame::Day),
(TimeFrame::Week, ApiTimeFrame::Week),
(TimeFrame::Month, ApiTimeFrame::Month),
(TimeFrame::Quarter, ApiTimeFrame::Quarter),
(TimeFrame::Year, ApiTimeFrame::Year),
(
TimeFrame::Custom(pos_or_panic!(2.0)),
ApiTimeFrame::Custom(2.0),
),
];
for (input, expected) in test_cases {
let result: ApiTimeFrame = input.into();
assert_eq!(result, expected, "Conversion failed for {:?}", input);
}
}
/// Test conversion from ApiTimeFrame to TimeFrame
#[test]
fn test_api_timeframe_to_timeframe_conversion() {
let test_cases = vec![
(ApiTimeFrame::Microsecond, TimeFrame::Microsecond),
(ApiTimeFrame::Millisecond, TimeFrame::Millisecond),
(ApiTimeFrame::Second, TimeFrame::Second),
(ApiTimeFrame::Minute, TimeFrame::Minute),
(ApiTimeFrame::Hour, TimeFrame::Hour),
(ApiTimeFrame::Day, TimeFrame::Day),
(ApiTimeFrame::Week, TimeFrame::Week),
(ApiTimeFrame::Month, TimeFrame::Month),
(ApiTimeFrame::Quarter, TimeFrame::Quarter),
(ApiTimeFrame::Year, TimeFrame::Year),
(
ApiTimeFrame::Custom(2.0),
TimeFrame::Custom(pos_or_panic!(2.0)),
),
];
for (input, expected) in test_cases {
let result: TimeFrame = input.into();
assert_eq!(result, expected, "Conversion failed for {:?}", input);
}
}
/// Test serialization and deserialization of ApiTimeFrame
#[test]
fn test_api_timeframe_serialization() {
let test_cases = vec![
ApiTimeFrame::Day,
ApiTimeFrame::Hour,
ApiTimeFrame::Custom(1.5),
];
for timeframe in test_cases {
let serialized = to_string(&timeframe).expect("Failed to serialize");
let deserialized: ApiTimeFrame = from_str(&serialized).expect("Failed to deserialize");
assert_eq!(timeframe, deserialized);
}
}
}
/// Test suite for ApiWalkType conversions and serialization
#[cfg(test)]
mod api_walktype_tests {
use super::*;
use rust_decimal_macros::dec;
use serde_json::{from_str, to_string};
/// Helper function to create test walk types
fn create_test_walk_types() -> Vec<WalkType> {
vec![
WalkType::Brownian {
dt: pos_or_panic!(0.004),
drift: dec!(0.05),
volatility: pos_or_panic!(0.25),
},
WalkType::GeometricBrownian {
dt: pos_or_panic!(0.004),
drift: dec!(0.05),
volatility: pos_or_panic!(0.25),
},
WalkType::LogReturns {
dt: pos_or_panic!(0.004),
expected_return: dec!(0.02),
volatility: pos_or_panic!(0.25),
autocorrelation: Some(dec!(0.1)),
},
WalkType::MeanReverting {
dt: pos_or_panic!(0.004),
volatility: pos_or_panic!(0.25),
speed: pos_or_panic!(0.5),
mean: pos_or_panic!(100.0),
},
WalkType::JumpDiffusion {
dt: pos_or_panic!(0.004),
drift: dec!(0.05),
volatility: pos_or_panic!(0.25),
intensity: pos_or_panic!(0.1),
jump_mean: dec!(0.02),
jump_volatility: pos_or_panic!(0.15),
},
WalkType::Historical {
timeframe: TimeFrame::Day,
prices: vec![
pos_or_panic!(100.0),
pos_or_panic!(101.0),
pos_or_panic!(102.0),
],
symbol: Some("AAPL".to_string()),
},
]
}
/// Test conversion from WalkType to ApiWalkType
#[test]
fn test_walktype_to_api_walktype_conversion() {
for walk_type in create_test_walk_types() {
let api_walk_type: ApiWalkType = walk_type.clone().into();
let converted_back: WalkType =
api_walk_type.try_into().expect("valid walk type converts");
// Ensure the converted back type matches the original
assert_eq!(
walk_type, converted_back,
"Conversion failed for {:?}",
walk_type
);
}
}
/// Test serialization and deserialization of ApiWalkType
#[test]
fn test_api_walktype_serialization() {
let test_cases = create_test_walk_types()
.into_iter()
.map(|wt| wt.into())
.collect::<Vec<ApiWalkType>>();
for walk_type in test_cases {
let serialized = to_string(&walk_type).expect("Failed to serialize ApiWalkType");
let deserialized: ApiWalkType =
from_str(&serialized).expect("Failed to deserialize ApiWalkType");
assert_eq!(
walk_type.to_string(),
deserialized.to_string(),
"Serialization/deserialization failed"
);
}
}
/// Test different variations of walk types with edge cases
#[test]
fn test_walktype_edge_cases() {
// Test conversion of walk types with extreme/default values
let edge_cases = vec![
WalkType::Brownian {
dt: pos_or_panic!(0.001),
drift: dec!(0.0),
volatility: pos_or_panic!(0.0),
},
WalkType::LogReturns {
dt: pos_or_panic!(1.0 / 252.0), // Trading day fraction
expected_return: dec!(0.0),
volatility: pos_or_panic!(0.5),
autocorrelation: None,
},
];
for walk_type in edge_cases {
let api_walk_type: ApiWalkType = walk_type.clone().into();
let converted_back: WalkType =
api_walk_type.try_into().expect("valid walk type converts");
assert_eq!(
walk_type, converted_back,
"Edge case conversion failed for {:?}",
walk_type
);
}
}
/// A zero `dt` is structurally invalid and must be rejected, not panic.
#[test]
fn test_apiwalktype_zero_dt_is_validation_error() {
let api = ApiWalkType::Brownian {
dt: 0.0,
drift: 0.05,
volatility: 0.2,
};
match WalkType::try_from(api) {
Err(ChainError::Validation { field, .. }) => assert_eq!(field, "dt"),
other => panic!("expected Validation error for dt, got {other:?}"),
}
}
/// A non-finite volatility must be rejected, not panic.
#[test]
fn test_apiwalktype_nan_volatility_is_validation_error() {
let api = ApiWalkType::Brownian {
dt: 0.004,
drift: 0.05,
volatility: f64::NAN,
};
match WalkType::try_from(api) {
Err(ChainError::Validation { field, .. }) => assert_eq!(field, "volatility"),
other => panic!("expected Validation error for volatility, got {other:?}"),
}
}
/// An autocorrelation outside `[-1, 1]` must be rejected.
#[test]
fn test_apiwalktype_autocorrelation_out_of_range_is_validation_error() {
let api = ApiWalkType::LogReturns {
dt: 0.004,
expected_return: 0.02,
volatility: 0.25,
autocorrelation: Some(1.5),
};
match WalkType::try_from(api) {
Err(ChainError::Validation { field, .. }) => assert_eq!(field, "autocorrelation"),
other => panic!("expected Validation error for autocorrelation, got {other:?}"),
}
}
/// A negative historical price must be rejected, not panic.
#[test]
fn test_apiwalktype_negative_historical_price_is_validation_error() {
let api = ApiWalkType::Historical {
timeframe: ApiTimeFrame::Day,
prices: vec![100.0, -1.0, 102.0],
symbol: None,
};
match WalkType::try_from(api) {
Err(ChainError::Validation { field, .. }) => assert_eq!(field, "prices"),
other => panic!("expected Validation error for prices, got {other:?}"),
}
}
/// A historical price series longer than the cap must be rejected.
#[test]
fn test_apiwalktype_oversized_historical_prices_is_validation_error() {
use crate::api::rest::limits::MAX_HISTORICAL_PRICES;
let prices = vec![100.0; *MAX_HISTORICAL_PRICES + 1];
let api = ApiWalkType::Historical {
timeframe: ApiTimeFrame::Day,
prices,
symbol: None,
};
match WalkType::try_from(api) {
Err(ChainError::Validation { field, .. }) => assert_eq!(field, "prices"),
other => panic!("expected Validation error for prices, got {other:?}"),
}
}
/// A negative or zero custom timeframe on a Historical walk must be a 400,
/// not a worker panic through the infallible conversion.
#[test]
fn test_apiwalktype_historical_negative_custom_timeframe_is_validation_error() {
for bad in [-1.0, 0.0] {
let api = ApiWalkType::Historical {
timeframe: ApiTimeFrame::Custom(bad),
prices: vec![100.0, 101.0],
symbol: None,
};
match WalkType::try_from(api) {
Err(ChainError::Validation { field, .. }) => {
assert_eq!(field, "method.timeframe")
}
other => panic!("expected Validation error for timeframe {bad}, got {other:?}"),
}
}
}
/// An injection-shaped historical symbol must be rejected as client input
/// (400) at the conversion boundary, not surface later as an adapter 500.
#[test]
fn test_apiwalktype_historical_invalid_symbol_is_validation_error() {
let api = ApiWalkType::Historical {
timeframe: ApiTimeFrame::Day,
prices: vec![],
symbol: Some("A' OR '1'='1".to_string()),
};
match WalkType::try_from(api) {
Err(ChainError::Validation { field, .. }) => assert_eq!(field, "method.symbol"),
other => panic!("expected Validation error for symbol, got {other:?}"),
}
}
/// Heston correlation outside [-1, 1] must be rejected at creation.
#[test]
fn test_apiwalktype_heston_rho_out_of_range_is_validation_error() {
let api = ApiWalkType::Heston {
dt: 0.004,
drift: 0.05,
volatility: 0.2,
kappa: 1.0,
theta: 0.04,
xi: 0.3,
rho: 1.5,
};
match WalkType::try_from(api) {
Err(ChainError::Validation { field, .. }) => assert_eq!(field, "rho"),
other => panic!("expected Validation error for rho, got {other:?}"),
}
}
/// GARCH parameters violating stationarity (alpha + beta >= 1) must be
/// rejected before the session is stored.
#[test]
fn test_apiwalktype_garch_nonstationary_is_validation_error() {
let api = ApiWalkType::Garch {
dt: 0.004,
drift: 0.0,
volatility: 0.2,
alpha: 0.6,
beta: 0.5,
};
match WalkType::try_from(api) {
Err(ChainError::Validation { field, reason }) => {
assert_eq!(field, "alpha");
assert!(reason.contains("stationarity"));
}
other => panic!("expected Validation error for alpha+beta, got {other:?}"),
}
}
}
/// Test suite for SessionId
#[cfg(test)]
mod session_id_tests {
use super::*;
use serde_json::{from_str, to_string};
#[test]
fn test_session_id_creation_and_display() {
let session_id_str = "6af613b6-569c-5c22-9c37-2ed93f31d3af";
let session_id = SessionId {
session_id: session_id_str.to_string(),
};
// Test string conversion
assert_eq!(format!("{}", session_id), session_id_str);
// Test serialization and deserialization
let serialized = to_string(&session_id).expect("Failed to serialize");
assert!(serialized.contains(session_id_str));
let deserialized: SessionId = from_str(&serialized).expect("Failed to deserialize");
assert_eq!(deserialized.session_id, session_id_str);
}
#[test]
fn test_session_id_rename_attribute() {
// Test that the sessionid attribute is correctly renamed during serialization
let session_id_str = "test-session-id";
let session_id = SessionId {
session_id: session_id_str.to_string(),
};
let serialized = to_string(&session_id).expect("Failed to serialize");
assert!(serialized.contains("\"sessionid\":\"test-session-id\""));
}
#[test]
fn test_display_implementations() {
let test_cases = vec![
(
ApiWalkType::Brownian {
dt: 0.004,
drift: 0.05,
volatility: 0.25,
},
r#"{"Brownian":{"dt":0.004,"drift":0.05,"volatility":0.25}}"#,
),
(
ApiWalkType::Historical {
timeframe: ApiTimeFrame::Day,
prices: vec![100.0, 101.0, 102.0],
symbol: Some("AAPL".to_string()),
},
r#"{"Historical":{"timeframe":"Day","prices":[100.0,101.0,102.0],"symbol":"AAPL"}}"#,
),
(
ApiWalkType::LogReturns {
dt: 0.004,
expected_return: 0.02,
volatility: 0.25,
autocorrelation: None,
},
r#"{"LogReturns":{"dt":0.004,"expected_return":0.02,"volatility":0.25,"autocorrelation":null}}"#,
),
];
for (walk_type, expected) in test_cases {
assert_eq!(
walk_type.to_string(),
expected,
"Display implementation failed for {:?}",
walk_type
);
}
}
#[test]
fn test_roundtrip_serialization() {
let test_cases = vec![
ApiWalkType::Brownian {
dt: 0.004,
drift: 0.05,
volatility: 0.25,
},
ApiWalkType::Historical {
timeframe: ApiTimeFrame::Day,
prices: vec![100.0, 101.0, 102.0],
symbol: Some("AAPL".to_string()),
},
ApiWalkType::LogReturns {
dt: 0.004,
expected_return: 0.02,
volatility: 0.25,
autocorrelation: None,
},
];
for walk_type in test_cases {
// Convert to string (JSON)
let json_str = walk_type.to_string();
// Deserialize back to ApiWalkType
let deserialized: ApiWalkType =
serde_json::from_str(&json_str).expect("Failed to deserialize");
// Ensure the deserialized version matches the original
assert_eq!(
walk_type, deserialized,
"Roundtrip serialization failed for {:?}",
walk_type
);
}
}
}