Skip to main content

Module vwap

Module vwap 

Source
Expand description

§VWAP (volume-weighted average price)

typical_t = (H + L + C) / 3   // or close-only via VwapPriceSource
vwap_t    = sum(typical_i * vol_i) / sum(vol_i)   // over session or rolling window

§Word problem

Session opens; bars print (TP=10, V=100) then (TP=11, V=100). What is cumulative VWAP?

Expect: first bar 10; second (10*100 + 11*100) / 200 = 10.5.

use finance_solution::stocks::ta::{vwap, VwapParams};
let h = [10.0, 11.0];
let l = [10.0, 11.0];
let c = [10.0, 11.0];
let v = [100.0, 100.0];
let s = vwap(&h, &l, &c, &v, VwapParams::cumulative_typical()).unwrap();
assert!((s.vwap[0].unwrap() - 10.0).abs() < 1e-12);
assert!((s.vwap[1].unwrap() - 10.5).abs() < 1e-12);

§Modes (VwapMode)

  • Cumulative — from bar 0 (or from last [VwapState::reset]) — classic intraday.
  • Rolling — last period bars only.

Day reset is your policy: call VwapState::reset() at session open, or rebuild state from the day’s history. The library never invents a calendar.

§Quant pattern

use finance_solution::stocks::ta::{VwapParams, ValidatedVwap, VwapState};

const INTRADAY: VwapParams = VwapParams::cumulative_typical();
let eng = ValidatedVwap::new(INTRADAY).unwrap();
let s = eng.compute(&h, &l, &c, &vol).unwrap();
let mut live = VwapState::new(INTRADAY).unwrap();
let _ = live.push_bars(&h, &l, &c, &vol).unwrap();
// live.reset(); // e.g. regular-session open — you decide
assert!(s.vwap[2].unwrap().is_finite());

§Sample solution table

period  typical  volume     vwap
------  -------  ------  -------
     0   9.5000  100.00   9.5000
     1  10.5000  200.00  10.1667
     2  11.5000  150.00  10.6111

Structs§

ValidatedVwap
Validated VWAP config.
VwapParams
VWAP parameter pack.
VwapSeries
VwapSolution

Enums§

VwapMode
Cumulative session vs rolling window.
VwapPriceSource
Price input for VWAP numerator.

Functions§

vwap
vwap_solution
Examples