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
#![allow(dead_code)]


//! # qants
//! 
//! ```qants``` is a collection of methods aimed to simplify work with crypto excahnges like ByBit and Binance;
//! crate allows: 
//! * connect directly to exchange, 
//! * get KLines. Historical and live through websocket connection 
//! * build statistics for techncial analysis. 
//!     * SMA
//!     * EMA
//!     * Stochastic


pub mod ta;


// mod oscillator;

pub use ta::moving_average::ma;



enum OHLC {
    Open(f64),
    High(f64),
    Low(f64),
    Close(f64),
    Volume(f64),
}


#[derive(Debug)]
pub struct Kline {
    values: Vec<f64>,
    start: Option<usize>,
}


impl Kline {
    /// Create Kline from `Vec<f64>` values. 
    /// Call Moving Average functions to get {EMA, SMA} - Exponential MA, Simple MA
    /// # Examples
    /// ```
    /// let values = Vec::from([1.0, 2.0, 3.0, 4.0, 5.0]);
    /// let kline_data = Kline::new(v);
    /// let sma = kline_data.rolling(window: 5).mean();
    /// assert_eq!(sma , 3.0)
    /// ```
    /// Returns SMA value 

    pub fn new(values: Vec<f64>) -> Kline {
        Kline { values, start: None }
    }    
}

pub fn sma(v: Vec<f64>) -> f64{
    let kline_data = Kline::new(v);
    let sma = kline_data.rolling(5).mean();
    sma
}

pub fn ema() {
    todo!()
}

pub fn stoch() {
    todo!()
}

#[test]
fn get_sma() {
    let v = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    assert_eq!(sma(v), 3.0)
}



enum Decision {
    ShortSell,
    LongBuy,
    Reduce,
}

enum Trend {
    UpTrend,
    DownTrend
}
trait TrendState {
    fn get_240_ema_value() -> Trend {
        // if current close price > current ema_240 => uptrend
        todo!()
    }
}

enum StochSignals {
    StochUpperSignal{signal: u8},
    StochMiddleSignal{signal: u8},
    StochBottomSignal{signal: u8},
}


trait StochState {
    fn stoch();
}

struct Stoch{
    k_length: u8,
    k_smoothing: u8,
    d_smoothing: u8,
}

impl Stoch {
    pub fn new(
        k_length: u8,
        k_smoothing: u8,
        d_smoothing: u8,
    ) -> Stoch {
        Stoch { k_length, k_smoothing, d_smoothing }
    }
}