hyperliquid-backtest 0.1.2

Comprehensive Rust library for backtesting trading strategies with Hyperliquid data, funding rates, and perpetual futures mechanics
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
//! Extended tests for the indicators module

use crate::indicators::*;
use crate::errors::Result;
use chrono::{DateTime, FixedOffset, TimeZone};
use std::collections::VecDeque;

/// Test that funding direction is correctly determined from rates
#[test]
fn test_funding_direction_from_rate_standard() {
    // Test positive funding rate
    let direction = FundingDirection::from_rate(0.0001);
    assert_eq!(direction, FundingDirection::Positive);
    
    // Test negative funding rate
    let direction = FundingDirection::from_rate(-0.0001);
    assert_eq!(direction, FundingDirection::Negative);
    
    // Test zero funding rate
    let direction = FundingDirection::from_rate(0.0);
    assert_eq!(direction, FundingDirection::Neutral);
}

/// Test that funding volatility calculation works correctly
#[test]
fn test_funding_volatility_calculation_standard() {
    // Test with consistent funding rates
    let rates = vec![0.0001, 0.0001, 0.0001, 0.0001, 0.0001];
    let volatility = calculate_funding_volatility(&rates);
    assert_eq!(volatility, 0.0); // No volatility with constant rates
    
    // Test with varying funding rates
    let rates = vec![0.0001, 0.0002, 0.0003, 0.0002, 0.0001];
    let volatility = calculate_funding_volatility(&rates);
    assert!(volatility > 0.0); // Should have some volatility
}

/// Test that funding momentum calculation works correctly
#[test]
fn test_funding_momentum_calculation_standard() {
    // Test with increasing funding rates
    let rates = vec![0.0001, 0.0002, 0.0003, 0.0004, 0.0005];
    let momentum = calculate_funding_momentum(&rates);
    assert!(momentum > 0.0); // Positive momentum
    
    // Test with decreasing funding rates
    let rates = vec![0.0005, 0.0004, 0.0003, 0.0002, 0.0001];
    let momentum = calculate_funding_momentum(&rates);
    assert!(momentum < 0.0); // Negative momentum
}

/// Test that funding arbitrage calculation works correctly
#[test]
fn test_funding_arbitrage_calculation_standard() {
    // Test with positive funding rate
    let funding_rate = 0.0005;
    let price = 50000.0;
    let opportunity = calculate_funding_arbitrage(funding_rate, price);
    
    assert!(opportunity.is_arbitrage);
    assert_eq!(opportunity.direction, FundingDirection::Positive);
    
    // Test with negative funding rate
    let funding_rate = -0.0005;
    let price = 50000.0;
    let opportunity = calculate_funding_arbitrage(funding_rate, price);
    
    assert!(opportunity.is_arbitrage);
    assert_eq!(opportunity.direction, FundingDirection::Negative);
}

/// Test that basis indicator calculation works correctly
#[test]
fn test_basis_indicator_calculation_standard() {
    // Test with positive basis
    let spot_price = 50000.0;
    let futures_price = 50500.0;
    let days_to_expiry = 30.0;
    
    let basis = calculate_basis_indicator(spot_price, futures_price, days_to_expiry);
    
    assert!(basis.basis > 0.0); // Positive basis (contango)
    assert!(basis.annualized_basis > 0.0);
    
    // Test with negative basis
    let spot_price = 50000.0;
    let futures_price = 49500.0;
    let days_to_expiry = 30.0;
    
    let basis = calculate_basis_indicator(spot_price, futures_price, days_to_expiry);
    
    assert!(basis.basis < 0.0); // Negative basis (backwardation)
    assert!(basis.annualized_basis < 0.0);
}

/// Test that FundingPredictionConfig works correctly
#[test]
fn test_funding_prediction_config() {
    // Test default config
    let default_config = FundingPredictionConfig::default();
    
    assert_eq!(default_config.lookback_periods, 48);
    
    // Weights should sum to 1.0
    let sum = default_config.volatility_weight + 
              default_config.momentum_weight + 
              default_config.basis_weight + 
              default_config.correlation_weight;
    
    assert!((sum - 1.0).abs() < 0.0001);
    
    // Test custom config
    let custom_config = FundingPredictionConfig {
        lookback_periods: 24,
        volatility_weight: 0.1,
        momentum_weight: 0.2,
        basis_weight: 0.3,
        correlation_weight: 0.4,
    };
    
    assert_eq!(custom_config.lookback_periods, 24);
    assert_eq!(custom_config.volatility_weight, 0.1);
    assert_eq!(custom_config.momentum_weight, 0.2);
    assert_eq!(custom_config.basis_weight, 0.3);
    assert_eq!(custom_config.correlation_weight, 0.4);
}

