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
use std::collections::HashMap;
use chrono::{DateTime, FixedOffset, Utc};

use crate::risk_manager::{RiskManager, RiskError, RiskOrder};
use crate::trading_mode::RiskConfig;
use crate::trading_mode_impl::{Position, OrderRequest, OrderSide, OrderType, TimeInForce};

// Helper function to create a test position
fn create_test_position(symbol: &str, size: f64, entry_price: f64, current_price: f64) -> Position {
    Position {
        symbol: symbol.to_string(),
        size,
        entry_price,
        current_price,
        unrealized_pnl: (current_price - entry_price) * size,
        realized_pnl: 0.0,
        funding_pnl: 0.0,
        timestamp: Utc::now().with_timezone(&FixedOffset::east(0)),
    }
}

// Helper function to create a test order
fn create_test_order(symbol: &str, side: OrderSide, quantity: f64, price: Option<f64>) -> OrderRequest {
    OrderRequest {
        symbol: symbol.to_string(),
        side,
        order_type: OrderType::Limit,
        quantity,
        price,
        reduce_only: false,
        time_in_force: TimeInForce::GoodTillCancel,
    }
}

#[test]
fn test_risk_manager_creation() {
    let config = RiskConfig::default();
    let portfolio_value = 10000.0;
    let risk_manager = RiskManager::new(config, portfolio_value);
    
    assert_eq!(risk_manager.config().max_position_size_pct, 0.1);
    assert_eq!(risk_manager.config().max_daily_loss_pct, 0.02);
    assert_eq!(risk_manager.config().stop_loss_pct, 0.05);
    assert_eq!(risk_manager.config().take_profit_pct, 0.1);
    assert_eq!(risk_manager.config().max_leverage, 3.0);
}

#[test]
fn test_position_size_validation() {
    let config = RiskConfig {
        max_position_size_pct: 0.1,  // 10% of portfolio
        max_daily_loss_pct: 0.02,    // 2% max daily loss
        stop_loss_pct: 0.05,         // 5% stop loss
        take_profit_pct: 0.1,        // 10% take profit
        max_leverage: 3.0,           // 3x max leverage
        max_concentration_pct: 0.25, // 25% max concentration in one asset class
        max_position_correlation: 0.7, // 0.7 maximum correlation between positions
        max_portfolio_volatility_pct: 0.2, // 20% maximum portfolio volatility
        volatility_sizing_factor: 0.5, // 50% volatility-based position sizing
        max_drawdown_pct: 0.15,     // 15% maximum drawdown before emergency stop
    };
    
    let portfolio_value = 10000.0;
    let mut risk_manager = RiskManager::new(config, portfolio_value);
    
    let mut positions = HashMap::new();
    
    // Test valid order within position size limit
    let order = create_test_order("BTC", OrderSide::Buy, 0.1, Some(9000.0));
    // Order value: 0.1 * 9000 = 900, which is < 10% of 10000 (1000)
    assert!(risk_manager.validate_order(&order, &positions).is_ok());
    
    // Test order exceeding position size limit
    let order = create_test_order("BTC", OrderSide::Buy, 0.2, Some(9000.0));
    // Order value: 0.2 * 9000 = 1800, which is > 10% of 10000 (1000)
    assert!(risk_manager.validate_order(&order, &positions).is_err());
    
    // Test with existing position
    positions.insert(
        "BTC".to_string(),
        create_test_position("BTC", 0.05, 8000.0, 9000.0)
    );
    
    // Test valid order with existing position
    let order = create_test_order("BTC", OrderSide::Buy, 0.05, Some(9000.0));
    // Existing position value: 0.05 * 9000 = 450
    // Order value: 0.05 * 9000 = 450
    // Total: 900, which is < 10% of 10000 (1000)
    assert!(risk_manager.validate_order(&order, &positions).is_ok());
    
    // Test order exceeding position size limit with existing position
    let order = create_test_order("BTC", OrderSide::Buy, 0.07, Some(9000.0));
    // Existing position value: 0.05 * 9000 = 450
    // Order value: 0.07 * 9000 = 630
    // Total: 1080, which is > 10% of 10000 (1000)
    assert!(risk_manager.validate_order(&order, &positions).is_err());
}

