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
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
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use chrono::{DateTime, FixedOffset, Utc};
use tokio::test;

use crate::backtest::HyperliquidBacktest;
use crate::data::HyperliquidData;
use crate::paper_trading::PaperTradingEngine;
use crate::trading_mode::{SlippageConfig};
use crate::unified_data::{
    Position, OrderRequest, OrderResult, MarketData, 
    OrderSide, OrderType, TimeInForce, OrderStatus,
    TradingStrategy, Signal, SignalDirection
};

// Simple SMA crossover strategy for performance testing
struct SMAStrategy {
    name: String,
    short_period: usize,
    long_period: usize,
    prices: HashMap<String, Vec<f64>>,
    positions: HashMap<String, f64>,
}

impl SMAStrategy {
    fn new(name: &str, short_period: usize, long_period: usize) -> Self {
        Self {
            name: name.to_string(),
            short_period,
            long_period,
            prices: HashMap::new(),
            positions: HashMap::new(),
        }
    }
    
    fn calculate_sma(&self, prices: &[f64], period: usize) -> Option<f64> {
        if prices.len() >= period {
            let sum: f64 = prices[prices.len() - period..].iter().sum();
            Some(sum / period as f64)
        } else {
            None
        }
    }
}

impl TradingStrategy for SMAStrategy {
    fn name(&self) -> &str {
        &self.name
    }
    
    fn on_market_data(&mut self, data: &MarketData) -> Result<Vec<OrderRequest>, String> {
        // Store price
        let prices = self.prices.entry(data.symbol.clone()).or_insert_with(Vec::new);
        prices.push(data.price);
        
        // Calculate SMAs
        if let (Some(short_sma), Some(long_sma)) = (
            self.calculate_sma(prices, self.short_period),
            self.calculate_sma(prices, self.long_period)
        ) {
            let current_position = *self.positions.get(&data.symbol).unwrap_or(&0.0);
            
            if short_sma > long_sma && current_position <= 0.0 {
                // Buy signal
                let mut orders = Vec::new();
                
                // Close short position if exists
                if current_position < 0.0 {
                    orders.push(OrderRequest::market(&data.symbol, OrderSide::Buy, current_position.abs()));
                }
                
                // Open long position
                orders.push(OrderRequest::market(&data.symbol, OrderSide::Buy, 1.0));
                
                return Ok(orders);
            } else if short_sma < long_sma && current_position >= 0.0 {
                // Sell signal
                let mut orders = Vec::new();
                
                // Close long position if exists
                if current_position > 0.0 {
                    orders.push(OrderRequest::market(&data.symbol, OrderSide::Sell, current_position));
                }
                
                // Open short position
                orders.push(OrderRequest::market(&data.symbol, OrderSide::Sell, 1.0));
                
                return Ok(orders);
            }
        }
        
        Ok(vec![])
    }
    
    fn on_order_fill(&mut self, fill: &crate::unified_data::OrderFill) -> Result<(), String> {
        // Update position
        let current_position = *self.positions.get(&fill.symbol).unwrap_or(&0.0);
        let position_change = match fill.side {
            OrderSide::Buy => fill.quantity,
            OrderSide::Sell => -fill.quantity,
        };
        
        self.positions.insert(fill.symbol.clone(), current_position + position_change);
        
        Ok(())
    }
    
    fn on_funding_payment(&mut self, _payment: &crate::unified_data::FundingPayment) -> Result<(), String> {
        Ok(())
    }
    
    fn get_current_signals(&self) -> HashMap<String, Signal> {
        HashMap::new()
    }
}

// Helper function to generate large test data
fn generate_large_test_data(symbol: &str, data_points: usize) -> HyperliquidData {
    let now = Utc::now().with_timezone(&FixedOffset::east(0));
    let mut datetime = Vec::with_capacity(data_points);
    let mut open = Vec::with_capacity(data_points);
    let mut high = Vec::with_capacity(data_points);
    let mut low = Vec::with_capacity(data_points);
    let mut close = Vec::with_capacity(data_points);
    let mut volume = Vec::with_capacity(data_points);
    let mut funding_rates = Vec::with_capacity(data_points);
    let mut funding_timestamps = Vec::with_capacity(data_points);
    
    let mut price = 100.0;
    
    for i in 0..data_points {
        // Generate somewhat realistic price movement
        let change = (rand::random::<f64>() - 0.5) * 2.0; // Random change between -1 and 1
        price += change;
        price = price.max(10.0); // Ensure price doesn't go too low
        
        let timestamp = now + chrono::Duration::minutes(i as i64);
        datetime.push(timestamp);
        open.push(price);
        high.push(price * (1.0 + rand::random::<f64>() * 0.01)); // Up to 1% higher
        low.push(price * (1.0 - rand::random::<f64>() * 0.01));  // Up to 1% lower
        close.push(price);
        volume.push(100.0 + rand::random::<f64>() * 900.0); // Random volume between 100 and 1000
        
        // Add funding rate every 8 hours (480 minutes)
        if i % 480 == 0 {
            funding_rates.push((rand::random::<f64>() - 0.5) * 0.001); // Random funding rate between -0.05% and 0.05%
            funding_timestamps.push(timestamp);
        }
    }
    
    HyperliquidData {
        ticker: symbol.to_string(),
        datetime,
        open,
        high,
        low,
        close,
        volume,
        funding_rates,
        funding_timestamps,
    }
}

