clmm-lp-optimization 0.2.0

Liquidity Provider Strategy Optimizer for Solana CLMMs
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
//! Parameter optimization for rebalancing strategies.
//!
//! This module provides optimization for strategy parameters such as
//! rebalancing thresholds, intervals, and IL limits.

use crate::constraints::RebalanceConstraints;
use crate::objective::ObjectiveFunction;
use crate::optimizer::OptimizationConfig;
use rust_decimal::Decimal;
use rust_decimal::prelude::{FromPrimitive, ToPrimitive};

/// Parameters for a threshold-based rebalancing strategy.
#[derive(Debug, Clone)]
pub struct ThresholdParams {
    /// Price deviation threshold to trigger rebalance.
    pub price_threshold: Decimal,
    /// IL threshold to trigger rebalance.
    pub il_threshold: Decimal,
    /// Whether to rebalance when out of range.
    pub rebalance_on_out_of_range: bool,
}

impl Default for ThresholdParams {
    fn default() -> Self {
        Self {
            price_threshold: Decimal::from_f64(0.05).unwrap(), // 5%
            il_threshold: Decimal::from_f64(0.03).unwrap(),    // 3%
            rebalance_on_out_of_range: true,
        }
    }
}

/// Parameters for a periodic rebalancing strategy.
#[derive(Debug, Clone)]
pub struct PeriodicParams {
    /// Interval between rebalances in steps.
    pub interval: u64,
    /// Whether to only rebalance when out of range.
    pub only_when_out_of_range: bool,
}

impl Default for PeriodicParams {
    fn default() -> Self {
        Self {
            interval: 24, // Daily
            only_when_out_of_range: false,
        }
    }
}

/// Parameters for an IL-limit strategy.
#[derive(Debug, Clone)]
pub struct ILLimitParams {
    /// Maximum IL before rebalancing.
    pub max_il: Decimal,
    /// Maximum IL before closing position.
    pub close_il: Option<Decimal>,
    /// Grace period in steps before acting.
    pub grace_period: u64,
}

impl Default for ILLimitParams {
    fn default() -> Self {
        Self {
            max_il: Decimal::from_f64(0.05).unwrap(),         // 5%
            close_il: Some(Decimal::from_f64(0.10).unwrap()), // 10%
            grace_period: 0,
        }
    }
}

/// Result of parameter optimization.
#[derive(Debug, Clone)]
pub struct ParameterOptimizationResult {
    /// Best threshold parameters found.
    pub threshold_params: Option<ThresholdParams>,
    /// Best periodic parameters found.
    pub periodic_params: Option<PeriodicParams>,
    /// Best IL limit parameters found.
    pub il_limit_params: Option<ILLimitParams>,
    /// Expected performance metrics.
    pub expected_fees: Decimal,
    /// Expected IL.
    pub expected_il: Decimal,
    /// Expected net PnL.
    pub expected_net_pnl: Decimal,
    /// Number of expected rebalances.
    pub expected_rebalances: u32,
    /// Score from objective function.
    pub score: Decimal,
}

/// Optimizer for rebalancing strategy parameters.
#[derive(Debug, Clone)]
pub struct ParameterOptimizer {
    /// Constraints for parameter search.
    pub constraints: RebalanceConstraints,
    /// Grid of price thresholds to search.
    price_thresholds: Vec<Decimal>,
    /// Grid of IL thresholds to search.
    il_thresholds: Vec<Decimal>,
    /// Grid of intervals to search.
    intervals: Vec<u64>,
}

impl Default for ParameterOptimizer {
    fn default() -> Self {
        Self::new()
    }
}

impl ParameterOptimizer {
    /// Creates a new parameter optimizer with default grids.
    #[must_use]
    pub fn new() -> Self {
        Self {
            constraints: RebalanceConstraints::default(),
            price_thresholds: vec![0.02, 0.03, 0.05, 0.07, 0.10, 0.15]
                .into_iter()
                .filter_map(Decimal::from_f64)
                .collect(),
            il_thresholds: vec![0.01, 0.02, 0.03, 0.05, 0.07, 0.10]
                .into_iter()
                .filter_map(Decimal::from_f64)
                .collect(),
            intervals: vec![6, 12, 24, 48, 72, 168], // 6h to 1 week
        }
    }

    /// Sets custom price threshold grid.
    #[must_use]
    pub fn with_price_thresholds(mut self, thresholds: Vec<Decimal>) -> Self {
        self.price_thresholds = thresholds;
        self
    }

