binance_client/http_api_v3/data/klines/
kline.rs

1//!
2//! A single kline.
3//!
4
5use rust_decimal::Decimal;
6use serde::Deserialize;
7
8///
9/// A single kline.
10///
11#[derive(Debug, Deserialize, Clone)]
12#[serde(rename_all = "camelCase")]
13pub struct Kline {
14    /// The kline open time in milliseconds since Unix epoch.
15    pub open_time: i64,
16    /// The kline open price.
17    pub open: Decimal,
18    /// The kline high price.
19    pub high: Decimal,
20    /// The kline low price.
21    pub low: Decimal,
22    /// The kline close price.
23    pub close: Decimal,
24    /// The kline volume in secondary token.
25    pub volume: Decimal,
26    /// The kline open time in milliseconds since Unix epoch.
27    pub close_time: i64,
28    /// The kline volume in primary token.
29    pub quote_asset_volume: Decimal,
30    /// The number of trades executed within the kline.
31    pub number_of_trades: i64,
32    /// The taker buy volume in secondary token.
33    pub taker_buy_base_asset_volume: Decimal,
34    /// The taker buy volume in primary token.
35    pub taker_buy_quote_asset_volume: Decimal,
36    /// The unknown value.
37    pub ignore: Decimal,
38}
39
40impl Kline {
41    ///
42    /// If the kline is green, that is, `close >= open`.
43    ///
44    pub fn is_green(&self) -> bool {
45        self.close >= self.open
46    }
47
48    ///
49    /// If the kline is red, that is, `close < open`.
50    ///
51    pub fn is_red(&self) -> bool {
52        self.close < self.open
53    }
54
55    ///
56    /// The average of low and high.
57    ///
58    pub fn middle(&self) -> Decimal {
59        (self.low + self.high) / Decimal::new(2, 0)
60    }
61
62    ///
63    /// The average of open and close.
64    ///
65    pub fn middle_body(&self) -> Decimal {
66        (self.open + self.close) / Decimal::new(2, 0)
67    }
68
69    ///
70    /// The average of open, high, low, and close.
71    ///
72    pub fn average(&self) -> Decimal {
73        (self.low + self.open + self.close + self.high) / Decimal::new(4, 0)
74    }
75}