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
use super::{TickBars, Candle};
const SYMBOL_STICK: &str = "│";
const SYMBOL_CANDLE: &str = "┃";
const SYMBOL_HALF_TOP: &str = "╽";
const SYMBOL_HALF_BOTTOM: &str = "╿";
const SYMBOL_HALF_CANDLE_TOP: &str = "╻";
const SYMBOL_HALF_CANDLE_BOTTOM: &str = "╹";
const SYMBOL_HALF_STICK_TOP: &str = "╷";
const SYMBOL_HALF_STICK_BOTTOM: &str = "╵";
const SYMBOL_NOTHING: &str = " ";
pub struct CandleStickGraph {
height: u32,
data: TickBars,
global_min: f32,
global_max: f32,
}
impl CandleStickGraph {
pub fn new(height: u32, data: TickBars) -> Self {
let global_min = data.get_candles()
.map(|candle| candle.low)
.min_by(|a, b| a.partial_cmp(b).unwrap())
.unwrap();
let global_max = data.get_candles()
.map(|candle| candle.high)
.max_by(|a, b| a.partial_cmp(b).unwrap())
.unwrap();
CandleStickGraph {
height,
data,
global_min,
global_max,
}
}
pub fn draw(&self) -> String {
let mut ret = String::new();
for y in (0..self.height).rev() {
if y % 4 == 0 {
ret += &format!("{:8.8} ",
self.global_min +
(y as f32 * (self.global_max - self.global_min)
/ self.height as f32))
} else {
ret += " "
}
for c in self.data.get_candles() {
ret += &self.render_candle_at(c, y);
}
ret += "\n"
}
ret
}
fn to_height_units(&self, x: f32) -> f32 {
(x - self.global_min) / (self.global_max - self.global_min)
* self.height as f32
}
fn render_candle_at(&self, candle: &Candle, height_unit: u32) -> String {
let height_unit = height_unit as f32;
let ts = self.to_height_units(candle.high);
let tc = self.to_height_units(candle.open.max(candle.close));
let bs = self.to_height_units(candle.low);
let bc = self.to_height_units(candle.open.min(candle.close));
if f32::ceil(ts) >= height_unit && height_unit >= f32::floor(tc) {
if tc - height_unit > 0.75 {
return SYMBOL_CANDLE.to_owned()
} else if (tc - height_unit) > 0.25 {
if (ts - height_unit) > 0.75 {
return SYMBOL_HALF_TOP.to_owned()
} else {
return SYMBOL_HALF_CANDLE_TOP.to_owned()
}
} else {
if (ts - height_unit) > 0.75 {
return SYMBOL_STICK.into()
} else if (ts - height_unit) > 0.25 {
return SYMBOL_HALF_STICK_TOP.into()
} else {
return SYMBOL_NOTHING.into()
}
}
} else if f32::floor(tc) >= height_unit && height_unit >= f32::ceil(bc) {
return SYMBOL_CANDLE.to_owned()
} else if f32::ceil(bc) >= height_unit && height_unit >= f32::floor(bs) {
if (bc - height_unit) < 0.25 {
return SYMBOL_CANDLE.to_owned()
} else if (bc - height_unit) < 0.75 {
if (bs - height_unit) < 0.25 {
return SYMBOL_HALF_BOTTOM.to_owned()
} else {
return SYMBOL_HALF_CANDLE_BOTTOM.to_owned()
}
} else {
if (bs - height_unit) < 0.25 {
return SYMBOL_STICK.into()
} else if (bs - height_unit) < 0.75 {
return SYMBOL_HALF_STICK_BOTTOM.into()
} else {
return SYMBOL_NOTHING.into()
}
}
} else {
return SYMBOL_NOTHING.into()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dtf;
#[test]
fn should_print_candlestick_graph_ok() {
static HOUR : u64 = 60 * 60 * 1000 - 1000;
static MINUTE : u64 = 60 * 1000;
let fname: &str = "test/test-data/bt_btceth.dtf";
let meta = dtf::file_format::read_meta(fname).unwrap();
let min_ts = meta.min_ts + HOUR;
let y_ts = 10 * MINUTE;
let max_ts = min_ts + HOUR + y_ts;
let ups = dtf::file_format::get_range_in_file(fname, min_ts, max_ts).unwrap();
let mut candles = TickBars::from(ups.as_slice());
candles.insert_continuation_candles();
let graph = CandleStickGraph::new(21, candles);
let plot = graph.draw();
assert_eq!(plot.replace(" ", "").as_str(), "".to_owned()+
"0.04068785 ╽╽
│
│ ╻
│ ╷ ╻╷╷ ┃
0.04055928 │ │ ┃││ ╻ │ ┃╹╻
│ │ ┃││╻ ╻ ┃ │ ┃ ╷
╵ ││ ┃ ┃││┃ ┃ ││╻│ ┃ │ ╻ ╻
┃ ││┃│ ┃ │ ┃ │┃
0.04043070 ┃ ┃ ││┃│ ┃ │ ┃ │┃ ┃┃│
┃ ││┃│ ┃ │ ┃ │┃ ┃┃│ ┃
┃ │╽┃│ ┃ │ ┃ │┃ │┃┃╿╻╹╷ ╻│ ╻╷╻╷
┃ │┃┃│ ┃ │ ┃ │┃ │┃┃│┃ │ ││ │││┃ ┃ ╻ ╻╻╻
0.04030213 ┃ ┃┃ ┃┃┃┃│ ╻│┃ │┃┃│┃ │ ││ │││┃ │ ╹╻╻ ┃
╵╻╹╵╿ │┃┃│┃ │ │ ┃ ┃ │┃ ┃┃ │ ┃┃┃ │┃
╵╻╵╹╹╵╹ ╽╻╵ ┃┃┃ ┃┃ │┃┃┃┃ │┃
│┃┃ ┃┃ │┃┃┃┃ │┃
0.04017356 │┃┃ ┃┃ │┃┃┃┃ │┃
│┃┃ ┃┃ │┃┃┃┃ │┃
│┃┃ ┃┃ │┃┃┃┃ │┃
│┃┃ ┃┃ ┃┃┃┃ ╽┃
0.04004499 │╹╹ ╹ │
".replace(" ", "").as_str());
}
}