/// Test that FundingRatePredictor works correctly
#[test]
fn test_funding_rate_predictor() {
    // Create a predictor with custom config
    let config = FundingPredictionConfig {
        lookback_periods: 10,
        volatility_weight: 0.25,
        momentum_weight: 0.25,
        basis_weight: 0.25,
        correlation_weight: 0.25,
    };
    
    let mut predictor = FundingRatePredictor::new(config);
    
    // Test with empty predictor
    let prediction = predictor.predict();
    assert_eq!(prediction.expected_rate, 0.0);
    assert_eq!(prediction.direction, FundingDirection::Neutral);
    assert_eq!(prediction.confidence, 0.0);
    
    // Add increasing observations
    for i in 0..10 {
        predictor.add_observation(0.0001 * i as f64);
    }
    
    // Test prediction with increasing trend
    let prediction = predictor.predict();
    assert!(prediction.expected_rate > 0.0009); // Should predict continued increase
    assert_eq!(prediction.direction, FundingDirection::Positive);
    assert!(prediction.confidence > 0.5); // Should have high confidence
    
    // Test volatility calculation
    let volatility = predictor.get_volatility();
    assert!(volatility > 0.0);
    
    // Test momentum calculation
    let momentum = predictor.get_momentum();
    assert!(momentum > 0.0);
    
    // Test with decreasing trend
    let mut predictor = FundingRatePredictor::new(config);
    for i in 0..10 {
        predictor.add_observation(0.001 - 0.0001 * i as f64);
    }
    
    let prediction = predictor.predict();
    assert!(prediction.expected_rate < 0.0001); // Should predict continued decrease
    assert_eq!(prediction.direction, FundingDirection::Positive); // Still positive but decreasing
    
    // Test with alternating values
    let mut predictor = FundingRatePredictor::new(config);
    for i in 0..10 {
        predictor.add_observation(if i % 2 == 0 { 0.0001 } else { -0.0001 });
    }
    
    let prediction = predictor.predict();
    assert!(prediction.confidence < 0.7); // Should have lower confidence with alternating values
}

/// Test that FundingRatePredictor correlation works correctly
#[test]
fn test_funding_rate_predictor_correlation() {
    // Create two predictors
    let config = FundingPredictionConfig::default();
    let mut predictor1 = FundingRatePredictor::new(config.clone());
    let mut predictor2 = FundingRatePredictor::new(config.clone());
    
    // Add identical observations to both
    for i in 0..10 {
        let rate = 0.0001 * i as f64;
        predictor1.add_observation(rate);
        predictor2.add_observation(rate);
    }
    
    // Test correlation with self (should be 1.0)
    let correlation = predictor1.correlation_with(&predictor1);
    assert!((correlation - 1.0).abs() < 0.0001);
    
    // Test correlation with identical predictor (should be 1.0)
    let correlation = predictor1.correlation_with(&predictor2);
    assert!((correlation - 1.0).abs() < 0.0001);
    
    // Create predictor with opposite trend
    let mut predictor3 = FundingRatePredictor::new(config);
    for i in 0..10 {
        let rate = 0.0001 * (9 - i) as f64;
        predictor3.add_observation(rate);
    }
    
    // Test correlation with opposite trend (should be negative)
    let correlation = predictor1.correlation_with(&predictor3);
    assert!(correlation < 0.0);
    
    // Test correlation with empty predictor
    let predictor4 = FundingRatePredictor::new(config);
    let correlation = predictor1.correlation_with(&predictor4);
    assert_eq!(correlation, 0.0);
}

/// Test that FundingRatePredictor anomaly detection works correctly
#[test]
fn test_funding_rate_predictor_anomaly_detection() {
    // Create a predictor with custom config
    let config = FundingPredictionConfig {
        lookback_periods: 10,
        volatility_weight: 0.25,
        momentum_weight: 0.25,
        basis_weight: 0.25,
        correlation_weight: 0.25,
    };
    
    let mut predictor = FundingRatePredictor::new(config);
    
    // Add normal observations
    for _ in 0..9 {
        predictor.add_observation(0.0001);
    }
    
    // Test with normal observation
    predictor.add_observation(0.0001);
    let anomaly = predictor.detect_anomaly();
    assert!(!anomaly.is_anomaly);
    
    // Test with slightly abnormal observation
    predictor.add_observation(0.0002);
    let anomaly = predictor.detect_anomaly();
    assert!(!anomaly.is_anomaly); // Should not be flagged as anomaly
    
    // Test with highly abnormal observation
    predictor.add_observation(0.001); // 10x higher
    let anomaly = predictor.detect_anomaly();
    assert!(anomaly.is_anomaly);
    assert!(anomaly.deviation > 3.0); // Should be more than 3 sigma
    assert_eq!(anomaly.direction, FundingDirection::Positive);
    
    // Test with highly abnormal negative observation
    predictor.add_observation(-0.001); // Much lower
    let anomaly = predictor.detect_anomaly();
    assert!(anomaly.is_anomaly);
    assert!(anomaly.deviation > 3.0);
    assert_eq!(anomaly.direction, FundingDirection::Negative);
}

