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
#![warn(missing_docs, missing_debug_implementations)]
mod methods;
use crate::core::{Candle, ValueType};
pub use methods::*;
#[inline]
pub fn sign(value: ValueType) -> ValueType {
((value > 0.) as i8 - (value < 0.) as i8) as ValueType
}
#[inline]
pub fn signi(value: ValueType) -> i8 {
(value > 0.) as i8 - (value < 0.) as i8
}
#[derive(Debug, Clone, Copy)]
pub struct RandomCandles(u16);
impl RandomCandles {
const DEFAULT_PRICE: ValueType = 1.0;
const DEFAULT_VOLUME: ValueType = 10.0;
pub fn new() -> Self {
Self::default()
}
pub fn first(&mut self) -> Candle {
let position = self.0;
self.0 = 0;
let candle = self.next().unwrap();
self.0 = position;
candle
}
}
impl Default for RandomCandles {
fn default() -> Self {
Self(0)
}
}
impl Iterator for RandomCandles {
type Item = Candle;
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: open,
high: high,
low: low,
close: close,
volume: volume,
};
self.0 = self.0.wrapping_sub(1);
Some(candle)
}
fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.0 = n as u16;
self.0 = self.0.wrapping_sub(1);
self.next()
}
}