finance-query 2.5.1

A Rust library for querying financial data
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
//! Threshold-based conditions for position management.
//!
//! This module provides conditions for stop-loss, take-profit, and trailing stops.

use crate::backtesting::strategy::StrategyContext;
use crate::indicators::Indicator;

use super::Condition;

/// Condition: position P/L is at or below the stop-loss threshold.
///
/// # Execution Model
///
/// This condition evaluates at **bar close**: it fires when the closing price
/// implies a loss ≥ `pct`. The resulting exit signal is deferred to the **next
/// bar's open** (identical to all strategy-signal exits).
///
/// For intrabar detection (fill same bar at `min(open, stop_level)`), use
/// [`BacktestConfig::stop_loss_pct`] instead. A −10% intraday move that closes
/// at −3% will be caught by the config field but missed by this condition.
///
/// # Example
///
/// ```ignore
/// use finance_query::backtesting::condition::*;
///
/// let exit = stop_loss(0.05); // Exit if loss >= 5% at bar close
/// ```
#[derive(Debug, Clone, Copy)]
pub struct StopLoss {
    /// Stop-loss percentage (e.g., 0.05 for 5%)
    pub pct: f64,
}

impl StopLoss {
    /// Create a new stop-loss condition.
    ///
    /// # Arguments
    ///
    /// * `pct` - Stop-loss percentage (e.g., 0.05 for 5%)
    pub fn new(pct: f64) -> Self {
        Self { pct }
    }
}

impl Condition for StopLoss {
    fn evaluate(&self, ctx: &StrategyContext) -> bool {
        if let Some(pos) = ctx.position {
            let pnl_pct = pos.unrealized_return_pct(ctx.close()) / 100.0;
            pnl_pct <= -self.pct
        } else {
            false
        }
    }

    fn required_indicators(&self) -> Vec<(String, Indicator)> {
        vec![]
    }

    fn description(&self) -> String {
        format!("stop loss at {:.1}%", self.pct * 100.0)
    }
}

/// Create a stop-loss condition.
///
/// # Example
///
/// ```ignore
/// use finance_query::backtesting::condition::*;
///
/// let exit = rsi(14).above(70.0).or(stop_loss(0.05));
/// ```
#[inline]
pub fn stop_loss(pct: f64) -> StopLoss {
    StopLoss::new(pct)
}

/// Condition: position P/L is at or above the take-profit threshold.
///
/// # Execution Model
///
/// This condition evaluates at **bar close**: it fires when the closing price
/// implies a gain ≥ `pct`. The resulting exit signal is deferred to the **next
/// bar's open** (identical to all strategy-signal exits).
///
/// For intrabar detection (fill same bar at `max(open, target_level)`), use
/// [`BacktestConfig::take_profit_pct`] instead.
///
/// # Example
///
/// ```ignore
/// use finance_query::backtesting::condition::*;
///
/// let exit = take_profit(0.10); // Exit if gain >= 10% at bar close
/// ```
#[derive(Debug, Clone, Copy)]
pub struct TakeProfit {
    /// Take-profit percentage (e.g., 0.10 for 10%)
    pub pct: f64,
}

impl TakeProfit {
    /// Create a new take-profit condition.
    ///
    /// # Arguments
    ///
    /// * `pct` - Take-profit percentage (e.g., 0.10 for 10%)
    pub fn new(pct: f64) -> Self {
        Self { pct }
    }
}

impl Condition for TakeProfit {
    fn evaluate(&self, ctx: &StrategyContext) -> bool {
        if let Some(pos) = ctx.position {
            let pnl_pct = pos.unrealized_return_pct(ctx.close()) / 100.0;
            pnl_pct >= self.pct
        } else {
            false
        }
    }

    fn required_indicators(&self) -> Vec<(String, Indicator)> {
        vec![]
    }

    fn description(&self) -> String {
        format!("take profit at {:.1}%", self.pct * 100.0)
    }
}

/// Create a take-profit condition.
///
/// # Example
///
/// ```ignore
/// use finance_query::backtesting::condition::*;
///
/// let exit = rsi(14).above(70.0).or(take_profit(0.15));
/// ```
#[inline]
pub fn take_profit(pct: f64) -> TakeProfit {
    TakeProfit::new(pct)
}

