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

use crate::strategies::trading_strategy::{
    TradingStrategy, StrategyConfig, StrategyState, BaseTradingStrategy
};
use crate::unified_data_impl::{
    MarketData, OrderRequest, OrderResult, Signal, FundingPayment,
    SignalDirection, OrderSide, OrderType
};
use crate::strategies::{FundingAwareConfig, SignalStrength, TradingSignal};

/// Enhanced SMA cross strategy with funding awareness that implements the TradingStrategy trait
pub struct EnhancedSmaStrategy {
    /// Base strategy implementation
    base: BaseTradingStrategy,
    
    /// Fast period for SMA
    fast_period: usize,
    
    /// Slow period for SMA
    slow_period: usize,
    
    /// Funding-aware configuration
    funding_config: FundingAwareConfig,
    
    /// Current signals by symbol
    signals: HashMap<String, Signal>,
    
    /// Price history by symbol
    price_history: HashMap<String, Vec<f64>>,
    
    /// Last funding rates by symbol
    last_funding_rates: HashMap<String, f64>,
}

impl EnhancedSmaStrategy {
    /// Create a new EnhancedSmaStrategy
    pub fn new(fast_period: usize, slow_period: usize) -> Self {
        if fast_period >= slow_period {
            panic!("Fast period must be less than slow period");
        }
        
        let config = StrategyConfig::new(
            "EnhancedSmaStrategy",
            "Enhanced SMA cross strategy with funding awareness",
            "1.0.0",
        )
        .with_number_param("fast_period", fast_period as f64)
        .with_number_param("slow_period", slow_period as f64)
        .with_number_param("funding_weight", 0.5)
        .with_number_param("funding_threshold", 0.0001)
        .with_bool_param("use_funding_direction", true)
        .with_bool_param("use_funding_prediction", false);
        
        let mut base = BaseTradingStrategy::new(
            "EnhancedSmaStrategy",
            "Enhanced SMA cross strategy with funding awareness",
            "1.0.0",
        );
        
        // Update the base strategy with our config
        base.update_config(config).expect("Failed to update config");
        
        Self {
            base,
            fast_period,
            slow_period,
            funding_config: FundingAwareConfig::default(),
            signals: HashMap::new(),
            price_history: HashMap::new(),
            last_funding_rates: HashMap::new(),
        }
    }
    
    /// Calculate SMA for a given period
    fn calculate_sma(&self, prices: &[f64], period: usize) -> Option<f64> {
        if prices.len() < period {
            return None;
        }
        
        let sum: f64 = prices[prices.len() - period..].iter().sum();
        Some(sum / period as f64)
    }
    
    /// Process funding rate to generate a trading signal
    fn process_funding(&self, funding_rate: f64) -> TradingSignal {
        if !self.funding_config.use_funding_direction || 
           funding_rate.abs() <= self.funding_config.funding_threshold {
            return TradingSignal::new(0.0, SignalStrength::Weak);
        }
        
        let position = if funding_rate > 0.0 { 1.0 } else { -1.0 };
        let strength = if funding_rate.abs() > self.funding_config.funding_threshold * 2.0 {
            SignalStrength::Medium
        } else {
            SignalStrength::Weak
        };
        
        TradingSignal::new(position, strength)
    }
    
    /// Combine base signal with funding signal
    fn combine_signals(&self, base_signal: f64, funding_signal: &TradingSignal) -> f64 {
        if funding_signal.is_neutral() {
            return base_signal;
        }
        
        // If signals agree, strengthen the position
        if (base_signal > 0.0 && funding_signal.is_long()) || 
           (base_signal < 0.0 && funding_signal.is_short()) {
            return base_signal;
        }
        
        // If signals disagree, reduce the position based on funding strength
        let weight = match funding_signal.strength {
            SignalStrength::Strong => self.funding_config.funding_weight,
            SignalStrength::Medium => self.funding_config.funding_weight * 0.5,
            SignalStrength::Weak => self.funding_config.funding_weight * 0.2,
        };
        
        base_signal * (1.0 - weight)
    }
    
    /// Convert internal trading signal to unified Signal
    fn convert_to_signal(&self, symbol: &str, position: f64, timestamp: DateTime<FixedOffset>) -> Signal {
        let direction = if position > 0.0 {
            SignalDirection::Buy
        } else if position < 0.0 {
            SignalDirection::Sell
        } else {
            SignalDirection::Neutral
        };
        
        let strength = position.abs();
        
        let mut metadata = HashMap::new();
        metadata.insert("strategy_type".to_string(), "enhanced_sma_cross".to_string());
        metadata.insert("fast_period".to_string(), self.fast_period.to_string());
        metadata.insert("slow_period".to_string(), self.slow_period.to_string());
        
        Signal {
            symbol: symbol.to_string(),
            direction,
            strength,
            timestamp,
            metadata,
        }
    }
}

