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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use crate::core::{Error, Method, Source, ValueType, OHLCV};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Converts timeseries to [Renko](https://en.wikipedia.org/wiki/Renko_chart) timeseries
///
/// Renko is very different from the simple timeseries. On each step it may generate any amount of blocks or not genereate it at all.
/// That's why it needs to be implement throw three different structures:
///
/// * [`Renko`] method itself.
///
/// When call [`Method::next`] on `Renko`, it always returns `RenkoOutput`.
///
/// * [`RenkoOutput`] which is `Renko`'s method output type.
///
/// It implements an [`Iterator`](std::iter::Iterator) trait for generating [`RenkoBlock`]s after each step of calling [`Method::next`] on [`Renko`].
/// `RenkoOutput` may produce any amount of `RenkoBlock`s or may not produce it at all.
///
/// * [`RenkoBlock`] is final entity of Renko chart.
///
/// It has `open` and `close` values which are similar to corresponding [`OHLCV`](crate::core::OHLCV)'s values.
///
/// So the final workflow is like that:
///
/// 1. Call [`Renko`]'s [`Method::next`] on some [`ValueType`] and get [`RenkoOutput`].
/// 2. Iterate over taken [`RenkoOutput`] to get some (or none) [`RenkoBlock`]s.
/// 3. Use produced [`RenkoBlock`]s on your own.
///
/// # Parameters
///
/// Has a tuple of 2 parameters \(`size`: [`ValueType`], `source`: [`Source`]\)
///
/// * `size`: [`ValueType`]. Represents relative block size.
///
/// `size` must be in range \(`0.0`; `1.0`\)
///
/// * `source`: [`Source`]. Represents which value of input's OHLCV it will use.
///
/// ```
/// use yata::prelude::*;
/// use yata::core::Source;
/// use yata::methods::Renko;
/// let first_timeseries_value = Candle { close: 123.456, ..Candle::default() };
/// let renko = Renko::new((0.01, Source::Close), &first_timeseries_value); // creates a Renko method with relative block size of 1%.
/// ```
///
/// # Input type
///
/// Input type is reference to [`OHLCV`]
///
/// # Output type
///
/// Input type is [`RenkoOutput`]
///
/// # Examples
///
/// ```
/// use yata::prelude::*;
/// use yata::core::Source;
/// use yata::methods::Renko;
///
/// // Here we just creating `Vec` of `OHLCV`s with only `close` value inside  
/// let inputs = (&[100.0, 100.5, 101.506, 105.0, 102.0, 101.4, 100.0])
///     .iter()
///     .map(|&v| Candle {
///         close: v,
///         ..Candle::default()
///     })
///     .collect::<Vec<_>>();
/// let mut renko = Renko::new((0.01, Source::Close), &inputs[0]).unwrap(); // renko with relative block size of 1%
///
/// assert!(renko.next(&inputs[0]).is_empty());
/// assert!(renko.next(&inputs[1]).is_empty());
/// assert_eq!(renko.next(&inputs[2]).len(), 1);
/// let blocks = renko.next(&inputs[3]);
/// assert_eq!(blocks.len(), 3);
/// blocks.for_each(|block| { println!("{:?}", &block); });
/// assert_eq!(renko.next(&inputs[4]).len(), 1);
/// assert_eq!(renko.next(&inputs[5]).len(), 1);
/// assert_eq!(renko.next(&inputs[6]).len(), 1);
/// ```
///
/// # Performance
///
/// O(1)
///
/// # See also
///
/// * [`HeikinAshi`](crate::methods::HeikinAshi)
///
/// [`ValueType`]: crate::core::ValueType
/// [`Source`']: crate::core::Source
/// [`OHLCV`]: crate::core::OHLCV

#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Renko {
	last_block_upper: ValueType,
	last_block_lower: ValueType,
	next_block_upper: ValueType,
	next_block_lower: ValueType,
	brick_size: ValueType,
	src: Source,
	volume: ValueType,
}

/// Single unit for [`Renko`] charts
///
/// May be produced by [`RenkoOutput`] iterator.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RenkoBlock {
	/// Current block's open value
	pub open: ValueType,

	/// Current block's close value
	pub close: ValueType,

	/// Average block's volume value
	pub volume: ValueType,
}

impl RenkoBlock {
	/// Returns upper bound of the Renko's block
	#[must_use]
	pub fn upper_bound(&self) -> ValueType {
		self.open.max(self.close)
	}

