MCPcopy Create free account
hub / github.com/Open-Quant/openquant / new_from_csv

Method new_from_csv

crates/openquant/src/microstructural_features.rs:629–666  ·  view source on GitHub ↗
(
        trades_path: &str,
        tick_num_series: &[usize],
        volume_encoding: Option<Vec<(f64, char)>>,
        pct_encoding: Option<Vec<(f64, char)>>,
    )

Source from the content-addressed store, hash-verified

627}
628
629/// Trades-based Amihud's lambda: the regression through the origin of per-trade absolute log
630/// returns on dollar volume, `Σ(DV · |r|) / Σ DV²` (AFML §19.4.2).
631///
632/// Returns `NaN` if every dollar volume is zero.
633///
634/// ```
635/// use openquant::microstructural_features::get_trades_based_amihud_lambda;
636///
637/// # fn main() -> Result<(), openquant::util::InputError> {
638/// // (100 * 0.01 + 200 * 0.02) / (100^2 + 200^2) = 5 / 50_000
639/// let lambda = get_trades_based_amihud_lambda(&[0.01, -0.02], &[100.0, 200.0])?;
640/// assert!((lambda - 1e-4).abs() < 1e-15);
641/// # Ok(())
642/// # }
643/// ```
644///
645/// # Errors
646///
647/// [`InputError::LengthMismatch`] if `dollar_volume.len() != log_ret.len()`.
648pub fn get_trades_based_amihud_lambda(
649 log_ret: &[f64],
650 dollar_volume: &[f64],
651) -> Result<f64, InputError> {
652 same_length("dollar_volume", dollar_volume, log_ret.len())?;
653 let num: f64 = dollar_volume.iter().zip(log_ret.iter()).map(|(x, y)| x * y.abs()).sum();
654 let den: f64 = dollar_volume.iter().map(|x| x * x).sum();
655 Ok(if den == 0.0 { f64::NAN } else { num / den })
656}
657
658/// Trades-based Hasbrouck's lambda: the regression through the origin of per-trade log
659/// returns on signed root dollar volume, `Σ(r · a√DV) / Σ(a√DV)²` (AFML §19.4.3).
660///
661/// `aggressor_flags` are `+1` (buy) or `-1` (sell). Returns `NaN` if every signed root
662/// dollar volume is zero, and `NaN` if any dollar volume is negative.
663///
664/// This regresses the *signed* return, as in AFML. Earlier versions regressed the absolute
665/// return, so buys and sells cancelled and the estimate was near zero under balanced flow
666/// ([#105](https://github.com/Open-Quant/openquant/issues/105)); results computed before
667/// that fix are not comparable.
668///
669/// ```

Callers

nothing calls this directly

Calls 2

parse_datetimeFunction · 0.85
lenMethod · 0.80

Tested by

no test coverage detected