kestrel-chartkit 0.11.3

High-performance Rust technical analysis library for indicator math, market regime classification, composite scoring, and SVG visualization.
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
//! Provider-neutral instrument contract specifications, currency definitions, and valuation models.
//!
//! Provides explicit contract units (multiplier, lot size, currencies) and FX conversion
//! so position sizing, notionals, and P&L can be computed consistently in an account currency.

use std::fmt;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// An ISO currency code or currency symbol identifier (e.g. "EUR", "USD", "GBP", "JPY").
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Currency(String);

impl Currency {
    /// Creates a new normalized currency identifier (trimmed, uppercase).
    pub fn new(code: impl AsRef<str>) -> Self {
        Self(code.as_ref().trim().to_uppercase())
    }

    pub fn eur() -> Self {
        Self("EUR".to_string())
    }

    pub fn usd() -> Self {
        Self("USD".to_string())
    }

    pub fn gbp() -> Self {
        Self("GBP".to_string())
    }

    pub fn chf() -> Self {
        Self("CHF".to_string())
    }

    pub fn jpy() -> Self {
        Self("JPY".to_string())
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for Currency {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl From<&str> for Currency {
    fn from(s: &str) -> Self {
        Self::new(s)
    }
}

impl From<String> for Currency {
    fn from(s: String) -> Self {
        Self::new(s)
    }
}

/// Category of traded financial contract.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum InstrumentType {
    /// Standard stock / equity share. Multiplier is typically 1.0.
    Equity,
    /// Linear commodity, index, or rate future with a fixed point multiplier.
    LinearFuture,
    /// Spot or forward Foreign Exchange pair.
    Forex,
    /// Linear Contract For Difference.
    Cfd,
    /// Spot Cryptocurrency.
    CryptoSpot,
    /// Derivative financial option contract.
    Option,
}

impl InstrumentType {
    /// Returns true if this instrument uses standard linear valuation (`price * quantity * multiplier`).
    ///
    /// Inverse, quanto, or non-linear option contracts are not linear.
    pub fn is_linear(&self) -> bool {
        match self {
            Self::Equity | Self::LinearFuture | Self::Forex | Self::Cfd | Self::CryptoSpot => true,
            Self::Option => false,
        }
    }
}

/// Detailed operative contract specifications complementing [`crate::model::InstrumentMeta`].
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ContractSpec {
    /// Currency in which the instrument's market price is quoted (e.g. USD for AAPL, EUR for DAX).
    pub price_currency: Currency,
    /// Currency in which margin and cash settlements occur.
    pub settlement_currency: Currency,
    /// Contract point multiplier (e.g. 1.0 for equities, 25.0 for DAX future, 100.0 for SPX).
    pub multiplier: f64,
    /// Minimum increment of order quantity (lot step size, e.g. 1.0 for futures, 0.0001 for crypto).
    pub quantity_step: f64,
    /// Minimum allowed order quantity. Must be >= `quantity_step`.
    pub min_quantity: f64,
    /// The structural category of the instrument.
    pub instrument_type: InstrumentType,
}

impl Default for ContractSpec {
    fn default() -> Self {
        Self {
            price_currency: Currency::usd(),
            settlement_currency: Currency::usd(),
            multiplier: 1.0,
            quantity_step: 1.0,
            min_quantity: 1.0,
            instrument_type: InstrumentType::Equity,
        }
    }
}

/// Errors when validating a [`ContractSpec`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ContractSpecError {
    NonPositiveMultiplier,
    NonFiniteMultiplier,
    NonPositiveQuantityStep,
    NonFiniteQuantityStep,
    InvalidMinQuantity,
    EmptyPriceCurrency,
    EmptySettlementCurrency,
}

impl fmt::Display for ContractSpecError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NonPositiveMultiplier => f.write_str("multiplier must be > 0"),
            Self::NonFiniteMultiplier => f.write_str("multiplier must be finite"),
            Self::NonPositiveQuantityStep => f.write_str("quantity_step must be > 0"),
            Self::NonFiniteQuantityStep => f.write_str("quantity_step must be finite"),
            Self::InvalidMinQuantity => {
                f.write_str("min_quantity must be finite and >= quantity_step")
            }
            Self::EmptyPriceCurrency => f.write_str("price_currency must not be empty"),
            Self::EmptySettlementCurrency => f.write_str("settlement_currency must not be empty"),
        }
    }
}

