Skip to main content

SeriesTransform

Trait SeriesTransform 

Source
pub trait SeriesTransform: Send {
    // Required methods
    fn name(&self) -> &str;
    fn output_kind(&self) -> SeriesKind;
    fn push(&mut self, bar: InputBar, out: &mut TransformOutput);
    fn peek(&self, bar: InputBar, out: &mut TransformOutput);
    fn reset(&mut self);

    // Provided method
    fn box_size(&self) -> Option<f64> { ... }
}
Expand description

Turns input bars into a new series, with the peek/push model of indicators: the output is the elements push wrote for every input but the last (finished, never changed again) followed by what peek writes for the last input (the elements it would finish and the ones still forming). Gap bars are never passed in.

§Examples

use ferrochart_core::frame::SeriesKind;
use ferrochart_core::plugin::{InputBar, SeriesTransform, TransformBar, TransformOutput};

/// One element per input whose close moved by at least 1 since the last one.
#[derive(Default)]
struct Steps {
    last: Option<f64>,
}

impl Steps {
    fn step(last: Option<f64>, bar: &InputBar) -> Option<TransformBar> {
        let from = last?;
        ((bar.close - from).abs() >= 1.0).then(|| TransformBar {
            open: from,
            high: from.max(bar.close),
            low: from.min(bar.close),
            close: bar.close,
            volume: f64::NAN,
            flag: 0,
        })
    }
}

impl SeriesTransform for Steps {
    fn name(&self) -> &str {
        "steps"
    }
    fn output_kind(&self) -> SeriesKind {
        SeriesKind::Candlestick
    }
    fn push(&mut self, bar: InputBar, out: &mut TransformOutput) {
        match Self::step(self.last, &bar) {
            Some(b) => {
                out.push(b);
                self.last = Some(bar.close);
            }
            None if self.last.is_none() => self.last = Some(bar.close),
            None => {}
        }
    }
    fn peek(&self, bar: InputBar, out: &mut TransformOutput) {
        if let Some(b) = Self::step(self.last, &bar) {
            out.push(b);
        }
    }
    fn reset(&mut self) {
        self.last = None;
    }
}

Required Methods§

Source

fn name(&self) -> &str

Name, e.g. "renko".

Source

fn output_kind(&self) -> SeriesKind

How the output is drawn: Candlestick, OhlcBar, Kagi or PointFigure.

Source

fn push(&mut self, bar: InputBar, out: &mut TransformOutput)

Commits bar and writes the elements it finishes.

Source

fn peek(&self, bar: InputBar, out: &mut TransformOutput)

Writes, without changing the state, the elements bar would finish and the ones still forming if it were the last input.

Source

fn reset(&mut self)

Forgets all inputs.

Provided Methods§

Source

fn box_size(&self) -> Option<f64>

Point & Figure: the price height of one box, to stack the X/O marks.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§