    /// Sets custom IL threshold grid.
    #[must_use]
    pub fn with_il_thresholds(mut self, thresholds: Vec<Decimal>) -> Self {
        self.il_thresholds = thresholds;
        self
    }

    /// Sets custom interval grid.
    #[must_use]
    pub fn with_intervals(mut self, intervals: Vec<u64>) -> Self {
        self.intervals = intervals;
        self
    }

    /// Sets constraints.
    #[must_use]
    pub fn with_constraints(mut self, constraints: RebalanceConstraints) -> Self {
        self.constraints = constraints;
        self
    }

    /// Optimizes threshold strategy parameters.
    pub fn optimize_threshold<O: ObjectiveFunction>(
        &self,
        config: &OptimizationConfig,
        range_width: Decimal,
        objective: &O,
    ) -> Vec<ThresholdCandidate> {
        let mut candidates = Vec::new();

        for &price_threshold in &self.price_thresholds {
            if !self.constraints.is_valid_price_threshold(price_threshold) {
                continue;
            }

            for &il_threshold in &self.il_thresholds {
                if !self.constraints.is_valid_il_threshold(il_threshold) {
                    continue;
                }

                for rebalance_on_oor in [true, false] {
                    let params = ThresholdParams {
                        price_threshold,
                        il_threshold,
                        rebalance_on_out_of_range: rebalance_on_oor,
                    };

                    let result = self.estimate_threshold_performance(&params, config, range_width);

                    let sim_result = create_sim_result(&result);
                    let score = objective.evaluate(&sim_result);

                    candidates.push(ThresholdCandidate {
                        params,
                        expected_fees: result.0,
                        expected_il: result.1,
                        expected_rebalances: result.2,
                        score,
                    });
                }
            }
        }

        // Sort by score descending
        candidates.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        candidates
    }

    /// Optimizes periodic strategy parameters.
    pub fn optimize_periodic<O: ObjectiveFunction>(
        &self,
        config: &OptimizationConfig,
        range_width: Decimal,
        objective: &O,
    ) -> Vec<PeriodicCandidate> {
        let mut candidates = Vec::new();

        for &interval in &self.intervals {
            if !self.constraints.is_valid_interval(interval) {
                continue;
            }

            for only_oor in [true, false] {
                let params = PeriodicParams {
                    interval,
                    only_when_out_of_range: only_oor,
                };

                let result = self.estimate_periodic_performance(&params, config, range_width);

                let sim_result = create_sim_result(&result);
                let score = objective.evaluate(&sim_result);

                candidates.push(PeriodicCandidate {
                    params,
                    expected_fees: result.0,
                    expected_il: result.1,
                    expected_rebalances: result.2,
                    score,
                });
            }
        }

        candidates.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        candidates
    }

    /// Optimizes IL limit strategy parameters.
    pub fn optimize_il_limit<O: ObjectiveFunction>(
        &self,
        config: &OptimizationConfig,
        range_width: Decimal,
        objective: &O,
    ) -> Vec<ILLimitCandidate> {
        let mut candidates = Vec::new();

        for &max_il in &self.il_thresholds {
            if !self.constraints.is_valid_il_threshold(max_il) {
                continue;
            }

            // Close IL options: None, 2x max_il, 3x max_il
            let close_options = vec![
                None,
                Some(max_il * Decimal::from(2)),
                Some(max_il * Decimal::from(3)),
            ];

            for close_il in close_options {
                for grace_period in [0, 3, 6] {
                    let params = ILLimitParams {
                        max_il,
                        close_il,
                        grace_period,
                    };

                    let result = self.estimate_il_limit_performance(&params, config, range_width);

                    let sim_result = create_sim_result(&result);
                    let score = objective.evaluate(&sim_result);

                    candidates.push(ILLimitCandidate {
                        params,
                        expected_fees: result.0,
                        expected_il: result.1,
                        expected_rebalances: result.2,
                        score,
                    });
                }
            }
        }

        candidates.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        candidates
    }

