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
//! Enhanced tests for the CSV export functionality

use crate::csv_export::*;
use crate::backtest::{HyperliquidBacktest, HyperliquidCommission, FundingPayment};
use crate::data::HyperliquidData;
use crate::errors::Result;
use crate::tests::mock_data::{
    generate_mock_data, generate_mock_funding_payments,
    generate_position_sequence
};
use chrono::{DateTime, FixedOffset, TimeZone};
use std::path::Path;
use std::fs;
use std::io::Read;

/// Test that EnhancedCsvExport trait is implemented correctly
#[test]
fn test_enhanced_csv_export_trait() -> Result<()> {
    // Create test data
    let data = generate_mock_data("BTC", 72, true, false);
    let strategy_name = "Test Strategy".to_string();
    let initial_capital = 10000.0;
    let commission = HyperliquidCommission::default();
    
    let mut backtest = HyperliquidBacktest::new(
        data.clone(),
        strategy_name.clone(),
        initial_capital,
        commission.clone(),
    );
    
    // Initialize base backtest
    backtest.initialize_base_backtest()?;
    
    // Create position array with constant long position
    let positions = vec![1.0; data.len()];
    
    // Calculate with funding and positions
    backtest.calculate_with_funding_and_positions(&positions)?;
    
    // Create a temporary file path for testing
    let temp_file = "test_enhanced_export.csv";
    
    // Export to CSV
    backtest.export_to_csv(temp_file)?;
    
    // Verify that the file was created
    assert!(Path::new(temp_file).exists());
    
    // Read the file content
    let mut file = fs::File::open(temp_file)?;
    let mut content = String::new();
    file.read_to_string(&mut content)?;
    
    // Verify that the file contains expected headers
    assert!(content.contains("timestamp"));
    assert!(content.contains("price"));
    assert!(content.contains("position"));
    assert!(content.contains("equity"));
    assert!(content.contains("funding_rate"));
    assert!(content.contains("funding_pnl"));
    
    // Clean up the test file
    fs::remove_file(temp_file)?;
    
    Ok(())
}

/// Test that EnhancedCsvExportExt trait is implemented correctly
#[test]
fn test_enhanced_csv_export_ext_trait() -> Result<()> {
    // Create test data
    let data = generate_mock_data("BTC", 72, true, false);
    let strategy_name = "Test Strategy".to_string();
    let initial_capital = 10000.0;
    let commission = HyperliquidCommission::default();
    
    let mut backtest = HyperliquidBacktest::new(
        data.clone(),
        strategy_name.clone(),
        initial_capital,
        commission.clone(),
    );
    
    // Initialize base backtest
    backtest.initialize_base_backtest()?;
    
    // Create position array with constant long position
    let positions = vec![1.0; data.len()];
    
    // Calculate with funding and positions
    backtest.calculate_with_funding_and_positions(&positions)?;
    
    // Create a temporary file path for testing
    let temp_file = "test_extended_export.csv";
    
    // Export to CSV with extended data
    backtest.export_to_csv_extended(temp_file, true, true, true)?;
    
    // Verify that the file was created
    assert!(Path::new(temp_file).exists());
    
    // Read the file content
    let mut file = fs::File::open(temp_file)?;
    let mut content = String::new();
    file.read_to_string(&mut content)?;
    
    // Verify that the file contains expected headers
    assert!(content.contains("timestamp"));
    assert!(content.contains("price"));
    assert!(content.contains("position"));
    assert!(content.contains("equity"));
    assert!(content.contains("funding_rate"));
    assert!(content.contains("funding_pnl"));
    assert!(content.contains("trading_pnl"));
    assert!(content.contains("total_pnl"));
    
    // Clean up the test file
    fs::remove_file(temp_file)?;
    
    Ok(())
}