/// Test that FundingRatePredictor cycle detection works correctly
#[test]
fn test_funding_rate_predictor_cycle_detection() {
    // Create a predictor with custom config
    let config = FundingPredictionConfig {
        lookback_periods: 24,
        volatility_weight: 0.25,
        momentum_weight: 0.25,
        basis_weight: 0.25,
        correlation_weight: 0.25,
    };
    
    let mut predictor = FundingRatePredictor::new(config);
    
    // Add observations with a clear 8-hour cycle
    for i in 0..24 {
        let hour = i % 8;
        let rate = match hour {
            0 => 0.0003, // Peak at 00:00
            1 => 0.0002,
            2 => 0.0001,
            3 => 0.0,
            4 => -0.0001, // Trough at 04:00
            5 => 0.0,
            6 => 0.0001,
            7 => 0.0002,
            _ => unreachable!(),
        };
        predictor.add_observation(rate);
    }
    
    // Detect funding cycle
    let cycle = predictor.detect_funding_cycle();
    
    // Should detect an 8-hour cycle
    assert_eq!(cycle.period_hours, 8);
    assert!(cycle.strength > 0.5); // Should have strong cycle detection
    assert!(cycle.is_significant);
    
    // Test with random data (should not detect a strong cycle)
    let mut predictor = FundingRatePredictor::new(config);
    for i in 0..24 {
        let rate = 0.0001 * ((i as f64 * 0.123).sin() + (i as f64 * 0.456).cos());
        predictor.add_observation(rate);
    }
    
    let cycle = predictor.detect_funding_cycle();
    assert!(cycle.strength < 0.7); // Should have weaker cycle detection
}

/// Test that OpenInterestChange works correctly
#[test]
fn test_open_interest_change() {
    // Test increasing open interest
    let prev_oi = 1000.0;
    let curr_oi = 1100.0;
    let price = 50000.0;
    
    let change = OpenInterestChange::new(prev_oi, curr_oi, price);
    
    assert_eq!(change.absolute_change, 100.0);
    assert_eq!(change.percentage_change, 0.1); // 10% increase
    assert_eq!(change.usd_value_change, 100.0 * 50000.0);
    assert!(change.is_increasing);
    assert!(!change.is_decreasing);
    
    // Test decreasing open interest
    let prev_oi = 1000.0;
    let curr_oi = 900.0;
    let price = 50000.0;
    
    let change = OpenInterestChange::new(prev_oi, curr_oi, price);
    
    assert_eq!(change.absolute_change, -100.0);
    assert_eq!(change.percentage_change, -0.1); // 10% decrease
    assert_eq!(change.usd_value_change, -100.0 * 50000.0);
    assert!(!change.is_increasing);
    assert!(change.is_decreasing);
    
    // Test unchanged open interest
    let prev_oi = 1000.0;
    let curr_oi = 1000.0;
    let price = 50000.0;
    
    let change = OpenInterestChange::new(prev_oi, curr_oi, price);
    
    assert_eq!(change.absolute_change, 0.0);
    assert_eq!(change.percentage_change, 0.0);
    assert_eq!(change.usd_value_change, 0.0);
    assert!(!change.is_increasing);
    assert!(!change.is_decreasing);
    
    // Test with zero previous OI
    let prev_oi = 0.0;
    let curr_oi = 1000.0;
    let price = 50000.0;
    
    let change = OpenInterestChange::new(prev_oi, curr_oi, price);
    
    assert_eq!(change.absolute_change, 1000.0);
    assert_eq!(change.percentage_change, 0.0); // Should handle division by zero
    assert_eq!(change.usd_value_change, 1000.0 * 50000.0);
    assert!(change.is_increasing);
    assert!(!change.is_decreasing);
}

