indexes_rs/v2/mfi/
types.rs1use serde::{Deserialize, Serialize};
2use std::collections::VecDeque;
3
4#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
6pub struct MFIConfig {
7 pub period: usize,
9 pub overbought: f64,
11 pub oversold: f64,
13}
14
15impl Default for MFIConfig {
16 fn default() -> Self {
17 Self {
18 period: 14,
19 overbought: 80.0,
20 oversold: 20.0,
21 }
22 }
23}
24
25#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
27pub struct MFIInput {
28 pub high: f64,
30 pub low: f64,
32 pub close: f64,
34 pub volume: f64,
36}
37
38#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
40pub struct MoneyFlow {
41 pub typical_price: f64,
43 pub raw_money_flow: f64,
45 pub flow_direction: f64,
47}
48
49#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
51pub struct MFIOutput {
52 pub mfi: f64,
54 pub typical_price: f64,
56 pub raw_money_flow: f64,
58 pub flow_direction: f64,
60 pub market_condition: MFIMarketCondition,
62}
63
64#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
66pub enum MFIMarketCondition {
67 Overbought,
69 Oversold,
71 Normal,
73 Insufficient,
75}
76
77#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
79pub struct MFIState {
80 pub config: MFIConfig,
82 pub money_flows: VecDeque<MoneyFlow>,
84 pub previous_typical_price: Option<f64>,
86 pub positive_money_flow_sum: f64,
88 pub negative_money_flow_sum: f64,
90 pub has_sufficient_data: bool,
92}
93
94impl MFIState {
95 pub fn new(config: MFIConfig) -> Self {
96 Self {
97 config,
98 money_flows: VecDeque::with_capacity(config.period),
99 previous_typical_price: None,
100 positive_money_flow_sum: 0.0,
101 negative_money_flow_sum: 0.0,
102 has_sufficient_data: false,
103 }
104 }
105}
106
107#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
109pub enum MFIError {
110 InvalidInput(String),
112 InvalidOHLC,
114 NegativeVolume,
116 InvalidPrice,
118 InvalidPeriod,
120 InvalidThresholds,
122 DivisionByZero,
124}