impl TradingStrategy for EnhancedSmaStrategy {
    fn name(&self) -> &str {
        self.base.name()
    }
    
    fn config(&self) -> &StrategyConfig {
        self.base.config()
    }
    
    fn config_mut(&mut self) -> &mut StrategyConfig {
        self.base.config_mut()
    }
    
    fn update_config(&mut self, config: StrategyConfig) -> Result<(), String> {
        // Update periods if present in the new config
        if let Some(fast_period) = config.get_number("fast_period") {
            self.fast_period = fast_period as usize;
        }
        
        if let Some(slow_period) = config.get_number("slow_period") {
            self.slow_period = slow_period as usize;
        }
        
        // Validate periods
        if self.fast_period >= self.slow_period {
            return Err("Fast period must be less than slow period".to_string());
        }
        
        // Update funding config if parameters are present
        let mut funding_config = self.funding_config.clone();
        
        if let Some(funding_weight) = config.get_number("funding_weight") {
            funding_config.funding_weight = funding_weight;
        }
        
        if let Some(funding_threshold) = config.get_number("funding_threshold") {
            funding_config.funding_threshold = funding_threshold;
        }
        
        if let Some(use_funding_direction) = config.get_bool("use_funding_direction") {
            funding_config.use_funding_direction = use_funding_direction;
        }
        
        if let Some(use_funding_prediction) = config.get_bool("use_funding_prediction") {
            funding_config.use_funding_prediction = use_funding_prediction;
        }
        
        self.funding_config = funding_config;
        
        // Update the base strategy config
        self.base.update_config(config)
    }
    
    fn state(&self) -> &StrategyState {
        self.base.state()
    }
    
    fn state_mut(&mut self) -> &mut StrategyState {
        self.base.state_mut()
    }
    
    fn on_market_data(&mut self, data: &MarketData) -> Result<Vec<OrderRequest>, String> {
        // Update price history
        let prices = self.price_history
            .entry(data.symbol.clone())
            .or_insert_with(Vec::new);
        
        prices.push(data.price);
        
        // Ensure we have enough price history
        if prices.len() < self.slow_period {
            debug!(
                "Not enough price history for {} ({}/{})",
                data.symbol,
                prices.len(),
                self.slow_period
            );
            return Ok(vec![]);
        }
        
        // Calculate SMAs
        let fast_sma = match self.calculate_sma(prices, self.fast_period) {
            Some(sma) => sma,
            None => {
                debug!("Failed to calculate fast SMA for {}", data.symbol);
                return Ok(vec![]);
            }
        };
        
        let slow_sma = match self.calculate_sma(prices, self.slow_period) {
            Some(sma) => sma,
            None => {
                debug!("Failed to calculate slow SMA for {}", data.symbol);
                return Ok(vec![]);
            }
        };
        
        // Calculate base signal from SMA cross
        let base_signal = if fast_sma > slow_sma {
            1.0 // Long signal
        } else if fast_sma < slow_sma {
            -1.0 // Short signal
        } else {
            0.0 // Neutral
        };
        
        // Get funding rate from market data
        let funding_signal = if let Some(funding_rate) = data.funding_rate {
            // Store the funding rate for future reference
            self.last_funding_rates.insert(data.symbol.clone(), funding_rate);
            
            // Process funding rate
            self.process_funding(funding_rate)
        } else if let Some(rate) = self.last_funding_rates.get(&data.symbol) {
            // Use stored funding rate
            self.process_funding(*rate)
        } else {
            // No funding rate available
            TradingSignal::new(0.0, SignalStrength::Weak)
        };
        
        // Combine signals
        let final_position = self.combine_signals(base_signal, &funding_signal);
        
        // Convert to unified Signal and store
        let signal = self.convert_to_signal(&data.symbol, final_position, data.timestamp);
        self.signals.insert(data.symbol.clone(), signal.clone());
        
        // Update state with current metrics
        self.state_mut().set_metric(&format!("{}_fast_sma", data.symbol), fast_sma);
        self.state_mut().set_metric(&format!("{}_slow_sma", data.symbol), slow_sma);
        self.state_mut().set_metric(&format!("{}_base_signal", data.symbol), base_signal);
        self.state_mut().set_metric(&format!("{}_final_signal", data.symbol), final_position);
        
        if let Some(funding_rate) = data.funding_rate {
            self.state_mut().set_metric(&format!("{}_funding_rate", data.symbol), funding_rate);
        }
        
        // Update state with signal
        self.state_mut().set_signal(&data.symbol, if final_position > 0.0 {
            "long"
        } else if final_position < 0.0 {
            "short"
        } else {
            "neutral"
        });
        
        // Generate order requests based on the final position
        let orders = if final_position != 0.0 {
            // Calculate position size (absolute value of final_position, capped at 1.0)
            let position_size = final_position.abs().min(1.0);
            
            // Create order request
            let side = if final_position > 0.0 {
                OrderSide::Buy
            } else {
                OrderSide::Sell
            };
            
            vec![OrderRequest::market(&data.symbol, side, position_size)]
        } else {
            // No signal, no orders
            vec![]
        };
        
        // Update state timestamp
        self.state_mut().update_timestamp(data.timestamp);
        
        Ok(orders)
    }
    
