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
use crate::;
/// Find the number of bars back to the lowest value in a lookback period
///
/// # Arguments
/// * `array` - Array of values to analyze
/// * `start_idx` - Starting index for analysis
/// * `lookback` - Number of bars to look back
///
/// # Visual Example
/// ```text
/// Given array: [5, 2, 4, 1, 3], start_idx = 4, lookback = 3
///
/// Lookback window: [4, 1, 3]
/// ^ ^ ^
/// Indices: 2 3 4
/// Values: 4 1 3
/// i values: 2 1 0
/// ↑
/// Lowest value (1)
///
/// Returns: 1 (number of bars back from start_idx to lowest value)
/// ```
///
/// # Returns
/// * `Result<usize, KandError>` - Number of bars back to lowest value if successful, error otherwise
///
/// # Errors
/// * Returns `KandError::InvalidParameter` if:
/// - The input array is empty
/// - `start_idx` is out of bounds
/// - `lookback` is 0
/// - `start_idx` is less than `lookback - 1`
/// Find the number of bars back to the highest value in a lookback period
///
/// # Arguments
/// * `array` - Array of values to analyze
/// * `start_idx` - Starting index for analysis
/// * `lookback` - Number of bars to look back
///
/// # Visual Example
/// ```text
/// Given array: [1, 4, 2, 5, 3], start_idx = 4, lookback = 3
///
/// Lookback window: [2, 5, 3]
/// ^ ^ ^
/// Indices: 2 3 4
/// Values: 2 5 3
/// i values: 2 1 0
/// ↑
/// Highest value (5)
///
/// Returns: 1 (number of bars back from start_idx to highest value)
/// ```
///
/// # Returns
/// * `Result<usize, KandError>` - Number of bars back to highest value if successful, error otherwise
///
/// # Errors
/// * Returns `KandError::InvalidParameter` if:
/// - The input array is empty
/// - `start_idx` is out of bounds
/// - `lookback` is 0
/// - `start_idx` is less than `lookback - 1`
/// Calculate k factor from period value (k = 2 / (period + 1))
///
/// # Arguments
/// * `period` - The period value to convert
///
/// # Returns
/// * `Result<TAFloat, KandError>` - The calculated k factor if successful, error otherwise
///
/// # Errors
/// * Returns `KandError::InvalidParameter` if `period` is 0
/// Calculate candlestick real body length
///
/// # Arguments
/// * `open` - Opening price
/// * `close` - Closing price
///
/// # Returns
/// * `TAFloat` - Absolute difference between open and close prices
/// Calculate candlestick upper shadow length
///
/// # Arguments
/// * `high` - High price
/// * `open` - Opening price
/// * `close` - Closing price
///
/// # Returns
/// * `TAFloat` - Length of upper shadow
/// Calculate candlestick lower shadow length
///
/// # Arguments
/// * `low` - Low price
/// * `open` - Opening price
/// * `close` - Closing price
///
/// # Returns
/// * `TAFloat` - Length of lower shadow
/// Check if there is a gap up between real bodies of two candlesticks
///
/// # Arguments
/// * `open2` - Opening price of second candle
/// * `close2` - Closing price of second candle
/// * `open1` - Opening price of first candle
/// * `close1` - Closing price of first candle
///
/// # Returns
/// * `bool` - True if gap up exists, false otherwise
/// Check if there is a gap down between real bodies of two candlesticks
///
/// # Arguments
/// * `open2` - Opening price of second candle
/// * `close2` - Closing price of second candle
/// * `open1` - Opening price of first candle
/// * `close1` - Closing price of first candle
///
/// # Returns
/// * `bool` - True if gap down exists, false otherwise