#[test]
fn test_leverage_validation() {
    let config = RiskConfig {
        max_position_size_pct: 0.5,  // 50% of portfolio (high to test leverage)
        max_daily_loss_pct: 0.02,    // 2% max daily loss
        stop_loss_pct: 0.05,         // 5% stop loss
        take_profit_pct: 0.1,        // 10% take profit
        max_leverage: 2.0,           // 2x max leverage
        max_concentration_pct: 0.25, // 25% max concentration in one asset class
        max_position_correlation: 0.7, // 0.7 maximum correlation between positions
        max_portfolio_volatility_pct: 0.2, // 20% maximum portfolio volatility
        volatility_sizing_factor: 0.5, // 50% volatility-based position sizing
        max_drawdown_pct: 0.15,     // 15% maximum drawdown before emergency stop
    };
    
    let portfolio_value = 10000.0;
    let mut risk_manager = RiskManager::new(config, portfolio_value);
    
    let mut positions = HashMap::new();
    positions.insert(
        "ETH".to_string(),
        create_test_position("ETH", 2.0, 1500.0, 1600.0)
    );
    // ETH position value: 2.0 * 1600 = 3200
    
    // Test valid order within leverage limit
    let order = create_test_order("BTC", OrderSide::Buy, 0.1, Some(9000.0));
    // Order value: 0.1 * 9000 = 900
    // Total position value: 3200 + 900 = 4100
    // Leverage: 4100 / 10000 = 0.41, which is < 2.0
    assert!(risk_manager.validate_order(&order, &positions).is_ok());
    
    // Test order exceeding leverage limit
    let order = create_test_order("BTC", OrderSide::Buy, 2.0, Some(9000.0));
    // Order value: 2.0 * 9000 = 18000
    // Total position value: 3200 + 18000 = 21200
    // Leverage: 21200 / 10000 = 2.12, which is > 2.0
    assert!(risk_manager.validate_order(&order, &positions).is_err());
}

#[test]
fn test_daily_loss_limit() {
    let config = RiskConfig {
        max_position_size_pct: 0.1,  // 10% of portfolio
        max_daily_loss_pct: 2.0,     // 2% max daily loss
        stop_loss_pct: 0.05,         // 5% stop loss
        take_profit_pct: 0.1,        // 10% take profit
        max_leverage: 3.0,           // 3x max leverage
        max_concentration_pct: 0.25, // 25% max concentration in one asset class
        max_position_correlation: 0.7, // 0.7 maximum correlation between positions
        max_portfolio_volatility_pct: 0.2, // 20% maximum portfolio volatility
        volatility_sizing_factor: 0.5, // 50% volatility-based position sizing
        max_drawdown_pct: 0.15,     // 15% maximum drawdown before emergency stop
    };
    
    let portfolio_value = 10000.0;
    let mut risk_manager = RiskManager::new(config, portfolio_value);
    
    // Update portfolio value with small loss (1%)
    assert!(risk_manager.update_portfolio_value(9900.0, -100.0).is_ok());
    
    // Verify daily loss is tracked correctly
    let (daily_loss_pct, _, _) = risk_manager.daily_risk_metrics();
    assert_eq!(daily_loss_pct, 1.0);
    
    // Update portfolio value with loss exceeding daily limit (3% total)
    assert!(risk_manager.update_portfolio_value(9700.0, -200.0).is_err());
    
    // Verify trading should be stopped
    assert!(risk_manager.should_stop_trading());
}

#[test]
fn test_stop_loss_generation() {
    let config = RiskConfig {
        max_position_size_pct: 0.1,  // 10% of portfolio
        max_daily_loss_pct: 2.0,     // 2% max daily loss
        stop_loss_pct: 0.05,         // 5% stop loss
        take_profit_pct: 0.1,        // 10% take profit
        max_leverage: 3.0,           // 3x max leverage
        max_concentration_pct: 0.25, // 25% max concentration in one asset class
        max_position_correlation: 0.7, // 0.7 maximum correlation between positions
        max_portfolio_volatility_pct: 0.2, // 20% maximum portfolio volatility
        volatility_sizing_factor: 0.5, // 50% volatility-based position sizing
        max_drawdown_pct: 0.15,     // 15% maximum drawdown before emergency stop
    };
    
    let portfolio_value = 10000.0;
    let risk_manager = RiskManager::new(config, portfolio_value);
    
    // Test stop loss for long position
    let long_position = create_test_position("BTC", 0.1, 10000.0, 10000.0);
    let stop_loss = risk_manager.generate_stop_loss(&long_position, "order1").unwrap();
    
    assert_eq!(stop_loss.symbol, "BTC");
    assert!(matches!(stop_loss.side, OrderSide::Sell));
    assert!(matches!(stop_loss.order_type, OrderType::StopMarket));
    assert_eq!(stop_loss.quantity, 0.1);
    assert_eq!(stop_loss.trigger_price, 9500.0); // 5% below entry price
    
    // Test stop loss for short position
    let short_position = create_test_position("BTC", -0.1, 10000.0, 10000.0);
    let stop_loss = risk_manager.generate_stop_loss(&short_position, "order2").unwrap();
    
    assert_eq!(stop_loss.symbol, "BTC");
    assert!(matches!(stop_loss.side, OrderSide::Buy));
    assert!(matches!(stop_loss.order_type, OrderType::StopMarket));
    assert_eq!(stop_loss.quantity, 0.1);
    assert_eq!(stop_loss.trigger_price, 10500.0); // 5% above entry price
}

