global: snapshot

This commit is contained in:
nym21
2026-03-07 18:17:20 +01:00
parent cbad78962f
commit 9bea14b341
20 changed files with 1256 additions and 1557 deletions

View File

@@ -476,7 +476,6 @@ impl UTXOCohorts<Rw> {
blocks,
prices,
starting_indexes,
height_to_market_cap,
ss,
exit,
)

View File

@@ -117,7 +117,6 @@ impl AllCohortMetrics {
self.relative.compute(
starting_indexes.height,
&self.unrealized,
&self.realized,
&self.supply.total.sats.height,
height_to_market_cap,
exit,

View File

@@ -1,14 +1,14 @@
use brk_cohort::Filter;
use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::{Dollars, Height, Indexes, Sats};
use brk_types::{Height, Indexes, Sats};
use vecdb::{AnyStoredVec, Exit, ReadableVec, Rw, StorageMode};
use crate::{blocks, prices};
use crate::distribution::metrics::{
ActivityFull, CohortMetricsBase, CostBasisBase, ImportConfig, OutputsMetrics, RealizedBase,
RelativeWithRelToAll, SupplyMetrics, UnrealizedBase,
RelativeBaseWithRelToAll, SupplyMetrics, UnrealizedBase,
};
/// Basic cohort metrics: no extensions, with relative (rel_to_all).
@@ -23,7 +23,7 @@ pub struct BasicCohortMetrics<M: StorageMode = Rw> {
pub realized: Box<RealizedBase<M>>,
pub cost_basis: Box<CostBasisBase<M>>,
pub unrealized: Box<UnrealizedBase<M>>,
pub relative: Box<RelativeWithRelToAll<M>>,
pub relative: Box<RelativeBaseWithRelToAll<M>>,
}
impl CohortMetricsBase for BasicCohortMetrics {
@@ -51,7 +51,7 @@ impl BasicCohortMetrics {
let unrealized = UnrealizedBase::forced_import(cfg)?;
let realized = RealizedBase::forced_import(cfg)?;
let relative = RelativeWithRelToAll::forced_import(cfg)?;
let relative = RelativeBaseWithRelToAll::forced_import(cfg)?;
Ok(Self {
filter: cfg.filter.clone(),
@@ -70,7 +70,6 @@ impl BasicCohortMetrics {
blocks: &blocks::Vecs,
prices: &prices::Vecs,
starting_indexes: &Indexes,
height_to_market_cap: &impl ReadableVec<Height, Dollars>,
all_supply_sats: &impl ReadableVec<Height, Sats>,
exit: &Exit,
) -> Result<()> {
@@ -84,10 +83,8 @@ impl BasicCohortMetrics {
self.relative.compute(
starting_indexes.height,
&self.unrealized,
&self.realized,
&self.unrealized.core,
&self.supply.total.sats.height,
height_to_market_cap,
all_supply_sats,
exit,
)?;

View File

@@ -95,7 +95,6 @@ impl ExtendedCohortMetrics {
self.relative.compute(
starting_indexes.height,
&self.unrealized,
&self.realized,
&self.supply.total.sats.height,
height_to_market_cap,
all_supply_sats,

View File

@@ -1,17 +1,12 @@
use brk_cohort::Filter;
use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::{
BasisPoints16, Height, Indexes, Sats, Version,
};
use vecdb::{AnyStoredVec, AnyVec, Exit, ReadableVec, Rw, StorageMode, WritableVec};
use brk_types::{Height, Indexes, Version};
use vecdb::{AnyStoredVec, AnyVec, Exit, Rw, StorageMode, WritableVec};
use crate::{blocks, prices};
use crate::internal::{
PercentFromHeight, RatioSatsBp16,
ValueFromHeight,
};
use crate::internal::ValueFromHeight;
use crate::distribution::{
metrics::{ActivityBase, ImportConfig, OutputsMetrics, RealizedMinimal, SupplyMetrics},
@@ -25,15 +20,8 @@ pub struct MinimalUnrealized<M: StorageMode = Rw> {
pub supply_in_loss: ValueFromHeight<M>,
}
/// Minimal relative metrics: supply in profit/loss relative to own supply.
#[derive(Traversable)]
pub struct MinimalRelative<M: StorageMode = Rw> {
pub supply_in_profit_rel_to_own_supply: PercentFromHeight<BasisPoints16, M>,
pub supply_in_loss_rel_to_own_supply: PercentFromHeight<BasisPoints16, M>,
}
/// MinimalCohortMetrics: supply, outputs, sent+ema, realized cap/price/mvrv/profit/loss,
/// supply in profit/loss, relative to own supply.
/// supply in profit/loss.
///
/// Used for type_, amount, and address cohorts.
/// Does NOT implement CohortMetricsBase — standalone, not aggregatable via trait.
@@ -46,7 +34,6 @@ pub struct MinimalCohortMetrics<M: StorageMode = Rw> {
pub activity: Box<ActivityBase<M>>,
pub realized: Box<RealizedMinimal<M>>,
pub unrealized: Box<MinimalUnrealized<M>>,
pub relative: Box<MinimalRelative<M>>,
}
impl MinimalUnrealized {
@@ -113,42 +100,6 @@ impl MinimalUnrealized {
}
}
impl MinimalRelative {
pub(crate) fn forced_import(cfg: &ImportConfig) -> Result<Self> {
Ok(Self {
supply_in_profit_rel_to_own_supply: cfg
.import("supply_in_profit_rel_to_own_supply", Version::ONE)?,
supply_in_loss_rel_to_own_supply: cfg
.import("supply_in_loss_rel_to_own_supply", Version::ONE)?,
})
}
pub(crate) fn compute(
&mut self,
max_from: Height,
supply_in_profit_sats: &impl ReadableVec<Height, Sats>,
supply_in_loss_sats: &impl ReadableVec<Height, Sats>,
supply_total_sats: &impl ReadableVec<Height, Sats>,
exit: &Exit,
) -> Result<()> {
self.supply_in_profit_rel_to_own_supply
.compute_binary::<Sats, Sats, RatioSatsBp16>(
max_from,
supply_in_profit_sats,
supply_total_sats,
exit,
)?;
self.supply_in_loss_rel_to_own_supply
.compute_binary::<Sats, Sats, RatioSatsBp16>(
max_from,
supply_in_loss_sats,
supply_total_sats,
exit,
)?;
Ok(())
}
}
impl MinimalCohortMetrics {
pub(crate) fn forced_import(cfg: &ImportConfig) -> Result<Self> {
Ok(Self {
@@ -158,7 +109,6 @@ impl MinimalCohortMetrics {
activity: Box::new(ActivityBase::forced_import(cfg)?),
realized: Box::new(RealizedMinimal::forced_import(cfg)?),
unrealized: Box::new(MinimalUnrealized::forced_import(cfg)?),
relative: Box::new(MinimalRelative::forced_import(cfg)?),
})
}
@@ -262,14 +212,6 @@ impl MinimalCohortMetrics {
exit,
)?;
self.relative.compute(
starting_indexes.height,
&self.unrealized.supply_in_profit.sats.height,
&self.unrealized.supply_in_loss.sats.height,
&self.supply.total.sats.height,
exit,
)?;
Ok(())
}
}

View File

@@ -55,7 +55,7 @@ pub use realized::{
RealizedAdjusted, RealizedBase, RealizedFull, RealizedLike, RealizedMinimal,
};
pub use relative::{
RelativeBaseWithRelToAll, RelativeForAll, RelativeWithExtended, RelativeWithRelToAll,
RelativeBaseWithRelToAll, RelativeForAll, RelativeWithExtended,
};
pub use supply::SupplyMetrics;
pub use unrealized::{UnrealizedBase, UnrealizedCore, UnrealizedFull, UnrealizedLike};

View File

@@ -5,7 +5,7 @@ use vecdb::{Exit, ReadableVec, Rw, StorageMode};
use crate::internal::{PercentFromHeight, RatioDollarsBp16, RatioDollarsBp32, RatioDollarsBps32};
use crate::distribution::metrics::{ImportConfig, UnrealizedBase};
use crate::distribution::metrics::{ImportConfig, UnrealizedCore};
/// Extended relative metrics for own market cap (extended && rel_to_all).
#[derive(Traversable)]
@@ -32,7 +32,7 @@ impl RelativeExtendedOwnMarketCap {
pub(crate) fn compute(
&mut self,
max_from: Height,
unrealized: &UnrealizedBase,
unrealized: &UnrealizedCore,
own_market_cap: &impl ReadableVec<Height, Dollars>,
exit: &Exit,
) -> Result<()> {

View File

@@ -1,11 +1,11 @@
use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::{BasisPoints16, BasisPointsSigned32, Dollars, Height, Version};
use vecdb::{Exit, Rw, StorageMode};
use vecdb::{Exit, ReadableVec, Rw, StorageMode};
use crate::internal::{PercentFromHeight, RatioDollarsBp16, RatioDollarsBps32};
use crate::distribution::metrics::{ImportConfig, UnrealizedBase};
use crate::distribution::metrics::{ImportConfig, UnrealizedCore};
/// Extended relative metrics for own total unrealized PnL (extended only).
#[derive(Traversable)]
@@ -32,28 +32,29 @@ impl RelativeExtendedOwnPnl {
pub(crate) fn compute(
&mut self,
max_from: Height,
unrealized: &UnrealizedBase,
unrealized: &UnrealizedCore,
gross_pnl_usd: &impl ReadableVec<Height, Dollars>,
exit: &Exit,
) -> Result<()> {
self.unrealized_profit_rel_to_own_gross_pnl
.compute_binary::<Dollars, Dollars, RatioDollarsBp16>(
max_from,
&unrealized.unrealized_profit.usd.height,
&unrealized.gross_pnl.usd.height,
gross_pnl_usd,
exit,
)?;
self.unrealized_loss_rel_to_own_gross_pnl
.compute_binary::<Dollars, Dollars, RatioDollarsBp16>(
max_from,
&unrealized.unrealized_loss.usd.height,
&unrealized.gross_pnl.usd.height,
gross_pnl_usd,
exit,
)?;
self.net_unrealized_pnl_rel_to_own_gross_pnl
.compute_binary::<Dollars, Dollars, RatioDollarsBps32>(
max_from,
&unrealized.net_unrealized_pnl.usd.height,
&unrealized.gross_pnl.usd.height,
gross_pnl_usd,
exit,
)?;
Ok(())

View File

@@ -4,7 +4,7 @@ use brk_types::{Dollars, Height, Sats};
use derive_more::{Deref, DerefMut};
use vecdb::{Exit, ReadableVec, Rw, StorageMode};
use crate::distribution::metrics::{ImportConfig, RealizedBase, UnrealizedBase};
use crate::distribution::metrics::{ImportConfig, UnrealizedFull};
use super::{RelativeFull, RelativeExtendedOwnPnl};
@@ -30,21 +30,20 @@ impl RelativeForAll {
pub(crate) fn compute(
&mut self,
max_from: Height,
unrealized: &UnrealizedBase,
realized: &RealizedBase,
unrealized: &UnrealizedFull,
supply_total_sats: &impl ReadableVec<Height, Sats>,
market_cap: &impl ReadableVec<Height, Dollars>,
exit: &Exit,
) -> Result<()> {
self.base.compute(
max_from,
unrealized,
realized,
&unrealized.inner.core,
supply_total_sats,
market_cap,
exit,
)?;
self.extended_own_pnl.compute(max_from, unrealized, exit)?;
self.extended_own_pnl
.compute(max_from, &unrealized.inner.core, &unrealized.gross_pnl.usd.height, exit)?;
Ok(())
}
}

View File

@@ -10,7 +10,7 @@ use crate::internal::{
Bps32ToFloat, LazyFromHeight, PercentFromHeight, RatioDollarsBp16, RatioDollarsBps32,
};
use crate::distribution::metrics::{ImportConfig, RealizedBase, UnrealizedBase};
use crate::distribution::metrics::{ImportConfig, UnrealizedCore};
use super::RelativeBase;
@@ -26,9 +26,6 @@ pub struct RelativeFull<M: StorageMode = Rw> {
pub unrealized_loss_rel_to_market_cap: PercentFromHeight<BasisPoints16, M>,
pub net_unrealized_pnl_rel_to_market_cap: PercentFromHeight<BasisPointsSigned32, M>,
pub nupl: LazyFromHeight<StoredF32, BasisPointsSigned32>,
pub invested_capital_in_profit_rel_to_realized_cap: PercentFromHeight<BasisPoints16, M>,
pub invested_capital_in_loss_rel_to_realized_cap: PercentFromHeight<BasisPoints16, M>,
}
impl RelativeFull {
@@ -59,29 +56,20 @@ impl RelativeFull {
.import("unrealized_loss_rel_to_market_cap", v2)?,
net_unrealized_pnl_rel_to_market_cap,
nupl,
invested_capital_in_profit_rel_to_realized_cap: cfg.import(
"invested_capital_in_profit_rel_to_realized_cap",
Version::ZERO,
)?,
invested_capital_in_loss_rel_to_realized_cap: cfg.import(
"invested_capital_in_loss_rel_to_realized_cap",
Version::ZERO,
)?,
})
}
pub(crate) fn compute(
&mut self,
max_from: Height,
unrealized: &UnrealizedBase,
realized: &RealizedBase,
unrealized: &UnrealizedCore,
supply_total_sats: &impl ReadableVec<Height, Sats>,
market_cap: &impl ReadableVec<Height, Dollars>,
exit: &Exit,
) -> Result<()> {
self.base.compute(
max_from,
&unrealized.core,
unrealized,
supply_total_sats,
exit,
)?;
@@ -107,20 +95,6 @@ impl RelativeFull {
market_cap,
exit,
)?;
self.invested_capital_in_profit_rel_to_realized_cap
.compute_binary::<Dollars, Dollars, RatioDollarsBp16>(
max_from,
&unrealized.invested_capital_in_profit.usd.height,
&realized.realized_cap.height,
exit,
)?;
self.invested_capital_in_loss_rel_to_realized_cap
.compute_binary::<Dollars, Dollars, RatioDollarsBp16>(
max_from,
&unrealized.invested_capital_in_loss.usd.height,
&realized.realized_cap.height,
exit,
)?;
Ok(())
}
}

View File

@@ -6,7 +6,6 @@ mod full;
mod to_all;
mod with_extended;
mod with_rel_to_all;
mod with_rel_to_all_base;
pub use base::RelativeBase;
pub use extended_own_market_cap::RelativeExtendedOwnMarketCap;
@@ -15,5 +14,4 @@ pub use for_all::RelativeForAll;
pub use full::RelativeFull;
pub use to_all::RelativeToAll;
pub use with_extended::RelativeWithExtended;
pub use with_rel_to_all::RelativeWithRelToAll;
pub use with_rel_to_all_base::RelativeBaseWithRelToAll;
pub use with_rel_to_all::RelativeBaseWithRelToAll;

View File

@@ -4,12 +4,12 @@ use brk_types::{Dollars, Height, Sats};
use derive_more::{Deref, DerefMut};
use vecdb::{Exit, ReadableVec, Rw, StorageMode};
use crate::distribution::metrics::{ImportConfig, RealizedBase, UnrealizedBase};
use crate::distribution::metrics::{ImportConfig, UnrealizedFull};
use super::{RelativeFull, RelativeExtendedOwnMarketCap, RelativeExtendedOwnPnl, RelativeToAll};
/// Full extended relative metrics (base + rel_to_all + own_market_cap + own_pnl).
/// Used by: sth, lth, age_range cohorts.
/// Used by: sth, lth cohorts.
#[derive(Deref, DerefMut, Traversable)]
pub struct RelativeWithExtended<M: StorageMode = Rw> {
#[deref]
@@ -38,8 +38,7 @@ impl RelativeWithExtended {
pub(crate) fn compute(
&mut self,
max_from: Height,
unrealized: &UnrealizedBase,
realized: &RealizedBase,
unrealized: &UnrealizedFull,
supply_total_sats: &impl ReadableVec<Height, Sats>,
market_cap: &impl ReadableVec<Height, Dollars>,
all_supply_sats: &impl ReadableVec<Height, Sats>,
@@ -48,8 +47,7 @@ impl RelativeWithExtended {
) -> Result<()> {
self.base.compute(
max_from,
unrealized,
realized,
&unrealized.inner.core,
supply_total_sats,
market_cap,
exit,
@@ -63,8 +61,9 @@ impl RelativeWithExtended {
exit,
)?;
self.extended_own_market_cap
.compute(max_from, unrealized, own_market_cap, exit)?;
self.extended_own_pnl.compute(max_from, unrealized, exit)?;
.compute(max_from, &unrealized.inner.core, own_market_cap, exit)?;
self.extended_own_pnl
.compute(max_from, &unrealized.inner.core, &unrealized.gross_pnl.usd.height, exit)?;
Ok(())
}
}

View File

@@ -1,50 +1,45 @@
use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::{Dollars, Height, Sats};
use brk_types::{Height, Sats};
use derive_more::{Deref, DerefMut};
use vecdb::{Exit, ReadableVec, Rw, StorageMode};
use crate::distribution::metrics::{ImportConfig, RealizedBase, UnrealizedBase};
use crate::distribution::metrics::{ImportConfig, UnrealizedCore};
use super::{RelativeFull, RelativeToAll};
use super::{RelativeBase, RelativeToAll};
/// Relative metrics with rel_to_all (no extended, no peak_regret).
/// Used by: epoch, year, type, amount, address cohorts.
/// Base relative metrics with rel_to_all.
/// Used by: age_range, epoch, class, min_age, max_age cohorts.
#[derive(Deref, DerefMut, Traversable)]
pub struct RelativeWithRelToAll<M: StorageMode = Rw> {
pub struct RelativeBaseWithRelToAll<M: StorageMode = Rw> {
#[deref]
#[deref_mut]
#[traversable(flatten)]
pub base: RelativeFull<M>,
pub base: RelativeBase<M>,
#[traversable(flatten)]
pub rel_to_all: RelativeToAll<M>,
}
impl RelativeWithRelToAll {
impl RelativeBaseWithRelToAll {
pub(crate) fn forced_import(cfg: &ImportConfig) -> Result<Self> {
Ok(Self {
base: RelativeFull::forced_import(cfg)?,
base: RelativeBase::forced_import(cfg)?,
rel_to_all: RelativeToAll::forced_import(cfg)?,
})
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn compute(
&mut self,
max_from: Height,
unrealized: &UnrealizedBase,
realized: &RealizedBase,
unrealized: &UnrealizedCore,
supply_total_sats: &impl ReadableVec<Height, Sats>,
market_cap: &impl ReadableVec<Height, Dollars>,
all_supply_sats: &impl ReadableVec<Height, Sats>,
exit: &Exit,
) -> Result<()> {
self.base.compute(
max_from,
unrealized,
realized,
supply_total_sats,
market_cap,
exit,
)?;
self.rel_to_all.compute(

View File

@@ -1,55 +0,0 @@
use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::{Height, Sats};
use derive_more::{Deref, DerefMut};
use vecdb::{Exit, ReadableVec, Rw, StorageMode};
use crate::distribution::metrics::{ImportConfig, UnrealizedCore};
use super::{RelativeBase, RelativeToAll};
/// Complete relative metrics with rel_to_all.
/// Used by CompleteCohortMetrics (epoch, class, min_age, max_age).
#[derive(Deref, DerefMut, Traversable)]
pub struct RelativeBaseWithRelToAll<M: StorageMode = Rw> {
#[deref]
#[deref_mut]
#[traversable(flatten)]
pub base: RelativeBase<M>,
#[traversable(flatten)]
pub rel_to_all: RelativeToAll<M>,
}
impl RelativeBaseWithRelToAll {
pub(crate) fn forced_import(cfg: &ImportConfig) -> Result<Self> {
Ok(Self {
base: RelativeBase::forced_import(cfg)?,
rel_to_all: RelativeToAll::forced_import(cfg)?,
})
}
pub(crate) fn compute(
&mut self,
max_from: Height,
unrealized: &UnrealizedCore,
supply_total_sats: &impl ReadableVec<Height, Sats>,
all_supply_sats: &impl ReadableVec<Height, Sats>,
exit: &Exit,
) -> Result<()> {
self.base.compute(
max_from,
unrealized,
supply_total_sats,
exit,
)?;
self.rel_to_all.compute(
max_from,
&unrealized.supply_in_profit.sats.height,
&unrealized.supply_in_loss.sats.height,
supply_total_sats,
all_supply_sats,
exit,
)?;
Ok(())
}
}

View File

@@ -1,14 +1,10 @@
use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::{Cents, CentsSats, CentsSquaredSats, Height, Indexes, Version};
use brk_types::{CentsSats, CentsSquaredSats, Height, Indexes, Version};
use derive_more::{Deref, DerefMut};
use vecdb::{AnyStoredVec, AnyVec, BytesVec, Exit, ReadableVec, Rw, StorageMode, WritableVec};
use crate::{
distribution::state::UnrealizedState,
internal::FiatFromHeight,
prices,
};
use crate::distribution::state::UnrealizedState;
use crate::distribution::metrics::ImportConfig;
@@ -21,11 +17,6 @@ pub struct UnrealizedBase<M: StorageMode = Rw> {
#[traversable(flatten)]
pub core: UnrealizedCore<M>,
pub gross_pnl: FiatFromHeight<Cents, M>,
pub invested_capital_in_profit: FiatFromHeight<Cents, M>,
pub invested_capital_in_loss: FiatFromHeight<Cents, M>,
pub invested_capital_in_profit_raw: M::Stored<BytesVec<Height, CentsSats>>,
pub invested_capital_in_loss_raw: M::Stored<BytesVec<Height, CentsSats>>,
pub investor_cap_in_profit_raw: M::Stored<BytesVec<Height, CentsSquaredSats>>,
@@ -38,11 +29,6 @@ impl UnrealizedBase {
let core = UnrealizedCore::forced_import(cfg)?;
let gross_pnl = cfg.import("unrealized_gross_pnl", v0)?;
let invested_capital_in_profit = cfg.import("invested_capital_in_profit", v0)?;
let invested_capital_in_loss = cfg.import("invested_capital_in_loss", v0)?;
let invested_capital_in_profit_raw =
cfg.import("invested_capital_in_profit_raw", v0)?;
let invested_capital_in_loss_raw = cfg.import("invested_capital_in_loss_raw", v0)?;
@@ -51,9 +37,6 @@ impl UnrealizedBase {
Ok(Self {
core,
gross_pnl,
invested_capital_in_profit,
invested_capital_in_loss,
invested_capital_in_profit_raw,
invested_capital_in_loss_raw,
investor_cap_in_profit_raw,
@@ -64,8 +47,6 @@ impl UnrealizedBase {
pub(crate) fn min_stateful_height_len(&self) -> usize {
self.core
.min_stateful_height_len()
.min(self.invested_capital_in_profit.cents.height.len())
.min(self.invested_capital_in_loss.cents.height.len())
.min(self.invested_capital_in_profit_raw.len())
.min(self.invested_capital_in_loss_raw.len())
.min(self.investor_cap_in_profit_raw.len())
@@ -79,15 +60,6 @@ impl UnrealizedBase {
) -> Result<()> {
self.core.truncate_push(height, height_state)?;
self.invested_capital_in_profit
.cents
.height
.truncate_push(height, height_state.invested_capital_in_profit)?;
self.invested_capital_in_loss
.cents
.height
.truncate_push(height, height_state.invested_capital_in_loss)?;
self.invested_capital_in_profit_raw.truncate_push(
height,
CentsSats::new(height_state.invested_capital_in_profit_raw),
@@ -110,8 +82,6 @@ impl UnrealizedBase {
pub(crate) fn collect_vecs_mut(&mut self) -> Vec<&mut dyn AnyStoredVec> {
let mut vecs = self.core.collect_vecs_mut();
vecs.push(&mut self.invested_capital_in_profit.cents.height as &mut dyn AnyStoredVec);
vecs.push(&mut self.invested_capital_in_loss.cents.height as &mut dyn AnyStoredVec);
vecs.push(&mut self.invested_capital_in_profit_raw as &mut dyn AnyStoredVec);
vecs.push(&mut self.invested_capital_in_loss_raw as &mut dyn AnyStoredVec);
vecs.push(&mut self.investor_cap_in_profit_raw as &mut dyn AnyStoredVec);
@@ -130,9 +100,6 @@ impl UnrealizedBase {
self.core
.compute_from_stateful(starting_indexes, &core_refs, exit)?;
sum_others!(self, starting_indexes, others, exit; invested_capital_in_profit.cents.height);
sum_others!(self, starting_indexes, others, exit; invested_capital_in_loss.cents.height);
let start = self
.invested_capital_in_profit_raw
.len()
@@ -196,19 +163,10 @@ impl UnrealizedBase {
pub(crate) fn compute_rest(
&mut self,
_prices: &prices::Vecs,
starting_indexes: &Indexes,
exit: &Exit,
) -> Result<()> {
self.core.compute_rest(starting_indexes, exit)?;
self.gross_pnl.cents.height.compute_add(
starting_indexes.height,
&self.core.unrealized_profit.cents.height,
&self.core.unrealized_loss.cents.height,
exit,
)?;
Ok(())
}
}

View File

@@ -1,13 +1,12 @@
use brk_error::Result;
use brk_traversable::Traversable;
use brk_types::{Cents, CentsSigned, Indexes, Version};
use brk_types::{Cents, CentsSats, CentsSigned, Height, Indexes, Version};
use derive_more::{Deref, DerefMut};
use vecdb::{Exit, Rw, StorageMode};
use vecdb::{AnyStoredVec, Exit, Rw, StorageMode, WritableVec};
use crate::distribution::state::UnrealizedState;
use crate::internal::{CentsSubtractToCentsSigned, FiatFromHeight};
use crate::prices;
use crate::distribution::metrics::ImportConfig;
use crate::{distribution::metrics::ImportConfig, prices};
use super::UnrealizedBase;
@@ -18,6 +17,10 @@ pub struct UnrealizedFull<M: StorageMode = Rw> {
#[traversable(flatten)]
pub inner: UnrealizedBase<M>,
pub gross_pnl: FiatFromHeight<Cents, M>,
pub invested_capital_in_profit: FiatFromHeight<Cents, M>,
pub invested_capital_in_loss: FiatFromHeight<Cents, M>,
pub pain_index: FiatFromHeight<Cents, M>,
pub greed_index: FiatFromHeight<Cents, M>,
pub net_sentiment: FiatFromHeight<CentsSigned, M>,
@@ -25,28 +28,95 @@ pub struct UnrealizedFull<M: StorageMode = Rw> {
impl UnrealizedFull {
pub(crate) fn forced_import(cfg: &ImportConfig) -> Result<Self> {
let v0 = Version::ZERO;
let inner = UnrealizedBase::forced_import(cfg)?;
let pain_index = cfg.import("pain_index", Version::ZERO)?;
let greed_index = cfg.import("greed_index", Version::ZERO)?;
let gross_pnl = cfg.import("unrealized_gross_pnl", v0)?;
let invested_capital_in_profit = cfg.import("invested_capital_in_profit", v0)?;
let invested_capital_in_loss = cfg.import("invested_capital_in_loss", v0)?;
let pain_index = cfg.import("pain_index", v0)?;
let greed_index = cfg.import("greed_index", v0)?;
let net_sentiment = cfg.import("net_sentiment", Version::ONE)?;
Ok(Self {
inner,
gross_pnl,
invested_capital_in_profit,
invested_capital_in_loss,
pain_index,
greed_index,
net_sentiment,
})
}
pub(crate) fn compute_rest(
pub(crate) fn truncate_push_all(
&mut self,
height: Height,
state: &UnrealizedState,
) -> Result<()> {
self.inner.truncate_push(height, state)?;
self.invested_capital_in_profit
.cents
.height
.truncate_push(height, state.invested_capital_in_profit)?;
self.invested_capital_in_loss
.cents
.height
.truncate_push(height, state.invested_capital_in_loss)?;
Ok(())
}
pub(crate) fn collect_vecs_mut(&mut self) -> Vec<&mut dyn AnyStoredVec> {
let mut vecs = self.inner.collect_vecs_mut();
vecs.push(&mut self.gross_pnl.cents.height as &mut dyn AnyStoredVec);
vecs.push(&mut self.invested_capital_in_profit.cents.height as &mut dyn AnyStoredVec);
vecs.push(&mut self.invested_capital_in_loss.cents.height as &mut dyn AnyStoredVec);
vecs.push(&mut self.pain_index.cents.height as &mut dyn AnyStoredVec);
vecs.push(&mut self.greed_index.cents.height as &mut dyn AnyStoredVec);
vecs.push(&mut self.net_sentiment.cents.height as &mut dyn AnyStoredVec);
vecs
}
pub(crate) fn compute_rest_all(
&mut self,
prices: &prices::Vecs,
starting_indexes: &Indexes,
exit: &Exit,
) -> Result<()> {
self.inner.compute_rest(prices, starting_indexes, exit)?;
self.inner.compute_rest(starting_indexes, exit)?;
self.gross_pnl.cents.height.compute_add(
starting_indexes.height,
&self.inner.core.unrealized_profit.cents.height,
&self.inner.core.unrealized_loss.cents.height,
exit,
)?;
self.invested_capital_in_profit.cents.height.compute_transform(
starting_indexes.height,
&self.inner.invested_capital_in_profit_raw,
|(h, raw, ..)| (h, CentsSats::to_cents(raw)),
exit,
)?;
self.invested_capital_in_loss.cents.height.compute_transform(
starting_indexes.height,
&self.inner.invested_capital_in_loss_raw,
|(h, raw, ..)| (h, CentsSats::to_cents(raw)),
exit,
)?;
self.compute_rest_extended(prices, starting_indexes, exit)?;
Ok(())
}
fn compute_rest_extended(
&mut self,
prices: &prices::Vecs,
starting_indexes: &Indexes,
exit: &Exit,
) -> Result<()> {
self.pain_index.cents.height.compute_transform3(
starting_indexes.height,
&self.inner.investor_cap_in_loss_raw,

View File

@@ -28,8 +28,8 @@ impl UnrealizedLike for UnrealizedBase {
fn truncate_push(&mut self, height: Height, state: &UnrealizedState) -> Result<()> {
self.truncate_push(height, state)
}
fn compute_rest(&mut self, prices: &prices::Vecs, starting_indexes: &Indexes, exit: &Exit) -> Result<()> {
self.compute_rest(prices, starting_indexes, exit)
fn compute_rest(&mut self, _prices: &prices::Vecs, starting_indexes: &Indexes, exit: &Exit) -> Result<()> {
self.compute_rest(starting_indexes, exit)
}
fn compute_net_sentiment_height(&mut self, _starting_indexes: &Indexes, _exit: &Exit) -> Result<()> {
Ok(())
@@ -41,10 +41,10 @@ impl UnrealizedLike for UnrealizedFull {
fn as_base_mut(&mut self) -> &mut UnrealizedBase { &mut self.inner }
fn min_stateful_height_len(&self) -> usize { self.inner.min_stateful_height_len() }
fn truncate_push(&mut self, height: Height, state: &UnrealizedState) -> Result<()> {
self.inner.truncate_push(height, state)
self.truncate_push_all(height, state)
}
fn compute_rest(&mut self, prices: &prices::Vecs, starting_indexes: &Indexes, exit: &Exit) -> Result<()> {
self.compute_rest(prices, starting_indexes, exit)
self.compute_rest_all(prices, starting_indexes, exit)
}
fn compute_net_sentiment_height(&mut self, starting_indexes: &Indexes, exit: &Exit) -> Result<()> {
self.compute_net_sentiment_height(starting_indexes, exit)