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
#![warn(missing_docs, missing_debug_implementations)]
mod methods;
use crate::core::{Candle, ValueType};
pub use methods::{MAInstance, MA};
#[inline]
#[must_use]
pub fn sign(value: ValueType) -> ValueType {
((value > 0.) as i8 - (value < 0.) as i8) as ValueType
}
#[inline]
#[must_use]
pub fn signi(value: ValueType) -> i8 {
(value > 0.) as i8 - (value < 0.) as i8
}
pub fn assert_eq_float(original: ValueType, calculated: ValueType) {
const SIGMA: ValueType = if cfg!(feature = "value_type_f32") {
4e-3
} else {
1e-10
};
assert!(
calculated.is_finite(),
"Calculated value is not a regular number: {}",
calculated
);
let diff = original - calculated;
let mid = (original.abs() + calculated.abs()) / 2.0;
if mid != 0. {
assert!(
(diff / mid).abs() <= SIGMA || diff < SIGMA,
"orignial={}, calculated={}, diff={}, relative diff={}",
original,
calculated,
diff,
(diff / original).abs(),
);
}
}
pub fn assert_neq_float(value1: ValueType, value2: ValueType) {
const SIGMA: ValueType = if cfg!(feature = "value_type_f32") {
1e-10
} else {
1e-20
};
let mid = (value1 + value2) / 2.0;
let diff = value1 - value2;
assert!(
(diff / mid).abs() > SIGMA,
"value#1={}, value#2={}",
value1,
value2
);
}
#[derive(Debug, Clone, Default)]
#[allow(missing_copy_implementations)]
pub struct RandomCandles(u16);
impl RandomCandles {
const DEFAULT_PRICE: ValueType = 1.0;
const DEFAULT_VOLUME: ValueType = 10.0;
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[allow(clippy::missing_panics_doc)]
pub fn first(&mut self) -> Candle {
let position = self.0;
self.0 = 0;
let candle = self.next().unwrap();
self.0 = position;
candle
}
}
impl Iterator for RandomCandles {
type Item = Candle;
#[allow(clippy::suboptimal_flops)]
fn next(&mut self) -> Option<Self::Item> {
let prev_position = self.0.wrapping_sub(1) as ValueType;
let position = self.0 as ValueType;
let close = Self::DEFAULT_PRICE + position.sin() / 2.;
let open = Self::DEFAULT_PRICE + prev_position.sin() / 2.;
let high = close.max(open) + (position * 1.4).tan().abs();
let low = close.min(open) - (position * 0.8).cos().abs() / 3.;
let volume = Self::DEFAULT_VOLUME * (position / 2.).sin() + Self::DEFAULT_VOLUME / 2.;
let candle = Self::Item {
open,
high,
low,
close,
volume,
};
self.0 = self.0.wrapping_sub(1);
Some(candle)
}
#[allow(clippy::cast_possible_truncation)]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.0 = n as u16;
self.0 = self.0.wrapping_sub(1);
self.next()
}
}