    fn on_order_fill(&mut self, fill: &OrderResult) -> Result<(), String> {
        // Update position in state
        let position_size = match fill.side {
            OrderSide::Buy => fill.filled_quantity,
            OrderSide::Sell => -fill.filled_quantity,
        };
        
        // Get current position or default to 0.0
        let current_position = self.state().positions.get(&fill.symbol).copied().unwrap_or(0.0);
        
        // Update position
        self.state_mut().set_position(&fill.symbol, current_position + position_size);
        
        // Update metrics
        if let Some(price) = fill.average_price {
            self.state_mut().set_metric(&format!("{}_last_fill_price", fill.symbol), price);
        }
        
        if let Some(fees) = fill.fees {
            // Get current fees or default to 0.0
            let current_fees = self.state().metrics.get("total_fees").copied().unwrap_or(0.0);
            self.state_mut().set_metric("total_fees", current_fees + fees);
        }
        
        info!(
            "Order filled: {} {} {} at {} with size {}",
            fill.symbol,
            fill.side,
            fill.order_type,
            fill.average_price.unwrap_or(0.0),
            fill.filled_quantity
        );
        
        Ok(())
    }
    
    fn on_funding_payment(&mut self, payment: &FundingPayment) -> Result<(), String> {
        // Update funding metrics in state
        let key = format!("{}_funding_payment", payment.symbol);
        
        // Get current funding payment or default to 0.0
        let current_payment = self.state().metrics.get(&key).copied().unwrap_or(0.0);
        
        // Update with new payment
        self.state_mut().set_metric(&key, current_payment + payment.amount);
        
        // Update total funding metrics
        let total_key = if payment.amount > 0.0 {
            "total_funding_received"
        } else {
            "total_funding_paid"
        };
        
        let current_total = self.state().metrics.get(total_key).copied().unwrap_or(0.0);
        self.state_mut().set_metric(total_key, current_total + payment.amount.abs());
        
        info!(
            "Funding payment for {}: rate={}, amount={}, position={}",
            payment.symbol,
            payment.rate,
            payment.amount,
            payment.position_size
        );
        
        Ok(())
    }
    
    fn get_current_signals(&self) -> HashMap<String, Signal> {
        self.signals.clone()
    }
    
    fn initialize(&mut self) -> Result<(), String> {
        info!(
            "Initializing EnhancedSmaStrategy with fast_period={}, slow_period={}",
            self.fast_period,
            self.slow_period
        );
        
        // Clear price history
        self.price_history.clear();
        
        Ok(())
    }
    
    fn shutdown(&mut self) -> Result<(), String> {
        info!("Shutting down EnhancedSmaStrategy");
        Ok(())
    }
}

/// Create a new enhanced SMA cross strategy with the specified parameters
pub fn create_enhanced_sma_strategy(
    fast_period: usize,
    slow_period: usize,
    funding_config: Option<FundingAwareConfig>,
) -> Result<Box<dyn TradingStrategy>, String> {
    if fast_period >= slow_period {
        return Err("Fast period must be less than slow period".to_string());
    }
    
    let mut strategy = EnhancedSmaStrategy::new(fast_period, slow_period);
    
    // Apply custom funding config if provided
    if let Some(config) = funding_config {
        strategy.funding_config = config;
    }
    
    Ok(Box::new(strategy))
}