/// Test that LiquidationImpact works correctly
#[test]
fn test_liquidation_impact() {
    // Test significant liquidation
    let liquidation_amount = 1000.0;
    let open_interest = 10000.0;
    let price = 50000.0;
    let price_impact = -0.02; // 2% price drop
    
    let impact = LiquidationImpact::new(liquidation_amount, open_interest, price, price_impact);
    
    assert_eq!(impact.liquidation_percentage, 0.1); // 10% of OI
    assert_eq!(impact.usd_value, liquidation_amount * price);
    assert_eq!(impact.price_impact, price_impact);
    assert!(impact.is_significant);
    
    // Test minor liquidation
    let liquidation_amount = 100.0;
    let open_interest = 10000.0;
    let price = 50000.0;
    let price_impact = -0.001; // 0.1% price drop
    
    let impact = LiquidationImpact::new(liquidation_amount, open_interest, price, price_impact);
    
    assert_eq!(impact.liquidation_percentage, 0.01); // 1% of OI
    assert_eq!(impact.usd_value, liquidation_amount * price);
    assert_eq!(impact.price_impact, price_impact);
    assert!(!impact.is_significant); // Below default threshold
    
    // Test with zero open interest
    let liquidation_amount = 1000.0;
    let open_interest = 0.0;
    let price = 50000.0;
    let price_impact = -0.02;
    
    let impact = LiquidationImpact::new(liquidation_amount, open_interest, price, price_impact);
    
    assert_eq!(impact.liquidation_percentage, 0.0); // Should handle division by zero
    assert_eq!(impact.usd_value, liquidation_amount * price);
    assert_eq!(impact.price_impact, price_impact);
    assert!(impact.is_significant); // Still significant due to price impact
}

/// Test that FundingPriceCorrelation works correctly
#[test]
fn test_funding_price_correlation() {
    // Test positive correlation
    let funding_rates = vec![0.0001, 0.0002, 0.0003, 0.0004, 0.0005];
    let prices = vec![50000.0, 50100.0, 50200.0, 50300.0, 50400.0];
    
    let correlation = FundingPriceCorrelation::calculate(&funding_rates, &prices);
    
    assert!(correlation.coefficient > 0.9); // Strong positive correlation
    assert!(correlation.is_significant);
    assert_eq!(correlation.relationship, "Positive");
    
    // Test negative correlation
    let funding_rates = vec![0.0001, 0.0002, 0.0003, 0.0004, 0.0005];
    let prices = vec![50400.0, 50300.0, 50200.0, 50100.0, 50000.0];
    
    let correlation = FundingPriceCorrelation::calculate(&funding_rates, &prices);
    
    assert!(correlation.coefficient < -0.9); // Strong negative correlation
    assert!(correlation.is_significant);
    assert_eq!(correlation.relationship, "Negative");
    
    // Test no correlation
    let funding_rates = vec![0.0001, 0.0002, 0.0001, 0.0002, 0.0001];
    let prices = vec![50000.0, 50100.0, 50000.0, 50100.0, 50000.0];
    
    let correlation = FundingPriceCorrelation::calculate(&funding_rates, &prices);
    
    assert!(correlation.coefficient.abs() < 0.5); // Weak correlation
    assert!(!correlation.is_significant);
    assert_eq!(correlation.relationship, "Weak");
    
    // Test with empty arrays
    let funding_rates: Vec<f64> = Vec::new();
    let prices: Vec<f64> = Vec::new();
    
    let correlation = FundingPriceCorrelation::calculate(&funding_rates, &prices);
    
    assert_eq!(correlation.coefficient, 0.0);
    assert!(!correlation.is_significant);
    assert_eq!(correlation.relationship, "Unknown");
    
    // Test with different length arrays
    let funding_rates = vec![0.0001, 0.0002, 0.0003];
    let prices = vec![50000.0, 50100.0, 50200.0, 50300.0, 50400.0];
    
    let correlation = FundingPriceCorrelation::calculate(&funding_rates, &prices);
    
    assert_eq!(correlation.coefficient, 0.0);
    assert!(!correlation.is_significant);
    assert_eq!(correlation.relationship, "Unknown");
}

/// Test that AsAny trait works correctly
#[test]
fn test_as_any_trait() {
    // Create a predictor
    let config = FundingPredictionConfig::default();
    let predictor = FundingRatePredictor::new(config);
    
    // Get as Any
    let any = predictor.as_any();
    
    // Downcast back to FundingRatePredictor
    let downcast = any.downcast_ref::<FundingRatePredictor>();
    assert!(downcast.is_some());
    
    // Downcast to wrong type
    let wrong_downcast = any.downcast_ref::<String>();
    assert!(wrong_downcast.is_none());
}