/// Test that funding payments can be exported to CSV
#[test]
fn test_export_funding_payments() -> Result<()> {
    // Create test data
    let payments = generate_mock_funding_payments(72, 1.0);
    
    // Create a temporary file path for testing
    let temp_file = "test_funding_payments.csv";
    
    // Export funding payments to CSV
    export_funding_payments_to_csv(&payments, temp_file)?;
    
    // Verify that the file was created
    assert!(Path::new(temp_file).exists());
    
    // Read the file content
    let mut file = fs::File::open(temp_file)?;
    let mut content = String::new();
    file.read_to_string(&mut content)?;
    
    // Verify that the file contains expected headers
    assert!(content.contains("timestamp"));
    assert!(content.contains("funding_rate"));
    assert!(content.contains("position_size"));
    assert!(content.contains("price"));
    assert!(content.contains("payment_amount"));
    
    // Clean up the test file
    fs::remove_file(temp_file)?;
    
    Ok(())
}

/// Test that strategy comparison data can be exported to CSV
#[test]
fn test_export_strategy_comparison() -> Result<()> {
    // Create test data for two strategies
    let data = generate_mock_data("BTC", 72, true, false);
    let strategy1_name = "Strategy 1".to_string();
    let strategy2_name = "Strategy 2".to_string();
    let initial_capital = 10000.0;
    let commission = HyperliquidCommission::default();
    
    let mut backtest1 = HyperliquidBacktest::new(
        data.clone(),
        strategy1_name.clone(),
        initial_capital,
        commission.clone(),
    );
    
    let mut backtest2 = HyperliquidBacktest::new(
        data.clone(),
        strategy2_name.clone(),
        initial_capital,
        commission.clone(),
    );
    
    // Initialize base backtests
    backtest1.initialize_base_backtest()?;
    backtest2.initialize_base_backtest()?;
    
    // Create position arrays
    let positions1 = generate_position_sequence(data.len(), "constant_long");
    let positions2 = generate_position_sequence(data.len(), "alternating");
    
    // Calculate with funding and positions
    backtest1.calculate_with_funding_and_positions(&positions1)?;
    backtest2.calculate_with_funding_and_positions(&positions2)?;
    
    // Create strategy comparison data
    let comparison = StrategyComparisonData {
        strategies: vec![backtest1, backtest2],
    };
    
    // Create a temporary file path for testing
    let temp_file = "test_strategy_comparison.csv";
    
    // Export strategy comparison to CSV
    comparison.export_to_csv(temp_file)?;
    
    // Verify that the file was created
    assert!(Path::new(temp_file).exists());
    
    // Read the file content
    let mut file = fs::File::open(temp_file)?;
    let mut content = String::new();
    file.read_to_string(&mut content)?;
    
    // Verify that the file contains expected headers
    assert!(content.contains("timestamp"));
    assert!(content.contains("price"));
    assert!(content.contains("Strategy 1_position"));
    assert!(content.contains("Strategy 1_equity"));
    assert!(content.contains("Strategy 2_position"));
    assert!(content.contains("Strategy 2_equity"));
    
    // Clean up the test file
    fs::remove_file(temp_file)?;
    
    Ok(())
}

/// Test that funding rate history can be exported to CSV
#[test]
fn test_export_funding_rate_history() -> Result<()> {
    // Create test data
    let data = generate_mock_data("BTC", 72, true, false);
    
    // Create a temporary file path for testing
    let temp_file = "test_funding_history.csv";
    
    // Export funding rate history to CSV
    export_funding_rate_history(&data, temp_file)?;
    
    // Verify that the file was created
    assert!(Path::new(temp_file).exists());
    
    // Read the file content
    let mut file = fs::File::open(temp_file)?;
    let mut content = String::new();
    file.read_to_string(&mut content)?;
    
    // Verify that the file contains expected headers
    assert!(content.contains("timestamp"));
    assert!(content.contains("funding_rate"));
    
    // Clean up the test file
    fs::remove_file(temp_file)?;
    
    Ok(())
}

