vectorbt-rust 1.2.0__tar.gz → 1.2.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {vectorbt_rust-1.2.0 → vectorbt_rust-1.2.2}/Cargo.lock +1 -1
- {vectorbt_rust-1.2.0 → vectorbt_rust-1.2.2}/Cargo.toml +1 -1
- {vectorbt_rust-1.2.0 → vectorbt_rust-1.2.2}/PKG-INFO +1 -1
- {vectorbt_rust-1.2.0 → vectorbt_rust-1.2.2}/pyproject.toml +1 -1
- {vectorbt_rust-1.2.0 → vectorbt_rust-1.2.2}/src/generic.rs +62 -30
- {vectorbt_rust-1.2.0 → vectorbt_rust-1.2.2}/src/portfolio.rs +20 -3
- {vectorbt_rust-1.2.0 → vectorbt_rust-1.2.2}/src/returns.rs +39 -4
- {vectorbt_rust-1.2.0 → vectorbt_rust-1.2.2}/README.md +0 -0
- {vectorbt_rust-1.2.0 → vectorbt_rust-1.2.2}/rustfmt.toml +0 -0
- {vectorbt_rust-1.2.0 → vectorbt_rust-1.2.2}/src/indicators.rs +0 -0
- {vectorbt_rust-1.2.0 → vectorbt_rust-1.2.2}/src/labels.rs +0 -0
- {vectorbt_rust-1.2.0 → vectorbt_rust-1.2.2}/src/lib.rs +0 -0
- {vectorbt_rust-1.2.0 → vectorbt_rust-1.2.2}/src/records.rs +0 -0
- {vectorbt_rust-1.2.0 → vectorbt_rust-1.2.2}/src/signals.rs +0 -0
|
@@ -1116,52 +1116,84 @@ pub(crate) fn nanstd_1d(a: &[f64], ddof: usize) -> f64 {
|
|
|
1116
1116
|
}
|
|
1117
1117
|
}
|
|
1118
1118
|
|
|
1119
|
-
|
|
1119
|
+
const ROLLING_EXTREMUM_SCAN_WINDOW: usize = 10;
|
|
1120
|
+
|
|
1121
|
+
/// Rolling min or max using a monotonic deque, O(n) regardless of `window`;
|
|
1122
|
+
/// small windows use a direct scan instead, which is faster there.
|
|
1123
|
+
///
|
|
1124
|
+
/// Ties are resolved like a naive scan seeded with the current element:
|
|
1125
|
+
/// the current element if it is extremal, else the earliest one.
|
|
1126
|
+
fn rolling_extremum_1d<const IS_MAX: bool>(a: &[f64], window: usize, minp: usize) -> Vec<f64> {
|
|
1120
1127
|
let n = a.len();
|
|
1121
1128
|
let mut out = vec![f64::NAN; n];
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
if
|
|
1128
|
-
|
|
1129
|
+
if window <= ROLLING_EXTREMUM_SCAN_WINDOW {
|
|
1130
|
+
// A direct scan is faster than the deque for small windows
|
|
1131
|
+
for i in 0..n {
|
|
1132
|
+
let mut best = a[i];
|
|
1133
|
+
let mut cnt = 0usize;
|
|
1134
|
+
let start = if i + 1 >= window { i + 1 - window } else { 0 };
|
|
1135
|
+
for &v in &a[start..=i] {
|
|
1136
|
+
if v.is_nan() {
|
|
1137
|
+
continue;
|
|
1138
|
+
}
|
|
1139
|
+
if best.is_nan() || (if IS_MAX { v > best } else { v < best }) {
|
|
1140
|
+
best = v;
|
|
1141
|
+
}
|
|
1142
|
+
cnt += 1;
|
|
1129
1143
|
}
|
|
1130
|
-
if
|
|
1131
|
-
|
|
1144
|
+
if cnt >= minp {
|
|
1145
|
+
out[i] = best;
|
|
1132
1146
|
}
|
|
1133
|
-
cnt += 1;
|
|
1134
|
-
}
|
|
1135
|
-
if cnt >= minp {
|
|
1136
|
-
out[i] = minv;
|
|
1137
1147
|
}
|
|
1148
|
+
return out;
|
|
1138
1149
|
}
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
let
|
|
1144
|
-
let mut out = vec![f64::NAN; n];
|
|
1150
|
+
// Indices are pushed in increasing order and never revisited, so a flat buffer suffices
|
|
1151
|
+
let mut deque = vec![0usize; n];
|
|
1152
|
+
let mut head = 0usize;
|
|
1153
|
+
let mut tail = 0usize;
|
|
1154
|
+
let mut cnt = 0usize;
|
|
1145
1155
|
for i in 0..n {
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
if a[j].is_nan() {
|
|
1151
|
-
continue;
|
|
1156
|
+
if i >= window {
|
|
1157
|
+
let old = i - window;
|
|
1158
|
+
if !a[old].is_nan() {
|
|
1159
|
+
cnt -= 1;
|
|
1152
1160
|
}
|
|
1153
|
-
if
|
|
1154
|
-
|
|
1161
|
+
if head < tail && deque[head] == old {
|
|
1162
|
+
head += 1;
|
|
1155
1163
|
}
|
|
1164
|
+
}
|
|
1165
|
+
let x = a[i];
|
|
1166
|
+
if !x.is_nan() {
|
|
1156
1167
|
cnt += 1;
|
|
1168
|
+
if IS_MAX {
|
|
1169
|
+
while head < tail && a[deque[tail - 1]] < x {
|
|
1170
|
+
tail -= 1;
|
|
1171
|
+
}
|
|
1172
|
+
} else {
|
|
1173
|
+
while head < tail && a[deque[tail - 1]] > x {
|
|
1174
|
+
tail -= 1;
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
deque[tail] = i;
|
|
1178
|
+
tail += 1;
|
|
1157
1179
|
}
|
|
1158
|
-
if cnt >= minp {
|
|
1159
|
-
|
|
1180
|
+
if cnt >= minp && head < tail {
|
|
1181
|
+
let best = a[deque[head]];
|
|
1182
|
+
// Prefer the current element on ties (matters for signed zeros)
|
|
1183
|
+
out[i] = if x == best { x } else { best };
|
|
1160
1184
|
}
|
|
1161
1185
|
}
|
|
1162
1186
|
out
|
|
1163
1187
|
}
|
|
1164
1188
|
|
|
1189
|
+
pub(crate) fn rolling_min_1d(a: &[f64], window: usize, minp: usize) -> Vec<f64> {
|
|
1190
|
+
rolling_extremum_1d::<false>(a, window, minp)
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
pub(crate) fn rolling_max_1d(a: &[f64], window: usize, minp: usize) -> Vec<f64> {
|
|
1194
|
+
rolling_extremum_1d::<true>(a, window, minp)
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1165
1197
|
pub(crate) fn rolling_mean_1d(a: &[f64], window: usize, minp: usize) -> Vec<f64> {
|
|
1166
1198
|
let n = a.len();
|
|
1167
1199
|
let mut out = vec![f64::NAN; n];
|
|
@@ -96,6 +96,23 @@ fn add(a: f64, b: f64) -> f64 {
|
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
+
// Relative tolerance of `floor_to_granularity`, about 8.9e-16. Covers floating-point rounding of the
|
|
100
|
+
// size, the granularity and their quotient (4.3 / 0.1 == 42.99999999999999), not a fraction of a lot.
|
|
101
|
+
// Same value as `size_granularity_rel_tol` in vectorbt/portfolio/nb.py.
|
|
102
|
+
const SIZE_GRANULARITY_REL_TOL: f64 = 4.0 * f64::EPSILON;
|
|
103
|
+
|
|
104
|
+
#[inline(always)]
|
|
105
|
+
fn floor_to_granularity(size: f64, size_granularity: f64) -> f64 {
|
|
106
|
+
let lots = size / size_granularity;
|
|
107
|
+
let nearest = (lots + 0.5).floor();
|
|
108
|
+
let lots = if (lots - nearest).abs() <= SIZE_GRANULARITY_REL_TOL * nearest.abs() {
|
|
109
|
+
nearest
|
|
110
|
+
} else {
|
|
111
|
+
lots.floor()
|
|
112
|
+
};
|
|
113
|
+
(lots * size_granularity).min(size)
|
|
114
|
+
}
|
|
115
|
+
|
|
99
116
|
#[inline(always)]
|
|
100
117
|
fn get_return(input_value: f64, output_value: f64) -> f64 {
|
|
101
118
|
if input_value == 0.0 {
|
|
@@ -847,7 +864,7 @@ fn buy(
|
|
|
847
864
|
|
|
848
865
|
// Adjust granularity
|
|
849
866
|
if !size_granularity.is_nan() {
|
|
850
|
-
adj_size = (adj_size
|
|
867
|
+
adj_size = floor_to_granularity(adj_size, size_granularity);
|
|
851
868
|
}
|
|
852
869
|
|
|
853
870
|
// Get cash required
|
|
@@ -873,7 +890,7 @@ fn buy(
|
|
|
873
890
|
let max_acq_size = max_req_cash / adj_price;
|
|
874
891
|
|
|
875
892
|
if !size_granularity.is_nan() {
|
|
876
|
-
final_size = (max_acq_size
|
|
893
|
+
final_size = floor_to_granularity(max_acq_size, size_granularity);
|
|
877
894
|
let new_req_cash = final_size * adj_price;
|
|
878
895
|
fees_paid = new_req_cash * fees + fixed_fees;
|
|
879
896
|
final_req_cash = new_req_cash + fees_paid;
|
|
@@ -1039,7 +1056,7 @@ fn sell(
|
|
|
1039
1056
|
|
|
1040
1057
|
// Adjust granularity
|
|
1041
1058
|
if !size_granularity.is_nan() {
|
|
1042
|
-
size_limit = (size_limit
|
|
1059
|
+
size_limit = floor_to_granularity(size_limit, size_granularity);
|
|
1043
1060
|
}
|
|
1044
1061
|
|
|
1045
1062
|
if is_close(size_limit, 0.0) {
|
|
@@ -534,14 +534,39 @@ pub(crate) fn omega_ratio_1d(returns: &[f64], ann_factor: f64, risk_free: f64, r
|
|
|
534
534
|
numer / denom
|
|
535
535
|
}
|
|
536
536
|
|
|
537
|
+
fn zero_std_ratio(mean: f64) -> f64 {
|
|
538
|
+
if mean == 0.0 {
|
|
539
|
+
f64::NAN
|
|
540
|
+
} else {
|
|
541
|
+
f64::INFINITY.copysign(mean)
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/// Whether all non-NaN values of `values - shift` are equal.
|
|
546
|
+
fn is_constant_shifted(values: &[f64], shift: f64) -> bool {
|
|
547
|
+
let mut first = f64::NAN;
|
|
548
|
+
for &value in values {
|
|
549
|
+
let adj_value = value - shift;
|
|
550
|
+
if adj_value.is_nan() {
|
|
551
|
+
continue;
|
|
552
|
+
}
|
|
553
|
+
if first.is_nan() {
|
|
554
|
+
first = adj_value;
|
|
555
|
+
} else if adj_value != first {
|
|
556
|
+
return false;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
true
|
|
560
|
+
}
|
|
561
|
+
|
|
537
562
|
pub(crate) fn sharpe_ratio_1d(returns: &[f64], ann_factor: f64, risk_free: f64, ddof: usize) -> f64 {
|
|
538
563
|
if returns.len() < 2 {
|
|
539
564
|
return f64::NAN;
|
|
540
565
|
}
|
|
541
566
|
let mean = nanmean_shifted(returns, risk_free);
|
|
542
567
|
let std = nanstd_shifted(returns, risk_free, ddof);
|
|
543
|
-
if std == 0.0 {
|
|
544
|
-
return
|
|
568
|
+
if std == 0.0 || (!std.is_nan() && is_constant_shifted(returns, risk_free)) {
|
|
569
|
+
return zero_std_ratio(mean);
|
|
545
570
|
}
|
|
546
571
|
mean / std * ann_factor.sqrt()
|
|
547
572
|
}
|
|
@@ -551,11 +576,18 @@ pub(crate) fn sharpe_ratio_2d(returns: ArrayView2<'_, f64>, ann_factor: f64, ris
|
|
|
551
576
|
if let Some(src) = returns.as_slice() {
|
|
552
577
|
let mut sums = vec![0.0f64; ncols];
|
|
553
578
|
let mut counts = vec![0usize; ncols];
|
|
579
|
+
let mut firsts = vec![f64::NAN; ncols];
|
|
580
|
+
let mut constant = vec![true; ncols];
|
|
554
581
|
for row in src.chunks_exact(ncols) {
|
|
555
582
|
for (col, &ret) in row.iter().enumerate() {
|
|
556
583
|
let adj_ret = ret - risk_free;
|
|
557
584
|
if !adj_ret.is_nan() {
|
|
558
585
|
sums[col] += adj_ret;
|
|
586
|
+
if counts[col] == 0 {
|
|
587
|
+
firsts[col] = adj_ret;
|
|
588
|
+
} else if adj_ret != firsts[col] {
|
|
589
|
+
constant[col] = false;
|
|
590
|
+
}
|
|
559
591
|
counts[col] += 1;
|
|
560
592
|
}
|
|
561
593
|
}
|
|
@@ -576,8 +608,8 @@ pub(crate) fn sharpe_ratio_2d(returns: ArrayView2<'_, f64>, ann_factor: f64, ris
|
|
|
576
608
|
if nrows >= 2 && counts[col] > ddof {
|
|
577
609
|
let mean = sums[col] / counts[col] as f64;
|
|
578
610
|
let std = (sq[col] / (counts[col] - ddof) as f64).sqrt();
|
|
579
|
-
out[col] = if std == 0.0 {
|
|
580
|
-
|
|
611
|
+
out[col] = if std == 0.0 || (!std.is_nan() && constant[col]) {
|
|
612
|
+
zero_std_ratio(mean)
|
|
581
613
|
} else {
|
|
582
614
|
mean / std * ann_sqrt
|
|
583
615
|
};
|
|
@@ -614,6 +646,9 @@ pub(crate) fn sortino_ratio_1d(returns: &[f64], ann_factor: f64, required_return
|
|
|
614
646
|
let average_annualized_return = nanmean_shifted(returns, required_return) * ann_factor;
|
|
615
647
|
let downside_risk = downside_risk_1d(returns, ann_factor, required_return);
|
|
616
648
|
if downside_risk == 0.0 {
|
|
649
|
+
if average_annualized_return == 0.0 {
|
|
650
|
+
return f64::NAN;
|
|
651
|
+
}
|
|
617
652
|
return f64::INFINITY;
|
|
618
653
|
}
|
|
619
654
|
average_annualized_return / downside_risk
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|