impl std::error::Error for ContractSpecError {}

impl ContractSpec {
    /// Validates the contractual consistency of the specification.
    pub fn validate(&self) -> Result<(), ContractSpecError> {
        if !self.multiplier.is_finite() {
            return Err(ContractSpecError::NonFiniteMultiplier);
        }
        if self.multiplier <= 0.0 {
            return Err(ContractSpecError::NonPositiveMultiplier);
        }
        if !self.quantity_step.is_finite() {
            return Err(ContractSpecError::NonFiniteQuantityStep);
        }
        if self.quantity_step <= 0.0 {
            return Err(ContractSpecError::NonPositiveQuantityStep);
        }
        if !self.min_quantity.is_finite() || self.min_quantity < self.quantity_step {
            return Err(ContractSpecError::InvalidMinQuantity);
        }
        if self.price_currency.as_str().is_empty() {
            return Err(ContractSpecError::EmptyPriceCurrency);
        }
        if self.settlement_currency.as_str().is_empty() {
            return Err(ContractSpecError::EmptySettlementCurrency);
        }
        Ok(())
    }

    /// Rounds `quantity` strictly downward to the nearest integer multiple of `quantity_step`.
    ///
    /// If the resulting quantity is strictly less than `min_quantity`, returns `0.0`
    /// (no trade generated below minimum threshold).
    pub fn round_quantity_down(&self, quantity: f64) -> f64 {
        if !quantity.is_finite() || quantity <= 0.0 || self.quantity_step <= 0.0 {
            return 0.0;
        }
        let steps = (quantity / self.quantity_step + 1e-12).floor();
        let rounded = steps * self.quantity_step;
        // Use a small epsilon to avoid floating point precision edge cases (e.g. 0.99999999999 < 1.0)
        if rounded + 1e-12 < self.min_quantity {
            0.0
        } else {
            rounded
        }
    }
}

/// An FX quote between two currencies: `base_currency / quote_currency = rate`.
///
/// Example: `EUR / USD = 1.08` means 1 EUR = 1.08 USD.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct FxRate {
    pub base: Currency,
    pub quote: Currency,
    pub rate: f64,
    /// Epoch timestamp of the quote in seconds or milliseconds (caller-defined convention).
    pub timestamp: i64,
}

impl FxRate {
    pub fn new(base: impl Into<Currency>, quote: impl Into<Currency>, rate: f64) -> Self {
        Self {
            base: base.into(),
            quote: quote.into(),
            rate,
            timestamp: 0,
        }
    }

    /// Converts an `amount` from `from` currency to `to` currency using this FX rate.
    ///
    /// If `from == to`, returns `Ok(amount)` without needing a valid FX quote.
    /// If missing or inverted, computes the direct or reciprocal rate.
    pub fn convert(
        &self,
        amount: f64,
        from: &Currency,
        to: &Currency,
    ) -> Result<f64, FxConversionError> {
        if from == to {
            return Ok(amount);
        }
        if !self.rate.is_finite() || self.rate <= 0.0 {
            return Err(FxConversionError::InvalidRate(self.rate));
        }

        if from == &self.base && to == &self.quote {
            // e.g. amount in EUR -> USD: amount * rate
            Ok(amount * self.rate)
        } else if from == &self.quote && to == &self.base {
            // e.g. amount in USD -> EUR: amount / rate
            Ok(amount / self.rate)
        } else {
            Err(FxConversionError::MissingPair {
                from: from.clone(),
                to: to.clone(),
            })
        }
    }
}

/// Errors during foreign exchange currency conversion.
#[derive(Debug, Clone, PartialEq)]
pub enum FxConversionError {
    InvalidRate(f64),
    MissingPair { from: Currency, to: Currency },
    StaleRate { age_seconds: i64, max_age: i64 },
}

impl fmt::Display for FxConversionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidRate(r) => write!(f, "invalid non-positive FX rate: {r}"),
            Self::MissingPair { from, to } => {
                write!(f, "no FX rate available to convert from {from} to {to}")
            }
            Self::StaleRate {
                age_seconds,
                max_age,
            } => write!(
                f,
                "FX rate is stale: age {age_seconds}s exceeds max {max_age}s"
            ),
        }
    }
}