/// Condition: check if we have any position.
#[derive(Debug, Clone, Copy)]
pub struct HasPosition;

impl Condition for HasPosition {
    fn evaluate(&self, ctx: &StrategyContext) -> bool {
        ctx.has_position()
    }

    fn required_indicators(&self) -> Vec<(String, Indicator)> {
        vec![]
    }

    fn description(&self) -> String {
        "has position".to_string()
    }
}

/// Create a condition that checks if we have any position.
#[inline]
pub fn has_position() -> HasPosition {
    HasPosition
}

/// Condition: check if we have no position.
#[derive(Debug, Clone, Copy)]
pub struct NoPosition;

impl Condition for NoPosition {
    fn evaluate(&self, ctx: &StrategyContext) -> bool {
        !ctx.has_position()
    }

    fn required_indicators(&self) -> Vec<(String, Indicator)> {
        vec![]
    }

    fn description(&self) -> String {
        "no position".to_string()
    }
}

/// Create a condition that checks if we have no position.
#[inline]
pub fn no_position() -> NoPosition {
    NoPosition
}

/// Condition: check if we have a long position.
#[derive(Debug, Clone, Copy)]
pub struct IsLong;

impl Condition for IsLong {
    fn evaluate(&self, ctx: &StrategyContext) -> bool {
        ctx.is_long()
    }

    fn required_indicators(&self) -> Vec<(String, Indicator)> {
        vec![]
    }

    fn description(&self) -> String {
        "is long".to_string()
    }
}

/// Create a condition that checks if we have a long position.
#[inline]
pub fn is_long() -> IsLong {
    IsLong
}

/// Condition: check if we have a short position.
#[derive(Debug, Clone, Copy)]
pub struct IsShort;

impl Condition for IsShort {
    fn evaluate(&self, ctx: &StrategyContext) -> bool {
        ctx.is_short()
    }

    fn required_indicators(&self) -> Vec<(String, Indicator)> {
        vec![]
    }

    fn description(&self) -> String {
        "is short".to_string()
    }
}

/// Create a condition that checks if we have a short position.
#[inline]
pub fn is_short() -> IsShort {
    IsShort
}

/// Condition: position P/L is positive (in profit).
#[derive(Debug, Clone, Copy)]
pub struct InProfit;

impl Condition for InProfit {
    fn evaluate(&self, ctx: &StrategyContext) -> bool {
        if let Some(pos) = ctx.position {
            pos.unrealized_return_pct(ctx.close()) > 0.0
        } else {
            false
        }
    }

    fn required_indicators(&self) -> Vec<(String, Indicator)> {
        vec![]
    }

    fn description(&self) -> String {
        "in profit".to_string()
    }
}

/// Create a condition that checks if position is profitable.
#[inline]
pub fn in_profit() -> InProfit {
    InProfit
}

/// Condition: position P/L is negative (in loss).
#[derive(Debug, Clone, Copy)]
pub struct InLoss;

impl Condition for InLoss {
    fn evaluate(&self, ctx: &StrategyContext) -> bool {
        if let Some(pos) = ctx.position {
            pos.unrealized_return_pct(ctx.close()) < 0.0
        } else {
            false
        }
    }

    fn required_indicators(&self) -> Vec<(String, Indicator)> {
        vec![]
    }

    fn description(&self) -> String {
        "in loss".to_string()
    }
}

/// Create a condition that checks if position is at a loss.
#[inline]
pub fn in_loss() -> InLoss {
    InLoss
}

/// Condition: position has been held for at least N bars.
#[derive(Debug, Clone, Copy)]
pub struct HeldForBars {
    /// Minimum number of bars the position must be held
    pub min_bars: usize,
}

impl HeldForBars {
    /// Create a new held-for-bars condition.
    pub fn new(min_bars: usize) -> Self {
        Self { min_bars }
    }
}

impl Condition for HeldForBars {
    fn evaluate(&self, ctx: &StrategyContext) -> bool {
        if let Some(pos) = ctx.position {
            // Count bars since entry
            let entry_idx = ctx
                .candles
                .iter()
                .position(|c| c.timestamp >= pos.entry_timestamp)
                .unwrap_or(0);
            let bars_held = ctx.index.saturating_sub(entry_idx);
            bars_held >= self.min_bars
        } else {
            false
        }
    }

