Trait yata::core::OHLCV

source ·
pub trait OHLCV: 'static {
Show 16 methods // Required methods fn open(&self) -> ValueType; fn high(&self) -> ValueType; fn low(&self) -> ValueType; fn close(&self) -> ValueType; fn volume(&self) -> ValueType; // Provided methods fn tp(&self) -> ValueType { ... } fn hl2(&self) -> ValueType { ... } fn ohlc4(&self) -> ValueType { ... } fn clv(&self) -> ValueType { ... } fn tr(&self, prev_candle: &dyn OHLCV) -> ValueType { ... } fn tr_close(&self, prev_close: ValueType) -> ValueType { ... } fn validate(&self) -> bool { ... } fn source(&self, source: Source) -> ValueType { ... } fn volumed_price(&self) -> ValueType { ... } fn is_rising(&self) -> bool { ... } fn is_falling(&self) -> bool { ... }
}
Expand description

Basic trait for implementing Open-High-Low-Close-Volume timeseries data.

It has already implemented for tuple of 5 float values:

use yata::prelude::OHLCV;
//         open high low  close, volume
let row = (2.0, 5.0, 1.0,  4.0,   10.0 );
assert_eq!(row.open(), row.0);
assert_eq!(row.high(), row.1);
assert_eq!(row.low(), row.2);
assert_eq!(row.close(), row.3);
assert_eq!(row.volume(), row.4);

See also Candle.

Required Methods§

source

fn open(&self) -> ValueType

Should return an open value of the period

source

fn high(&self) -> ValueType

Should return an highest value of the period

source

fn low(&self) -> ValueType

Should return an lowest value of the period

source

fn close(&self) -> ValueType

Should return an close value of the candle

source

fn volume(&self) -> ValueType

Should return volume value for the period

Provided Methods§

source

fn tp(&self) -> ValueType

Calculates Typical price. It’s just a simple (High + Low + Close) / 3

§Examples
use yata::prelude::*;
use yata::core::Candle;

let candle = Candle {
    high: 10.0,
    low: 5.0,
    close: 9.0,
    ..Candle::default()
};

assert_eq!(candle.tp(), 8.0);
source

fn hl2(&self) -> ValueType

Calculates arithmetic average of high and low values of the candle

§Examples
use yata::prelude::*;
use yata::core::Candle;

let candle = Candle {
    high: 10.0,
    low: 5.0,
    ..Candle::default()
};

assert_eq!(candle.hl2(), 7.5);
source

fn ohlc4(&self) -> ValueType

Calculates arithmetic average of high, low, open and close values of the candle

§Examples
use yata::prelude::*;
use yata::core::Candle;

let candle = Candle {
    high: 10.0,
    low: 5.0,
    open: 2.0,
    close: 3.0,
    ..Candle::default()
};

assert_eq!(candle.ohlc4(), 5.0);
source

fn clv(&self) -> ValueType

CLV = [(close - low) - (high - close)] / (high - low)

§Examples
use yata::prelude::*;
use yata::core::Candle;
let candle = Candle {
    high: 5.0,
    low: 2.0,
    close: 4.0,
    ..Candle::default()
};

assert_eq!(candle.clv(), ((candle.close()-candle.low()) - (candle.high() - candle.close()))/(candle.high() - candle.low()));
assert_eq!(candle.clv(), ((4. - 2.) - (5. - 4.))/(5. - 2.));
source

fn tr(&self, prev_candle: &dyn OHLCV) -> ValueType

Calculates True Range over last two candles

§Examples
use yata::prelude::*;
use yata::core::Candle;

let candle1 = Candle {
    close: 70.0,
    ..Candle::default()
};

let candle2 = Candle {
    high: 100.0,
    low: 50.0,
    ..Candle::default()
};

let tr = candle2.tr(&candle1);
assert_eq!(tr, 50.);
use yata::prelude::*;
use yata::core::Candle;

let candle1 = Candle {
    close: 30.0,
    ..Candle::default()
};

let candle2 = Candle {
    high: 100.0,
    low: 50.0,
    ..Candle::default()
};

let tr = candle2.tr(&candle1);
assert_eq!(tr, 70.);
source

fn tr_close(&self, prev_close: ValueType) -> ValueType

Calculates True Range over last two candles using close price from the previous candle.

source

fn validate(&self) -> bool

Validates candle attributes

Returns true if validates OK

§Examples
use yata::prelude::*;
use yata::core::Candle;

let candle1 = Candle {
    open: 7.0,
    high: 10.0,
    low: 7.0,
    close: 11.0, // cannot be more than high

    ..Candle::default()
};
let candle2 = Candle {
    open: 10.0,
    high: 10.0,
    low: 11.0, // low cannot be more than any other value of the candle
    close: 10.0,

    ..Candle::default()
};

assert!(!candle1.validate());
assert!(!candle2.validate());
source

fn source(&self, source: Source) -> ValueType

Returns Source field value of the candle.

§Examples
use yata::prelude::*;
use yata::core::{Candle, Source};
let candle = Candle {
    open: 12.0,
    high: 15.0,
    low: 7.0,
    close: 10.0,
    ..Candle::default()
};
assert_eq!(OHLCV::source(&candle, Source::Low), 7.0);
assert_eq!(OHLCV::source(&candle, "close".to_string().parse().unwrap()), 10.0);
source

fn volumed_price(&self) -> ValueType

Volumed price

Same as OHLCV::tp() * OHLCV::volume()

source

fn is_rising(&self) -> bool

Checks if candle is “rising”: it’s close value greater than open value

source

fn is_falling(&self) -> bool

Checks if candle is “falling”: it’s close value smaller than open value

Trait Implementations§

source§

impl From<&(dyn OHLCV + 'static)> for Candle

source§

fn from(src: &dyn OHLCV) -> Self

Converts to this type from the input type.

Implementations on Foreign Types§

source§

impl OHLCV for (ValueType, ValueType, ValueType, ValueType, ValueType)

source§

impl OHLCV for [ValueType; 5]

Implementors§