impl std::error::Error for FxConversionError {}

/// Errors during contract valuation and sizing calculations.
#[derive(Debug, Clone, PartialEq)]
pub enum ValuationError {
    InvalidContract(ContractSpecError),
    FxUnavailable(FxConversionError),
    NonPositivePrice(f64),
    NonFiniteInput(&'static str),
}

impl fmt::Display for ValuationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidContract(e) => write!(f, "invalid contract specification: {e}"),
            Self::FxUnavailable(e) => write!(f, "currency conversion failed: {e}"),
            Self::NonPositivePrice(p) => write!(f, "price must be positive: {p}"),
            Self::NonFiniteInput(field) => write!(f, "input {field} must be finite"),
        }
    }
}

impl std::error::Error for ValuationError {}

impl From<ContractSpecError> for ValuationError {
    fn from(e: ContractSpecError) -> Self {
        Self::InvalidContract(e)
    }
}

impl From<FxConversionError> for ValuationError {
    fn from(e: FxConversionError) -> Self {
        Self::FxUnavailable(e)
    }
}

/// Computes the total notional value of a position in account currency:
/// `quantity * price * multiplier * fx_rate`.
///
/// `fx_rate` converts from `spec.price_currency` to the account currency.
/// If `spec.price_currency == account_currency`, pass `Some(1.0)` or use [`FxRate::convert`].
/// If FX is required but unavailable, passing `None` returns [`ValuationError::FxUnavailable`].
pub fn notional_value(
    price: f64,
    quantity: f64,
    spec: &ContractSpec,
    fx_to_account: Option<f64>,
) -> Result<f64, ValuationError> {
    spec.validate()?;
    if !price.is_finite() || price <= 0.0 {
        return Err(ValuationError::NonPositivePrice(price));
    }
    if !quantity.is_finite() || quantity < 0.0 {
        return Err(ValuationError::NonFiniteInput("quantity"));
    }
    let fx = match fx_to_account {
        Some(rate) if rate.is_finite() && rate > 0.0 => rate,
        Some(invalid) => {
            return Err(ValuationError::FxUnavailable(
                FxConversionError::InvalidRate(invalid),
            ))
        }
        None => {
            return Err(ValuationError::FxUnavailable(
                FxConversionError::MissingPair {
                    from: spec.price_currency.clone(),
                    to: spec.settlement_currency.clone(),
                },
            ))
        }
    };
    Ok(quantity * price * spec.multiplier * fx)
}

/// Computes the stop risk amount in account currency:
/// `quantity * |entry - stop| * multiplier * fx_rate`.
pub fn stop_risk_amount(
    entry: f64,
    stop: f64,
    quantity: f64,
    spec: &ContractSpec,
    fx_to_account: Option<f64>,
) -> Result<f64, ValuationError> {
    spec.validate()?;
    if !entry.is_finite() || entry <= 0.0 {
        return Err(ValuationError::NonPositivePrice(entry));
    }
    if !stop.is_finite() || stop <= 0.0 {
        return Err(ValuationError::NonPositivePrice(stop));
    }
    if !quantity.is_finite() || quantity < 0.0 {
        return Err(ValuationError::NonFiniteInput("quantity"));
    }
    let fx = match fx_to_account {
        Some(rate) if rate.is_finite() && rate > 0.0 => rate,
        Some(invalid) => {
            return Err(ValuationError::FxUnavailable(
                FxConversionError::InvalidRate(invalid),
            ))
        }
        None => {
            return Err(ValuationError::FxUnavailable(
                FxConversionError::MissingPair {
                    from: spec.price_currency.clone(),
                    to: spec.settlement_currency.clone(),
                },
            ))
        }
    };
    let price_diff = (entry - stop).abs();
    Ok(quantity * price_diff * spec.multiplier * fx)
}