#[test]
fn test_take_profit_generation() {
    let config = RiskConfig {
        max_position_size_pct: 0.1,  // 10% of portfolio
        max_daily_loss_pct: 2.0,     // 2% max daily loss
        stop_loss_pct: 0.05,         // 5% stop loss
        take_profit_pct: 0.1,        // 10% take profit
        max_leverage: 3.0,           // 3x max leverage
        max_concentration_pct: 0.25, // 25% max concentration in one asset class
        max_position_correlation: 0.7, // 0.7 maximum correlation between positions
        max_portfolio_volatility_pct: 0.2, // 20% maximum portfolio volatility
        volatility_sizing_factor: 0.5, // 50% volatility-based position sizing
        max_drawdown_pct: 0.15,     // 15% maximum drawdown before emergency stop
    };
    
    let portfolio_value = 10000.0;
    let risk_manager = RiskManager::new(config, portfolio_value);
    
    // Test take profit for long position
    let long_position = create_test_position("BTC", 0.1, 10000.0, 10000.0);
    let take_profit = risk_manager.generate_take_profit(&long_position, "order1").unwrap();
    
    assert_eq!(take_profit.symbol, "BTC");
    assert!(matches!(take_profit.side, OrderSide::Sell));
    assert!(matches!(take_profit.order_type, OrderType::TakeProfitMarket));
    assert_eq!(take_profit.quantity, 0.1);
    assert_eq!(take_profit.trigger_price, 11000.0); // 10% above entry price
    
    // Test take profit for short position
    let short_position = create_test_position("BTC", -0.1, 10000.0, 10000.0);
    let take_profit = risk_manager.generate_take_profit(&short_position, "order2").unwrap();
    
    assert_eq!(take_profit.symbol, "BTC");
    assert!(matches!(take_profit.side, OrderSide::Buy));
    assert!(matches!(take_profit.order_type, OrderType::TakeProfitMarket));
    assert_eq!(take_profit.quantity, 0.1);
    assert_eq!(take_profit.trigger_price, 9000.0); // 10% below entry price
}

#[test]
fn test_risk_orders_triggering() {
    let config = RiskConfig {
        max_position_size_pct: 0.1,
        max_daily_loss_pct: 2.0,
        stop_loss_pct: 0.05,
        take_profit_pct: 0.1,
        max_leverage: 3.0,
        max_concentration_pct: 0.25,
        max_position_correlation: 0.7,
        max_portfolio_volatility_pct: 0.2,
        volatility_sizing_factor: 0.5,
        max_drawdown_pct: 0.15,
        max_concentration_pct: 0.25,
        max_position_correlation: 0.7,
        max_portfolio_volatility_pct: 0.2,
        volatility_sizing_factor: 0.5,
        max_drawdown_pct: 0.15,
    };
    
    let portfolio_value = 10000.0;
    let mut risk_manager = RiskManager::new(config, portfolio_value);
    
    // Create and register a stop loss order
    let long_position = create_test_position("BTC", 0.1, 10000.0, 10000.0);
    let stop_loss = risk_manager.generate_stop_loss(&long_position, "order1").unwrap();
    risk_manager.register_stop_loss(stop_loss);
    
    // Create and register a take profit order
    let take_profit = risk_manager.generate_take_profit(&long_position, "order1").unwrap();
    risk_manager.register_take_profit(take_profit);
    
    // Test no orders triggered at current price
    let mut current_prices = HashMap::new();
    current_prices.insert("BTC".to_string(), 10000.0);
    let triggered = risk_manager.check_risk_orders(&current_prices);
    assert_eq!(triggered.len(), 0);
    
    // Test stop loss triggered
    current_prices.insert("BTC".to_string(), 9400.0); // Below stop loss price
    let triggered = risk_manager.check_risk_orders(&current_prices);
    assert_eq!(triggered.len(), 1);
    assert!(triggered[0].is_stop_loss);
    
    // Register new orders
    let long_position = create_test_position("BTC", 0.1, 10000.0, 10000.0);
    let stop_loss = risk_manager.generate_stop_loss(&long_position, "order2").unwrap();
    risk_manager.register_stop_loss(stop_loss);
    let take_profit = risk_manager.generate_take_profit(&long_position, "order2").unwrap();
    risk_manager.register_take_profit(take_profit);
    
    // Test take profit triggered
    current_prices.insert("BTC".to_string(), 11100.0); // Above take profit price
    let triggered = risk_manager.check_risk_orders(&current_prices);
    assert_eq!(triggered.len(), 1);
    assert!(triggered[0].is_take_profit);
}