	/// Returns lower bound of the Renko's block
	#[must_use]
	pub fn lower_bound(&self) -> ValueType {
		self.open.min(self.close)
	}

	/// Returns sign of the Renko's block
	#[must_use]
	pub fn sign(&self) -> i8 {
		1 - (self.close < self.open) as i8 * 2
	}
}

impl OHLCV for RenkoBlock {
	#[inline]
	fn open(&self) -> ValueType {
		self.open
	}

	#[inline]
	fn close(&self) -> ValueType {
		self.close
	}

	#[inline]
	fn high(&self) -> ValueType {
		self.open.max(self.close)
	}

	#[inline]
	fn low(&self) -> ValueType {
		self.open.min(self.close)
	}

	#[inline]
	fn volume(&self) -> ValueType {
		self.volume
	}
}
/// [`Renko`]'s method [output type](crate::core::Method::Output)
///
/// Implements [`Iterator`](std::iter::Iterator) trait for generating [`RenkoBlock`]s.
#[derive(Debug, Clone)]
#[allow(missing_copy_implementations)]
pub struct RenkoOutput {
	len: usize,
	pos: usize,
	brick_size: ValueType,
	base_line: ValueType,
	block_volume: ValueType,
}

impl RenkoOutput {
	/// Checks if there is no generated blocks
	///
	/// Returns `true` if there is no generated blocks.
	/// Otherwise returns `false`.
	#[must_use]
	pub const fn is_empty(&self) -> bool {
		self.len == 0
	}

	/// Returns `true` if Renko has at least one rising block at this step
	#[must_use]
	#[inline]
	#[allow(clippy::missing_const_for_fn)]
	pub fn is_rising(&self) -> bool {
		self.len > 0 && self.brick_size.is_sign_positive()
	}

	/// Returns `true` if Renko has at least one falling block at this step
	#[must_use]
	#[inline]
	#[allow(clippy::missing_const_for_fn)]
	pub fn is_falling(&self) -> bool {
		self.len > 0 && self.brick_size.is_sign_negative()
	}

	/// Returns the size of all the blocks at this step
	#[must_use]
	#[inline]
	pub fn gap(&self) -> ValueType {
		self.brick_size * self.len as ValueType
	}

	/// Returns sign of the Renko's blocks
	#[must_use]
	pub fn sign(&self) -> i8 {
		self.is_rising() as i8 - self.is_falling() as i8
	}
}

impl OHLCV for RenkoOutput {
	#[inline]
	fn open(&self) -> ValueType {
		self.base_line
	}

	#[inline]
	fn close(&self) -> ValueType {
		self.base_line + self.gap()
	}

	#[inline]
	fn high(&self) -> ValueType {
		self.open().max(self.close())
	}

	#[inline]
	fn low(&self) -> ValueType {
		self.open().min(self.close())
	}

	#[inline]
	fn volume(&self) -> ValueType {
		self.block_volume * self.len as ValueType
	}
}

impl Iterator for RenkoOutput {
	type Item = RenkoBlock;

	fn next(&mut self) -> Option<Self::Item> {
		if self.pos == self.len {
			return None;
		}

		let block = RenkoBlock {
			// open: (1. + self.pos as ValueType * self.brick_size) * self.base_line,
			open: self.brick_size.mul_add(self.pos as ValueType, 1.) * self.base_line,
			// close: (1. + (self.pos + 1) as ValueType * self.brick_size) * self.base_line,
			close: self.brick_size.mul_add((self.pos + 1) as ValueType, 1.) * self.base_line,

			volume: self.block_volume,
		};

		self.pos += 1;

		Some(block)
	}

	#[inline]
	fn size_hint(&self) -> (usize, Option<usize>) {
		let size = self.len - self.pos;
		(size, Some(size))
	}

	#[inline]
	fn count(self) -> usize {
		self.len - self.pos
	}

	#[inline]
	fn nth(&mut self, n: usize) -> Option<Self::Item> {
		self.pos += n;
		self.next()
	}

	#[inline]
	fn last(mut self) -> Option<Self::Item> {
		if self.pos == self.len {
			None
		} else {
			self.pos = self.len - 1;
			self.next()
		}
	}
}

impl ExactSizeIterator for RenkoOutput {
	#[inline]
	fn len(&self) -> usize {
		self.len - self.pos
	}
}

impl std::iter::FusedIterator for RenkoOutput {}

impl<'a> Method<'a> for Renko {
	type Params = (ValueType, Source);
	type Input = &'a dyn OHLCV;
	type Output = RenkoOutput;