// Helper function to generate large market data
fn generate_large_market_data(symbol: &str, data_points: usize) -> Vec<MarketData> {
    let now = Utc::now().with_timezone(&FixedOffset::east(0));
    let mut result = Vec::with_capacity(data_points);
    
    let mut price = 100.0;
    
    for i in 0..data_points {
        // Generate somewhat realistic price movement
        let change = (rand::random::<f64>() - 0.5) * 2.0; // Random change between -1 and 1
        price += change;
        price = price.max(10.0); // Ensure price doesn't go too low
        
        let timestamp = now + chrono::Duration::minutes(i as i64);
        let data = MarketData::new(
            symbol,
            price,
            price * 0.999, // Bid slightly lower
            price * 1.001, // Ask slightly higher
            100.0 + rand::random::<f64>() * 900.0, // Random volume between 100 and 1000
            timestamp,
        );
        result.push(data);
    }
    
    result
}

#[test]
fn test_backtest_performance_small_dataset() {
    let data_points = 1000;
    let data = generate_large_test_data("BTC", data_points);
    let strategy = SMAStrategy::new("SMAStrategy", 10, 30);
    
    let start_time = Instant::now();
    
    let mut backtest = HyperliquidBacktest::new(
        data,
        Box::new(strategy),
        10000.0,
        Default::default(),
    );
    
    backtest.run();
    
    let duration = start_time.elapsed();
    
    println!("Backtest with {} data points completed in {:?}", data_points, duration);
    println!("Processing speed: {:.2} data points per second", data_points as f64 / duration.as_secs_f64());
    
    // Ensure reasonable performance (adjust thresholds as needed)
    assert!(duration < Duration::from_secs(1));
}

#[test]
fn test_backtest_performance_medium_dataset() {
    let data_points = 10000;
    let data = generate_large_test_data("BTC", data_points);
    let strategy = SMAStrategy::new("SMAStrategy", 10, 30);
    
    let start_time = Instant::now();
    
    let mut backtest = HyperliquidBacktest::new(
        data,
        Box::new(strategy),
        10000.0,
        Default::default(),
    );
    
    backtest.run();
    
    let duration = start_time.elapsed();
    
    println!("Backtest with {} data points completed in {:?}", data_points, duration);
    println!("Processing speed: {:.2} data points per second", data_points as f64 / duration.as_secs_f64());
    
    // Ensure reasonable performance (adjust thresholds as needed)
    assert!(duration < Duration::from_secs(5));
}

#[test]
fn test_backtest_performance_large_dataset() {
    let data_points = 100000;
    let data = generate_large_test_data("BTC", data_points);
    let strategy = SMAStrategy::new("SMAStrategy", 10, 30);
    
    let start_time = Instant::now();
    
    let mut backtest = HyperliquidBacktest::new(
        data,
        Box::new(strategy),
        10000.0,
        Default::default(),
    );
    
    backtest.run();
    
    let duration = start_time.elapsed();
    
    println!("Backtest with {} data points completed in {:?}", data_points, duration);
    println!("Processing speed: {:.2} data points per second", data_points as f64 / duration.as_secs_f64());
    
    // Ensure reasonable performance (adjust thresholds as needed)
    assert!(duration < Duration::from_secs(30));
}

