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
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use std::convert::TryFrom;
use std::str::FromStr;

use crate::core::{Error, ValueType, OHLCV};

/// Source enum represents common parts of a *Candle*
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
pub enum Source {
	/// *Close* part of a candle
	Close,

	/// *Open* part of a candle
	Open,

	/// *High* part of a candle
	High,

	/// *Low* part of a candle
	Low,

	/// (*High*+*Low*)/2 part of a candle
	HL2,

	/// [Typical price](https://en.wikipedia.org/wiki/Typical_price) of a candle
	TP,

	/// *Volume* part of a candle
	Volume,

	/// Same as `typical price * volume`
	#[cfg_attr(feature = "serde", serde(rename = "volumed_price"))]
	VolumedPrice,
}

impl FromStr for Source {
	type Err = Error;

	fn from_str(s: &str) -> Result<Self, Self::Err> {
		match s.to_ascii_lowercase().trim() {
			"close" => Ok(Self::Close),
			"high" => Ok(Self::High),
			"low" => Ok(Self::Low),
			"volume" => Ok(Self::Volume),
			"tp" | "hlc3" => Ok(Self::TP),
			"hl2" => Ok(Self::HL2),
			"open" => Ok(Self::Open),
			"volumed_price" => Ok(Self::VolumedPrice),

			value => Err(Error::SourceParse(value.to_string())),
		}
	}
}

impl TryFrom<&str> for Source {
	type Error = Error;

	fn try_from(s: &str) -> Result<Self, Self::Error> {
		Self::from_str(s)
	}
}

impl TryFrom<String> for Source {
	type Error = Error;

	fn try_from(s: String) -> Result<Self, Self::Error> {
		Self::from_str(s.as_str())
	}
}

impl From<Source> for &'static str {
	fn from(value: Source) -> Self {
		match value {
			Source::Close => "close",
			Source::High => "high",
			Source::Low => "low",
			Source::Open => "open",
			Source::TP => "tp",
			Source::HL2 => "hl2",
			Source::Volume => "volume",
			Source::VolumedPrice => "volumed_price",
		}
	}
}

impl From<Source> for String {
	fn from(value: Source) -> Self {
		let s: &str = value.into();
		s.to_string()
	}
}

/// Simple Candlestick structure for implementing [`OHLCV`]
///
/// Can be also used by an alias [`Candlestick`]
///
/// You may convert simple tuples of 4 or 5 float values into Candle:
/// ```
/// use yata::prelude::Candle;
/// //               open  high  low  close
/// let my_candle = (3.0,  5.0,  2.0, 4.0  );
/// let converted: Candle = my_candle.into();
/// println!("{:?}", converted);
/// ```
///
/// ```
/// use yata::prelude::Candle;
/// //               open  high  low  close  volume
/// let my_candle = (3.0,  5.0,  2.0, 4.0  ,  50.0 );
/// let converted: Candle = my_candle.into();
/// println!("{:?}", converted);
/// ```
#[derive(Debug, Clone, Copy, Default, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Candle {
	/// *Open* value of the candle
	pub open: ValueType,

	/// *High* value of the candle
	pub high: ValueType,

	/// *Low* value of the candle
	pub low: ValueType,

	/// *Close* value of the candle
	pub close: ValueType,

	/// *Volume* value of the candle
	pub volume: ValueType,
}

impl Candle {
	/// Creates `Candle` from any another `OHLCV`-object.
	pub fn from<T: OHLCV>(src: &T) -> Self {
		Self {
			open: src.open(),
			high: src.high(),
			low: src.low(),
			close: src.close(),
			volume: src.volume(),
		}
	}
}

/// Just an alias for [Candle]
pub type Candlestick = Candle;

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

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

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

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

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

impl From<(ValueType, ValueType, ValueType, ValueType)> for Candle {
	fn from(value: (ValueType, ValueType, ValueType, ValueType)) -> Self {
		Self {
			open: value.0,
			high: value.1,
			low: value.2,
			close: value.3,
			volume: ValueType::NAN,
		}
	}
}

impl From<(ValueType, ValueType, ValueType, ValueType, ValueType)> for Candle {
	fn from(value: (ValueType, ValueType, ValueType, ValueType, ValueType)) -> Self {
		Self {
			open: value.0,
			high: value.1,
			low: value.2,
			close: value.3,
			volume: value.4,
		}
	}
}

impl PartialEq for Candle {
	fn eq(&self, other: &Self) -> bool {
		self.open.to_bits() == other.open.to_bits()
			&& self.high.to_bits() == other.high.to_bits()
			&& self.low.to_bits() == other.low.to_bits()
			&& self.close.to_bits() == other.close.to_bits()
			&& self.volume.to_bits() == other.volume.to_bits()
	}
}

impl Eq for Candle {}

#[cfg(test)]
mod tests {
	use super::Source;

	#[test]
	fn test_source_to_string_str() {
		let values = [
			Source::Open,
			Source::High,
			Source::Low,
			Source::Close,
			Source::Volume,
			Source::VolumedPrice,
			Source::TP,
			Source::HL2,
		];

		values.iter().for_each(|&v| {
			let r1: String = v.into();
			let r2: &str = v.into();

			assert_eq!(r1, r2);

			match v {
				Source::Open => assert_eq!("open", r1),
				Source::High => assert_eq!("high", r1),
				Source::Low => assert_eq!("low", r1),
				Source::Close => assert_eq!("close", r1),
				Source::Volume => assert_eq!("volume", r1),
				Source::VolumedPrice => assert_eq!("volumed_price", r1),
				Source::TP => assert_eq!("tp", r1),
				Source::HL2 => assert_eq!("hl2", r1),
			}
		});
	}

	#[test]
	fn test_source_from_string() {
		let values = [
			"oPeN",
			"HIGH",
			"low",
			"cLose",
			"volume",
			"vOluMeD_prIcE",
			"tP",
			"hlc3",
			"Hl2",
		];

		values.iter().enumerate().for_each(|(i, s)| {
			let r: Source = s.parse().unwrap();
			match i {
				0 => assert_eq!(Source::Open, r),
				1 => assert_eq!(Source::High, r),
				2 => assert_eq!(Source::Low, r),
				3 => assert_eq!(Source::Close, r),
				4 => assert_eq!(Source::Volume, r),
				5 => assert_eq!(Source::VolumedPrice, r),
				6 | 7 => assert_eq!(Source::TP, r),
				8 => assert_eq!(Source::HL2, r),
				_ => panic!("Wow. You cannot be here."),
			}
		});

		let src: Result<Source, _> = "some other string".parse();

		assert!(src.is_err());
	}
}