Skip to main content

Module state

Module state 

Source
Expand description

Incremental state machines for live / streaming bar updates.

§What this is

Pure math state: push one bar at a time, push_bars for multi-bar payloads, or seed with from_history. Not a market-data engine — your quant system owns the feed, symbols, and calendars.

§Constructor naming: newFinanceResult (not try_new)

Fallible construction uses new, matching this crate’s Result-only style (Schedule::new_repeating, File::open in the standard library — fallibility lives in the return type, not a try_ prefix). There is no panicking twin.

§What this is not

  • No sockets, no multi-symbol registry, no auto “session open”
  • Day reset of VWAP/RVOL is your call to VwapState::reset / rebuild

§Quant engine sketch (per symbol)

use finance_solution::stocks::ta::{
    StochasticParams, StochState, VwapParams, VwapState, EmaState,
};

const FAST: StochasticParams = StochasticParams::fast(9, 3);

struct SymbolPipeline {
    stoch: StochState,
    vwap: VwapState,
    ema20: EmaState,
}

impl SymbolPipeline {
    fn new() -> finance_solution::FinanceResult<Self> {
        Ok(Self {
            stoch: StochState::new(FAST)?,
            vwap: VwapState::new(VwapParams::cumulative_typical())?,
            ema20: EmaState::new(20)?,
        })
    }

    /// Seed from historical bars, then only push live bars.
    fn seed_history(
        &mut self,
        high: &[f64],
        low: &[f64],
        close: &[f64],
        volume: &[f64],
    ) -> finance_solution::FinanceResult<()> {
        self.stoch = StochState::from_history(FAST, high, low, close)?;
        self.vwap = VwapState::from_history(
            VwapParams::cumulative_typical(),
            high, low, close, volume,
        )?;
        self.ema20 = EmaState::from_history(20, close)?;
        Ok(())
    }

    fn on_bar(
        &mut self,
        high: f64,
        low: f64,
        close: f64,
        volume: f64,
    ) -> finance_solution::FinanceResult<()> {
        let _kd = self.stoch.push(high, low, close)?;
        let _vw = self.vwap.push(high, low, close, volume)?;
        let _e = self.ema20.push(close)?;
        Ok(())
    }

    /// Caller owns the calendar — e.g. regular-session open.
    fn on_session_open_reset_vwap(&mut self) {
        self.vwap.reset();
        // stoch/ema often continue; reset only if *your* strategy wants it
    }
}

§Parity with batch

Streaming state is defined to match batch series functions on the same path (within floating-point tolerance). Prefer batch compute for research; prefer state for live multi-symbol updates.

Re-exports§

pub use crate::stocks::ta::moving_average::EmaState;
pub use crate::stocks::ta::moving_average::SmaState;

Structs§

BollingerBarOutput
One-bar Bollinger output.
BollingerState
Incremental Bollinger Bands (sample stdev on the window).
KeltnerBarOutput
KeltnerState
Incremental Keltner (EMA mid + Wilder ATR).
MacdState
Incremental MACD (fast/slow/signal EMAs).
RvolState
Incremental relative volume.
StochBarOutput
One-bar stochastic output (warm-up allowed as None).
StochState
Incremental stochastic (fast/full via StochasticParams).
VwapState
Incremental VWAP (cumulative or rolling). Call VwapState::reset at session open if desired.