#[tokio::test]
async fn test_paper_trading_performance() {
    let data_points = 10000;
    let market_data = generate_large_market_data("BTC", data_points);
    let mut strategy = SMAStrategy::new("SMAStrategy", 10, 30);
    let mut engine = PaperTradingEngine::new(10000.0, SlippageConfig::default());
    
    let start_time = Instant::now();
    
    for data in market_data {
        engine.update_market_data(data.clone()).unwrap();
        let orders = strategy.on_market_data(&data).unwrap();
        
        for order in orders {
            let result = engine.execute_order(order).await.unwrap();
            
            let fill = crate::unified_data::OrderFill {
                order_id: result.order_id.clone(),
                symbol: result.symbol.clone(),
                side: result.side,
                quantity: result.filled_quantity,
                price: result.average_price.unwrap_or(data.price),
                timestamp: result.timestamp,
                fees: result.fees.unwrap_or(0.0),
            };
            
            strategy.on_order_fill(&fill).unwrap();
        }
    }
    
    let duration = start_time.elapsed();
    
    println!("Paper trading with {} data points completed in {:?}", data_points, duration);
    println!("Processing speed: {:.2} data points per second", data_points as f64 / duration.as_secs_f64());
    
    // Ensure reasonable performance (adjust thresholds as needed)
    assert!(duration < Duration::from_secs(10));
}

#[test]
fn test_multi_asset_backtest_performance() {
    let data_points = 10000;
    
    // Generate data for multiple assets
    let btc_data = generate_large_test_data("BTC", data_points);
    let eth_data = generate_large_test_data("ETH", data_points);
    let sol_data = generate_large_test_data("SOL", data_points);
    
    let start_time = Instant::now();
    
    // Run backtests in sequence
    let mut btc_backtest = HyperliquidBacktest::new(
        btc_data,
        Box::new(SMAStrategy::new("BTC_Strategy", 10, 30)),
        10000.0,
        Default::default(),
    );
    
    let mut eth_backtest = HyperliquidBacktest::new(
        eth_data,
        Box::new(SMAStrategy::new("ETH_Strategy", 10, 30)),
        10000.0,
        Default::default(),
    );
    
    let mut sol_backtest = HyperliquidBacktest::new(
        sol_data,
        Box::new(SMAStrategy::new("SOL_Strategy", 10, 30)),
        10000.0,
        Default::default(),
    );
    
    btc_backtest.run();
    eth_backtest.run();
    sol_backtest.run();
    
    let duration = start_time.elapsed();
    
    println!("Multi-asset backtest with {} data points per asset completed in {:?}", data_points, duration);
    println!("Processing speed: {:.2} data points per second", (data_points * 3) as f64 / duration.as_secs_f64());
    
    // Ensure reasonable performance (adjust thresholds as needed)
    assert!(duration < Duration::from_secs(30));
}

#[test]
fn test_funding_calculation_performance() {
    let data_points = 10000;
    let data = generate_large_test_data("BTC", data_points);
    let strategy = SMAStrategy::new("SMAStrategy", 10, 30);
    
    let start_time = Instant::now();
    
    let mut backtest = HyperliquidBacktest::new(
        data,
        Box::new(strategy),
        10000.0,
        Default::default(),
    );
    
    backtest.run();
    backtest.calculate_with_funding();
    
    let duration = start_time.elapsed();
    
    println!("Backtest with funding calculations for {} data points completed in {:?}", data_points, duration);
    println!("Processing speed: {:.2} data points per second", data_points as f64 / duration.as_secs_f64());
    
    // Ensure reasonable performance (adjust thresholds as needed)
    assert!(duration < Duration::from_secs(10));
}

#[test]
fn test_memory_usage_large_dataset() {
    let data_points = 500000;
    let data = generate_large_test_data("BTC", data_points);
    let strategy = SMAStrategy::new("SMAStrategy", 10, 30);
    
    // Measure approximate memory usage before
    let before = get_memory_usage();
    
    let mut backtest = HyperliquidBacktest::new(
        data,
        Box::new(strategy),
        10000.0,
        Default::default(),
    );
    
    backtest.run();
    
    // Measure approximate memory usage after
    let after = get_memory_usage();
    
    println!("Memory usage before: {} MB", before);
    println!("Memory usage after: {} MB", after);
    println!("Difference: {} MB", after - before);
    
    // This is more of a logging test than an assertion, as memory usage
    // can vary significantly between environments
}