    /// Estimates performance for threshold strategy.
    fn estimate_threshold_performance(
        &self,
        params: &ThresholdParams,
        config: &OptimizationConfig,
        range_width: Decimal,
    ) -> (Decimal, Decimal, u32) {
        // Estimate number of rebalances based on volatility and thresholds
        let vol_dec = Decimal::from_f64(config.volatility).unwrap_or(Decimal::ZERO);
        let steps = config.simulation_steps as u32;

        // Higher volatility + lower threshold = more rebalances
        let rebalance_rate = vol_dec / params.price_threshold;
        let expected_rebalances = (Decimal::from(steps) * rebalance_rate / Decimal::from(10))
            .to_u32()
            .unwrap_or(0)
            .min(steps);

        // Fees: more rebalances = more time in optimal range = more fees
        let time_in_range =
            estimate_time_in_range(range_width, config.volatility, expected_rebalances);
        let base_fees = estimate_base_fees(config, range_width, time_in_range);

        // IL: rebalancing reduces IL but costs tx fees
        let base_il = estimate_base_il(range_width, config.volatility);
        let il_reduction = Decimal::from(expected_rebalances) * Decimal::from_f64(0.01).unwrap();
        let effective_il = (base_il - il_reduction).max(Decimal::ZERO);

        // Subtract tx costs
        let tx_costs = Decimal::from(expected_rebalances) * config.tx_cost;
        let net_fees = base_fees - tx_costs;

        (net_fees, effective_il, expected_rebalances)
    }

    /// Estimates performance for periodic strategy.
    fn estimate_periodic_performance(
        &self,
        params: &PeriodicParams,
        config: &OptimizationConfig,
        range_width: Decimal,
    ) -> (Decimal, Decimal, u32) {
        let steps = config.simulation_steps as u32;
        let expected_rebalances = steps / params.interval as u32;

        let time_in_range =
            estimate_time_in_range(range_width, config.volatility, expected_rebalances);
        let base_fees = estimate_base_fees(config, range_width, time_in_range);

        let base_il = estimate_base_il(range_width, config.volatility);
        let il_reduction = Decimal::from(expected_rebalances) * Decimal::from_f64(0.008).unwrap();
        let effective_il = (base_il - il_reduction).max(Decimal::ZERO);

        let tx_costs = Decimal::from(expected_rebalances) * config.tx_cost;
        let net_fees = base_fees - tx_costs;

        (net_fees, effective_il, expected_rebalances)
    }

    /// Estimates performance for IL limit strategy.
    fn estimate_il_limit_performance(
        &self,
        params: &ILLimitParams,
        config: &OptimizationConfig,
        range_width: Decimal,
    ) -> (Decimal, Decimal, u32) {
        let vol_dec = Decimal::from_f64(config.volatility).unwrap_or(Decimal::ZERO);
        let steps = config.simulation_steps as u32;

        // Rebalances triggered when IL exceeds threshold
        let il_trigger_rate = vol_dec * vol_dec / params.max_il;
        let expected_rebalances = (Decimal::from(steps) * il_trigger_rate / Decimal::from(5))
            .to_u32()
            .unwrap_or(0)
            .min(steps);

        let time_in_range =
            estimate_time_in_range(range_width, config.volatility, expected_rebalances);
        let base_fees = estimate_base_fees(config, range_width, time_in_range);

        // IL is capped at max_il due to rebalancing
        let effective_il = params.max_il;

        let tx_costs = Decimal::from(expected_rebalances) * config.tx_cost;
        let net_fees = base_fees - tx_costs;

        (net_fees, effective_il, expected_rebalances)
    }
}

/// Candidate result for threshold optimization.
#[derive(Debug, Clone)]
pub struct ThresholdCandidate {
    /// The parameters.
    pub params: ThresholdParams,
    /// Expected fees.
    pub expected_fees: Decimal,
    /// Expected IL.
    pub expected_il: Decimal,
    /// Expected number of rebalances.
    pub expected_rebalances: u32,
    /// Objective score.
    pub score: Decimal,
}

/// Candidate result for periodic optimization.
#[derive(Debug, Clone)]
pub struct PeriodicCandidate {
    /// The parameters.
    pub params: PeriodicParams,
    /// Expected fees.
    pub expected_fees: Decimal,
    /// Expected IL.
    pub expected_il: Decimal,
    /// Expected number of rebalances.
    pub expected_rebalances: u32,
    /// Objective score.
    pub score: Decimal,
}

/// Candidate result for IL limit optimization.
#[derive(Debug, Clone)]
pub struct ILLimitCandidate {
    /// The parameters.
    pub params: ILLimitParams,
    /// Expected fees.
    pub expected_fees: Decimal,
    /// Expected IL.
    pub expected_il: Decimal,
    /// Expected number of rebalances.
    pub expected_rebalances: u32,
    /// Objective score.
    pub score: Decimal,
}

