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
use crate;
/// Calculates the raw stochastic value (%K) for a single tick.
///
/// # Arguments
/// * `price_data` - A slice of `Hlc` structs representing the price data.
/// * `index` - The index of the tick for which to calculate the %K.
/// * `k_length` - The lookback period length over which to calculate the %K.
///
/// # Returns
/// An `Option<f64>` containing the calculated %K if it can be determined,
/// `None` if there is insufficient data.
///
/// # Examples
///
/// ```
/// use oscillatorsetups::oscillators::stochastic::k_for_tick;
/// use oscillatorsetups::oscillators::models::Hlc;
///
/// let price_data = vec![
/// Hlc { price_high: 1.0, price_low: 0.9, price_close: 0.95 },
/// Hlc { price_high: 1.1, price_low: 1.0, price_close: 1.05 },
/// Hlc { price_high: 1.2, price_low: 1.1, price_close: 1.15 },
/// ];
///
/// assert_eq!(k_for_tick(&price_data, 2, 3), Some(83.33333333333331));
/// ```
/// Calculates the raw stochastic value (%K) for a slice of price data.
///
/// # Arguments
/// * `price_data` - A slice of `Hlc` structs representing the price data.
/// * `k_length` - The lookback period length over which to calculate the %K.
///
/// # Returns
/// A vector of `Option<f64>`, each containing a calculated %K for corresponding tick if it can be determined,
/// `None` if there was insufficient data for that tick.
///
/// # Examples
///
/// ```
/// use oscillatorsetups::oscillators::{
/// stochastic::k_for_ticks,
/// models::Hlc
/// };
///
/// let price_data = vec![
/// Hlc { price_high: 1.0, price_low: 0.9, price_close: 0.95 },
/// Hlc { price_high: 1.1, price_low: 1.0, price_close: 1.05 },
/// Hlc { price_high: 1.2, price_low: 1.1, price_close: 1.15 },
/// ];
///
/// assert_eq!(k_for_ticks(&price_data, 3), vec![None, None, Some(83.33333333333331)]);
/// ```
/// Calculates the Simple Moving Average (SMA) of %K values for a single tick.
///
/// # Arguments
/// * `k_values` - A slice of `Option<f64>` representing the %K values.
/// * `index` - The index of the tick for which to calculate the SMA.
/// * `d_length` - The period length over which to calculate the SMA.
///
/// # Returns
/// An `Option<f64>` containing the calculated SMA if it can be determined.
/// Returns `None` if there is insufficient data to calculate the SMA.
///
/// # Examples
/// ```
/// use oscillatorsetups::oscillators::stochastic::d_for_tick;
///
/// let k_values = vec![Some(10.0), Some(20.0), Some(30.0), Some(40.0)];
///
/// assert_eq!(d_for_tick(&k_values, 3, 3), Some(30.0));
/// ```
/// Calculates the Simple Moving Average (SMA) of %K values for a slice of price data.
/// # Arguments
/// * `k_values` - A slice of `Option<f64>` representing the %K values.
/// * `d_length` - The period length over which to calculate the SMA.
///
/// # Returns
/// A `Vec<Option<f64>>` where each element is the SMA of the `d_length` elements in `k_values` preceding it,
/// or `None` if there's not enough preceding data to compute an SMA.
///
/// # Examples
/// ```
/// use oscillatorsetups::oscillators::stochastic::d_for_ticks;
///
/// let k_values = vec![Some(10.0), Some(20.0), Some(30.0), Some(40.0), Some(50.0), Some(60.0)];
/// let d_length = 3;
/// let d_values = d_for_ticks(&k_values, d_length);
///
/// assert_eq!(d_values, vec![None, None, Some(20.0), Some(30.0), Some(40.0), Some(50.0)]);
/// ```
/// Represents the Stochastic Oscillator values at a single tick.
///
/// # Examples
/// ```
/// use oscillatorsetups::oscillators::stochastic::StochValues;
///
/// let stoch = StochValues {
/// k_line: Some(80.0),
/// d_line: Some(70.0),
/// };
/// ```
/// Generates the Stochastic Oscillator values for a slice of price data.
///
/// # Arguments
/// * `price_data` - A slice of `Hlc` representing the price data.
/// * `k_length` - The lookback period length over which to calculate the raw %K.
/// * `k_smoothing` - The period length over which to smooth the raw %K values.
/// * `d_smoothing` - The period length over which to smooth the %D values.
///
/// # Returns
/// A vector of [StochValues], each representing the Stochastic Oscillator values at a corresponding tick.
///
/// # Examples
/// ```
/// use crate::oscillatorsetups::oscillators::{models::Hlc, stochastic::{stochastic, StochValues}};
///
/// let price_data = vec![
/// Hlc::new(1768.34, 1763.93, 1768.34),
/// Hlc::new(1769.47, 1767.37, 1769.00),
/// Hlc::new(1768.99, 1767.99, 1767.99),
/// Hlc::new(1769.46, 1767.99, 1768.11),
/// Hlc::new(1768.49, 1764.74, 1766.35),
/// Hlc::new(1766.99, 1764.22, 1765.24),
/// Hlc::new(1766.49, 1764.30, 1765.40),
/// Hlc::new(1765.43, 1763.26, 1764.61),
/// Hlc::new(1767.02, 1764.85, 1765.11),
/// Hlc::new(1767.02, 1764.05, 1766.90),
/// Hlc::new(1766.97, 1763.61, 1764.50),
/// Hlc::new(1765.28, 1762.07, 1763.58),
/// Hlc::new(1763.44, 1761.71, 1761.90),
/// Hlc::new(1763.49, 1760.01, 1763.49),
/// Hlc::new(1765.00, 1761.00, 1765.00),
/// Hlc::new(1763.96, 1760.40, 1763.91),
/// ];
/// let k_length = 14;
/// let k_smoothing = 1;
/// let d_smoothing = 3;
///
/// let stoch_values = stochastic(&price_data, k_length, k_smoothing, d_smoothing);
/// let expected_stoch_values = vec![
/// StochValues { k_line: None, d_line: None },
/// StochValues { k_line: None, d_line: None },
/// StochValues { k_line: None, d_line: None },
/// StochValues { k_line: None, d_line: None },
/// StochValues { k_line: None, d_line: None },
/// StochValues { k_line: None, d_line: None },
/// StochValues { k_line: None, d_line: None },
/// StochValues { k_line: None, d_line: None },
/// StochValues { k_line: None, d_line: None },
/// StochValues { k_line: None, d_line: None },
/// StochValues { k_line: None, d_line: None },
/// StochValues { k_line: None, d_line: None },
/// StochValues { k_line: None, d_line: None },
///
/// StochValues { k_line: Some(36.78646934460893), d_line: None },
/// StochValues { k_line: Some(52.74841437632124), d_line: None },
///
/// StochValues { k_line: Some(41.26984126984203), d_line:Some(43.601574996924064) },
/// ];
/// ```