    fn required_indicators(&self) -> Vec<(String, Indicator)> {
        vec![]
    }

    fn description(&self) -> String {
        format!("held for {} bars", self.min_bars)
    }
}

/// Create a condition that checks if position has been held for at least N bars.
#[inline]
pub fn held_for_bars(min_bars: usize) -> HeldForBars {
    HeldForBars::new(min_bars)
}

/// Condition: trailing stop triggered when price retraces from peak/trough.
///
/// For long positions: tracks the highest price since entry and triggers
/// when price falls by `trail_pct` from that high.
///
/// For short positions: tracks the lowest price since entry and triggers
/// when price rises by `trail_pct` from that low.
///
/// # Execution Model
///
/// The peak/trough is computed from bar **highs/lows** since entry, but the
/// trigger test uses the **bar close**. The exit signal is deferred to the
/// **next bar's open** (identical to all strategy-signal exits).
///
/// For intrabar enforcement, use [`BacktestConfig::trailing_stop_pct`] instead,
/// which fills on the same bar when the trailing level is breached intraday.
///
/// # Example
///
/// ```ignore
/// use finance_query::backtesting::condition::*;
///
/// // Exit if price drops 3% from highest point since entry
/// let exit = trailing_stop(0.03);
/// ```
#[derive(Debug, Clone, Copy)]
pub struct TrailingStop {
    /// Trail percentage (e.g., 0.03 for 3%)
    pub trail_pct: f64,
}

impl TrailingStop {
    /// Create a new trailing stop condition.
    ///
    /// # Arguments
    ///
    /// * `trail_pct` - Trail percentage (e.g., 0.03 for 3%)
    pub fn new(trail_pct: f64) -> Self {
        Self { trail_pct }
    }
}

impl Condition for TrailingStop {
    fn evaluate(&self, ctx: &StrategyContext) -> bool {
        if let Some(pos) = ctx.position {
            // Find the entry index
            let entry_idx = ctx
                .candles
                .iter()
                .position(|c| c.timestamp >= pos.entry_timestamp)
                .unwrap_or(0);

            // Compute peak/trough from entry to current candle (inclusive)
            let current_close = ctx.close();

            match pos.side {
                crate::backtesting::position::PositionSide::Long => {
                    // For long: find highest high since entry
                    let peak = ctx.candles[entry_idx..=ctx.index]
                        .iter()
                        .map(|c| c.high)
                        .fold(f64::NEG_INFINITY, f64::max);

                    // Trigger if current price is trail_pct below peak
                    current_close <= peak * (1.0 - self.trail_pct)
                }
                crate::backtesting::position::PositionSide::Short => {
                    // For short: find lowest low since entry
                    let trough = ctx.candles[entry_idx..=ctx.index]
                        .iter()
                        .map(|c| c.low)
                        .fold(f64::INFINITY, f64::min);

                    // Trigger if current price is trail_pct above trough
                    current_close >= trough * (1.0 + self.trail_pct)
                }
            }
        } else {
            false
        }
    }

    fn required_indicators(&self) -> Vec<(String, Indicator)> {
        vec![]
    }

    fn description(&self) -> String {
        format!("trailing stop at {:.1}%", self.trail_pct * 100.0)
    }
}

/// Create a trailing stop condition.
///
/// The trailing stop tracks the best price (highest for longs, lowest for shorts)
/// since position entry and triggers when price retraces by the specified percentage.
///
/// # Example
///
/// ```ignore
/// use finance_query::backtesting::condition::*;
///
/// // Exit if price drops 3% from the highest point since entry
/// let exit = trailing_stop(0.03);
/// ```
#[inline]
pub fn trailing_stop(trail_pct: f64) -> TrailingStop {
    TrailingStop::new(trail_pct)
}

/// Condition: trailing take-profit triggered when profit retraces from peak.
///
/// For long positions: tracks the highest profit since entry and triggers
/// when profit falls by `trail_pct` from that peak profit.
///
/// For short positions: tracks the highest profit since entry and triggers
/// when profit falls by `trail_pct` from that peak profit.
///
/// This is useful for locking in gains - it only triggers after you've been
/// in profit and then profit starts declining.
///
/// # Example
///
/// ```ignore
/// use finance_query::backtesting::condition::*;
///
/// // Exit if profit drops 2% from highest profit achieved
/// let exit = trailing_take_profit(0.02);
/// ```
#[derive(Debug, Clone, Copy)]
pub struct TrailingTakeProfit {
    /// Trail percentage from peak profit (e.g., 0.02 for 2%)
    pub trail_pct: f64,
}