// Helper functions

fn estimate_time_in_range(width: Decimal, volatility: f64, rebalances: u32) -> Decimal {
    let vol_factor = Decimal::from_f64(1.0 - volatility.min(0.9)).unwrap_or(Decimal::ONE);
    let width_factor = width * Decimal::from(2);
    let rebalance_bonus = Decimal::from(rebalances) * Decimal::from_f64(0.5).unwrap();

    let base_time = Decimal::from(50);
    let time = base_time + (width_factor * Decimal::from(100) * vol_factor) + rebalance_bonus;

    time.min(Decimal::from(100))
}

fn estimate_base_fees(
    config: &OptimizationConfig,
    width: Decimal,
    time_in_range: Decimal,
) -> Decimal {
    let volume_factor = Decimal::from(config.pool_liquidity) / Decimal::from(1_000_000_000u64);
    let width_factor = if width.is_zero() {
        Decimal::ONE
    } else {
        Decimal::ONE / width
    };

    let steps = Decimal::from(config.simulation_steps);
    let base_fees = config.fee_rate * volume_factor * steps;

    base_fees * width_factor * time_in_range / Decimal::from(100)
}

fn estimate_base_il(width: Decimal, volatility: f64) -> Decimal {
    let vol_dec = Decimal::from_f64(volatility).unwrap_or(Decimal::ZERO);
    let vol_squared = vol_dec * vol_dec;

    if width.is_zero() {
        return vol_squared;
    }

    vol_squared / width / Decimal::from(10)
}

fn create_sim_result(
    result: &(Decimal, Decimal, u32),
) -> clmm_lp_domain::value_objects::simulation_result::SimulationResult {
    clmm_lp_domain::value_objects::simulation_result::SimulationResult {
        final_position_value: Decimal::ZERO,
        total_fees_earned: result.0,
        total_il: result.1,
        net_pnl: result.0 - result.1,
        max_drawdown: result.1,
        time_in_range_percentage: Decimal::ZERO,
        sharpe_ratio: None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::objective::MaximizeNetPnL;

    #[test]
    fn test_parameter_optimizer_creation() {
        let optimizer = ParameterOptimizer::new();
        assert!(!optimizer.price_thresholds.is_empty());
        assert!(!optimizer.il_thresholds.is_empty());
        assert!(!optimizer.intervals.is_empty());
    }

    #[test]
    fn test_optimize_threshold() {
        let optimizer = ParameterOptimizer::new();
        let config = OptimizationConfig::default();
        let range_width = Decimal::from_f64(0.10).unwrap();

        let candidates = optimizer.optimize_threshold(&config, range_width, &MaximizeNetPnL);

        assert!(!candidates.is_empty());
        // Should be sorted by score descending
        for i in 1..candidates.len() {
            assert!(candidates[i - 1].score >= candidates[i].score);
        }
    }

    #[test]
    fn test_optimize_periodic() {
        let optimizer = ParameterOptimizer::new();
        let config = OptimizationConfig::default();
        let range_width = Decimal::from_f64(0.10).unwrap();

        let candidates = optimizer.optimize_periodic(&config, range_width, &MaximizeNetPnL);

        assert!(!candidates.is_empty());
        for i in 1..candidates.len() {
            assert!(candidates[i - 1].score >= candidates[i].score);
        }
    }

    #[test]
    fn test_optimize_il_limit() {
        let optimizer = ParameterOptimizer::new();
        let config = OptimizationConfig::default();
        let range_width = Decimal::from_f64(0.10).unwrap();

        let candidates = optimizer.optimize_il_limit(&config, range_width, &MaximizeNetPnL);

        assert!(!candidates.is_empty());
        for i in 1..candidates.len() {
            assert!(candidates[i - 1].score >= candidates[i].score);
        }
    }

    #[test]
    fn test_threshold_params_default() {
        let params = ThresholdParams::default();
        assert_eq!(params.price_threshold, Decimal::from_f64(0.05).unwrap());
        assert!(params.rebalance_on_out_of_range);
    }

    #[test]
    fn test_periodic_params_default() {
        let params = PeriodicParams::default();
        assert_eq!(params.interval, 24);
    }

    #[test]
    fn test_il_limit_params_default() {
        let params = ILLimitParams::default();
        assert_eq!(params.max_il, Decimal::from_f64(0.05).unwrap());
        assert!(params.close_il.is_some());
    }
}