use crate::chart::BandOptions;
use crate::data::{DataChange, PaneId, Timestamp};
use crate::geom::Rgba;
use crate::series::Source;
mod custom;
mod layer;
mod min_max;
mod transform;
pub use custom::{AxisMode, CustomSeries, SeriesCtx};
pub(crate) use custom::{CustomChange, CustomStore};
pub use layer::{LayerCtx, LayerPlugin, LayerPosition};
pub use min_max::MinMaxIndex;
pub use transform::{MAX_OUTPUT_PER_CALL, SeriesTransform, TransformBar, TransformOutput};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Placement {
Overlay,
NewPane,
Pane(PaneId),
Volume,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OutputKind {
Line,
Histogram,
Points,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct OutputSpec {
pub name: &'static str,
pub kind: OutputKind,
pub color: Option<Rgba>,
pub shift: i32,
pub fill_to: Option<(usize, BandOptions)>,
}
impl OutputSpec {
pub const fn line(name: &'static str) -> Self {
Self {
name,
kind: OutputKind::Line,
color: None,
shift: 0,
fill_to: None,
}
}
pub const fn histogram(name: &'static str) -> Self {
Self {
name,
kind: OutputKind::Histogram,
color: None,
shift: 0,
fill_to: None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct InputBar {
pub time: Timestamp,
pub open: f64,
pub high: f64,
pub low: f64,
pub close: f64,
pub volume: f64,
}
#[derive(Clone, Copy)]
pub struct IndicatorInput<'a> {
store: Source<'a>,
index: &'a crate::data::TimeIndex,
first: i64,
len: usize,
}
impl<'a> IndicatorInput<'a> {
pub(crate) fn new(store: Source<'a>, index: &'a crate::data::TimeIndex) -> Self {
let (first, end) = store.span().unwrap_or((0, 0));
Self {
store,
index,
first,
len: usize::try_from(end - first).unwrap_or(0),
}
}
pub(crate) fn first(&self) -> i64 {
self.first
}
pub fn len(&self) -> usize {
self.len
}
pub fn is_empty(&self) -> bool {
self.len == 0
}
pub fn bar(&self, slot: usize) -> Option<InputBar> {
if slot >= self.len {
return None;
}
let j = self.first + i64::try_from(slot).ok()?;
let [open, high, low, close] = self.store.bar_at(j)?;
Some(InputBar {
time: self.index.time_at(j)?,
open,
high,
low,
close,
volume: self.store.volume_at(j).unwrap_or(f64::NAN),
})
}
pub fn has_volume(&self) -> bool {
self.store.has_volume()
}
}
#[derive(Clone, Debug, Default)]
pub struct IndicatorOutputs {
start: usize,
len: usize,
active: usize,
cols: Vec<Vec<f64>>,
}
impl IndicatorOutputs {
pub(crate) fn reset(&mut self, outputs: usize, start: usize, len: usize) {
self.start = start.min(len);
self.len = len;
if self.cols.len() < outputs {
self.cols.resize_with(outputs, Vec::new);
}
self.active = outputs;
for c in &mut self.cols[..outputs] {
c.clear();
c.resize(len - self.start, f64::NAN);
}
}
pub(crate) fn shrink(&mut self) {
for c in &mut self.cols {
c.clear();
c.shrink_to(64);
}
}
pub(crate) fn columns(&self) -> &[Vec<f64>] {
&self.cols[..self.active]
}
pub fn start(&self) -> usize {
self.start
}
pub fn len(&self) -> usize {
self.len
}
pub fn is_empty(&self) -> bool {
self.start >= self.len
}
pub fn column(&mut self, o: usize) -> &mut [f64] {
self.cols[..self.active]
.get_mut(o)
.map_or(&mut [], Vec::as_mut_slice)
}
}
pub trait Indicator: Send {
fn name(&self) -> &str;
fn outputs(&self) -> &[OutputSpec];
fn default_placement(&self) -> Placement;
fn warmup(&self) -> usize;
fn needs_volume(&self) -> bool {
false
}
fn update(
&mut self,
input: &IndicatorInput<'_>,
change: DataChange,
out: &mut IndicatorOutputs,
);
}