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

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

use crate::core::{Sequence, ValueType, OHLC, OHLCV};

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

	/// *Open* part of a candle
	#[cfg_attr(feature = "serde", serde(rename = "open"))]
	Open,

	/// *High* part of a candle
	#[cfg_attr(feature = "serde", serde(rename = "high"))]
	High,

	/// *Low* part of a candle
	#[cfg_attr(feature = "serde", serde(rename = "low"))]
	Low,

	/// (*High*+*Low*)/2 part of a candle
	#[cfg_attr(feature = "serde", serde(rename = "hl2"))]
	HL2,

	/// Typical price of a candle
	#[cfg_attr(feature = "serde", serde(rename = "tp"))]
	TP,

	/// *Volume* part of a candle
	#[cfg_attr(feature = "serde", serde(rename = "volume"))]
	Volume,
}

impl FromStr for Source {
	type Err = String;

	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" => Ok(Self::TP),
			"hl2" => Ok(Self::HL2),
			"open" => Ok(Self::Open),

			_ => Err(format!("Unknown source {}", s)),
		}
	}
}

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

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

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

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

/// Simple Candlestick structure for implementing [OHLC] and [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, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Candle {
	/// *Open* value of the candle
	#[cfg_attr(feature = "serde", serde(rename = "open"))]
	pub open: ValueType,

	/// *High* value of the candle
	#[cfg_attr(feature = "serde", serde(rename = "high"))]
	pub high: ValueType,

	/// *Low* value of the candle
	#[cfg_attr(feature = "serde", serde(rename = "low"))]
	pub low: ValueType,

	/// *Close* value of the candle
	#[cfg_attr(feature = "serde", serde(rename = "close"))]
	pub close: ValueType,

	/// *Volume* value of the candle
	#[cfg_attr(feature = "serde", serde(rename = "volume"))]
	pub volume: ValueType,
}

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

impl OHLC 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
	}
}

impl OHLCV for Candle {
	#[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: 0.0,
		}
	}
}

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,
		}
	}
}

/// Just an alias for the Sequence of any `T`
pub type Candles<T> = Sequence<T>;