use crate::data::lod::Lod;
use crate::data::ohlc::{OhlcStore, check_columns};
use crate::data::value::{ValueStore, check_values, densify};
use crate::data::{
BarUpdate, ChangeLog, DataChange, OhlcColumns, OhlcLevelView, SeriesMapping, TimeIndex,
Timestamp, ValidationMode, ValueColumns,
};
use crate::error::{Error, Result};
use crate::frame::SeriesKind;
use crate::plugin::CustomStore;
use crate::transform::store::TransformStore;
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum SeriesData {
Ohlc(OhlcColumns),
Value(ValueColumns),
}
impl From<OhlcColumns> for SeriesData {
fn from(c: OhlcColumns) -> Self {
Self::Ohlc(c)
}
}
impl From<ValueColumns> for SeriesData {
fn from(c: ValueColumns) -> Self {
Self::Value(c)
}
}
impl SeriesData {
pub(crate) fn times(&self) -> &[Timestamp] {
match self {
Self::Ohlc(c) => &c.time,
Self::Value(c) => &c.time,
}
}
pub(crate) fn take_times(mut self) -> Result<(Vec<Timestamp>, Self)> {
let (time, n, column) = match &mut self {
Self::Ohlc(c) => (&mut c.time, c.open.len(), "open"),
Self::Value(c) => (&mut c.time, c.value.len(), "value"),
};
if time.len() != n {
return Err(Error::LengthMismatch { column });
}
let times = core::mem::take(time);
Ok((times, self))
}
pub(crate) fn point(kind: SeriesKind, bar: BarUpdate) -> Self {
if is_value_kind(kind) {
Self::Value(ValueColumns {
time: vec![bar.time],
value: vec![bar.close],
color: None,
})
} else {
Self::Ohlc(OhlcStore::bar_columns(bar, true))
}
}
pub(crate) fn concat(self, later: Self) -> Result<Self> {
match (self, later) {
(Self::Ohlc(mut a), Self::Ohlc(b)) => {
if a.volume.is_some() != b.volume.is_some() {
return Err(Error::LengthMismatch { column: "volume" });
}
a.time.extend_from_slice(&b.time);
a.open.extend_from_slice(&b.open);
a.high.extend_from_slice(&b.high);
a.low.extend_from_slice(&b.low);
a.close.extend_from_slice(&b.close);
if let (Some(x), Some(y)) = (a.volume.as_mut(), b.volume.as_ref()) {
x.extend_from_slice(y);
}
Ok(Self::Ohlc(a))
}
(Self::Value(mut a), Self::Value(b)) => {
let (na, nb) = (a.value.len(), b.value.len());
a.color = match (a.color.take(), b.color) {
(None, None) => None,
(x, y) => {
let mut c = x.unwrap_or_else(|| vec![0; na]);
c.extend(y.unwrap_or_else(|| vec![0; nb]));
Some(c)
}
};
a.time.extend_from_slice(&b.time);
a.value.extend_from_slice(&b.value);
Ok(Self::Value(a))
}
(Self::Ohlc(_), _) => Err(Error::InvalidArgument("this series needs OHLC data")),
(Self::Value(_), _) => Err(Error::InvalidArgument("this series needs value data")),
}
}
}
const NOT_ON_AXIS: Error =
Error::InvalidArgument("the times must be bars of the chart's time axis");
const NO_OWN_DATA: Error =
Error::InvalidArgument("this series has no data of its own; use its source series");
#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
pub(crate) enum Store {
None,
Ohlc(OhlcStore),
HeikinAshi {
raw: OhlcStore,
ha: OhlcStore,
},
Value(ValueStore),
Custom(Box<CustomStore>),
Transform(Box<TransformStore>),
}
#[derive(Clone, Copy)]
pub(crate) enum Source<'a> {
Plain(PlainSource<'a>),
Custom(&'a Store),
}
#[derive(Clone, Copy)]
pub(crate) enum PlainSource<'a> {
None,
Ohlc {
span: &'a OhlcStore,
bars: &'a OhlcStore,
},
Value(&'a ValueStore),
}
impl Source<'_> {
pub(crate) fn span(&self) -> Option<(i64, i64)> {
match self {
Self::Custom(s) => s.span(),
Self::Plain(PlainSource::None) => None,
Self::Plain(PlainSource::Ohlc { span: s, .. }) => {
(s.len() > 0).then(|| (s.first(), s.end()))
}
Self::Plain(PlainSource::Value(s)) => (s.len() > 0).then(|| (s.first(), s.end())),
}
}
pub(crate) fn bar_at(&self, j: i64) -> Option<[f64; 4]> {
match self {
Self::Custom(s) => s.bar_at(j),
Self::Plain(PlainSource::None) => None,
Self::Plain(PlainSource::Ohlc { bars, .. }) => bars.bar_at(j),
Self::Plain(PlainSource::Value(s)) => {
s.value_at(j).filter(|v| !v.is_nan()).map(|v| [v; 4])
}
}
}
pub(crate) fn volume_at(&self, j: i64) -> Option<f64> {
match self {
Self::Plain(PlainSource::Ohlc { bars, .. }) => bars.volume_at(j),
Self::Custom(s) => s.volume_at(j),
Self::Plain(_) => None,
}
}
pub(crate) fn has_volume(&self) -> bool {
match self {
Self::Plain(PlainSource::Ohlc { bars, .. }) => bars.has_volume(),
Self::Custom(s) => s.has_volume(),
Self::Plain(_) => false,
}
}
}
pub(crate) fn is_ohlc_kind(kind: SeriesKind) -> bool {
matches!(
kind,
SeriesKind::Candlestick | SeriesKind::OhlcBar | SeriesKind::HeikinAshi
)
}
pub(crate) fn is_value_kind(kind: SeriesKind) -> bool {
matches!(
kind,
SeriesKind::Line
| SeriesKind::Area
| SeriesKind::Baseline
| SeriesKind::Histogram
| SeriesKind::Points
)
}
fn ha_bar(prev: Option<[f64; 4]>, raw: [f64; 4]) -> [f64; 4] {
let [o, h, l, c] = raw;
let close = (o + h + l + c) * 0.25;
let open = match prev {
Some([po, _, _, pc]) => (po + pc) * 0.5,
None => (o + c) * 0.5,
};
[open, h.max(open).max(close), l.min(open).min(close), close]
}
fn ha_columns(raw: &OhlcStore, from: i64, to: i64, mut prev: Option<[f64; 4]>) -> OhlcColumns {
let n = usize::try_from(to - from).unwrap_or(0);
let mut cols = OhlcColumns {
time: Vec::new(),
open: Vec::with_capacity(n),
high: Vec::with_capacity(n),
low: Vec::with_capacity(n),
close: Vec::with_capacity(n),
volume: raw.has_volume().then(|| Vec::with_capacity(n)),
};
for j in from..to {
let x = match raw.bar_at(j) {
Some(bar) => {
let x = ha_bar(prev, bar);
prev = Some(x);
x
}
None => [f64::NAN; 4],
};
cols.open.push(x[0]);
cols.high.push(x[1]);
cols.low.push(x[2]);
cols.close.push(x[3]);
if let Some(v) = cols.volume.as_mut() {
v.push(raw.volume_at(j).unwrap_or(0.0));
}
}
cols
}
fn last_bar_before(s: &OhlcStore, j: i64) -> Option<[f64; 4]> {
(s.first()..j.min(s.end())).rev().find_map(|k| s.bar_at(k))
}
fn rebuild_ha(raw: &OhlcStore, ha: &mut OhlcStore) -> Result<()> {
ha.replace_at(
raw.first(),
ha_columns(raw, raw.first(), raw.end(), None),
ValidationMode::Repair,
)?;
Ok(())
}
fn extend_ha(raw: &OhlcStore, ha: &mut OhlcStore, from: i64) -> Result<()> {
if ha.len() == 0 || ha.first() != raw.first() || ha.end() != from {
return rebuild_ha(raw, ha);
}
let prev = last_bar_before(ha, from);
ha.append(
None,
ha_columns(raw, from, raw.end(), prev),
ValidationMode::Repair,
)?;
Ok(())
}
fn densify_ohlc(
offsets: Option<&[u32]>,
cols: &OhlcColumns,
lead: usize,
trail: usize,
) -> Result<OhlcColumns> {
let span = match offsets {
Some(o) => o.last().map_or(0, |&x| x as usize + 1),
None => cols.open.len(),
};
let n = lead + span + trail;
let column = |v: f64| -> Result<Vec<f64>> {
let mut c = Vec::new();
c.try_reserve_exact(n).map_err(|_| Error::OutOfMemory)?;
c.resize(n, v);
Ok(c)
};
let mut out = OhlcColumns {
time: Vec::new(),
open: column(f64::NAN)?,
high: column(f64::NAN)?,
low: column(f64::NAN)?,
close: column(f64::NAN)?,
volume: match cols.volume {
Some(_) => Some(column(0.0)?),
None => None,
},
};
for i in 0..cols.open.len() {
let at = lead + offsets.and_then(|o| o.get(i)).map_or(i, |&x| x as usize);
out.open[at] = cols.open[i];
out.high[at] = cols.high[i];
out.low[at] = cols.low[i];
out.close[at] = cols.close[i];
if let (Some(dst), Some(src)) = (out.volume.as_mut(), cols.volume.as_ref()) {
dst[at] = src[i];
}
}
Ok(out)
}
fn bar_update(time: i64, x: [f64; 4], volume: Option<f64>) -> BarUpdate {
BarUpdate {
time,
open: x[0],
high: x[1],
low: x[2],
close: x[3],
volume,
}
}
fn check_value_cols(c: &ValueColumns) -> Result<()> {
if c.value.len() != c.time.len() {
return Err(Error::LengthMismatch { column: "value" });
}
if c.color.as_ref().is_some_and(|k| k.len() != c.time.len()) {
return Err(Error::LengthMismatch { column: "color" });
}
check_values(&c.value)
}
fn split(m: SeriesMapping) -> (i64, Option<Vec<u32>>) {
match m {
SeriesMapping::Aligned { first } => (first, None),
SeriesMapping::Mapped { first, offsets } => (first, Some(offsets)),
}
}
fn dense_with_anchor(
offsets: Option<&[u32]>,
cols: &ValueColumns,
anchor: Option<(i64, f64)>,
interpolate: bool,
) -> Result<(Vec<f64>, Option<Vec<u32>>)> {
let n = cols.value.len();
let mut offs: Vec<i64> = match offsets {
Some(o) => o.iter().map(|&x| i64::from(x)).collect(),
None => (0..i64::try_from(n).unwrap_or(0)).collect(),
};
let mut values = cols.value.clone();
let mut colors = cols.color.clone();
let before = anchor.is_some_and(|(at, _)| at < 0);
if let Some((at, v)) = anchor {
if before {
offs.insert(0, at);
values.insert(0, v);
if let Some(c) = colors.as_mut() {
c.insert(0, 0);
}
} else {
offs.push(at);
values.push(v);
if let Some(c) = colors.as_mut() {
c.push(0);
}
}
}
let base = offs.first().copied().unwrap_or(0);
let rel: Vec<u32> = offs
.iter()
.map(|&o| u32::try_from(o - base).map_err(|_| NOT_ON_AXIS))
.collect::<Result<_>>()?;
let (mut v, mut c) = densify(&rel, &values, colors.as_deref(), interpolate)?;
if anchor.is_some() {
if before {
v.remove(0);
if let Some(c) = c.as_mut() {
c.remove(0);
}
} else {
v.pop();
if let Some(c) = c.as_mut() {
c.pop();
}
}
}
Ok((v, c))
}
impl Store {
pub(crate) fn for_kind(kind: SeriesKind) -> Self {
match kind {
SeriesKind::HeikinAshi => Self::HeikinAshi {
raw: OhlcStore::default(),
ha: OhlcStore::default(),
},
k if is_ohlc_kind(k) => Self::Ohlc(OhlcStore::default()),
k if is_value_kind(k) => Self::Value(ValueStore::default()),
_ => Self::None,
}
}
pub(crate) fn source(&self) -> Source<'_> {
Source::Plain(match self {
Self::None => PlainSource::None,
Self::Ohlc(s) => PlainSource::Ohlc { span: s, bars: s },
Self::HeikinAshi { raw, ha } => PlainSource::Ohlc {
span: raw,
bars: ha,
},
Self::Transform(t) => PlainSource::Ohlc {
span: &t.out,
bars: &t.out,
},
Self::Value(s) => PlainSource::Value(s),
Self::Custom(_) => return Source::Custom(self),
})
}
fn drawn(&self) -> Option<&OhlcStore> {
match self {
Self::Ohlc(s) | Self::HeikinAshi { ha: s, .. } => Some(s),
Self::Transform(t) => Some(&t.out),
_ => None,
}
}
pub(crate) fn transform(&self) -> Option<&TransformStore> {
match self {
Self::Transform(t) => Some(t),
_ => None,
}
}
pub(crate) fn len(&self) -> usize {
match self {
Self::None => 0,
Self::Ohlc(s) | Self::HeikinAshi { ha: s, .. } => s.len(),
Self::Value(s) => s.len(),
Self::Custom(c) => c.len(),
Self::Transform(t) => t.out.len(),
}
}
pub(crate) fn is_empty(&self) -> bool {
self.len() == 0
}
pub(crate) fn span(&self) -> Option<(i64, i64)> {
match self {
Self::None => None,
Self::Ohlc(s) | Self::HeikinAshi { raw: s, .. } => {
(s.len() > 0).then(|| (s.first(), s.end()))
}
Self::Transform(t) => (t.out.len() > 0).then(|| (t.out.first(), t.out.end())),
Self::Value(s) => (s.len() > 0).then(|| (s.first(), s.end())),
Self::Custom(c) => c.span(),
}
}
pub(crate) fn takes_data(&self) -> bool {
matches!(
self,
Self::Ohlc(_) | Self::HeikinAshi { .. } | Self::Value(_) | Self::Transform(_)
)
}
pub(crate) fn custom(&self) -> Option<&CustomStore> {
match self {
Self::Custom(c) => Some(c),
_ => None,
}
}
pub(crate) fn log(&self) -> Option<&ChangeLog> {
match self {
Self::None => None,
Self::Ohlc(s) | Self::HeikinAshi { ha: s, .. } => Some(s.log()),
Self::Value(s) => Some(s.log()),
Self::Custom(c) => Some(&c.log),
Self::Transform(t) => Some(t.out.log()),
}
}
pub(crate) fn lod(&self) -> Option<&Lod> {
match self {
Self::None | Self::Custom(_) => None,
Self::Ohlc(s) | Self::HeikinAshi { ha: s, .. } => Some(s.lod()),
Self::Transform(t) => Some(t.out.lod()),
Self::Value(s) => Some(s.lod()),
}
}
pub(crate) fn has_volume(&self) -> bool {
self.drawn().is_some_and(OhlcStore::has_volume)
}
pub(crate) fn level_view(&self, level: u8) -> Option<OhlcLevelView<'_>> {
match self {
Self::None | Self::Custom(_) => None,
Self::Ohlc(s) | Self::HeikinAshi { ha: s, .. } => s.level_view(level),
Self::Transform(t) => t.out.level_view(level),
Self::Value(s) => s.level_view(level),
}
}
pub(crate) fn colors(&self) -> Option<(i64, &[u32])> {
match self {
Self::Value(s) => s.colors().map(|c| (s.first(), c)),
_ => None,
}
}
pub(crate) fn bar_at(&self, j: i64) -> Option<[f64; 4]> {
match self {
Self::None => None,
Self::Ohlc(s) | Self::HeikinAshi { ha: s, .. } => s.bar_at(j),
Self::Transform(t) => t.out.bar_at(j),
Self::Value(s) => s.value_at(j).filter(|v| !v.is_nan()).map(|v| [v; 4]),
Self::Custom(c) => c.value_at(j).map(|v| [v; 4]),
}
}
pub(crate) fn volume_at(&self, j: i64) -> Option<f64> {
self.drawn()?.volume_at(j)
}
pub(crate) fn extremes(&mut self, from: i64, to: i64) -> Option<(f64, f64)> {
match self {
Self::None | Self::Custom(_) => None,
Self::Ohlc(s) | Self::HeikinAshi { ha: s, .. } => s.extremes(from, to),
Self::Transform(t) => t.out.extremes(from, to),
Self::Value(s) => s.extremes(from, to),
}
}
pub(crate) fn heap_bytes(&self) -> usize {
match self {
Self::None => 0,
Self::Ohlc(s) => s.heap_bytes(),
Self::HeikinAshi { raw, ha } => raw.heap_bytes() + ha.heap_bytes(),
Self::Value(s) => s.heap_bytes(),
Self::Custom(c) => c.heap_bytes(),
Self::Transform(t) => t.heap_bytes(),
}
}
pub(crate) fn snapshot(&self, index: &TimeIndex) -> Option<SeriesData> {
match self {
Self::None | Self::Custom(_) | Self::Transform(_) => None,
Self::Ohlc(s) | Self::HeikinAshi { raw: s, .. } => {
let mut cols = OhlcColumns {
volume: s.has_volume().then(Vec::new),
..OhlcColumns::default()
};
for j in s.first()..s.end() {
let (Some(t), Some(b)) = (index.time_at(j), s.bar_at(j)) else {
continue;
};
cols.time.push(t);
cols.open.push(b[0]);
cols.high.push(b[1]);
cols.low.push(b[2]);
cols.close.push(b[3]);
if let Some(v) = cols.volume.as_mut() {
v.push(s.volume_at(j).unwrap_or(0.0));
}
}
Some(SeriesData::Ohlc(cols))
}
Self::Value(s) => {
let mut cols = ValueColumns {
color: s.colors().map(|_| Vec::new()),
..ValueColumns::default()
};
for j in s.first()..s.end() {
let (Some(t), Some(v)) = (index.time_at(j), s.value_at(j)) else {
continue;
};
if v.is_nan() {
continue;
}
cols.time.push(t);
cols.value.push(v);
if let (Some(c), Some(src)) = (cols.color.as_mut(), s.colors()) {
let p = usize::try_from(j - s.first()).unwrap_or(0);
c.push(src.get(p).copied().unwrap_or(0));
}
}
Some(SeriesData::Value(cols))
}
}
}
}
pub(crate) struct Op<'a> {
pub kind: SeriesKind,
pub index: &'a TimeIndex,
pub mode: ValidationMode,
}
impl Op<'_> {
fn ohlc(data: SeriesData) -> Result<OhlcColumns> {
match data {
SeriesData::Ohlc(c) => Ok(c),
SeriesData::Value(_) => Err(Error::InvalidArgument("this series needs OHLC data")),
}
}
fn value(data: SeriesData) -> Result<ValueColumns> {
match data {
SeriesData::Value(c) => Ok(c),
SeriesData::Ohlc(_) => Err(Error::InvalidArgument("this series needs value data")),
}
}
fn interpolate(&self) -> bool {
!matches!(self.kind, SeriesKind::Histogram | SeriesKind::Points)
}
fn map(&self, times: &[Timestamp]) -> Result<SeriesMapping> {
self.index.map_series(times)?.ok_or(NOT_ON_AXIS)
}
fn check_times(cols: &OhlcColumns) -> Result<()> {
if cols.time.len() == cols.open.len() {
Ok(())
} else {
Err(Error::LengthMismatch { column: "open" })
}
}
pub(crate) fn set(&self, store: &mut Store, data: SeriesData) -> Result<usize> {
match store {
Store::None | Store::Custom(_) | Store::Transform(_) => Err(NO_OWN_DATA),
Store::Ohlc(s) => self.set_ohlc(s, Self::ohlc(data)?),
Store::HeikinAshi { raw, ha } => {
let repaired = self.set_ohlc(raw, Self::ohlc(data)?)?;
rebuild_ha(raw, ha)?;
Ok(repaired)
}
Store::Value(s) => {
let cols = Self::value(data)?;
check_value_cols(&cols)?;
let (first, offsets) = split(self.map(&cols.time)?);
match offsets {
None => s.replace(first, cols.value, cols.color)?,
Some(o) => {
let (v, c) =
densify(&o, &cols.value, cols.color.as_deref(), self.interpolate())?;
s.replace(first, v, c)?;
}
}
Ok(0)
}
}
}
pub(crate) fn set_aligned(
&self,
store: &mut Store,
first: i64,
data: SeriesData,
) -> Result<usize> {
match store {
Store::None | Store::Custom(_) | Store::Transform(_) => Err(NO_OWN_DATA),
Store::Ohlc(s) => s.replace_at(first, Self::ohlc(data)?, self.mode),
Store::HeikinAshi { raw, ha } => {
let repaired = raw.replace_at(first, Self::ohlc(data)?, self.mode)?;
rebuild_ha(raw, ha)?;
Ok(repaired)
}
Store::Value(s) => {
let cols = Self::value(data)?;
s.replace(first, cols.value, cols.color)?;
Ok(0)
}
}
}
fn set_ohlc(&self, s: &mut OhlcStore, mut cols: OhlcColumns) -> Result<usize> {
Self::check_times(&cols)?;
match self.map(&cols.time)? {
SeriesMapping::Aligned { first } => s.replace_at(first, cols, self.mode),
SeriesMapping::Mapped { first, offsets } => {
let repaired = check_columns(&mut cols, self.mode)?;
s.replace_at(first, densify_ohlc(Some(&offsets), &cols, 0, 0)?, self.mode)?;
Ok(repaired)
}
}
}
pub(crate) fn append(&self, store: &mut Store, data: SeriesData) -> Result<usize> {
if store.is_empty() {
return self.set(store, data);
}
match store {
Store::None | Store::Custom(_) | Store::Transform(_) => Err(NO_OWN_DATA),
Store::Ohlc(s) => self.append_ohlc(s, Self::ohlc(data)?),
Store::HeikinAshi { raw, ha } => {
let old_end = raw.end();
let repaired = self.append_ohlc(raw, Self::ohlc(data)?)?;
extend_ha(raw, ha, old_end)?;
Ok(repaired)
}
Store::Value(s) => {
let cols = Self::value(data)?;
check_value_cols(&cols)?;
let (first, offsets) = split(self.map(&cols.time)?);
if first < s.end() {
return Err(Error::TimeNotIncreasing { index: 0 });
}
if offsets.is_none() && first == s.end() {
s.append(&cols.value, cols.color.as_deref())?;
return Ok(0);
}
let anchor = s.value_at(s.end() - 1).map(|v| (s.end() - 1 - first, v));
let (v, c) =
dense_with_anchor(offsets.as_deref(), &cols, anchor, self.interpolate())?;
s.append(&v, c.as_deref())?;
Ok(0)
}
}
}
fn append_ohlc(&self, s: &mut OhlcStore, mut cols: OhlcColumns) -> Result<usize> {
Self::check_times(&cols)?;
if cols.open.is_empty() {
return Ok(0);
}
let (first, offsets) = split(self.map(&cols.time)?);
if first < s.end() {
return Err(Error::TimeNotIncreasing { index: 0 });
}
let lead = usize::try_from(first - s.end()).map_err(|_| NOT_ON_AXIS)?;
if offsets.is_none() && lead == 0 {
return s.append(None, cols, self.mode);
}
let repaired = check_columns(&mut cols, self.mode)?;
s.append(
None,
densify_ohlc(offsets.as_deref(), &cols, lead, 0)?,
self.mode,
)?;
Ok(repaired)
}
pub(crate) fn prepend(&self, store: &mut Store, data: SeriesData) -> Result<usize> {
if store.is_empty() {
return self.set(store, data);
}
match store {
Store::None | Store::Custom(_) | Store::Transform(_) => Err(NO_OWN_DATA),
Store::Ohlc(s) => self.prepend_ohlc(s, Self::ohlc(data)?),
Store::HeikinAshi { raw, ha } => {
let repaired = self.prepend_ohlc(raw, Self::ohlc(data)?)?;
rebuild_ha(raw, ha)?;
Ok(repaired)
}
Store::Value(s) => {
let cols = Self::value(data)?;
check_value_cols(&cols)?;
if cols.value.is_empty() {
return Ok(0);
}
let (first, offsets) = split(self.map(&cols.time)?);
let last_off = offsets
.as_ref()
.and_then(|o| o.last().map(|&x| i64::from(x)))
.unwrap_or_else(|| i64::try_from(cols.value.len()).unwrap_or(0) - 1);
if first + last_off >= s.first() {
return Err(Error::TimeNotIncreasing {
index: cols.value.len().saturating_sub(1),
});
}
if offsets.is_none() && first + last_off + 1 == s.first() {
s.prepend(&cols.value, cols.color.as_deref())?;
return Ok(0);
}
let anchor = s.value_at(s.first()).map(|v| (s.first() - first, v));
let (v, c) =
dense_with_anchor(offsets.as_deref(), &cols, anchor, self.interpolate())?;
s.prepend(&v, c.as_deref())?;
Ok(0)
}
}
}
fn prepend_ohlc(&self, s: &mut OhlcStore, mut cols: OhlcColumns) -> Result<usize> {
Self::check_times(&cols)?;
let n = cols.open.len();
if n == 0 {
return Ok(0);
}
let (first, offsets) = split(self.map(&cols.time)?);
let span = offsets
.as_ref()
.and_then(|o| o.last())
.map_or(n, |&x| x as usize + 1);
let end = first.saturating_add(i64::try_from(span).unwrap_or(i64::MAX));
if end > s.first() {
return Err(Error::TimeNotIncreasing { index: n - 1 });
}
let trail = usize::try_from(s.first() - end).map_err(|_| NOT_ON_AXIS)?;
if offsets.is_none() && trail == 0 {
return s.prepend(None, cols, self.mode);
}
let repaired = check_columns(&mut cols, self.mode)?;
s.prepend(
None,
densify_ohlc(offsets.as_deref(), &cols, 0, trail)?,
self.mode,
)?;
Ok(repaired)
}
pub(crate) fn update_last(
&self,
store: &mut Store,
bar: BarUpdate,
) -> Result<(DataChange, usize)> {
let j = self.index.index_of(bar.time).ok_or(NOT_ON_AXIS)?;
match store {
Store::None | Store::Custom(_) | Store::Transform(_) => Err(NO_OWN_DATA),
Store::Ohlc(s) => self.update_ohlc(s, j, bar),
Store::HeikinAshi { raw, ha } => {
let old_end = raw.end();
let (change, repaired) = self.update_ohlc(raw, j, bar)?;
let in_place = change == DataChange::UpdateLast
&& ha.len() > 0
&& ha.first() == raw.first()
&& ha.end() == raw.end();
if in_place {
let h = match raw.bar_at(j) {
Some(x) => ha_bar(last_bar_before(ha, j), x),
None => [f64::NAN; 4],
};
ha.set_last(
bar_update(bar.time, h, raw.volume_at(j)),
ValidationMode::Repair,
)?;
} else if change == DataChange::UpdateLast {
rebuild_ha(raw, ha)?;
} else {
extend_ha(raw, ha, old_end)?;
}
Ok((change, repaired))
}
Store::Value(s) => {
let v = bar.close;
check_values(&[v])?;
if s.len() > 0 {
let last = s.end() - 1;
if j == last {
s.set_last(v, None)?;
return Ok((DataChange::UpdateLast, 0));
}
if j < last {
return Err(Error::TimeNotIncreasing { index: 0 });
}
}
let old_end = s.end();
let fresh = s.len() == 0;
self.append(store, SeriesData::point(self.kind, bar))?;
let count = if fresh { 1 } else { appended(old_end, store) };
Ok((DataChange::Append { count }, 0))
}
}
}
fn update_ohlc(
&self,
s: &mut OhlcStore,
j: i64,
bar: BarUpdate,
) -> Result<(DataChange, usize)> {
if s.len() > 0 {
if j == s.end() - 1 {
return Ok((DataChange::UpdateLast, s.set_last(bar, self.mode)?));
}
if j < s.end() - 1 {
return Err(Error::TimeNotIncreasing { index: 0 });
}
let old_end = s.end();
let repaired = self.append_ohlc(s, OhlcStore::bar_columns(bar, true))?;
let count = usize::try_from(s.end() - old_end).unwrap_or(1);
return Ok((DataChange::Append { count }, repaired));
}
let repaired = s.replace_at(j, OhlcStore::bar_columns(bar, false), self.mode)?;
Ok((DataChange::Append { count: 1 }, repaired))
}
}
fn appended(old_end: i64, store: &Store) -> usize {
store
.span()
.and_then(|(_, end)| usize::try_from(end - old_end).ok())
.unwrap_or(1)
}
#[cfg(test)]
#[allow(clippy::float_cmp)] mod tests {
use super::*;
use proptest::prelude::*;
fn bars(times: &[i64], closes: &[f64]) -> OhlcColumns {
OhlcColumns {
time: times.to_vec(),
open: closes.to_vec(),
high: closes.iter().map(|c| c + 1.0).collect(),
low: closes.iter().map(|c| c - 1.0).collect(),
close: closes.to_vec(),
volume: None,
}
}
fn op(kind: SeriesKind, index: &TimeIndex) -> Op<'_> {
Op {
kind,
index,
mode: ValidationMode::Reject,
}
}
fn axis(times: &[i64]) -> TimeIndex {
TimeIndex::from_times(times.to_vec()).unwrap()
}
fn v(time: &[i64], value: &[f64]) -> SeriesData {
SeriesData::Value(ValueColumns {
time: time.to_vec(),
value: value.to_vec(),
color: None,
})
}
#[test]
fn sparse_line_is_densified_and_extended() {
let index = axis(&[10, 20, 30, 40, 50]);
let mut line = Store::for_kind(SeriesKind::Line);
let o = op(SeriesKind::Line, &index);
o.set(&mut line, v(&[10, 30], &[1.0, 3.0])).unwrap();
assert_eq!(line.bar_at(1), Some([2.0; 4])); o.append(&mut line, v(&[50], &[7.0])).unwrap();
assert_eq!(line.bar_at(3), Some([5.0; 4]));
assert_eq!(line.len(), 5);
assert!(o.set(&mut line, v(&[15], &[1.0])).is_err()); let mut hist = Store::for_kind(SeriesKind::Histogram);
op(SeriesKind::Histogram, &index)
.set(&mut hist, v(&[10, 30], &[1.0, 3.0]))
.unwrap();
assert_eq!(hist.bar_at(1), None);
assert_eq!(hist.span(), Some((0, 3)));
}
#[test]
fn sparse_ohlc_gets_gap_bars() {
let index = axis(&[1, 2, 3, 4, 5, 6]);
let mut s = Store::for_kind(SeriesKind::OhlcBar);
let o = op(SeriesKind::OhlcBar, &index);
o.set(&mut s, SeriesData::Ohlc(bars(&[1, 3], &[1.0, 3.0])))
.unwrap();
assert_eq!(s.span(), Some((0, 3)));
assert_eq!(s.bar_at(1), None);
assert_eq!(s.bar_at(2), Some([3.0, 4.0, 2.0, 3.0]));
let bar = BarUpdate {
time: 5,
open: 5.0,
high: 6.0,
low: 4.0,
close: 5.5,
volume: None,
};
assert_eq!(
o.update_last(&mut s, bar).unwrap().0,
DataChange::Append { count: 2 }
);
assert_eq!(
o.update_last(&mut s, bar).unwrap().0,
DataChange::UpdateLast
);
assert_eq!(s.bar_at(3), None);
assert_eq!(s.bar_at(4), Some([5.0, 6.0, 4.0, 5.5]));
o.prepend(&mut s, SeriesData::Ohlc(bars(&[1], &[0.5])))
.unwrap_err(); let Some(SeriesData::Ohlc(snap)) = s.snapshot(&index) else {
panic!("no snapshot");
};
assert_eq!(snap.time, [1, 3, 5]);
let mut bad = bars(&[6], &[1.0]);
bad.open[0] = f64::NAN;
assert!(o.append(&mut s, SeriesData::Ohlc(bad)).is_err());
assert!(
o.append(&mut s, SeriesData::Ohlc(bars(&[6], &[f64::NAN])))
.is_ok()
);
assert_eq!(s.bar_at(5), None);
}
#[test]
fn heikin_ashi_follows_the_formula() {
let index = axis(&[1, 2, 3]);
let mut s = Store::for_kind(SeriesKind::HeikinAshi);
let raw = OhlcColumns {
time: vec![1, 3],
open: vec![10.0, 12.0],
high: vec![14.0, 15.0],
low: vec![9.0, 11.0],
close: vec![12.0, 13.0],
volume: None,
};
op(SeriesKind::HeikinAshi, &index)
.set(&mut s, SeriesData::Ohlc(raw))
.unwrap();
assert_eq!(s.bar_at(0), Some([11.0, 14.0, 9.0, 11.25]));
assert_eq!(s.bar_at(1), None);
assert_eq!(s.bar_at(2), Some([11.125, 15.0, 11.0, 12.75]));
}
proptest! {
#[test]
fn heikin_ashi_incremental_equals_full(
closes in proptest::collection::vec(1.0f64..100.0, 2..60),
ticks in proptest::collection::vec((0i64..3, 1.0f64..100.0), 1..30),
) {
let times: Vec<i64> = (0..closes.len() as i64).collect();
let index = axis(&(0..=closes.len() as i64 + 2 * 30).collect::<Vec<_>>());
let mut s = Store::for_kind(SeriesKind::HeikinAshi);
let o = op(SeriesKind::HeikinAshi, &index);
o.set(&mut s, SeriesData::Ohlc(bars(×, &closes))).unwrap();
let mut all = bars(×, &closes);
for (step, c) in ticks {
let t = *all.time.last().unwrap() + step;
let bar = BarUpdate { time: t, open: c, high: c + 1.0, low: c - 1.0, close: c, volume: None };
o.update_last(&mut s, bar).unwrap();
if step > 0 {
all.time.push(t); all.open.push(c); all.high.push(c + 1.0);
all.low.push(c - 1.0); all.close.push(c);
} else {
let p = all.time.len() - 1;
all.open[p] = c; all.high[p] = c + 1.0; all.low[p] = c - 1.0; all.close[p] = c;
}
}
let mut full = Store::for_kind(SeriesKind::HeikinAshi);
o.set(&mut full, SeriesData::Ohlc(all.clone())).unwrap();
prop_assert_eq!(s.span(), full.span());
for j in 0..index.len() as i64 {
prop_assert_eq!(s.bar_at(j), full.bar_at(j));
}
}
}
}