#[test]
fn test_emergency_stop() {
    let config = RiskConfig::default();
    let portfolio_value = 10000.0;
    let mut risk_manager = RiskManager::new(config, portfolio_value);
    
    // Initially, emergency stop should be false
    assert!(!risk_manager.should_stop_trading());
    
    // Activate emergency stop
    risk_manager.activate_emergency_stop();
    assert!(risk_manager.should_stop_trading());
    
    // Orders should be rejected when emergency stop is active
    let positions = HashMap::new();
    let order = create_test_order("BTC", OrderSide::Buy, 0.1, Some(10000.0));
    assert!(risk_manager.validate_order(&order, &positions).is_err());
    
    // Deactivate emergency stop
    risk_manager.deactivate_emergency_stop();
    assert!(!risk_manager.should_stop_trading());
    
    // Orders should be accepted again
    assert!(risk_manager.validate_order(&order, &positions).is_ok());
}

#[test]
fn test_margin_requirements() {
    let config = RiskConfig {
        max_position_size_pct: 0.5,
        max_daily_loss_pct: 2.0,
        stop_loss_pct: 0.05,
        take_profit_pct: 0.1,
        max_leverage: 5.0,  // 5x max leverage
        max_concentration_pct: 0.25,
        max_position_correlation: 0.7,
        max_portfolio_volatility_pct: 0.2,
        volatility_sizing_factor: 0.5,
        max_drawdown_pct: 0.15,
    };
    
    let portfolio_value = 10000.0;
    let mut risk_manager = RiskManager::new(config, portfolio_value);
    
    // Test margin calculation
    let position_value = 20000.0;
    let required_margin = risk_manager.calculate_required_margin(position_value);
    assert_eq!(required_margin, 4000.0); // 20000 / 5 = 4000
    
    // Test with insufficient margin
    risk_manager.update_available_margin(3000.0);
    
    let positions = HashMap::new();
    let order = create_test_order("BTC", OrderSide::Buy, 0.5, Some(10000.0));
    // Order value: 0.5 * 10000 = 5000
    // Required margin: 5000 / 5 = 1000
    // Available margin: 3000
    assert!(risk_manager.validate_order(&order, &positions).is_ok());
    
    // Test with insufficient margin
    let order = create_test_order("BTC", OrderSide::Buy, 2.0, Some(10000.0));
    // Order value: 2.0 * 10000 = 20000
    // Required margin: 20000 / 5 = 4000
    // Available margin: 3000
    assert!(risk_manager.validate_order(&order, &positions).is_err());
}

#[test]
fn test_risk_config_update() {
    let config = RiskConfig::default();
    let portfolio_value = 10000.0;
    let mut risk_manager = RiskManager::new(config, portfolio_value);
    
    // Initial config
    assert_eq!(risk_manager.config().max_position_size_pct, 0.1);
    
    // Update config
    let new_config = RiskConfig {
        max_position_size_pct: 0.2,
        max_daily_loss_pct: 0.03,
        stop_loss_pct: 0.07,
        take_profit_pct: 0.15,
        max_leverage: 4.0,
        max_concentration_pct: 0.3,
        max_position_correlation: 0.8,
        max_portfolio_volatility_pct: 0.25,
        volatility_sizing_factor: 0.6,
        max_drawdown_pct: 0.2,
    };
    
    risk_manager.update_config(new_config);
    
    // Verify updated config
    assert_eq!(risk_manager.config().max_position_size_pct, 0.2);
    assert_eq!(risk_manager.config().max_daily_loss_pct, 0.03);
    assert_eq!(risk_manager.config().stop_loss_pct, 0.07);
    assert_eq!(risk_manager.config().take_profit_pct, 0.15);
    assert_eq!(risk_manager.config().max_leverage, 4.0);
}