impl TrailingTakeProfit {
    /// Create a new trailing take-profit condition.
    ///
    /// # Arguments
    ///
    /// * `trail_pct` - Trail percentage from peak profit (e.g., 0.02 for 2%)
    pub fn new(trail_pct: f64) -> Self {
        Self { trail_pct }
    }
}

impl Condition for TrailingTakeProfit {
    fn evaluate(&self, ctx: &StrategyContext) -> bool {
        if let Some(pos) = ctx.position {
            // Find the entry index
            let entry_idx = ctx
                .candles
                .iter()
                .position(|c| c.timestamp >= pos.entry_timestamp)
                .unwrap_or(0);

            // Compute peak profit from entry to current
            let peak_profit_pct = ctx.candles[entry_idx..=ctx.index]
                .iter()
                .map(|c| pos.unrealized_return_pct(c.close))
                .fold(f64::NEG_INFINITY, f64::max);

            // Only trigger if we've been in profit and current profit is below peak by trail_pct
            let current_profit_pct = pos.unrealized_return_pct(ctx.close());

            // Convert trail_pct to percentage points (e.g., 0.02 -> 2.0 percentage points)
            let trail_threshold = self.trail_pct * 100.0;

            peak_profit_pct > 0.0 && current_profit_pct <= peak_profit_pct - trail_threshold
        } else {
            false
        }
    }

    fn required_indicators(&self) -> Vec<(String, Indicator)> {
        vec![]
    }

    fn description(&self) -> String {
        format!("trailing take profit at {:.1}%", self.trail_pct * 100.0)
    }
}

/// Create a trailing take-profit condition.
///
/// This condition tracks the peak profit since entry and triggers when
/// profit drops by the specified percentage from that peak. It only triggers
/// after the position has been in profit.
///
/// # Example
///
/// ```ignore
/// use finance_query::backtesting::condition::*;
///
/// // Exit if profit drops 2% from peak profit
/// let exit = trailing_take_profit(0.02);
/// ```
#[inline]
pub fn trailing_take_profit(trail_pct: f64) -> TrailingTakeProfit {
    TrailingTakeProfit::new(trail_pct)
}

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

    #[test]
    fn test_stop_loss_description() {
        let sl = stop_loss(0.05);
        assert_eq!(sl.description(), "stop loss at 5.0%");
    }

    #[test]
    fn test_take_profit_description() {
        let tp = take_profit(0.10);
        assert_eq!(tp.description(), "take profit at 10.0%");
    }

    #[test]
    fn test_position_conditions_descriptions() {
        assert_eq!(has_position().description(), "has position");
        assert_eq!(no_position().description(), "no position");
        assert_eq!(is_long().description(), "is long");
        assert_eq!(is_short().description(), "is short");
        assert_eq!(in_profit().description(), "in profit");
        assert_eq!(in_loss().description(), "in loss");
    }

    #[test]
    fn test_held_for_bars_description() {
        let hfb = held_for_bars(5);
        assert_eq!(hfb.description(), "held for 5 bars");
    }

    #[test]
    fn test_trailing_stop_description() {
        let ts = trailing_stop(0.03);
        assert_eq!(ts.description(), "trailing stop at 3.0%");
    }

    #[test]
    fn test_trailing_take_profit_description() {
        let ttp = trailing_take_profit(0.02);
        assert_eq!(ttp.description(), "trailing take profit at 2.0%");
    }

    #[test]
    fn test_no_indicators_required() {
        assert!(stop_loss(0.05).required_indicators().is_empty());
        assert!(take_profit(0.10).required_indicators().is_empty());
        assert!(has_position().required_indicators().is_empty());
        assert!(no_position().required_indicators().is_empty());
        assert!(trailing_stop(0.03).required_indicators().is_empty());
        assert!(trailing_take_profit(0.02).required_indicators().is_empty());
    }
}