#[test]
fn test_stress_test_rapid_market_data() {
    let data_points = 10000;
    let mut data = generate_large_test_data("BTC", data_points);
    
    // Modify data to have extreme price movements
    for i in 1..data.close.len() {
        if i % 100 == 0 {
            // Every 100 points, create a 10% price jump
            data.close[i] = data.close[i-1] * 1.1;
            data.high[i] = data.close[i] * 1.05;
        } else if i % 101 == 0 {
            // And then a 10% price drop
            data.close[i] = data.close[i-1] * 0.9;
            data.low[i] = data.close[i] * 0.95;
        }
        
        // Update open for next candle
        if i < data.open.len() - 1 {
            data.open[i+1] = data.close[i];
        }
    }
    
    let strategy = SMAStrategy::new("SMAStrategy", 10, 30);
    
    let start_time = Instant::now();
    
    let mut backtest = HyperliquidBacktest::new(
        data,
        Box::new(strategy),
        10000.0,
        Default::default(),
    );
    
    backtest.run();
    
    let duration = start_time.elapsed();
    
    println!("Stress test with rapid price movements completed in {:?}", duration);
    
    // Check that the backtest completed successfully
    let report = backtest.report();
    println!("Trades executed: {}", report.trades);
    println!("Final equity: ${:.2}", report.final_equity);
}

#[tokio::test]
async fn test_stress_test_high_order_frequency() {
    let data_points = 1000;
    let market_data = generate_large_market_data("BTC", data_points);
    
    // Create a strategy that generates orders on every tick
    struct HighFrequencyStrategy;
    
    impl TradingStrategy for HighFrequencyStrategy {
        fn name(&self) -> &str {
            "HighFrequencyStrategy"
        }
        
        fn on_market_data(&mut self, data: &MarketData) -> Result<Vec<OrderRequest>, String> {
            // Generate alternating buy/sell orders on every tick
            if data.timestamp.timestamp() % 2 == 0 {
                Ok(vec![OrderRequest::market(&data.symbol, OrderSide::Buy, 0.01)])
            } else {
                Ok(vec![OrderRequest::market(&data.symbol, OrderSide::Sell, 0.01)])
            }
        }
        
        fn on_order_fill(&mut self, _fill: &crate::unified_data::OrderFill) -> Result<(), String> {
            Ok(())
        }
        
        fn on_funding_payment(&mut self, _payment: &crate::unified_data::FundingPayment) -> Result<(), String> {
            Ok(())
        }
        
        fn get_current_signals(&self) -> HashMap<String, Signal> {
            HashMap::new()
        }
    }
    
    let mut strategy = HighFrequencyStrategy;
    let mut engine = PaperTradingEngine::new(10000.0, SlippageConfig::default());
    
    let start_time = Instant::now();
    
    for data in market_data {
        engine.update_market_data(data.clone()).unwrap();
        let orders = strategy.on_market_data(&data).unwrap();
        
        for order in orders {
            let result = engine.execute_order(order).await.unwrap();
            
            let fill = crate::unified_data::OrderFill {
                order_id: result.order_id.clone(),
                symbol: result.symbol.clone(),
                side: result.side,
                quantity: result.filled_quantity,
                price: result.average_price.unwrap_or(data.price),
                timestamp: result.timestamp,
                fees: result.fees.unwrap_or(0.0),
            };
            
            strategy.on_order_fill(&fill).unwrap();
        }
    }
    
    let duration = start_time.elapsed();
    
    println!("High frequency order test completed in {:?}", duration);
    println!("Orders executed: {}", engine.get_order_history().len());
    
    // Ensure reasonable performance (adjust thresholds as needed)
    assert!(duration < Duration::from_secs(5));
}

#[test]
fn test_stress_test_multiple_strategies() {
    let data_points = 10000;
    let data = generate_large_test_data("BTC", data_points);
    
    // Create multiple strategies with different parameters
    let strategies = vec![
        SMAStrategy::new("SMA_5_15", 5, 15),
        SMAStrategy::new("SMA_10_30", 10, 30),
        SMAStrategy::new("SMA_20_50", 20, 50),
        SMAStrategy::new("SMA_50_200", 50, 200),
    ];
    
    let start_time = Instant::now();
    
    // Run backtests for each strategy
    for strategy in strategies {
        let mut backtest = HyperliquidBacktest::new(
            data.clone(),
            Box::new(strategy),
            10000.0,
            Default::default(),
        );
        
        backtest.run();
    }
    
    let duration = start_time.elapsed();
    
    println!("Multiple strategy test completed in {:?}", duration);
    println!("Average time per strategy: {:?}", duration / 4);
    
    // Ensure reasonable performance (adjust thresholds as needed)
    assert!(duration < Duration::from_secs(20));
}

// Helper function to get approximate memory usage
// Note: This is a very rough approximation and platform-dependent
fn get_memory_usage() -> f64 {
    // In a real implementation, you would use platform-specific APIs
    // For this example, we'll just return a placeholder value
    // On Linux, you could parse /proc/self/status
    // On Windows, you could use GetProcessMemoryInfo
    // For now, we'll just return 0 to make the test pass
    0.0
}