	fn new((brick_size, src): Self::Params, candle: Self::Input) -> Result<Self, Error> {
		let value = candle.source(src);

		if (ValueType::EPSILON..1.0).contains(&brick_size) {
			let half_size = value * brick_size * 0.5;
			Ok(Self {
				brick_size,
				last_block_upper: value + half_size,
				last_block_lower: value - half_size,
				next_block_upper: (value + half_size) * (1. + brick_size),
				next_block_lower: (value - half_size) * (1. - brick_size),
				src,
				volume: 0.0,
			})
		} else {
			Err(Error::WrongMethodParameters)
		}
	}

	#[inline]
	#[allow(clippy::cast_possible_truncation)]
	#[allow(clippy::cast_sign_loss)]
	#[allow(clippy::suboptimal_flops)]
	#[allow(clippy::assign_op_pattern)]
	fn next(&mut self, candle: Self::Input) -> Self::Output {
		let value = candle.source(self.src);
		self.volume += candle.volume();

		if value >= self.next_block_upper {
			let len = ((value - self.last_block_upper) / self.last_block_upper / self.brick_size)
				as usize;
			let base_line = self.last_block_upper;

			self.last_block_upper = base_line * (1. + self.brick_size * len as ValueType);
			self.last_block_lower = base_line * (1. + self.brick_size * (len - 1) as ValueType);

			self.next_block_upper = self.last_block_upper * (1. + self.brick_size);
			self.next_block_lower = self.last_block_lower * (1. - self.brick_size);

			let volume = self.volume;
			self.volume = 0.0;
			RenkoOutput {
				len,
				pos: 0,
				brick_size: self.brick_size,
				base_line,
				block_volume: volume / len as ValueType,
			}
		} else if value <= self.next_block_lower {
			let len = ((self.last_block_lower - value) / self.last_block_lower / self.brick_size)
				as usize;
			let base_line = self.last_block_lower;

			self.last_block_upper = base_line * (1. - self.brick_size * (len - 1) as ValueType);
			self.last_block_lower = base_line * (1. - self.brick_size * len as ValueType);

			self.next_block_upper = self.last_block_upper * (1. + self.brick_size);
			self.next_block_lower = self.last_block_lower * (1. - self.brick_size);

			let volume = self.volume;
			self.volume = 0.0;
			RenkoOutput {
				len,
				pos: 0,
				brick_size: -self.brick_size,
				base_line,
				block_volume: volume / len as ValueType,
			}
		} else {
			RenkoOutput {
				len: 0,
				pos: 0,
				brick_size: ValueType::NAN,
				base_line: ValueType::NAN,
				block_volume: ValueType::NAN,
			}
		}
	}
}

#[cfg(test)]
mod tests {
	use crate::core::Source;
	use crate::prelude::Candle;

	use super::{Method, Renko};

	#[test]
	#[allow(clippy::match_same_arms)]
	#[allow(clippy::clone_on_copy)]
	fn test_renko() {
		//						0	   1	   2	   3	  4	     5	    6
		let inputs = (&[100.0, 100.5, 101.506, 105.0, 102.0, 101.4, 100.0])
			.iter()
			.map(|&v| Candle {
				close: v,
				..Candle::default()
			})
			.collect::<Vec<_>>();

		let mut renko = Renko::new((0.01, Source::Close), &inputs[0]).unwrap();
		inputs
			.iter()
			.map(|x| (renko.clone(), renko.next(x), renko.clone()))
			.enumerate()
			.for_each(|(i, (r1, x, r2))| match i {
				0 => assert!(x.is_empty(), "{:?} => {:?} ::: {:?}", r1, r2, x),
				1 => assert!(x.is_empty(), "{:?} => {:?} ::: {:?}", r1, r2, x),
				2 => assert_eq!(x.len(), 1, "{:?} => {:?} ::: {:?}", r1, r2, x),
				3 => assert_eq!(x.len(), 3, "{:?} => {:?} ::: {:?}", r1, r2, x),
				4 => assert_eq!(x.len(), 1, "{:?} => {:?} ::: {:?}", r1, r2, x),
				5 => assert_eq!(x.len(), 1, "{:?} => {:?} ::: {:?}", r1, r2, x),
				6 => assert_eq!(x.len(), 1, "{:?} => {:?} ::: {:?}", r1, r2, x),
				_ => panic!("Expected match arm for index {}", i),
			});
	}
}