/// Test that CSV export handles edge cases correctly
#[test]
fn test_csv_export_edge_cases() -> Result<()> {
    // Test with empty data
    let empty_data = HyperliquidData {
        symbol: "BTC".to_string(),
        datetime: Vec::new(),
        open: Vec::new(),
        high: Vec::new(),
        low: Vec::new(),
        close: Vec::new(),
        volume: Vec::new(),
        funding_rates: Vec::new(),
    };
    
    let strategy_name = "Empty Strategy".to_string();
    let initial_capital = 10000.0;
    let commission = HyperliquidCommission::default();
    
    let mut empty_backtest = HyperliquidBacktest::new(
        empty_data.clone(),
        strategy_name.clone(),
        initial_capital,
        commission.clone(),
    );
    
    // Initialize base backtest
    empty_backtest.initialize_base_backtest()?;
    
    // Create a temporary file path for testing
    let temp_file = "test_empty_export.csv";
    
    // Export to CSV
    empty_backtest.export_to_csv(temp_file)?;
    
    // Verify that the file was created
    assert!(Path::new(temp_file).exists());
    
    // Read the file content
    let mut file = fs::File::open(temp_file)?;
    let mut content = String::new();
    file.read_to_string(&mut content)?;
    
    // Verify that the file contains headers but no data rows
    assert!(content.contains("timestamp"));
    assert_eq!(content.lines().count(), 1); // Only header line
    
    // Clean up the test file
    fs::remove_file(temp_file)?;
    
    // Test with empty funding payments
    let empty_payments: Vec<FundingPayment> = Vec::new();
    let temp_file = "test_empty_payments.csv";
    
    // Export empty funding payments to CSV
    export_funding_payments_to_csv(&empty_payments, temp_file)?;
    
    // Verify that the file was created
    assert!(Path::new(temp_file).exists());
    
    // Read the file content
    let mut file = fs::File::open(temp_file)?;
    let mut content = String::new();
    file.read_to_string(&mut content)?;
    
    // Verify that the file contains headers but no data rows
    assert!(content.contains("timestamp"));
    assert_eq!(content.lines().count(), 1); // Only header line
    
    // Clean up the test file
    fs::remove_file(temp_file)?;
    
    Ok(())
}

/// Test that CSV export handles invalid file paths
#[test]
fn test_csv_export_invalid_path() {
    // Create test data
    let data = generate_mock_data("BTC", 72, true, false);
    let strategy_name = "Test Strategy".to_string();
    let initial_capital = 10000.0;
    let commission = HyperliquidCommission::default();
    
    let mut backtest = HyperliquidBacktest::new(
        data.clone(),
        strategy_name.clone(),
        initial_capital,
        commission.clone(),
    );
    
    // Initialize base backtest
    backtest.initialize_base_backtest().unwrap();
    
    // Try to export to an invalid path
    let invalid_path = "/invalid/path/that/does/not/exist/file.csv";
    let result = backtest.export_to_csv(invalid_path);
    
    // Should fail with IO error
    assert!(result.is_err());
    if let Err(e) = result {
        match e {
            crate::errors::HyperliquidBacktestError::Io(_) => {}, // Expected
            _ => panic!("Expected IO error"),
        }
    }
}

/// Test that CSV export handles special characters correctly
#[test]
fn test_csv_export_special_characters() -> Result<()> {
    // Create test data with special characters in strategy name
    let data = generate_mock_data("BTC", 10, true, false);
    let strategy_name = "Strategy with, special \"characters\"".to_string();
    let initial_capital = 10000.0;
    let commission = HyperliquidCommission::default();
    
    let mut backtest = HyperliquidBacktest::new(
        data.clone(),
        strategy_name.clone(),
        initial_capital,
        commission.clone(),
    );
    
    // Initialize base backtest
    backtest.initialize_base_backtest()?;
    
    // Create position array
    let positions = vec![1.0; data.len()];
    
    // Calculate with funding and positions
    backtest.calculate_with_funding_and_positions(&positions)?;
    
    // Create a temporary file path for testing
    let temp_file = "test_special_chars.csv";
    
    // Export to CSV
    backtest.export_to_csv(temp_file)?;
    
    // Verify that the file was created
    assert!(Path::new(temp_file).exists());
    
    // Read the file content
    let mut file = fs::File::open(temp_file)?;
    let mut content = String::new();
    file.read_to_string(&mut content)?;
    
    // Verify that the file contains data
    assert!(content.contains("timestamp"));
    assert!(content.lines().count() > 1); // Header + data rows
    
    // Clean up the test file
    fs::remove_file(temp_file)?;
    
    Ok(())
}