diff --git a/crates/brk_computer/src/distribution/metrics/realized/adjusted.rs b/crates/brk_computer/src/distribution/metrics/realized/adjusted.rs index e7d0ba3d0..127a1b571 100644 --- a/crates/brk_computer/src/distribution/metrics/realized/adjusted.rs +++ b/crates/brk_computer/src/distribution/metrics/realized/adjusted.rs @@ -1,11 +1,11 @@ use brk_error::Result; use brk_traversable::Traversable; use brk_types::{Cents, Height, StoredF64, Version}; -use vecdb::{Exit, Ident, ReadableCloneableVec, ReadableVec, Rw, StorageMode}; +use vecdb::{Exit, ReadableVec, Rw, StorageMode}; use crate::{ ComputeIndexes, blocks, - internal::{ComputedFromHeight, LazyFromHeight, RatioCents64, RollingWindows}, + internal::{ComputedFromHeight, RatioCents64, RollingEmas7d30d, RollingWindows}, }; use crate::distribution::metrics::ImportConfig; @@ -23,10 +23,7 @@ pub struct RealizedAdjusted { // === Adjusted SOPR (rolling window ratios) === pub adjusted_sopr: RollingWindows, - pub adjusted_sopr_24h_7d_ema: ComputedFromHeight, - pub adjusted_sopr_7d_ema: LazyFromHeight, - pub adjusted_sopr_24h_30d_ema: ComputedFromHeight, - pub adjusted_sopr_30d_ema: LazyFromHeight, + pub adjusted_sopr_ema: RollingEmas7d30d, } impl RealizedAdjusted { @@ -55,32 +52,9 @@ impl RealizedAdjusted { let adjusted_sopr = RollingWindows::forced_import( cfg.db, &cfg.name("adjusted_sopr"), cfg.version + v1, cfg.indexes, )?; - - macro_rules! import_computed { - ($name:expr) => { - ComputedFromHeight::forced_import( - cfg.db, - &cfg.name($name), - cfg.version + v1, - cfg.indexes, - )? - }; - } - - let adjusted_sopr_24h_7d_ema = import_computed!("adjusted_sopr_24h_7d_ema"); - let adjusted_sopr_7d_ema = LazyFromHeight::from_computed::( - &cfg.name("adjusted_sopr_7d_ema"), - cfg.version + v1, - adjusted_sopr_24h_7d_ema.height.read_only_boxed_clone(), - &adjusted_sopr_24h_7d_ema, - ); - let adjusted_sopr_24h_30d_ema = import_computed!("adjusted_sopr_24h_30d_ema"); - let adjusted_sopr_30d_ema = LazyFromHeight::from_computed::( - &cfg.name("adjusted_sopr_30d_ema"), - cfg.version + v1, - adjusted_sopr_24h_30d_ema.height.read_only_boxed_clone(), - &adjusted_sopr_24h_30d_ema, - ); + let adjusted_sopr_ema = RollingEmas7d30d::forced_import( + cfg.db, &cfg.name("adjusted_sopr_24h"), cfg.version + v1, cfg.indexes, + )?; Ok(RealizedAdjusted { adjusted_value_created, @@ -88,10 +62,7 @@ impl RealizedAdjusted { adjusted_value_created_sum, adjusted_value_destroyed_sum, adjusted_sopr, - adjusted_sopr_24h_7d_ema, - adjusted_sopr_7d_ema, - adjusted_sopr_24h_30d_ema, - adjusted_sopr_30d_ema, + adjusted_sopr_ema, }) } @@ -140,22 +111,13 @@ impl RealizedAdjusted { } // Adjusted SOPR EMAs (based on 24h window) - self.adjusted_sopr_24h_7d_ema - .height - .compute_rolling_ema( - starting_indexes.height, - &blocks.count.height_1w_ago, - &self.adjusted_sopr._24h.height, - exit, - )?; - self.adjusted_sopr_24h_30d_ema - .height - .compute_rolling_ema( - starting_indexes.height, - &blocks.count.height_1m_ago, - &self.adjusted_sopr._24h.height, - exit, - )?; + self.adjusted_sopr_ema.compute_from_24h( + starting_indexes.height, + &blocks.count.height_1w_ago, + &blocks.count.height_1m_ago, + &self.adjusted_sopr._24h.height, + exit, + )?; Ok(()) } diff --git a/crates/brk_computer/src/distribution/metrics/realized/base.rs b/crates/brk_computer/src/distribution/metrics/realized/base.rs index 1cf774bed..925e3cdda 100644 --- a/crates/brk_computer/src/distribution/metrics/realized/base.rs +++ b/crates/brk_computer/src/distribution/metrics/realized/base.rs @@ -4,7 +4,7 @@ use brk_types::{ Bitcoin, Cents, CentsSats, CentsSigned, CentsSquaredSats, Dollars, Height, Sats, StoredF32, StoredF64, Version, }; use vecdb::{ - AnyStoredVec, AnyVec, BytesVec, Exit, Ident, ImportableVec, ReadableCloneableVec, + AnyStoredVec, AnyVec, BytesVec, Exit, ImportableVec, ReadableCloneableVec, ReadableVec, Rw, StorageMode, WritableVec, }; @@ -15,7 +15,7 @@ use crate::{ CentsPlus, CentsUnsignedToDollars, ComputedFromHeightCumulative, ComputedFromHeight, ComputedFromHeightRatio, NegCentsUnsignedToDollars, ValueFromHeightCumulative, LazyFromHeight, PercentageCentsF32, PercentageCentsSignedCentsF32, PercentageCentsSignedDollarsF32, Price, RatioCents64, - RollingWindows, Identity, ValueFromHeight, + RollingEmas7d30d, RollingWindows, Identity, ValueFromHeight, }, prices, }; @@ -85,18 +85,12 @@ pub struct RealizedBase { // === SOPR (rolling window ratios) === pub sopr: RollingWindows, - pub sopr_24h_7d_ema: ComputedFromHeight, - pub sopr_7d_ema: LazyFromHeight, - pub sopr_24h_30d_ema: ComputedFromHeight, - pub sopr_30d_ema: LazyFromHeight, + pub sopr_ema: RollingEmas7d30d, // === Sell Side Risk === pub realized_value_sum: RollingWindows, pub sell_side_risk_ratio: RollingWindows, - pub sell_side_risk_ratio_24h_7d_ema: ComputedFromHeight, - pub sell_side_risk_ratio_7d_ema: LazyFromHeight, - pub sell_side_risk_ratio_24h_30d_ema: ComputedFromHeight, - pub sell_side_risk_ratio_30d_ema: LazyFromHeight, + pub sell_side_risk_ratio_ema: RollingEmas7d30d, // === Net Realized PnL Deltas === pub net_realized_pnl_cumulative_30d_delta: ComputedFromHeight, @@ -349,51 +343,13 @@ impl RealizedBase { cfg.db, &cfg.name("sell_side_risk_ratio"), cfg.version + v1, cfg.indexes, )?; - // === EMA imports + identity aliases === - macro_rules! import_computed { - ($name:expr) => { - ComputedFromHeight::forced_import( - cfg.db, - &cfg.name($name), - cfg.version + v1, - cfg.indexes, - )? - }; - } - - let sopr_24h_7d_ema = import_computed!("sopr_24h_7d_ema"); - let sopr_7d_ema = LazyFromHeight::from_computed::( - &cfg.name("sopr_7d_ema"), - cfg.version + v1, - sopr_24h_7d_ema.height.read_only_boxed_clone(), - &sopr_24h_7d_ema, - ); - let sopr_24h_30d_ema = import_computed!("sopr_24h_30d_ema"); - let sopr_30d_ema = LazyFromHeight::from_computed::( - &cfg.name("sopr_30d_ema"), - cfg.version + v1, - sopr_24h_30d_ema.height.read_only_boxed_clone(), - &sopr_24h_30d_ema, - ); - - let sell_side_risk_ratio_24h_7d_ema = import_computed!("sell_side_risk_ratio_24h_7d_ema"); - let sell_side_risk_ratio_7d_ema = LazyFromHeight::from_computed::( - &cfg.name("sell_side_risk_ratio_7d_ema"), - cfg.version + v1, - sell_side_risk_ratio_24h_7d_ema - .height - .read_only_boxed_clone(), - &sell_side_risk_ratio_24h_7d_ema, - ); - let sell_side_risk_ratio_24h_30d_ema = import_computed!("sell_side_risk_ratio_24h_30d_ema"); - let sell_side_risk_ratio_30d_ema = LazyFromHeight::from_computed::( - &cfg.name("sell_side_risk_ratio_30d_ema"), - cfg.version + v1, - sell_side_risk_ratio_24h_30d_ema - .height - .read_only_boxed_clone(), - &sell_side_risk_ratio_24h_30d_ema, - ); + // === EMA imports === + let sopr_ema = RollingEmas7d30d::forced_import( + cfg.db, &cfg.name("sopr_24h"), cfg.version + v1, cfg.indexes, + )?; + let sell_side_risk_ratio_ema = RollingEmas7d30d::forced_import( + cfg.db, &cfg.name("sell_side_risk_ratio_24h"), cfg.version + v1, cfg.indexes, + )?; let peak_regret_rel_to_realized_cap = ComputedFromHeight::forced_import( cfg.db, @@ -443,16 +399,10 @@ impl RealizedBase { value_created_sum, value_destroyed_sum, sopr, - sopr_24h_7d_ema, - sopr_7d_ema, - sopr_24h_30d_ema, - sopr_30d_ema, + sopr_ema, realized_value_sum, sell_side_risk_ratio, - sell_side_risk_ratio_24h_7d_ema, - sell_side_risk_ratio_7d_ema, - sell_side_risk_ratio_24h_30d_ema, - sell_side_risk_ratio_30d_ema, + sell_side_risk_ratio_ema, net_realized_pnl_cumulative_30d_delta: ComputedFromHeight::forced_import( cfg.db, &cfg.name("net_realized_pnl_cumulative_30d_delta"), @@ -939,36 +889,22 @@ impl RealizedBase { )?; // SOPR EMAs (based on 24h window) - self.sopr_24h_7d_ema.height.compute_rolling_ema( + self.sopr_ema.compute_from_24h( starting_indexes.height, &blocks.count.height_1w_ago, - &self.sopr._24h.height, - exit, - )?; - self.sopr_24h_30d_ema.height.compute_rolling_ema( - starting_indexes.height, &blocks.count.height_1m_ago, &self.sopr._24h.height, exit, )?; // Sell side risk EMAs (based on 24h window) - self.sell_side_risk_ratio_24h_7d_ema - .height - .compute_rolling_ema( - starting_indexes.height, - &blocks.count.height_1w_ago, - &self.sell_side_risk_ratio._24h.height, - exit, - )?; - self.sell_side_risk_ratio_24h_30d_ema - .height - .compute_rolling_ema( - starting_indexes.height, - &blocks.count.height_1m_ago, - &self.sell_side_risk_ratio._24h.height, - exit, - )?; + self.sell_side_risk_ratio_ema.compute_from_24h( + starting_indexes.height, + &blocks.count.height_1w_ago, + &blocks.count.height_1m_ago, + &self.sell_side_risk_ratio._24h.height, + exit, + )?; // Realized profit/loss/net relative to realized cap self.realized_profit_rel_to_realized_cap diff --git a/crates/brk_computer/src/internal/emas/mod.rs b/crates/brk_computer/src/internal/emas/mod.rs new file mode 100644 index 000000000..99a5634e2 --- /dev/null +++ b/crates/brk_computer/src/internal/emas/mod.rs @@ -0,0 +1,5 @@ +mod _14d; +mod _7d_30d; + +pub use _14d::*; +pub use _7d_30d::*; diff --git a/crates/brk_computer/src/internal/mod.rs b/crates/brk_computer/src/internal/mod.rs index 0e42c96ff..df9d33c41 100644 --- a/crates/brk_computer/src/internal/mod.rs +++ b/crates/brk_computer/src/internal/mod.rs @@ -1,6 +1,7 @@ mod compute; mod distribution_stats; mod eager_indexes; +mod emas; mod from_height; mod from_tx; mod group; @@ -21,6 +22,7 @@ mod windows; pub(crate) use compute::*; pub(crate) use distribution_stats::*; pub(crate) use eager_indexes::*; +pub(crate) use emas::*; pub(crate) use from_height::*; pub(crate) use from_tx::*; pub(crate) use group::*; diff --git a/crates/brk_computer/src/internal/rolling/emas/mod.rs b/crates/brk_computer/src/internal/rolling/emas/mod.rs new file mode 100644 index 000000000..99a5634e2 --- /dev/null +++ b/crates/brk_computer/src/internal/rolling/emas/mod.rs @@ -0,0 +1,5 @@ +mod _14d; +mod _7d_30d; + +pub use _14d::*; +pub use _7d_30d::*; diff --git a/crates/brk_computer/src/internal/rolling/mod.rs b/crates/brk_computer/src/internal/rolling/mod.rs index 8e62a242c..f90f60fc3 100644 --- a/crates/brk_computer/src/internal/rolling/mod.rs +++ b/crates/brk_computer/src/internal/rolling/mod.rs @@ -1,9 +1,11 @@ mod distribution; +mod emas; mod full; mod value_windows; mod windows; pub use distribution::*; +pub use emas::*; pub use full::*; pub use value_windows::*; pub use windows::*;