/// Computes the realized or unrealized P&L in account currency:
/// `(exit - entry) * direction * quantity * multiplier * fx_rate`.
pub fn contract_pnl(
    entry: f64,
    exit: f64,
    quantity: f64,
    is_long: bool,
    spec: &ContractSpec,
    fx_to_account: Option<f64>,
) -> Result<f64, ValuationError> {
    spec.validate()?;
    if !entry.is_finite() || entry <= 0.0 {
        return Err(ValuationError::NonPositivePrice(entry));
    }
    if !exit.is_finite() || exit <= 0.0 {
        return Err(ValuationError::NonPositivePrice(exit));
    }
    if !quantity.is_finite() || quantity < 0.0 {
        return Err(ValuationError::NonFiniteInput("quantity"));
    }
    let fx = match fx_to_account {
        Some(rate) if rate.is_finite() && rate > 0.0 => rate,
        Some(invalid) => {
            return Err(ValuationError::FxUnavailable(
                FxConversionError::InvalidRate(invalid),
            ))
        }
        None => {
            return Err(ValuationError::FxUnavailable(
                FxConversionError::MissingPair {
                    from: spec.price_currency.clone(),
                    to: spec.settlement_currency.clone(),
                },
            ))
        }
    };
    let diff = if is_long { exit - entry } else { entry - exit };
    Ok(diff * quantity * spec.multiplier * fx)
}

/// Computes the single-tick value in account currency:
/// `tick_size * multiplier * fx_rate`.
pub fn contract_tick_value(
    tick_size: f64,
    spec: &ContractSpec,
    fx_to_account: Option<f64>,
) -> Result<f64, ValuationError> {
    spec.validate()?;
    if !tick_size.is_finite() || tick_size <= 0.0 {
        return Err(ValuationError::NonPositivePrice(tick_size));
    }
    let fx = match fx_to_account {
        Some(rate) if rate.is_finite() && rate > 0.0 => rate,
        Some(invalid) => {
            return Err(ValuationError::FxUnavailable(
                FxConversionError::InvalidRate(invalid),
            ))
        }
        None => {
            return Err(ValuationError::FxUnavailable(
                FxConversionError::MissingPair {
                    from: spec.price_currency.clone(),
                    to: spec.settlement_currency.clone(),
                },
            ))
        }
    };
    Ok(tick_size * spec.multiplier * fx)
}

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

    #[test]
    fn test_contract_spec_validation() {
        let default_spec = ContractSpec::default();
        assert_eq!(default_spec.validate(), Ok(()));

        let bad_multiplier = ContractSpec {
            multiplier: 0.0,
            ..ContractSpec::default()
        };
        assert_eq!(
            bad_multiplier.validate(),
            Err(ContractSpecError::NonPositiveMultiplier)
        );

        let bad_step = ContractSpec {
            quantity_step: -1.0,
            ..ContractSpec::default()
        };
        assert_eq!(
            bad_step.validate(),
            Err(ContractSpecError::NonPositiveQuantityStep)
        );

        let bad_min = ContractSpec {
            quantity_step: 10.0,
            min_quantity: 5.0,
            ..ContractSpec::default()
        };
        assert_eq!(
            bad_min.validate(),
            Err(ContractSpecError::InvalidMinQuantity)
        );
    }

    #[test]
    fn test_quantity_down_rounding() {
        let spec = ContractSpec {
            quantity_step: 0.5,
            min_quantity: 1.0,
            ..ContractSpec::default()
        };
        assert_eq!(spec.round_quantity_down(2.8), 2.5);
        assert_eq!(spec.round_quantity_down(1.0), 1.0);
        // Strictly below min_quantity -> 0.0 (no trade)
        assert_eq!(spec.round_quantity_down(0.9), 0.0);
        assert_eq!(spec.round_quantity_down(0.5), 0.0);
    }

    #[test]
    fn test_fx_conversion() {
        let fx = FxRate::new("EUR", "USD", 1.08);

        // Same currency -> identity without rate check
        assert_eq!(
            fx.convert(100.0, &Currency::eur(), &Currency::eur())
                .unwrap(),
            100.0
        );

        // Direct EUR -> USD
        let usd = fx
            .convert(100.0, &Currency::eur(), &Currency::usd())
            .unwrap();
        assert!((usd - 108.0).abs() < 1e-9);

        // Reciprocal USD -> EUR
        let eur = fx
            .convert(108.0, &Currency::usd(), &Currency::eur())
            .unwrap();
        assert!((eur - 100.0).abs() < 1e-9);

        // Missing pair
        let err = fx.convert(100.0, &Currency::gbp(), &Currency::usd());
        assert!(matches!(err, Err(FxConversionError::MissingPair { .. })));
    }
}