mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-24 06:39:58 -07:00
global: snapshot
This commit is contained in:
@@ -12,8 +12,6 @@ use vecdb::{AnyStoredVec, Database, Exit, ReadableVec, Rw, StorageMode};
|
||||
|
||||
use crate::{blocks, distribution::DynCohortVecs, indexes, prices};
|
||||
|
||||
use crate::distribution::metrics::CohortMetricsBase;
|
||||
|
||||
use super::{super::traits::CohortVecs, vecs::AddressCohortVecs};
|
||||
|
||||
const VERSION: Version = Version::new(0);
|
||||
@@ -97,7 +95,6 @@ impl AddressCohorts {
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
// 1. Compute addr_count_change_1m using rolling window
|
||||
self.par_iter_mut().try_for_each(|v| {
|
||||
v.addr_count_change_1m.height.compute_rolling_change(
|
||||
starting_indexes.height,
|
||||
@@ -107,18 +104,9 @@ impl AddressCohorts {
|
||||
)
|
||||
})?;
|
||||
|
||||
// 2. Compute all metrics except net_sentiment
|
||||
self.par_iter_mut()
|
||||
.try_for_each(|v| v.compute_rest_part1(blocks, prices, starting_indexes, exit))?;
|
||||
|
||||
// 3. Compute net_sentiment.height for aggregate cohorts (weighted average).
|
||||
// Separate cohorts already computed net_sentiment in step 2 (inside compute_rest_part1).
|
||||
self.for_each_aggregate(|vecs, sources| {
|
||||
let metrics: Vec<_> = sources.iter().map(|v| &v.metrics).collect();
|
||||
vecs.metrics
|
||||
.compute_net_sentiment_from_others(starting_indexes, &metrics, exit)
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -11,28 +11,24 @@ use crate::{
|
||||
blocks, distribution::state::AddressCohortState, indexes, internal::ComputedFromHeight, prices,
|
||||
};
|
||||
|
||||
use crate::distribution::metrics::{BasicCohortMetrics, CohortMetricsBase, ImportConfig};
|
||||
use crate::distribution::metrics::{CoreCohortMetrics, ImportConfig};
|
||||
|
||||
use super::super::traits::{CohortVecs, DynCohortVecs};
|
||||
#[derive(Traversable)]
|
||||
pub struct AddressCohortVecs<M: StorageMode = Rw> {
|
||||
/// Starting height when state was imported
|
||||
starting_height: Option<Height>,
|
||||
|
||||
/// Runtime state for block-by-block processing
|
||||
#[traversable(skip)]
|
||||
pub state: Option<Box<AddressCohortState>>,
|
||||
|
||||
/// Metric vectors
|
||||
#[traversable(flatten)]
|
||||
pub metrics: BasicCohortMetrics<M>,
|
||||
pub metrics: CoreCohortMetrics<M>,
|
||||
|
||||
pub addr_count: ComputedFromHeight<StoredU64, M>,
|
||||
pub addr_count_change_1m: ComputedFromHeight<StoredF64, M>,
|
||||
}
|
||||
|
||||
impl AddressCohortVecs {
|
||||
/// Import address cohort from database.
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
filter: Filter,
|
||||
@@ -56,7 +52,7 @@ impl AddressCohortVecs {
|
||||
|
||||
state: states_path.map(|path| Box::new(AddressCohortState::new(path, &full_name))),
|
||||
|
||||
metrics: BasicCohortMetrics::forced_import(&cfg)?,
|
||||
metrics: CoreCohortMetrics::forced_import(&cfg)?,
|
||||
|
||||
addr_count: ComputedFromHeight::forced_import(
|
||||
db,
|
||||
@@ -73,20 +69,19 @@ impl AddressCohortVecs {
|
||||
})
|
||||
}
|
||||
|
||||
/// Reset starting height to zero.
|
||||
pub(crate) fn reset_starting_height(&mut self) {
|
||||
self.starting_height = Some(Height::ZERO);
|
||||
}
|
||||
|
||||
/// Returns a parallel iterator over all vecs for parallel writing.
|
||||
pub(crate) fn par_iter_vecs_mut(
|
||||
&mut self,
|
||||
) -> impl ParallelIterator<Item = &mut dyn AnyStoredVec> {
|
||||
rayon::iter::once(&mut self.addr_count.height as &mut dyn AnyStoredVec)
|
||||
.chain(self.metrics.par_iter_mut())
|
||||
let mut vecs: Vec<&mut dyn AnyStoredVec> = Vec::new();
|
||||
vecs.push(&mut self.addr_count.height as &mut dyn AnyStoredVec);
|
||||
vecs.extend(self.metrics.collect_all_vecs_mut());
|
||||
vecs.into_par_iter()
|
||||
}
|
||||
|
||||
/// Commit state to disk (separate from vec writes for parallelization).
|
||||
pub(crate) fn write_state(&mut self, height: Height, cleanup: bool) -> Result<()> {
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
state.inner.write(height, cleanup)?;
|
||||
@@ -117,15 +112,10 @@ impl DynCohortVecs for AddressCohortVecs {
|
||||
}
|
||||
|
||||
fn import_state(&mut self, starting_height: Height) -> Result<Height> {
|
||||
// Import state from runtime state if present
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
// State files are saved AT height H, so to resume at H+1 we need to import at H
|
||||
// Decrement first, then increment result to match expected starting_height
|
||||
if let Some(mut prev_height) = starting_height.decremented() {
|
||||
// Import cost_basis_data state file (may adjust prev_height to actual file found)
|
||||
prev_height = state.inner.import_at_or_before(prev_height)?;
|
||||
|
||||
// Restore supply state from height-indexed vectors
|
||||
state.inner.supply.value = self
|
||||
.metrics
|
||||
.supply
|
||||
@@ -143,14 +133,12 @@ impl DynCohortVecs for AddressCohortVecs {
|
||||
.unwrap();
|
||||
state.addr_count = *self.addr_count.height.collect_one(prev_height).unwrap();
|
||||
|
||||
// Restore realized cap from persisted exact values
|
||||
state.inner.restore_realized_cap();
|
||||
|
||||
let result = prev_height.incremented();
|
||||
self.starting_height = Some(result);
|
||||
Ok(result)
|
||||
} else {
|
||||
// starting_height is 0, nothing to import
|
||||
self.starting_height = Some(Height::ZERO);
|
||||
Ok(Height::ZERO)
|
||||
}
|
||||
@@ -174,12 +162,22 @@ impl DynCohortVecs for AddressCohortVecs {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Push addr_count from state
|
||||
if let Some(state) = self.state.as_ref() {
|
||||
self.addr_count
|
||||
.height
|
||||
.truncate_push(height, state.addr_count.into())?;
|
||||
self.metrics.truncate_push(height, &state.inner)?;
|
||||
self.metrics
|
||||
.supply
|
||||
.truncate_push(height, state.inner.supply.value)?;
|
||||
self.metrics
|
||||
.outputs
|
||||
.truncate_push(height, state.inner.supply.utxo_count)?;
|
||||
self.metrics
|
||||
.activity
|
||||
.truncate_push(height, state.inner.sent)?;
|
||||
self.metrics
|
||||
.realized
|
||||
.truncate_push(height, &state.inner.realized)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -189,15 +187,14 @@ impl DynCohortVecs for AddressCohortVecs {
|
||||
&mut self,
|
||||
height: Height,
|
||||
height_price: Cents,
|
||||
is_day_boundary: bool,
|
||||
_is_day_boundary: bool,
|
||||
) -> Result<()> {
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
self.metrics.compute_then_truncate_push_unrealized_states(
|
||||
height,
|
||||
height_price,
|
||||
&mut state.inner,
|
||||
is_day_boundary,
|
||||
)?;
|
||||
state.inner.apply_pending();
|
||||
let unrealized_state = state.inner.compute_unrealized_state(height_price);
|
||||
self.metrics
|
||||
.unrealized
|
||||
.truncate_push(height, &unrealized_state)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -210,14 +207,7 @@ impl DynCohortVecs for AddressCohortVecs {
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.metrics
|
||||
.compute_rest_part1(blocks, prices, starting_indexes, exit)?;
|
||||
// Separate cohorts (with state) compute net_sentiment = greed - pain directly.
|
||||
// Aggregate cohorts get it via weighted average in groups.rs.
|
||||
if self.state.is_some() {
|
||||
self.metrics
|
||||
.compute_net_sentiment_height(starting_indexes, exit)?;
|
||||
}
|
||||
Ok(())
|
||||
.compute_rest_part1(blocks, prices, starting_indexes, exit)
|
||||
}
|
||||
|
||||
fn write_state(&mut self, height: Height, cleanup: bool) -> Result<()> {
|
||||
@@ -257,7 +247,7 @@ impl CohortVecs for AddressCohortVecs {
|
||||
.as_slice(),
|
||||
exit,
|
||||
)?;
|
||||
self.metrics.compute_from_stateful(
|
||||
self.metrics.compute_from_sources(
|
||||
starting_indexes,
|
||||
&others.iter().map(|v| &v.metrics).collect::<Vec<_>>(),
|
||||
exit,
|
||||
|
||||
@@ -13,8 +13,9 @@ use vecdb::{AnyStoredVec, Database, Exit, ReadOnlyClone, ReadableVec, Rw, Storag
|
||||
use crate::{blocks, distribution::DynCohortVecs, indexes, prices};
|
||||
|
||||
use crate::distribution::metrics::{
|
||||
AdjustedCohortMetrics, AllCohortMetrics, BasicCohortMetrics, CohortMetricsBase,
|
||||
ExtendedAdjustedCohortMetrics, ExtendedCohortMetrics, ImportConfig, SupplyMetrics,
|
||||
AllCohortMetrics, BasicCohortMetrics, CohortMetricsBase, CompleteCohortMetrics,
|
||||
CoreCohortMetrics, ExtendedAdjustedCohortMetrics, ExtendedCohortMetrics, ImportConfig,
|
||||
MinimalCohortMetrics, SupplyMetrics,
|
||||
};
|
||||
|
||||
use super::{percentiles::PercentileCache, vecs::UTXOCohortVecs};
|
||||
@@ -38,15 +39,15 @@ pub struct UTXOCohorts<M: StorageMode = Rw> {
|
||||
pub all: UTXOCohortVecs<AllCohortMetrics<M>>,
|
||||
pub sth: UTXOCohortVecs<ExtendedAdjustedCohortMetrics<M>>,
|
||||
pub lth: UTXOCohortVecs<ExtendedCohortMetrics<M>>,
|
||||
pub age_range: ByAgeRange<UTXOCohortVecs<ExtendedCohortMetrics<M>>>,
|
||||
pub max_age: ByMaxAge<UTXOCohortVecs<AdjustedCohortMetrics<M>>>,
|
||||
pub min_age: ByMinAge<UTXOCohortVecs<BasicCohortMetrics<M>>>,
|
||||
pub ge_amount: ByGreatEqualAmount<UTXOCohortVecs<BasicCohortMetrics<M>>>,
|
||||
pub amount_range: ByAmountRange<UTXOCohortVecs<BasicCohortMetrics<M>>>,
|
||||
pub lt_amount: ByLowerThanAmount<UTXOCohortVecs<BasicCohortMetrics<M>>>,
|
||||
pub epoch: ByEpoch<UTXOCohortVecs<BasicCohortMetrics<M>>>,
|
||||
pub class: ByClass<UTXOCohortVecs<BasicCohortMetrics<M>>>,
|
||||
pub type_: BySpendableType<UTXOCohortVecs<BasicCohortMetrics<M>>>,
|
||||
pub age_range: ByAgeRange<UTXOCohortVecs<BasicCohortMetrics<M>>>,
|
||||
pub max_age: ByMaxAge<UTXOCohortVecs<CompleteCohortMetrics<M>>>,
|
||||
pub min_age: ByMinAge<UTXOCohortVecs<CompleteCohortMetrics<M>>>,
|
||||
pub ge_amount: ByGreatEqualAmount<UTXOCohortVecs<CoreCohortMetrics<M>>>,
|
||||
pub amount_range: ByAmountRange<UTXOCohortVecs<CoreCohortMetrics<M>>>,
|
||||
pub lt_amount: ByLowerThanAmount<UTXOCohortVecs<CoreCohortMetrics<M>>>,
|
||||
pub epoch: ByEpoch<UTXOCohortVecs<CoreCohortMetrics<M>>>,
|
||||
pub class: ByClass<UTXOCohortVecs<CoreCohortMetrics<M>>>,
|
||||
pub type_: BySpendableType<UTXOCohortVecs<MinimalCohortMetrics<M>>>,
|
||||
#[traversable(skip)]
|
||||
pub(super) percentile_cache: PercentileCache,
|
||||
/// Cached partition_point positions for tick_tock boundary searches.
|
||||
@@ -83,25 +84,6 @@ impl UTXOCohorts<Rw> {
|
||||
|
||||
// Phase 2: Import separate (stateful) cohorts.
|
||||
|
||||
// age_range: ExtendedCohortMetrics with full state
|
||||
let age_range = {
|
||||
ByAgeRange::try_new(&|f: Filter, name: &'static str| -> Result<_> {
|
||||
let full_name = CohortContext::Utxo.full_name(&f, name);
|
||||
let cfg = ImportConfig {
|
||||
db,
|
||||
filter: &f,
|
||||
full_name: &full_name,
|
||||
version: v,
|
||||
indexes,
|
||||
};
|
||||
let state = Some(Box::new(UTXOCohortState::new(states_path, &full_name)));
|
||||
Ok(UTXOCohortVecs::new(
|
||||
state,
|
||||
ExtendedCohortMetrics::forced_import(&cfg)?,
|
||||
))
|
||||
})?
|
||||
};
|
||||
|
||||
// Helper for separate cohorts with BasicCohortMetrics + full state
|
||||
let basic_separate =
|
||||
|f: Filter, name: &'static str| -> Result<UTXOCohortVecs<BasicCohortMetrics>> {
|
||||
@@ -120,10 +102,46 @@ impl UTXOCohorts<Rw> {
|
||||
))
|
||||
};
|
||||
|
||||
let amount_range = ByAmountRange::try_new(&basic_separate)?;
|
||||
let epoch = ByEpoch::try_new(&basic_separate)?;
|
||||
let class = ByClass::try_new(&basic_separate)?;
|
||||
let type_ = BySpendableType::try_new(&basic_separate)?;
|
||||
let age_range = ByAgeRange::try_new(&basic_separate)?;
|
||||
|
||||
let core_separate =
|
||||
|f: Filter, name: &'static str| -> Result<UTXOCohortVecs<CoreCohortMetrics>> {
|
||||
let full_name = CohortContext::Utxo.full_name(&f, name);
|
||||
let cfg = ImportConfig {
|
||||
db,
|
||||
filter: &f,
|
||||
full_name: &full_name,
|
||||
version: v,
|
||||
indexes,
|
||||
};
|
||||
let state = Some(Box::new(UTXOCohortState::new(states_path, &full_name)));
|
||||
Ok(UTXOCohortVecs::new(
|
||||
state,
|
||||
CoreCohortMetrics::forced_import(&cfg)?,
|
||||
))
|
||||
};
|
||||
|
||||
let amount_range = ByAmountRange::try_new(&core_separate)?;
|
||||
let epoch = ByEpoch::try_new(&core_separate)?;
|
||||
let class = ByClass::try_new(&core_separate)?;
|
||||
|
||||
let type_ = BySpendableType::try_new(
|
||||
&|f: Filter, name: &'static str| -> Result<UTXOCohortVecs<MinimalCohortMetrics>> {
|
||||
let full_name = CohortContext::Utxo.full_name(&f, name);
|
||||
let cfg = ImportConfig {
|
||||
db,
|
||||
filter: &f,
|
||||
full_name: &full_name,
|
||||
version: v,
|
||||
indexes,
|
||||
};
|
||||
let state = Some(Box::new(UTXOCohortState::new(states_path, &full_name)));
|
||||
Ok(UTXOCohortVecs::new(
|
||||
state,
|
||||
MinimalCohortMetrics::forced_import(&cfg)?,
|
||||
))
|
||||
},
|
||||
)?;
|
||||
|
||||
// Phase 3: Import "all" cohort with pre-imported supply.
|
||||
let all = UTXOCohortVecs::new(
|
||||
@@ -161,7 +179,7 @@ impl UTXOCohorts<Rw> {
|
||||
UTXOCohortVecs::new(None, ExtendedCohortMetrics::forced_import(&cfg)?)
|
||||
};
|
||||
|
||||
// max_age: AdjustedCohortMetrics (adjusted + peak_regret)
|
||||
// max_age: CompleteCohortMetrics (no state, aggregates from age_range)
|
||||
let max_age = {
|
||||
ByMaxAge::try_new(&|f: Filter, name: &'static str| -> Result<_> {
|
||||
let full_name = CohortContext::Utxo.full_name(&f, name);
|
||||
@@ -174,12 +192,12 @@ impl UTXOCohorts<Rw> {
|
||||
};
|
||||
Ok(UTXOCohortVecs::new(
|
||||
None,
|
||||
AdjustedCohortMetrics::forced_import(&cfg)?,
|
||||
CompleteCohortMetrics::forced_import(&cfg)?,
|
||||
))
|
||||
})?
|
||||
};
|
||||
|
||||
// min_age: BasicCohortMetrics
|
||||
// min_age: CompleteCohortMetrics
|
||||
let min_age = {
|
||||
ByMinAge::try_new(&|f: Filter, name: &'static str| -> Result<_> {
|
||||
let full_name = CohortContext::Utxo.full_name(&f, name);
|
||||
@@ -192,14 +210,14 @@ impl UTXOCohorts<Rw> {
|
||||
};
|
||||
Ok(UTXOCohortVecs::new(
|
||||
None,
|
||||
BasicCohortMetrics::forced_import(&cfg)?,
|
||||
CompleteCohortMetrics::forced_import(&cfg)?,
|
||||
))
|
||||
})?
|
||||
};
|
||||
|
||||
// ge_amount, lt_amount: BasicCohortMetrics (no state)
|
||||
let basic_no_state =
|
||||
|f: Filter, name: &'static str| -> Result<UTXOCohortVecs<BasicCohortMetrics>> {
|
||||
// ge_amount, lt_amount: CoreCohortMetrics (no state)
|
||||
let core_no_state =
|
||||
|f: Filter, name: &'static str| -> Result<UTXOCohortVecs<CoreCohortMetrics>> {
|
||||
let full_name = CohortContext::Utxo.full_name(&f, name);
|
||||
let cfg = ImportConfig {
|
||||
db,
|
||||
@@ -210,12 +228,12 @@ impl UTXOCohorts<Rw> {
|
||||
};
|
||||
Ok(UTXOCohortVecs::new(
|
||||
None,
|
||||
BasicCohortMetrics::forced_import(&cfg)?,
|
||||
CoreCohortMetrics::forced_import(&cfg)?,
|
||||
))
|
||||
};
|
||||
|
||||
let lt_amount = ByLowerThanAmount::try_new(&basic_no_state)?;
|
||||
let ge_amount = ByGreatEqualAmount::try_new(&basic_no_state)?;
|
||||
let lt_amount = ByLowerThanAmount::try_new(&core_no_state)?;
|
||||
let ge_amount = ByGreatEqualAmount::try_new(&core_no_state)?;
|
||||
|
||||
Ok(Self {
|
||||
all,
|
||||
@@ -295,20 +313,20 @@ impl UTXOCohorts<Rw> {
|
||||
}),
|
||||
Box::new(|| {
|
||||
min_age.par_iter_mut().try_for_each(|vecs| {
|
||||
let sources = filter_sources_from(ar.iter(), Some(vecs.metrics.filter()));
|
||||
vecs.metrics.compute_base_from_others(si, &sources, exit)
|
||||
let sources = filter_sources_from(ar.iter(), Some(&vecs.metrics.filter));
|
||||
vecs.metrics.compute_from_sources(si, &sources, exit)
|
||||
})
|
||||
}),
|
||||
Box::new(|| {
|
||||
max_age.par_iter_mut().try_for_each(|vecs| {
|
||||
let sources = filter_sources_from(ar.iter(), Some(vecs.metrics.filter()));
|
||||
vecs.metrics.compute_base_from_others(si, &sources, exit)
|
||||
let sources = filter_sources_from(ar.iter(), Some(&vecs.metrics.filter));
|
||||
vecs.metrics.compute_from_sources(si, &sources, exit)
|
||||
})
|
||||
}),
|
||||
Box::new(|| {
|
||||
ge_amount.par_iter_mut().chain(lt_amount.par_iter_mut()).try_for_each(|vecs| {
|
||||
let sources = filter_sources_from(amr.iter(), Some(vecs.metrics.filter()));
|
||||
vecs.metrics.compute_base_from_others(si, &sources, exit)
|
||||
let sources = filter_core_sources_from(amr.iter(), Some(&vecs.metrics.filter));
|
||||
vecs.metrics.compute_from_sources(si, &sources, exit)
|
||||
})
|
||||
}),
|
||||
];
|
||||
@@ -366,15 +384,15 @@ impl UTXOCohorts<Rw> {
|
||||
|
||||
// 2. Compute net_sentiment.height for aggregate cohorts (weighted average).
|
||||
// Separate cohorts already computed net_sentiment in step 1 (inside compute_rest_part1).
|
||||
// Note: min_age, max_age, epoch, class are Complete tier — no net_sentiment.
|
||||
// Note: ge_amount, lt_amount, amount_range are Core tier — no net_sentiment.
|
||||
{
|
||||
let Self {
|
||||
all, sth, lth, age_range, max_age, min_age,
|
||||
ge_amount, amount_range, lt_amount,
|
||||
all, sth, lth, age_range,
|
||||
..
|
||||
} = self;
|
||||
|
||||
let ar = &*age_range;
|
||||
let amr = &*amount_range;
|
||||
let si = starting_indexes;
|
||||
|
||||
let tasks: Vec<Box<dyn FnOnce() -> Result<()> + Send + '_>> = vec![
|
||||
@@ -390,24 +408,6 @@ impl UTXOCohorts<Rw> {
|
||||
let sources = filter_sources_from(ar.iter(), Some(lth.metrics.filter()));
|
||||
lth.metrics.compute_net_sentiment_from_others_dyn(si, &sources, exit)
|
||||
}),
|
||||
Box::new(|| {
|
||||
min_age.par_iter_mut().try_for_each(|vecs| {
|
||||
let sources = filter_sources_from(ar.iter(), Some(vecs.metrics.filter()));
|
||||
vecs.metrics.compute_net_sentiment_from_others_dyn(si, &sources, exit)
|
||||
})
|
||||
}),
|
||||
Box::new(|| {
|
||||
max_age.par_iter_mut().try_for_each(|vecs| {
|
||||
let sources = filter_sources_from(ar.iter(), Some(vecs.metrics.filter()));
|
||||
vecs.metrics.compute_net_sentiment_from_others_dyn(si, &sources, exit)
|
||||
})
|
||||
}),
|
||||
Box::new(|| {
|
||||
ge_amount.par_iter_mut().chain(lt_amount.par_iter_mut()).try_for_each(|vecs| {
|
||||
let sources = filter_sources_from(amr.iter(), Some(vecs.metrics.filter()));
|
||||
vecs.metrics.compute_net_sentiment_from_others_dyn(si, &sources, exit)
|
||||
})
|
||||
}),
|
||||
];
|
||||
|
||||
tasks
|
||||
@@ -479,14 +479,14 @@ impl UTXOCohorts<Rw> {
|
||||
Box::new(|| sth.metrics.compute_rest_part2(blocks, prices, starting_indexes, height_to_market_cap, vc, vd, ss, exit)),
|
||||
Box::new(|| lth.metrics.compute_rest_part2(blocks, prices, starting_indexes, height_to_market_cap, ss, exit)),
|
||||
Box::new(|| age_range.par_iter_mut().try_for_each(|v| v.metrics.compute_rest_part2(blocks, prices, starting_indexes, height_to_market_cap, ss, exit))),
|
||||
Box::new(|| max_age.par_iter_mut().try_for_each(|v| v.metrics.compute_rest_part2(blocks, prices, starting_indexes, height_to_market_cap, vc, vd, ss, exit))),
|
||||
Box::new(|| max_age.par_iter_mut().try_for_each(|v| v.metrics.compute_rest_part2(blocks, prices, starting_indexes, height_to_market_cap, ss, exit))),
|
||||
Box::new(|| min_age.par_iter_mut().try_for_each(|v| v.metrics.compute_rest_part2(blocks, prices, starting_indexes, height_to_market_cap, ss, exit))),
|
||||
Box::new(|| ge_amount.par_iter_mut().try_for_each(|v| v.metrics.compute_rest_part2(blocks, prices, starting_indexes, height_to_market_cap, ss, exit))),
|
||||
Box::new(|| epoch.par_iter_mut().try_for_each(|v| v.metrics.compute_rest_part2(blocks, prices, starting_indexes, height_to_market_cap, ss, exit))),
|
||||
Box::new(|| class.par_iter_mut().try_for_each(|v| v.metrics.compute_rest_part2(blocks, prices, starting_indexes, height_to_market_cap, ss, exit))),
|
||||
Box::new(|| amount_range.par_iter_mut().try_for_each(|v| v.metrics.compute_rest_part2(blocks, prices, starting_indexes, height_to_market_cap, ss, exit))),
|
||||
Box::new(|| lt_amount.par_iter_mut().try_for_each(|v| v.metrics.compute_rest_part2(blocks, prices, starting_indexes, height_to_market_cap, ss, exit))),
|
||||
Box::new(|| type_.par_iter_mut().try_for_each(|v| v.metrics.compute_rest_part2(blocks, prices, starting_indexes, height_to_market_cap, ss, exit))),
|
||||
Box::new(|| type_.par_iter_mut().try_for_each(|v| v.metrics.compute_rest_part2(prices, starting_indexes, exit))),
|
||||
];
|
||||
|
||||
tasks
|
||||
@@ -613,3 +613,17 @@ fn filter_sources_from<'a, M: CohortMetricsBase + 'a>(
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Filter CoreCohortMetrics source cohorts by an optional filter.
|
||||
fn filter_core_sources_from<'a>(
|
||||
sources: impl Iterator<Item = &'a UTXOCohortVecs<CoreCohortMetrics>>,
|
||||
filter: Option<&Filter>,
|
||||
) -> Vec<&'a CoreCohortMetrics> {
|
||||
match filter {
|
||||
Some(f) => sources
|
||||
.filter(|v| f.includes(&v.metrics.filter))
|
||||
.map(|v| &v.metrics)
|
||||
.collect(),
|
||||
None => sources.map(|v| &v.metrics).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ use vecdb::{Exit, ReadableVec};
|
||||
|
||||
use crate::{blocks, distribution::state::UTXOCohortState, prices};
|
||||
|
||||
use crate::distribution::metrics::CohortMetricsBase;
|
||||
use crate::distribution::metrics::{CohortMetricsBase, CompleteCohortMetrics, CoreCohortMetrics, MinimalCohortMetrics};
|
||||
|
||||
use super::super::traits::DynCohortVecs;
|
||||
|
||||
@@ -161,3 +161,391 @@ impl<Metrics: CohortMetricsBase + Traversable> DynCohortVecs for UTXOCohortVecs<
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Filtered for UTXOCohortVecs<MinimalCohortMetrics> {
|
||||
fn filter(&self) -> &Filter {
|
||||
&self.metrics.filter
|
||||
}
|
||||
}
|
||||
|
||||
impl DynCohortVecs for UTXOCohortVecs<MinimalCohortMetrics> {
|
||||
fn min_stateful_height_len(&self) -> usize {
|
||||
self.metrics.min_stateful_height_len()
|
||||
}
|
||||
|
||||
fn reset_state_starting_height(&mut self) {
|
||||
self.state_starting_height = Some(Height::ZERO);
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
state.reset();
|
||||
}
|
||||
}
|
||||
|
||||
fn import_state(&mut self, starting_height: Height) -> Result<Height> {
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
if let Some(mut prev_height) = starting_height.decremented() {
|
||||
prev_height = state.import_at_or_before(prev_height)?;
|
||||
|
||||
state.supply.value = self
|
||||
.metrics
|
||||
.supply
|
||||
.total
|
||||
.sats
|
||||
.height
|
||||
.collect_one(prev_height)
|
||||
.unwrap();
|
||||
state.supply.utxo_count = *self
|
||||
.metrics
|
||||
.outputs
|
||||
.utxo_count
|
||||
.height
|
||||
.collect_one(prev_height)
|
||||
.unwrap();
|
||||
|
||||
state.restore_realized_cap();
|
||||
|
||||
let result = prev_height.incremented();
|
||||
self.state_starting_height = Some(result);
|
||||
Ok(result)
|
||||
} else {
|
||||
self.state_starting_height = Some(Height::ZERO);
|
||||
Ok(Height::ZERO)
|
||||
}
|
||||
} else {
|
||||
self.state_starting_height = Some(starting_height);
|
||||
Ok(starting_height)
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_computed_versions(&mut self, base_version: Version) -> Result<()> {
|
||||
self.metrics.validate_computed_versions(base_version)
|
||||
}
|
||||
|
||||
fn truncate_push(&mut self, height: Height) -> Result<()> {
|
||||
if self.state_starting_height.is_some_and(|h| h > height) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if let Some(state) = self.state.as_ref() {
|
||||
self.metrics
|
||||
.supply
|
||||
.truncate_push(height, state.supply.value)?;
|
||||
self.metrics
|
||||
.outputs
|
||||
.truncate_push(height, state.supply.utxo_count)?;
|
||||
self.metrics
|
||||
.realized
|
||||
.truncate_push(height, state.realized.cap())?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn compute_then_truncate_push_unrealized_states(
|
||||
&mut self,
|
||||
height: Height,
|
||||
height_price: Cents,
|
||||
_is_day_boundary: bool,
|
||||
) -> Result<()> {
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
state.apply_pending();
|
||||
let unrealized_state = state.compute_unrealized_state(height_price);
|
||||
self.metrics
|
||||
.unrealized
|
||||
.truncate_push(height, &unrealized_state)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn compute_rest_part1(
|
||||
&mut self,
|
||||
blocks: &blocks::Vecs,
|
||||
prices: &prices::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.metrics
|
||||
.compute_rest_part1(blocks, prices, starting_indexes, exit)
|
||||
}
|
||||
|
||||
fn write_state(&mut self, height: Height, cleanup: bool) -> Result<()> {
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
state.write(height, cleanup)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reset_cost_basis_data_if_needed(&mut self) -> Result<()> {
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
state.reset_cost_basis_data_if_needed()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reset_single_iteration_values(&mut self) {
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
state.reset_single_iteration_values();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Filtered for UTXOCohortVecs<CoreCohortMetrics> {
|
||||
fn filter(&self) -> &Filter {
|
||||
&self.metrics.filter
|
||||
}
|
||||
}
|
||||
|
||||
impl DynCohortVecs for UTXOCohortVecs<CoreCohortMetrics> {
|
||||
fn min_stateful_height_len(&self) -> usize {
|
||||
self.metrics.min_stateful_height_len()
|
||||
}
|
||||
|
||||
fn reset_state_starting_height(&mut self) {
|
||||
self.state_starting_height = Some(Height::ZERO);
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
state.reset();
|
||||
}
|
||||
}
|
||||
|
||||
fn import_state(&mut self, starting_height: Height) -> Result<Height> {
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
if let Some(mut prev_height) = starting_height.decremented() {
|
||||
prev_height = state.import_at_or_before(prev_height)?;
|
||||
|
||||
state.supply.value = self
|
||||
.metrics
|
||||
.supply
|
||||
.total
|
||||
.sats
|
||||
.height
|
||||
.collect_one(prev_height)
|
||||
.unwrap();
|
||||
state.supply.utxo_count = *self
|
||||
.metrics
|
||||
.outputs
|
||||
.utxo_count
|
||||
.height
|
||||
.collect_one(prev_height)
|
||||
.unwrap();
|
||||
|
||||
state.restore_realized_cap();
|
||||
|
||||
let result = prev_height.incremented();
|
||||
self.state_starting_height = Some(result);
|
||||
Ok(result)
|
||||
} else {
|
||||
self.state_starting_height = Some(Height::ZERO);
|
||||
Ok(Height::ZERO)
|
||||
}
|
||||
} else {
|
||||
self.state_starting_height = Some(starting_height);
|
||||
Ok(starting_height)
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_computed_versions(&mut self, base_version: Version) -> Result<()> {
|
||||
self.metrics.validate_computed_versions(base_version)
|
||||
}
|
||||
|
||||
fn truncate_push(&mut self, height: Height) -> Result<()> {
|
||||
if self.state_starting_height.is_some_and(|h| h > height) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if let Some(state) = self.state.as_ref() {
|
||||
self.metrics
|
||||
.supply
|
||||
.truncate_push(height, state.supply.value)?;
|
||||
self.metrics
|
||||
.outputs
|
||||
.truncate_push(height, state.supply.utxo_count)?;
|
||||
self.metrics.activity.truncate_push(height, state.sent)?;
|
||||
self.metrics
|
||||
.realized
|
||||
.truncate_push(height, &state.realized)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn compute_then_truncate_push_unrealized_states(
|
||||
&mut self,
|
||||
height: Height,
|
||||
height_price: Cents,
|
||||
_is_day_boundary: bool,
|
||||
) -> Result<()> {
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
state.apply_pending();
|
||||
let unrealized_state = state.compute_unrealized_state(height_price);
|
||||
self.metrics
|
||||
.unrealized
|
||||
.truncate_push(height, &unrealized_state)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn compute_rest_part1(
|
||||
&mut self,
|
||||
blocks: &blocks::Vecs,
|
||||
prices: &prices::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.metrics
|
||||
.compute_rest_part1(blocks, prices, starting_indexes, exit)
|
||||
}
|
||||
|
||||
fn write_state(&mut self, height: Height, cleanup: bool) -> Result<()> {
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
state.write(height, cleanup)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reset_cost_basis_data_if_needed(&mut self) -> Result<()> {
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
state.reset_cost_basis_data_if_needed()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reset_single_iteration_values(&mut self) {
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
state.reset_single_iteration_values();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Filtered for UTXOCohortVecs<CompleteCohortMetrics> {
|
||||
fn filter(&self) -> &Filter {
|
||||
&self.metrics.filter
|
||||
}
|
||||
}
|
||||
|
||||
impl DynCohortVecs for UTXOCohortVecs<CompleteCohortMetrics> {
|
||||
fn min_stateful_height_len(&self) -> usize {
|
||||
self.metrics.min_stateful_height_len()
|
||||
}
|
||||
|
||||
fn reset_state_starting_height(&mut self) {
|
||||
self.state_starting_height = Some(Height::ZERO);
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
state.reset();
|
||||
}
|
||||
}
|
||||
|
||||
fn import_state(&mut self, starting_height: Height) -> Result<Height> {
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
if let Some(mut prev_height) = starting_height.decremented() {
|
||||
prev_height = state.import_at_or_before(prev_height)?;
|
||||
|
||||
state.supply.value = self
|
||||
.metrics
|
||||
.supply
|
||||
.total
|
||||
.sats
|
||||
.height
|
||||
.collect_one(prev_height)
|
||||
.unwrap();
|
||||
state.supply.utxo_count = *self
|
||||
.metrics
|
||||
.outputs
|
||||
.utxo_count
|
||||
.height
|
||||
.collect_one(prev_height)
|
||||
.unwrap();
|
||||
|
||||
state.restore_realized_cap();
|
||||
|
||||
let result = prev_height.incremented();
|
||||
self.state_starting_height = Some(result);
|
||||
Ok(result)
|
||||
} else {
|
||||
self.state_starting_height = Some(Height::ZERO);
|
||||
Ok(Height::ZERO)
|
||||
}
|
||||
} else {
|
||||
self.state_starting_height = Some(starting_height);
|
||||
Ok(starting_height)
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_computed_versions(&mut self, base_version: Version) -> Result<()> {
|
||||
self.metrics.validate_computed_versions(base_version)
|
||||
}
|
||||
|
||||
fn truncate_push(&mut self, height: Height) -> Result<()> {
|
||||
if self.state_starting_height.is_some_and(|h| h > height) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if let Some(state) = self.state.as_ref() {
|
||||
self.metrics
|
||||
.supply
|
||||
.truncate_push(height, state.supply.value)?;
|
||||
self.metrics
|
||||
.outputs
|
||||
.truncate_push(height, state.supply.utxo_count)?;
|
||||
self.metrics.activity.truncate_push(
|
||||
height,
|
||||
state.sent,
|
||||
state.satblocks_destroyed,
|
||||
state.satdays_destroyed,
|
||||
)?;
|
||||
self.metrics
|
||||
.realized
|
||||
.truncate_push(height, &state.realized)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn compute_then_truncate_push_unrealized_states(
|
||||
&mut self,
|
||||
height: Height,
|
||||
height_price: Cents,
|
||||
_is_day_boundary: bool,
|
||||
) -> Result<()> {
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
state.apply_pending();
|
||||
self.metrics
|
||||
.cost_basis
|
||||
.truncate_push_minmax(height, state)?;
|
||||
let unrealized_state = state.compute_unrealized_state(height_price);
|
||||
self.metrics
|
||||
.unrealized
|
||||
.truncate_push(height, &unrealized_state)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn compute_rest_part1(
|
||||
&mut self,
|
||||
blocks: &blocks::Vecs,
|
||||
prices: &prices::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.metrics
|
||||
.compute_rest_part1(blocks, prices, starting_indexes, exit)
|
||||
}
|
||||
|
||||
fn write_state(&mut self, height: Height, cleanup: bool) -> Result<()> {
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
state.write(height, cleanup)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reset_cost_basis_data_if_needed(&mut self) -> Result<()> {
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
state.reset_cost_basis_data_if_needed()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reset_single_iteration_values(&mut self) {
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
state.reset_single_iteration_values();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,57 +1,44 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Bitcoin, Height, Indexes, Sats, StoredF64, Version};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use rayon::prelude::*;
|
||||
use vecdb::{AnyStoredVec, AnyVec, Exit, Rw, StorageMode, WritableVec};
|
||||
|
||||
use crate::{
|
||||
blocks,
|
||||
internal::{ComputedFromHeightCumulativeSum, RollingEmas2w, ValueFromHeightCumulative},
|
||||
};
|
||||
use crate::internal::ComputedFromHeightCumulativeSum;
|
||||
|
||||
use super::ImportConfig;
|
||||
use crate::{blocks, distribution::metrics::ImportConfig, prices};
|
||||
|
||||
/// Activity metrics for a cohort.
|
||||
#[derive(Traversable)]
|
||||
use super::CoreActivity;
|
||||
|
||||
#[derive(Deref, DerefMut, Traversable)]
|
||||
pub struct ActivityMetrics<M: StorageMode = Rw> {
|
||||
/// Total satoshis sent at each height + derived indexes
|
||||
pub sent: ValueFromHeightCumulative<M>,
|
||||
#[deref]
|
||||
#[deref_mut]
|
||||
#[traversable(flatten)]
|
||||
pub core: CoreActivity<M>,
|
||||
|
||||
/// 14-day EMA of sent supply (sats, btc, usd)
|
||||
pub sent_ema: RollingEmas2w<M>,
|
||||
|
||||
/// Coin-blocks destroyed (in BTC)
|
||||
pub coinblocks_destroyed: ComputedFromHeightCumulativeSum<StoredF64, M>,
|
||||
|
||||
/// Coin-days destroyed (in BTC)
|
||||
pub coindays_destroyed: ComputedFromHeightCumulativeSum<StoredF64, M>,
|
||||
}
|
||||
|
||||
impl ActivityMetrics {
|
||||
/// Import activity metrics from database.
|
||||
pub(crate) fn forced_import(cfg: &ImportConfig) -> Result<Self> {
|
||||
Ok(Self {
|
||||
sent: cfg.import_value_cumulative("sent", Version::ZERO)?,
|
||||
sent_ema: cfg.import_emas_2w("sent", Version::ZERO)?,
|
||||
|
||||
core: CoreActivity::forced_import(cfg)?,
|
||||
coinblocks_destroyed: cfg
|
||||
.import_cumulative_sum("coinblocks_destroyed", Version::ONE)?,
|
||||
coindays_destroyed: cfg.import_cumulative_sum("coindays_destroyed", Version::ONE)?,
|
||||
})
|
||||
}
|
||||
|
||||
/// Get minimum length across height-indexed vectors.
|
||||
pub(crate) fn min_len(&self) -> usize {
|
||||
self.sent
|
||||
.base
|
||||
.sats
|
||||
.height
|
||||
.len()
|
||||
self.core
|
||||
.min_len()
|
||||
.min(self.coinblocks_destroyed.height.len())
|
||||
.min(self.coindays_destroyed.height.len())
|
||||
}
|
||||
|
||||
/// Push activity state values to height-indexed vectors.
|
||||
pub(crate) fn truncate_push(
|
||||
&mut self,
|
||||
height: Height,
|
||||
@@ -59,7 +46,7 @@ impl ActivityMetrics {
|
||||
satblocks_destroyed: Sats,
|
||||
satdays_destroyed: Sats,
|
||||
) -> Result<()> {
|
||||
self.sent.base.sats.height.truncate_push(height, sent)?;
|
||||
self.core.truncate_push(height, sent)?;
|
||||
self.coinblocks_destroyed.height.truncate_push(
|
||||
height,
|
||||
StoredF64::from(Bitcoin::from(satblocks_destroyed)),
|
||||
@@ -71,29 +58,29 @@ impl ActivityMetrics {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns a parallel iterator over all vecs for parallel writing.
|
||||
pub(crate) fn par_iter_mut(&mut self) -> impl ParallelIterator<Item = &mut dyn AnyStoredVec> {
|
||||
vec![
|
||||
&mut self.sent.base.sats.height as &mut dyn AnyStoredVec,
|
||||
&mut self.core.sent.base.sats.height as &mut dyn AnyStoredVec,
|
||||
&mut self.coinblocks_destroyed.height as &mut dyn AnyStoredVec,
|
||||
&mut self.coindays_destroyed.height as &mut dyn AnyStoredVec,
|
||||
]
|
||||
.into_par_iter()
|
||||
}
|
||||
|
||||
/// Validate computed versions against base version.
|
||||
pub(crate) fn validate_computed_versions(&mut self, _base_version: Version) -> Result<()> {
|
||||
// Validation logic for computed vecs
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Compute aggregate values from separate cohorts.
|
||||
pub(crate) fn compute_from_stateful(
|
||||
&mut self,
|
||||
starting_indexes: &Indexes,
|
||||
others: &[&Self],
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
let core_refs: Vec<&CoreActivity> = others.iter().map(|o| &o.core).collect();
|
||||
self.core
|
||||
.compute_from_stateful(starting_indexes, &core_refs, exit)?;
|
||||
|
||||
macro_rules! sum_others {
|
||||
($($field:tt).+) => {
|
||||
self.$($field).+.compute_sum_of_others(
|
||||
@@ -104,29 +91,22 @@ impl ActivityMetrics {
|
||||
};
|
||||
}
|
||||
|
||||
sum_others!(sent.base.sats.height);
|
||||
sum_others!(coinblocks_destroyed.height);
|
||||
sum_others!(coindays_destroyed.height);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// First phase of computed metrics (indexes from height).
|
||||
pub(crate) fn compute_rest_part1(
|
||||
&mut self,
|
||||
blocks: &blocks::Vecs,
|
||||
prices: &prices::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
let window_starts = blocks.count.window_starts();
|
||||
self.core
|
||||
.compute_rest_part1(blocks, prices, starting_indexes, exit)?;
|
||||
|
||||
// 14-day EMA of sent (sats and dollars)
|
||||
self.sent_ema.compute(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_2w_ago,
|
||||
&self.sent.base.sats.height,
|
||||
&self.sent.base.cents.height,
|
||||
exit,
|
||||
)?;
|
||||
let window_starts = blocks.count.window_starts();
|
||||
|
||||
self.coinblocks_destroyed
|
||||
.compute_rest(starting_indexes.height, &window_starts, exit)?;
|
||||
@@ -0,0 +1,79 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Height, Indexes, Sats, Version};
|
||||
use rayon::prelude::*;
|
||||
use vecdb::{AnyStoredVec, AnyVec, Exit, Rw, StorageMode, WritableVec};
|
||||
|
||||
use crate::internal::{RollingEmas2w, ValueFromHeightCumulative};
|
||||
|
||||
use crate::{blocks, distribution::metrics::ImportConfig, prices};
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct CoreActivity<M: StorageMode = Rw> {
|
||||
pub sent: ValueFromHeightCumulative<M>,
|
||||
pub sent_ema: RollingEmas2w<M>,
|
||||
}
|
||||
|
||||
impl CoreActivity {
|
||||
pub(crate) fn forced_import(cfg: &ImportConfig) -> Result<Self> {
|
||||
Ok(Self {
|
||||
sent: cfg.import_value_cumulative("sent", Version::ZERO)?,
|
||||
sent_ema: cfg.import_emas_2w("sent", Version::ZERO)?,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn min_len(&self) -> usize {
|
||||
self.sent.base.sats.height.len()
|
||||
}
|
||||
|
||||
pub(crate) fn truncate_push(&mut self, height: Height, sent: Sats) -> Result<()> {
|
||||
self.sent.base.sats.height.truncate_push(height, sent)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn par_iter_mut(&mut self) -> impl ParallelIterator<Item = &mut dyn AnyStoredVec> {
|
||||
vec![&mut self.sent.base.sats.height as &mut dyn AnyStoredVec].into_par_iter()
|
||||
}
|
||||
|
||||
pub(crate) fn validate_computed_versions(&mut self, _base_version: Version) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn compute_from_stateful(
|
||||
&mut self,
|
||||
starting_indexes: &Indexes,
|
||||
others: &[&Self],
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.sent.base.sats.height.compute_sum_of_others(
|
||||
starting_indexes.height,
|
||||
&others
|
||||
.iter()
|
||||
.map(|v| &v.sent.base.sats.height)
|
||||
.collect::<Vec<_>>(),
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn compute_rest_part1(
|
||||
&mut self,
|
||||
blocks: &blocks::Vecs,
|
||||
prices: &prices::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.sent
|
||||
.compute(prices, starting_indexes.height, exit)?;
|
||||
|
||||
self.sent_ema.compute(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_2w_ago,
|
||||
&self.sent.base.sats.height,
|
||||
&self.sent.base.cents.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
mod base;
|
||||
mod core;
|
||||
|
||||
pub use base::*;
|
||||
pub use core::*;
|
||||
197
crates/brk_computer/src/distribution/metrics/cohort/complete.rs
Normal file
197
crates/brk_computer/src/distribution/metrics/cohort/complete.rs
Normal file
@@ -0,0 +1,197 @@
|
||||
use brk_cohort::Filter;
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Dollars, Height, Indexes, Sats, Version};
|
||||
use rayon::prelude::*;
|
||||
use vecdb::{AnyStoredVec, Exit, ReadableVec, Rw, StorageMode};
|
||||
|
||||
use crate::{blocks, prices};
|
||||
|
||||
use crate::distribution::metrics::{
|
||||
ActivityMetrics, CohortMetricsBase, CostBasisBase, ImportConfig, OutputsMetrics,
|
||||
RealizedComplete, RelativeCompleteWithRelToAll, SupplyMetrics, UnrealizedComplete,
|
||||
};
|
||||
|
||||
/// Complete cohort metrics (Tier C): ~216 stored vecs.
|
||||
///
|
||||
/// Used for epoch, class, min_age, max_age cohorts.
|
||||
/// Everything in Core, plus cost basis, CDD, value created/destroyed,
|
||||
/// sent in profit/loss, net PnL change, etc.
|
||||
///
|
||||
/// Does NOT include source-only fields (peak_regret, invested_capital,
|
||||
/// raw BytesVecs) or extended-only fields (investor_price, sell_side_risk,
|
||||
/// pain/greed/net_sentiment).
|
||||
///
|
||||
/// Does NOT implement CohortMetricsBase — standalone, not usable as Source.
|
||||
#[derive(Traversable)]
|
||||
pub struct CompleteCohortMetrics<M: StorageMode = Rw> {
|
||||
#[traversable(skip)]
|
||||
pub filter: Filter,
|
||||
pub supply: Box<SupplyMetrics<M>>,
|
||||
pub outputs: Box<OutputsMetrics<M>>,
|
||||
pub activity: Box<ActivityMetrics<M>>,
|
||||
pub realized: Box<RealizedComplete<M>>,
|
||||
pub cost_basis: Box<CostBasisBase<M>>,
|
||||
pub unrealized: Box<UnrealizedComplete<M>>,
|
||||
pub relative: Box<RelativeCompleteWithRelToAll<M>>,
|
||||
}
|
||||
|
||||
impl CompleteCohortMetrics {
|
||||
pub(crate) fn forced_import(cfg: &ImportConfig) -> Result<Self> {
|
||||
Ok(Self {
|
||||
filter: cfg.filter.clone(),
|
||||
supply: Box::new(SupplyMetrics::forced_import(cfg)?),
|
||||
outputs: Box::new(OutputsMetrics::forced_import(cfg)?),
|
||||
activity: Box::new(ActivityMetrics::forced_import(cfg)?),
|
||||
realized: Box::new(RealizedComplete::forced_import(cfg)?),
|
||||
cost_basis: Box::new(CostBasisBase::forced_import(cfg)?),
|
||||
unrealized: Box::new(UnrealizedComplete::forced_import(cfg)?),
|
||||
relative: Box::new(RelativeCompleteWithRelToAll::forced_import(cfg)?),
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn min_stateful_height_len(&self) -> usize {
|
||||
self.supply
|
||||
.min_len()
|
||||
.min(self.outputs.min_len())
|
||||
.min(self.activity.min_len())
|
||||
.min(self.realized.min_stateful_height_len())
|
||||
.min(self.unrealized.min_stateful_height_len())
|
||||
.min(self.cost_basis.min_stateful_height_len())
|
||||
}
|
||||
|
||||
pub(crate) fn validate_computed_versions(&mut self, base_version: Version) -> Result<()> {
|
||||
self.supply.validate_computed_versions(base_version)?;
|
||||
self.activity.validate_computed_versions(base_version)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn collect_all_vecs_mut(&mut self) -> Vec<&mut dyn AnyStoredVec> {
|
||||
let mut vecs: Vec<&mut dyn AnyStoredVec> = Vec::new();
|
||||
vecs.extend(self.supply.par_iter_mut().collect::<Vec<_>>());
|
||||
vecs.extend(self.outputs.par_iter_mut().collect::<Vec<_>>());
|
||||
vecs.extend(self.activity.par_iter_mut().collect::<Vec<_>>());
|
||||
vecs.extend(self.realized.collect_vecs_mut());
|
||||
vecs.extend(self.cost_basis.collect_vecs_mut());
|
||||
vecs.extend(self.unrealized.collect_vecs_mut());
|
||||
vecs
|
||||
}
|
||||
|
||||
/// Aggregate Complete-tier metrics from Source cohort refs.
|
||||
pub(crate) fn compute_from_sources(
|
||||
&mut self,
|
||||
starting_indexes: &Indexes,
|
||||
others: &[&dyn CohortMetricsBase],
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
// Supply, outputs, activity: use their existing compute_from_stateful
|
||||
self.supply.compute_from_stateful(
|
||||
starting_indexes,
|
||||
&others.iter().map(|v| v.supply()).collect::<Vec<_>>(),
|
||||
exit,
|
||||
)?;
|
||||
self.outputs.compute_from_stateful(
|
||||
starting_indexes,
|
||||
&others.iter().map(|v| v.outputs()).collect::<Vec<_>>(),
|
||||
exit,
|
||||
)?;
|
||||
self.activity.compute_from_stateful(
|
||||
starting_indexes,
|
||||
&others.iter().map(|v| v.activity()).collect::<Vec<_>>(),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Realized: aggregate only Complete-tier fields from Source's RealizedBase
|
||||
let realized_complete_refs: Vec<&RealizedComplete> = others
|
||||
.iter()
|
||||
.map(|v| &v.realized_base().complete)
|
||||
.collect();
|
||||
self.realized
|
||||
.compute_from_stateful(starting_indexes, &realized_complete_refs, exit)?;
|
||||
|
||||
// Unrealized: aggregate only Complete-tier fields
|
||||
let unrealized_complete_refs: Vec<&UnrealizedComplete> = others
|
||||
.iter()
|
||||
.map(|v| &v.unrealized_base().complete)
|
||||
.collect();
|
||||
self.unrealized
|
||||
.compute_from_stateful(starting_indexes, &unrealized_complete_refs, exit)?;
|
||||
|
||||
// Cost basis: use existing aggregation
|
||||
self.cost_basis.compute_from_stateful(
|
||||
starting_indexes,
|
||||
&others
|
||||
.iter()
|
||||
.map(|v| v.cost_basis_base())
|
||||
.collect::<Vec<_>>(),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// First phase: compute index transforms.
|
||||
pub(crate) fn compute_rest_part1(
|
||||
&mut self,
|
||||
blocks: &blocks::Vecs,
|
||||
prices: &prices::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.supply
|
||||
.compute(prices, starting_indexes.height, exit)?;
|
||||
self.supply
|
||||
.compute_rest_part1(blocks, starting_indexes, exit)?;
|
||||
self.outputs
|
||||
.compute_rest(blocks, starting_indexes, exit)?;
|
||||
self.activity
|
||||
.sent
|
||||
.compute(prices, starting_indexes.height, exit)?;
|
||||
self.activity
|
||||
.compute_rest_part1(blocks, prices, starting_indexes, exit)?;
|
||||
|
||||
self.realized
|
||||
.sent_in_profit
|
||||
.compute(prices, starting_indexes.height, exit)?;
|
||||
self.realized
|
||||
.sent_in_loss
|
||||
.compute(prices, starting_indexes.height, exit)?;
|
||||
self.realized
|
||||
.compute_rest_part1(starting_indexes, exit)?;
|
||||
|
||||
self.unrealized.compute_rest(starting_indexes, exit)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Second phase: compute relative metrics and remaining.
|
||||
pub(crate) fn compute_rest_part2(
|
||||
&mut self,
|
||||
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<()> {
|
||||
self.realized.compute_rest_part2(
|
||||
blocks,
|
||||
prices,
|
||||
starting_indexes,
|
||||
&self.supply.total.btc.height,
|
||||
height_to_market_cap,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.relative.compute(
|
||||
starting_indexes.height,
|
||||
&self.unrealized,
|
||||
&self.supply.total.sats.height,
|
||||
height_to_market_cap,
|
||||
all_supply_sats,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
153
crates/brk_computer/src/distribution/metrics/cohort/core.rs
Normal file
153
crates/brk_computer/src/distribution/metrics/cohort/core.rs
Normal file
@@ -0,0 +1,153 @@
|
||||
use brk_cohort::Filter;
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Dollars, Height, Indexes, Sats, Version};
|
||||
use rayon::prelude::*;
|
||||
use vecdb::{AnyStoredVec, Exit, ReadableVec, Rw, StorageMode};
|
||||
|
||||
use crate::{blocks, prices};
|
||||
|
||||
use crate::distribution::metrics::{
|
||||
CoreActivity, CoreRealized, ImportConfig, OutputsMetrics,
|
||||
RelativeCompleteWithRelToAll, SupplyMetrics, UnrealizedComplete,
|
||||
};
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct CoreCohortMetrics<M: StorageMode = Rw> {
|
||||
#[traversable(skip)]
|
||||
pub filter: Filter,
|
||||
pub supply: Box<SupplyMetrics<M>>,
|
||||
pub outputs: Box<OutputsMetrics<M>>,
|
||||
pub activity: Box<CoreActivity<M>>,
|
||||
pub realized: Box<CoreRealized<M>>,
|
||||
pub unrealized: Box<UnrealizedComplete<M>>,
|
||||
pub relative: Box<RelativeCompleteWithRelToAll<M>>,
|
||||
}
|
||||
|
||||
impl CoreCohortMetrics {
|
||||
pub(crate) fn forced_import(cfg: &ImportConfig) -> Result<Self> {
|
||||
Ok(Self {
|
||||
filter: cfg.filter.clone(),
|
||||
supply: Box::new(SupplyMetrics::forced_import(cfg)?),
|
||||
outputs: Box::new(OutputsMetrics::forced_import(cfg)?),
|
||||
activity: Box::new(CoreActivity::forced_import(cfg)?),
|
||||
realized: Box::new(CoreRealized::forced_import(cfg)?),
|
||||
unrealized: Box::new(UnrealizedComplete::forced_import(cfg)?),
|
||||
relative: Box::new(RelativeCompleteWithRelToAll::forced_import(cfg)?),
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn min_stateful_height_len(&self) -> usize {
|
||||
self.supply
|
||||
.min_len()
|
||||
.min(self.outputs.min_len())
|
||||
.min(self.activity.min_len())
|
||||
.min(self.realized.min_stateful_height_len())
|
||||
.min(self.unrealized.min_stateful_height_len())
|
||||
}
|
||||
|
||||
pub(crate) fn validate_computed_versions(&mut self, base_version: Version) -> Result<()> {
|
||||
self.supply.validate_computed_versions(base_version)?;
|
||||
self.activity.validate_computed_versions(base_version)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn collect_all_vecs_mut(&mut self) -> Vec<&mut dyn AnyStoredVec> {
|
||||
let mut vecs: Vec<&mut dyn AnyStoredVec> = Vec::new();
|
||||
vecs.extend(self.supply.par_iter_mut().collect::<Vec<_>>());
|
||||
vecs.extend(self.outputs.par_iter_mut().collect::<Vec<_>>());
|
||||
vecs.extend(self.activity.par_iter_mut().collect::<Vec<_>>());
|
||||
vecs.extend(self.realized.collect_vecs_mut());
|
||||
vecs.extend(self.unrealized.collect_vecs_mut());
|
||||
vecs
|
||||
}
|
||||
|
||||
pub(crate) fn compute_from_sources(
|
||||
&mut self,
|
||||
starting_indexes: &Indexes,
|
||||
others: &[&CoreCohortMetrics],
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.supply.compute_from_stateful(
|
||||
starting_indexes,
|
||||
&others.iter().map(|v| v.supply.as_ref()).collect::<Vec<_>>(),
|
||||
exit,
|
||||
)?;
|
||||
self.outputs.compute_from_stateful(
|
||||
starting_indexes,
|
||||
&others.iter().map(|v| v.outputs.as_ref()).collect::<Vec<_>>(),
|
||||
exit,
|
||||
)?;
|
||||
self.activity.compute_from_stateful(
|
||||
starting_indexes,
|
||||
&others.iter().map(|v| v.activity.as_ref()).collect::<Vec<_>>(),
|
||||
exit,
|
||||
)?;
|
||||
self.realized.compute_from_stateful(
|
||||
starting_indexes,
|
||||
&others.iter().map(|v| v.realized.as_ref()).collect::<Vec<_>>(),
|
||||
exit,
|
||||
)?;
|
||||
self.unrealized.compute_from_stateful(
|
||||
starting_indexes,
|
||||
&others.iter().map(|v| v.unrealized.as_ref()).collect::<Vec<_>>(),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn compute_rest_part1(
|
||||
&mut self,
|
||||
blocks: &blocks::Vecs,
|
||||
prices: &prices::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.supply
|
||||
.compute(prices, starting_indexes.height, exit)?;
|
||||
self.supply
|
||||
.compute_rest_part1(blocks, starting_indexes, exit)?;
|
||||
self.outputs
|
||||
.compute_rest(blocks, starting_indexes, exit)?;
|
||||
|
||||
self.activity
|
||||
.compute_rest_part1(blocks, prices, starting_indexes, exit)?;
|
||||
|
||||
self.realized
|
||||
.compute_rest_part1(starting_indexes, exit)?;
|
||||
|
||||
self.unrealized.compute_rest(starting_indexes, exit)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn compute_rest_part2(
|
||||
&mut self,
|
||||
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<()> {
|
||||
self.realized.compute_rest_part2(
|
||||
blocks,
|
||||
prices,
|
||||
starting_indexes,
|
||||
&self.supply.total.btc.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.relative.compute(
|
||||
starting_indexes.height,
|
||||
&self.unrealized,
|
||||
&self.supply.total.sats.height,
|
||||
height_to_market_cap,
|
||||
all_supply_sats,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
302
crates/brk_computer/src/distribution/metrics/cohort/minimal.rs
Normal file
302
crates/brk_computer/src/distribution/metrics/cohort/minimal.rs
Normal file
@@ -0,0 +1,302 @@
|
||||
use brk_cohort::Filter;
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{
|
||||
BasisPoints16, BasisPoints32, Bitcoin, Cents, Dollars, Height, Indexes, Sats, StoredF32,
|
||||
Version,
|
||||
};
|
||||
use rayon::prelude::*;
|
||||
use vecdb::{AnyStoredVec, AnyVec, Exit, ReadableCloneableVec, ReadableVec, Rw, StorageMode, WritableVec};
|
||||
|
||||
use crate::{blocks, prices};
|
||||
|
||||
use crate::internal::{
|
||||
CentsUnsignedToDollars, ComputedFromHeight, ComputedFromHeightRatio, Identity, LazyFromHeight,
|
||||
PercentFromHeight, Price, RatioSatsBp16, ValueFromHeight,
|
||||
};
|
||||
|
||||
use crate::distribution::{
|
||||
metrics::{ImportConfig, OutputsMetrics, SupplyMetrics},
|
||||
state::UnrealizedState,
|
||||
};
|
||||
|
||||
/// Minimal realized metrics: realized cap, realized price, and MVRV ratio.
|
||||
#[derive(Traversable)]
|
||||
pub struct MinimalRealized<M: StorageMode = Rw> {
|
||||
pub realized_cap_cents: ComputedFromHeight<Cents, M>,
|
||||
pub realized_cap: LazyFromHeight<Dollars, Cents>,
|
||||
pub realized_price: Price<ComputedFromHeight<Cents, M>>,
|
||||
pub realized_price_ratio: ComputedFromHeightRatio<M>,
|
||||
pub mvrv: LazyFromHeight<StoredF32>,
|
||||
}
|
||||
|
||||
/// Minimal unrealized metrics: supply in profit/loss only.
|
||||
#[derive(Traversable)]
|
||||
pub struct MinimalUnrealized<M: StorageMode = Rw> {
|
||||
pub supply_in_profit: ValueFromHeight<M>,
|
||||
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 (Tier A): ~15 stored vecs.
|
||||
///
|
||||
/// Used for type_ cohorts where most metrics are irrelevant.
|
||||
/// Does NOT implement CohortMetricsBase — standalone, not aggregatable.
|
||||
#[derive(Traversable)]
|
||||
pub struct MinimalCohortMetrics<M: StorageMode = Rw> {
|
||||
#[traversable(skip)]
|
||||
pub filter: Filter,
|
||||
pub supply: Box<SupplyMetrics<M>>,
|
||||
pub outputs: Box<OutputsMetrics<M>>,
|
||||
pub realized: Box<MinimalRealized<M>>,
|
||||
pub unrealized: Box<MinimalUnrealized<M>>,
|
||||
pub relative: Box<MinimalRelative<M>>,
|
||||
}
|
||||
|
||||
impl MinimalRealized {
|
||||
pub(crate) fn forced_import(cfg: &ImportConfig) -> Result<Self> {
|
||||
let realized_cap_cents = cfg.import_computed("realized_cap_cents", Version::ZERO)?;
|
||||
let realized_cap = LazyFromHeight::from_computed::<CentsUnsignedToDollars>(
|
||||
&cfg.name("realized_cap"),
|
||||
cfg.version,
|
||||
realized_cap_cents.height.read_only_boxed_clone(),
|
||||
&realized_cap_cents,
|
||||
);
|
||||
|
||||
let realized_price = cfg.import_price("realized_price", Version::ONE)?;
|
||||
let realized_price_ratio = cfg.import_ratio("realized_price", Version::ONE)?;
|
||||
let mvrv = LazyFromHeight::from_lazy::<Identity<StoredF32>, BasisPoints32>(
|
||||
&cfg.name("mvrv"),
|
||||
cfg.version,
|
||||
&realized_price_ratio.ratio,
|
||||
);
|
||||
|
||||
Ok(Self {
|
||||
realized_cap_cents,
|
||||
realized_cap,
|
||||
realized_price,
|
||||
realized_price_ratio,
|
||||
mvrv,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn min_stateful_height_len(&self) -> usize {
|
||||
self.realized_cap_cents.height.len()
|
||||
}
|
||||
|
||||
pub(crate) fn truncate_push(&mut self, height: Height, cap: Cents) -> Result<()> {
|
||||
self.realized_cap_cents
|
||||
.height
|
||||
.truncate_push(height, cap)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn collect_vecs_mut(&mut self) -> Vec<&mut dyn AnyStoredVec> {
|
||||
vec![&mut self.realized_cap_cents.height as &mut dyn AnyStoredVec]
|
||||
}
|
||||
|
||||
pub(crate) fn compute_rest_part2(
|
||||
&mut self,
|
||||
prices: &prices::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
height_to_supply: &impl ReadableVec<Height, Bitcoin>,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.realized_price.cents.height.compute_transform2(
|
||||
starting_indexes.height,
|
||||
&self.realized_cap_cents.height,
|
||||
height_to_supply,
|
||||
|(i, cap_cents, supply, ..)| {
|
||||
let cap = cap_cents.as_u128();
|
||||
let supply_sats = Sats::from(supply).as_u128();
|
||||
if supply_sats == 0 {
|
||||
(i, Cents::ZERO)
|
||||
} else {
|
||||
(i, Cents::from(cap * Sats::ONE_BTC_U128 / supply_sats))
|
||||
}
|
||||
},
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.realized_price_ratio.compute_ratio(
|
||||
starting_indexes,
|
||||
&prices.price.cents.height,
|
||||
&self.realized_price.cents.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl MinimalUnrealized {
|
||||
pub(crate) fn forced_import(cfg: &ImportConfig) -> Result<Self> {
|
||||
Ok(Self {
|
||||
supply_in_profit: cfg.import_value("supply_in_profit", Version::ZERO)?,
|
||||
supply_in_loss: cfg.import_value("supply_in_loss", Version::ZERO)?,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn min_stateful_height_len(&self) -> usize {
|
||||
self.supply_in_profit
|
||||
.sats
|
||||
.height
|
||||
.len()
|
||||
.min(self.supply_in_loss.sats.height.len())
|
||||
}
|
||||
|
||||
pub(crate) fn truncate_push(
|
||||
&mut self,
|
||||
height: Height,
|
||||
state: &UnrealizedState,
|
||||
) -> Result<()> {
|
||||
self.supply_in_profit
|
||||
.sats
|
||||
.height
|
||||
.truncate_push(height, state.supply_in_profit)?;
|
||||
self.supply_in_loss
|
||||
.sats
|
||||
.height
|
||||
.truncate_push(height, state.supply_in_loss)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn collect_vecs_mut(&mut self) -> Vec<&mut dyn AnyStoredVec> {
|
||||
vec![
|
||||
&mut self.supply_in_profit.base.sats.height as &mut dyn AnyStoredVec,
|
||||
&mut self.supply_in_profit.base.cents.height as &mut dyn AnyStoredVec,
|
||||
&mut self.supply_in_loss.base.sats.height as &mut dyn AnyStoredVec,
|
||||
&mut self.supply_in_loss.base.cents.height as &mut dyn AnyStoredVec,
|
||||
]
|
||||
}
|
||||
|
||||
pub(crate) fn compute_rest(
|
||||
&mut self,
|
||||
prices: &prices::Vecs,
|
||||
max_from: Height,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.supply_in_profit.compute(prices, max_from, exit)?;
|
||||
self.supply_in_loss.compute(prices, max_from, exit)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl MinimalRelative {
|
||||
pub(crate) fn forced_import(cfg: &ImportConfig) -> Result<Self> {
|
||||
Ok(Self {
|
||||
supply_in_profit_rel_to_own_supply: cfg
|
||||
.import_percent_bp16("supply_in_profit_rel_to_own_supply", Version::ONE)?,
|
||||
supply_in_loss_rel_to_own_supply: cfg
|
||||
.import_percent_bp16("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 {
|
||||
filter: cfg.filter.clone(),
|
||||
supply: Box::new(SupplyMetrics::forced_import(cfg)?),
|
||||
outputs: Box::new(OutputsMetrics::forced_import(cfg)?),
|
||||
realized: Box::new(MinimalRealized::forced_import(cfg)?),
|
||||
unrealized: Box::new(MinimalUnrealized::forced_import(cfg)?),
|
||||
relative: Box::new(MinimalRelative::forced_import(cfg)?),
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn min_stateful_height_len(&self) -> usize {
|
||||
self.supply
|
||||
.min_len()
|
||||
.min(self.outputs.min_len())
|
||||
.min(self.realized.min_stateful_height_len())
|
||||
.min(self.unrealized.min_stateful_height_len())
|
||||
}
|
||||
|
||||
pub(crate) fn validate_computed_versions(&mut self, base_version: Version) -> Result<()> {
|
||||
self.supply.validate_computed_versions(base_version)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn collect_all_vecs_mut(&mut self) -> Vec<&mut dyn AnyStoredVec> {
|
||||
let mut vecs: Vec<&mut dyn AnyStoredVec> = Vec::new();
|
||||
vecs.extend(self.supply.par_iter_mut().collect::<Vec<_>>());
|
||||
vecs.extend(self.outputs.par_iter_mut().collect::<Vec<_>>());
|
||||
vecs.extend(self.realized.collect_vecs_mut());
|
||||
vecs.extend(self.unrealized.collect_vecs_mut());
|
||||
vecs
|
||||
}
|
||||
|
||||
pub(crate) fn compute_rest_part1(
|
||||
&mut self,
|
||||
blocks: &blocks::Vecs,
|
||||
prices: &prices::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.supply
|
||||
.compute(prices, starting_indexes.height, exit)?;
|
||||
self.supply
|
||||
.compute_rest_part1(blocks, starting_indexes, exit)?;
|
||||
self.outputs
|
||||
.compute_rest(blocks, starting_indexes, exit)?;
|
||||
self.unrealized
|
||||
.compute_rest(prices, starting_indexes.height, exit)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn compute_rest_part2(
|
||||
&mut self,
|
||||
prices: &prices::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.realized.compute_rest_part2(
|
||||
prices,
|
||||
starting_indexes,
|
||||
&self.supply.total.btc.height,
|
||||
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(())
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,17 @@
|
||||
mod adjusted;
|
||||
mod all;
|
||||
mod basic;
|
||||
mod complete;
|
||||
mod core;
|
||||
mod extended;
|
||||
mod extended_adjusted;
|
||||
mod minimal;
|
||||
|
||||
pub use adjusted::*;
|
||||
pub use all::*;
|
||||
pub use basic::*;
|
||||
pub use complete::*;
|
||||
pub use core::*;
|
||||
pub use extended::*;
|
||||
pub use extended_adjusted::*;
|
||||
pub use minimal::*;
|
||||
|
||||
@@ -168,7 +168,7 @@ pub trait CohortMetricsBase: Send + Sync {
|
||||
.sent
|
||||
.compute(prices, starting_indexes.height, exit)?;
|
||||
self.activity_mut()
|
||||
.compute_rest_part1(blocks, starting_indexes, exit)?;
|
||||
.compute_rest_part1(blocks, prices, starting_indexes, exit)?;
|
||||
|
||||
self.realized_base_mut()
|
||||
.sent_in_profit
|
||||
|
||||
@@ -1,38 +1,38 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{
|
||||
BasisPoints32, BasisPointsSigned32, Bitcoin, Cents, CentsSats, CentsSigned,
|
||||
CentsSquaredSats, Dollars, Height, Indexes, Sats, StoredF32, StoredF64, Version,
|
||||
};
|
||||
use vecdb::{
|
||||
AnyStoredVec, AnyVec, BytesVec, Exit, ReadableCloneableVec, ReadableVec, Rw, StorageMode,
|
||||
WritableVec,
|
||||
BasisPoints32, Bitcoin, Cents, CentsSats, CentsSquaredSats, Dollars, Height, Indexes, Version,
|
||||
};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use vecdb::{AnyStoredVec, AnyVec, BytesVec, Exit, ReadableVec, Rw, StorageMode, WritableVec};
|
||||
|
||||
use crate::{
|
||||
blocks,
|
||||
distribution::state::RealizedState,
|
||||
internal::{
|
||||
CentsPlus, CentsUnsignedToDollars, ComputedFromHeight, ComputedFromHeightCumulative,
|
||||
ComputedFromHeightRatio, FiatFromHeight, Identity, LazyFromHeight,
|
||||
NegCentsUnsignedToDollars, PercentFromHeight, PercentRollingEmas1w1m,
|
||||
PercentRollingWindows, Price, RatioCents64, RatioCentsBp32,
|
||||
RatioCentsSignedCentsBps32, RatioCentsSignedDollarsBps32, RollingEmas1w1m, RollingEmas2w,
|
||||
RollingWindows, ValueFromHeightCumulative,
|
||||
ComputedFromHeight, ComputedFromHeightCumulative, ComputedFromHeightRatio,
|
||||
PercentFromHeight, PercentRollingEmas1w1m, PercentRollingWindows, Price, RatioCentsBp32,
|
||||
},
|
||||
prices,
|
||||
};
|
||||
|
||||
use crate::distribution::metrics::ImportConfig;
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct RealizedBase<M: StorageMode = Rw> {
|
||||
pub realized_cap_cents: ComputedFromHeight<Cents, M>,
|
||||
pub realized_cap: LazyFromHeight<Dollars, Cents>,
|
||||
pub realized_price: Price<ComputedFromHeight<Cents, M>>,
|
||||
pub realized_price_ratio: ComputedFromHeightRatio<M>,
|
||||
pub realized_cap_change_1m: ComputedFromHeight<CentsSigned, M>,
|
||||
use super::RealizedComplete;
|
||||
|
||||
/// Full realized metrics (Source/Extended tier).
|
||||
///
|
||||
/// Contains all Complete-tier fields (via Deref to RealizedComplete) plus:
|
||||
/// - Source-only: peak_regret, peak_regret_rel_to_realized_cap
|
||||
/// - Extended-only: investor_price, price bands, cap_raw, sell_side_risk_ratio
|
||||
#[derive(Deref, DerefMut, Traversable)]
|
||||
pub struct RealizedBase<M: StorageMode = Rw> {
|
||||
#[deref]
|
||||
#[deref_mut]
|
||||
#[traversable(flatten)]
|
||||
pub complete: RealizedComplete<M>,
|
||||
|
||||
// --- Extended-only fields ---
|
||||
pub investor_price: Price<ComputedFromHeight<Cents, M>>,
|
||||
pub investor_price_ratio: ComputedFromHeightRatio<M>,
|
||||
|
||||
@@ -42,93 +42,20 @@ pub struct RealizedBase<M: StorageMode = Rw> {
|
||||
pub cap_raw: M::Stored<BytesVec<Height, CentsSats>>,
|
||||
pub investor_cap_raw: M::Stored<BytesVec<Height, CentsSquaredSats>>,
|
||||
|
||||
pub mvrv: LazyFromHeight<StoredF32>,
|
||||
|
||||
pub realized_profit: ComputedFromHeightCumulative<Cents, M>,
|
||||
pub realized_profit_ema_1w: ComputedFromHeight<Cents, M>,
|
||||
pub realized_loss: ComputedFromHeightCumulative<Cents, M>,
|
||||
pub realized_loss_ema_1w: ComputedFromHeight<Cents, M>,
|
||||
pub neg_realized_loss: LazyFromHeight<Dollars, Cents>,
|
||||
pub net_realized_pnl: ComputedFromHeightCumulative<CentsSigned, M>,
|
||||
pub net_realized_pnl_ema_1w: ComputedFromHeight<CentsSigned, M>,
|
||||
pub gross_pnl: FiatFromHeight<Cents, M>,
|
||||
|
||||
pub realized_profit_rel_to_realized_cap: PercentFromHeight<BasisPoints32, M>,
|
||||
pub realized_loss_rel_to_realized_cap: PercentFromHeight<BasisPoints32, M>,
|
||||
pub net_realized_pnl_rel_to_realized_cap: PercentFromHeight<BasisPointsSigned32, M>,
|
||||
|
||||
pub profit_value_created: ComputedFromHeight<Cents, M>,
|
||||
pub profit_value_destroyed: ComputedFromHeight<Cents, M>,
|
||||
pub loss_value_created: ComputedFromHeight<Cents, M>,
|
||||
pub loss_value_destroyed: ComputedFromHeight<Cents, M>,
|
||||
|
||||
pub value_created: ComputedFromHeight<Cents, M>,
|
||||
pub value_destroyed: ComputedFromHeight<Cents, M>,
|
||||
|
||||
pub capitulation_flow: LazyFromHeight<Dollars, Cents>,
|
||||
pub profit_flow: LazyFromHeight<Dollars, Cents>,
|
||||
|
||||
pub value_created_sum: RollingWindows<Cents, M>,
|
||||
pub value_destroyed_sum: RollingWindows<Cents, M>,
|
||||
|
||||
pub sopr: RollingWindows<StoredF64, M>,
|
||||
pub sopr_24h_ema: RollingEmas1w1m<StoredF64, M>,
|
||||
|
||||
pub gross_pnl_sum: RollingWindows<Cents, M>,
|
||||
pub sell_side_risk_ratio: PercentRollingWindows<BasisPoints32, M>,
|
||||
pub sell_side_risk_ratio_24h_ema: PercentRollingEmas1w1m<BasisPoints32, M>,
|
||||
|
||||
pub net_pnl_change_1m: ComputedFromHeight<CentsSigned, M>,
|
||||
pub net_pnl_change_1m_rel_to_realized_cap: PercentFromHeight<BasisPointsSigned32, M>,
|
||||
pub net_pnl_change_1m_rel_to_market_cap: PercentFromHeight<BasisPointsSigned32, M>,
|
||||
|
||||
// --- Source-only fields ---
|
||||
pub peak_regret: ComputedFromHeightCumulative<Cents, M>,
|
||||
pub peak_regret_rel_to_realized_cap: PercentFromHeight<BasisPoints32, M>,
|
||||
|
||||
pub sent_in_profit: ValueFromHeightCumulative<M>,
|
||||
pub sent_in_profit_ema: RollingEmas2w<M>,
|
||||
pub sent_in_loss: ValueFromHeightCumulative<M>,
|
||||
pub sent_in_loss_ema: RollingEmas2w<M>,
|
||||
}
|
||||
|
||||
impl RealizedBase {
|
||||
pub(crate) fn forced_import(cfg: &ImportConfig) -> Result<Self> {
|
||||
let v0 = Version::ZERO;
|
||||
let v1 = Version::ONE;
|
||||
|
||||
let realized_cap_cents = cfg.import_computed("realized_cap_cents", v0)?;
|
||||
let realized_cap = LazyFromHeight::from_computed::<CentsUnsignedToDollars>(
|
||||
&cfg.name("realized_cap"),
|
||||
cfg.version,
|
||||
realized_cap_cents.height.read_only_boxed_clone(),
|
||||
&realized_cap_cents,
|
||||
);
|
||||
let complete = RealizedComplete::forced_import(cfg)?;
|
||||
|
||||
let realized_profit = cfg.import_cumulative("realized_profit", v0)?;
|
||||
let realized_profit_ema_1w = cfg.import_computed("realized_profit_ema_1w", v0)?;
|
||||
let realized_loss = cfg.import_cumulative("realized_loss", v0)?;
|
||||
let realized_loss_ema_1w = cfg.import_computed("realized_loss_ema_1w", v0)?;
|
||||
|
||||
let neg_realized_loss = LazyFromHeight::from_height_source::<NegCentsUnsignedToDollars>(
|
||||
&cfg.name("neg_realized_loss"),
|
||||
cfg.version + Version::ONE,
|
||||
realized_loss.height.read_only_boxed_clone(),
|
||||
cfg.indexes,
|
||||
);
|
||||
|
||||
let net_realized_pnl = cfg.import_cumulative("net_realized_pnl", v0)?;
|
||||
let net_realized_pnl_ema_1w = cfg.import_computed("net_realized_pnl_ema_1w", v0)?;
|
||||
let peak_regret = cfg.import_cumulative("realized_peak_regret", Version::new(2))?;
|
||||
let gross_pnl = cfg.import_fiat("realized_gross_pnl", v0)?;
|
||||
|
||||
let realized_profit_rel_to_realized_cap =
|
||||
cfg.import_percent_bp32("realized_profit_rel_to_realized_cap", Version::new(2))?;
|
||||
let realized_loss_rel_to_realized_cap =
|
||||
cfg.import_percent_bp32("realized_loss_rel_to_realized_cap", Version::new(2))?;
|
||||
let net_realized_pnl_rel_to_realized_cap =
|
||||
cfg.import_percent_bps32("net_realized_pnl_rel_to_realized_cap", Version::new(2))?;
|
||||
|
||||
let realized_price = cfg.import_price("realized_price", v1)?;
|
||||
let investor_price = cfg.import_price("investor_price", v0)?;
|
||||
let investor_price_ratio = cfg.import_ratio("investor_price", v0)?;
|
||||
let lower_price_band = cfg.import_price("lower_price_band", v0)?;
|
||||
@@ -137,132 +64,41 @@ impl RealizedBase {
|
||||
let cap_raw = cfg.import_bytes("cap_raw", v0)?;
|
||||
let investor_cap_raw = cfg.import_bytes("investor_cap_raw", v0)?;
|
||||
|
||||
let profit_value_created = cfg.import_computed("profit_value_created", v0)?;
|
||||
let profit_value_destroyed = cfg.import_computed("profit_value_destroyed", v0)?;
|
||||
let loss_value_created = cfg.import_computed("loss_value_created", v0)?;
|
||||
let loss_value_destroyed = cfg.import_computed("loss_value_destroyed", v0)?;
|
||||
let value_created = cfg.import_computed("value_created", v0)?;
|
||||
let value_destroyed = cfg.import_computed("value_destroyed", v0)?;
|
||||
|
||||
let capitulation_flow = LazyFromHeight::from_computed::<CentsUnsignedToDollars>(
|
||||
&cfg.name("capitulation_flow"),
|
||||
cfg.version,
|
||||
loss_value_destroyed.height.read_only_boxed_clone(),
|
||||
&loss_value_destroyed,
|
||||
);
|
||||
let profit_flow = LazyFromHeight::from_computed::<CentsUnsignedToDollars>(
|
||||
&cfg.name("profit_flow"),
|
||||
cfg.version,
|
||||
profit_value_destroyed.height.read_only_boxed_clone(),
|
||||
&profit_value_destroyed,
|
||||
);
|
||||
|
||||
let realized_price_ratio = cfg.import_ratio("realized_price", v1)?;
|
||||
let mvrv = LazyFromHeight::from_lazy::<Identity<StoredF32>, BasisPoints32>(
|
||||
&cfg.name("mvrv"),
|
||||
cfg.version,
|
||||
&realized_price_ratio.ratio,
|
||||
);
|
||||
|
||||
// Rolling windows
|
||||
let value_created_sum = cfg.import_rolling("value_created", v1)?;
|
||||
let value_destroyed_sum = cfg.import_rolling("value_destroyed", v1)?;
|
||||
let gross_pnl_sum = cfg.import_rolling("gross_pnl_sum", v1)?;
|
||||
let sopr = cfg.import_rolling("sopr", v1)?;
|
||||
let sell_side_risk_ratio = cfg.import_percent_rolling_bp32("sell_side_risk_ratio", Version::new(2))?;
|
||||
|
||||
// EMAs
|
||||
let sopr_24h_ema = cfg.import_emas_1w_1m("sopr_24h", v1)?;
|
||||
let sell_side_risk_ratio =
|
||||
cfg.import_percent_rolling_bp32("sell_side_risk_ratio", Version::new(2))?;
|
||||
let sell_side_risk_ratio_24h_ema =
|
||||
cfg.import_percent_emas_1w_1m_bp32("sell_side_risk_ratio_24h", Version::new(2))?;
|
||||
|
||||
let peak_regret = cfg.import_cumulative("realized_peak_regret", Version::new(2))?;
|
||||
let peak_regret_rel_to_realized_cap =
|
||||
cfg.import_percent_bp32("realized_peak_regret_rel_to_realized_cap", Version::new(2))?;
|
||||
|
||||
Ok(Self {
|
||||
realized_cap_cents,
|
||||
realized_cap,
|
||||
realized_price,
|
||||
realized_price_ratio,
|
||||
realized_cap_change_1m: cfg.import_computed("realized_cap_change_1m", v0)?,
|
||||
complete,
|
||||
investor_price,
|
||||
investor_price_ratio,
|
||||
lower_price_band,
|
||||
upper_price_band,
|
||||
cap_raw,
|
||||
investor_cap_raw,
|
||||
mvrv,
|
||||
realized_profit,
|
||||
realized_profit_ema_1w,
|
||||
realized_loss,
|
||||
realized_loss_ema_1w,
|
||||
neg_realized_loss,
|
||||
net_realized_pnl,
|
||||
net_realized_pnl_ema_1w,
|
||||
gross_pnl,
|
||||
realized_profit_rel_to_realized_cap,
|
||||
realized_loss_rel_to_realized_cap,
|
||||
net_realized_pnl_rel_to_realized_cap,
|
||||
profit_value_created,
|
||||
profit_value_destroyed,
|
||||
loss_value_created,
|
||||
loss_value_destroyed,
|
||||
value_created,
|
||||
value_destroyed,
|
||||
capitulation_flow,
|
||||
profit_flow,
|
||||
value_created_sum,
|
||||
value_destroyed_sum,
|
||||
sopr,
|
||||
sopr_24h_ema,
|
||||
gross_pnl_sum,
|
||||
sell_side_risk_ratio,
|
||||
sell_side_risk_ratio_24h_ema,
|
||||
net_pnl_change_1m: cfg.import_computed("net_pnl_change_1m", Version::new(3))?,
|
||||
net_pnl_change_1m_rel_to_realized_cap: cfg
|
||||
.import_percent_bps32("net_pnl_change_1m_rel_to_realized_cap", Version::new(4))?,
|
||||
net_pnl_change_1m_rel_to_market_cap: cfg
|
||||
.import_percent_bps32("net_pnl_change_1m_rel_to_market_cap", Version::new(4))?,
|
||||
peak_regret,
|
||||
peak_regret_rel_to_realized_cap,
|
||||
sent_in_profit: cfg.import_value_cumulative("sent_in_profit", v0)?,
|
||||
sent_in_profit_ema: cfg.import_emas_2w("sent_in_profit", v0)?,
|
||||
sent_in_loss: cfg.import_value_cumulative("sent_in_loss", v0)?,
|
||||
sent_in_loss_ema: cfg.import_emas_2w("sent_in_loss", v0)?,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn min_stateful_height_len(&self) -> usize {
|
||||
[
|
||||
self.realized_cap.height.len(),
|
||||
self.realized_profit.height.len(),
|
||||
self.realized_loss.height.len(),
|
||||
self.investor_price.cents.height.len(),
|
||||
self.cap_raw.len(),
|
||||
self.investor_cap_raw.len(),
|
||||
self.profit_value_created.height.len(),
|
||||
self.profit_value_destroyed.height.len(),
|
||||
self.loss_value_created.height.len(),
|
||||
self.loss_value_destroyed.height.len(),
|
||||
self.peak_regret.height.len(),
|
||||
self.sent_in_profit.base.sats.height.len(),
|
||||
self.sent_in_loss.base.sats.height.len(),
|
||||
]
|
||||
.into_iter()
|
||||
.min()
|
||||
.unwrap()
|
||||
self.complete
|
||||
.min_stateful_height_len()
|
||||
.min(self.investor_price.cents.height.len())
|
||||
.min(self.cap_raw.len())
|
||||
.min(self.investor_cap_raw.len())
|
||||
.min(self.peak_regret.height.len())
|
||||
}
|
||||
|
||||
pub(crate) fn truncate_push(&mut self, height: Height, state: &RealizedState) -> Result<()> {
|
||||
self.realized_cap_cents
|
||||
.height
|
||||
.truncate_push(height, state.cap())?;
|
||||
self.realized_profit
|
||||
.height
|
||||
.truncate_push(height, state.profit())?;
|
||||
self.realized_loss
|
||||
.height
|
||||
.truncate_push(height, state.loss())?;
|
||||
self.complete.truncate_push(height, state)?;
|
||||
self.investor_price
|
||||
.cents
|
||||
.height
|
||||
@@ -270,51 +106,20 @@ impl RealizedBase {
|
||||
self.cap_raw.truncate_push(height, state.cap_raw())?;
|
||||
self.investor_cap_raw
|
||||
.truncate_push(height, state.investor_cap_raw())?;
|
||||
self.profit_value_created
|
||||
.height
|
||||
.truncate_push(height, state.profit_value_created())?;
|
||||
self.profit_value_destroyed
|
||||
.height
|
||||
.truncate_push(height, state.profit_value_destroyed())?;
|
||||
self.loss_value_created
|
||||
.height
|
||||
.truncate_push(height, state.loss_value_created())?;
|
||||
self.loss_value_destroyed
|
||||
.height
|
||||
.truncate_push(height, state.loss_value_destroyed())?;
|
||||
self.peak_regret
|
||||
.height
|
||||
.truncate_push(height, state.peak_regret())?;
|
||||
self.sent_in_profit
|
||||
.base
|
||||
.sats
|
||||
.height
|
||||
.truncate_push(height, state.sent_in_profit())?;
|
||||
self.sent_in_loss
|
||||
.base
|
||||
.sats
|
||||
.height
|
||||
.truncate_push(height, state.sent_in_loss())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn collect_vecs_mut(&mut self) -> Vec<&mut dyn AnyStoredVec> {
|
||||
vec![
|
||||
&mut self.realized_cap_cents.height as &mut dyn AnyStoredVec,
|
||||
&mut self.realized_profit.height,
|
||||
&mut self.realized_loss.height,
|
||||
&mut self.investor_price.cents.height,
|
||||
&mut self.cap_raw as &mut dyn AnyStoredVec,
|
||||
&mut self.investor_cap_raw as &mut dyn AnyStoredVec,
|
||||
&mut self.profit_value_created.height,
|
||||
&mut self.profit_value_destroyed.height,
|
||||
&mut self.loss_value_created.height,
|
||||
&mut self.loss_value_destroyed.height,
|
||||
&mut self.peak_regret.height,
|
||||
&mut self.sent_in_profit.base.sats.height,
|
||||
&mut self.sent_in_loss.base.sats.height,
|
||||
]
|
||||
let mut vecs = self.complete.collect_vecs_mut();
|
||||
vecs.push(&mut self.investor_price.cents.height as &mut dyn AnyStoredVec);
|
||||
vecs.push(&mut self.cap_raw as &mut dyn AnyStoredVec);
|
||||
vecs.push(&mut self.investor_cap_raw as &mut dyn AnyStoredVec);
|
||||
vecs.push(&mut self.peak_regret.height as &mut dyn AnyStoredVec);
|
||||
vecs
|
||||
}
|
||||
|
||||
pub(crate) fn compute_from_stateful(
|
||||
@@ -323,19 +128,11 @@ impl RealizedBase {
|
||||
others: &[&Self],
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
macro_rules! sum_others {
|
||||
($($field:tt).+) => {
|
||||
self.$($field).+.compute_sum_of_others(
|
||||
starting_indexes.height,
|
||||
&others.iter().map(|v| &v.$($field).+).collect::<Vec<_>>(),
|
||||
exit,
|
||||
)?
|
||||
};
|
||||
}
|
||||
|
||||
sum_others!(realized_cap_cents.height);
|
||||
sum_others!(realized_profit.height);
|
||||
sum_others!(realized_loss.height);
|
||||
// Delegate Complete-tier aggregation
|
||||
let complete_refs: Vec<&RealizedComplete> =
|
||||
others.iter().map(|o| &o.complete).collect();
|
||||
self.complete
|
||||
.compute_from_stateful(starting_indexes, &complete_refs, exit)?;
|
||||
|
||||
// Aggregate raw values for investor_price computation
|
||||
let investor_price_dep_version = others
|
||||
@@ -396,13 +193,15 @@ impl RealizedBase {
|
||||
self.investor_price.cents.height.write()?;
|
||||
}
|
||||
|
||||
sum_others!(profit_value_created.height);
|
||||
sum_others!(profit_value_destroyed.height);
|
||||
sum_others!(loss_value_created.height);
|
||||
sum_others!(loss_value_destroyed.height);
|
||||
sum_others!(peak_regret.height);
|
||||
sum_others!(sent_in_profit.base.sats.height);
|
||||
sum_others!(sent_in_loss.base.sats.height);
|
||||
// Source-only: peak_regret
|
||||
self.peak_regret.height.compute_sum_of_others(
|
||||
starting_indexes.height,
|
||||
&others
|
||||
.iter()
|
||||
.map(|v| &v.peak_regret.height)
|
||||
.collect::<Vec<_>>(),
|
||||
exit,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -412,34 +211,7 @@ impl RealizedBase {
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.realized_profit
|
||||
.compute_rest(starting_indexes.height, exit)?;
|
||||
self.realized_loss
|
||||
.compute_rest(starting_indexes.height, exit)?;
|
||||
|
||||
self.net_realized_pnl
|
||||
.compute(starting_indexes.height, exit, |vec| {
|
||||
vec.compute_transform2(
|
||||
starting_indexes.height,
|
||||
&self.realized_profit.height,
|
||||
&self.realized_loss.height,
|
||||
|(i, profit, loss, ..)| {
|
||||
(
|
||||
i,
|
||||
CentsSigned::new(profit.inner() as i64 - loss.inner() as i64),
|
||||
)
|
||||
},
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
self.gross_pnl.cents.height.compute_add(
|
||||
starting_indexes.height,
|
||||
&self.realized_profit.height,
|
||||
&self.realized_loss.height,
|
||||
exit,
|
||||
)?;
|
||||
self.complete.compute_rest_part1(starting_indexes, exit)?;
|
||||
|
||||
self.peak_regret
|
||||
.compute_rest(starting_indexes.height, exit)?;
|
||||
@@ -457,29 +229,17 @@ impl RealizedBase {
|
||||
height_to_market_cap: &impl ReadableVec<Height, Dollars>,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.realized_price.cents.height.compute_transform2(
|
||||
starting_indexes.height,
|
||||
&self.realized_cap_cents.height,
|
||||
height_to_supply,
|
||||
|(i, cap_cents, supply, ..)| {
|
||||
let cap = cap_cents.as_u128();
|
||||
let supply_sats = Sats::from(supply).as_u128();
|
||||
if supply_sats == 0 {
|
||||
(i, Cents::ZERO)
|
||||
} else {
|
||||
(i, Cents::from(cap * Sats::ONE_BTC_U128 / supply_sats))
|
||||
}
|
||||
},
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.realized_price_ratio.compute_ratio(
|
||||
// Compute all Complete-tier fields
|
||||
self.complete.compute_rest_part2(
|
||||
blocks,
|
||||
prices,
|
||||
starting_indexes,
|
||||
&prices.price.cents.height,
|
||||
&self.realized_price.cents.height,
|
||||
height_to_supply,
|
||||
height_to_market_cap,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Extended-only: investor_price ratio and price bands
|
||||
self.investor_price_ratio.compute_ratio(
|
||||
starting_indexes,
|
||||
&prices.price.cents.height,
|
||||
@@ -489,7 +249,7 @@ impl RealizedBase {
|
||||
|
||||
self.lower_price_band.cents.height.compute_transform2(
|
||||
starting_indexes.height,
|
||||
&self.realized_price.cents.height,
|
||||
&self.complete.realized_price.cents.height,
|
||||
&self.investor_price.cents.height,
|
||||
|(i, rp, ip, ..)| {
|
||||
let rp = rp.as_u128();
|
||||
@@ -506,7 +266,7 @@ impl RealizedBase {
|
||||
self.upper_price_band.cents.height.compute_transform2(
|
||||
starting_indexes.height,
|
||||
&self.investor_price.cents.height,
|
||||
&self.realized_price.cents.height,
|
||||
&self.complete.realized_price.cents.height,
|
||||
|(i, ip, rp, ..)| {
|
||||
let ip = ip.as_u128();
|
||||
let rp = rp.as_u128();
|
||||
@@ -519,126 +279,22 @@ impl RealizedBase {
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.realized_cap_change_1m.height.compute_rolling_change(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_1m_ago,
|
||||
&self.realized_cap_cents.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Compute value_created/destroyed from stored components
|
||||
self.value_created
|
||||
.compute_binary::<Cents, Cents, CentsPlus>(
|
||||
starting_indexes.height,
|
||||
&self.profit_value_created.height,
|
||||
&self.loss_value_created.height,
|
||||
exit,
|
||||
)?;
|
||||
self.value_destroyed
|
||||
.compute_binary::<Cents, Cents, CentsPlus>(
|
||||
starting_indexes.height,
|
||||
&self.profit_value_destroyed.height,
|
||||
&self.loss_value_destroyed.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
let window_starts = blocks.count.window_starts();
|
||||
self.value_created_sum.compute_rolling_sum(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
&self.value_created.height,
|
||||
exit,
|
||||
)?;
|
||||
self.value_destroyed_sum.compute_rolling_sum(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
&self.value_destroyed.height,
|
||||
exit,
|
||||
)?;
|
||||
self.gross_pnl_sum.compute_rolling_sum(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
&self.gross_pnl.cents.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Compute SOPR from rolling sums
|
||||
for ((sopr, vc), vd) in self
|
||||
.sopr
|
||||
.as_mut_array()
|
||||
.into_iter()
|
||||
.zip(self.value_created_sum.as_array())
|
||||
.zip(self.value_destroyed_sum.as_array())
|
||||
{
|
||||
sopr.compute_binary::<Cents, Cents, RatioCents64>(
|
||||
starting_indexes.height,
|
||||
&vc.height,
|
||||
&vd.height,
|
||||
exit,
|
||||
)?;
|
||||
}
|
||||
|
||||
// Compute sell-side risk ratios
|
||||
// Extended-only: sell-side risk ratios
|
||||
for (ssrr, rv) in self
|
||||
.sell_side_risk_ratio
|
||||
.as_mut_array()
|
||||
.into_iter()
|
||||
.zip(self.gross_pnl_sum.as_array())
|
||||
.zip(self.complete.gross_pnl_sum.as_array())
|
||||
{
|
||||
ssrr.compute_binary::<Cents, Cents, RatioCentsBp32>(
|
||||
starting_indexes.height,
|
||||
&rv.height,
|
||||
&self.realized_cap_cents.height,
|
||||
&self.complete.realized_cap_cents.height,
|
||||
exit,
|
||||
)?;
|
||||
}
|
||||
|
||||
// 7d EMAs
|
||||
self.realized_profit_ema_1w.height.compute_rolling_ema(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_1w_ago,
|
||||
&self.realized_profit.height,
|
||||
exit,
|
||||
)?;
|
||||
self.realized_loss_ema_1w.height.compute_rolling_ema(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_1w_ago,
|
||||
&self.realized_loss.height,
|
||||
exit,
|
||||
)?;
|
||||
self.net_realized_pnl_ema_1w.height.compute_rolling_ema(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_1w_ago,
|
||||
&self.net_realized_pnl.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// 14-day EMA of sent in profit/loss
|
||||
self.sent_in_profit_ema.compute(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_2w_ago,
|
||||
&self.sent_in_profit.base.sats.height,
|
||||
&self.sent_in_profit.base.cents.height,
|
||||
exit,
|
||||
)?;
|
||||
self.sent_in_loss_ema.compute(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_2w_ago,
|
||||
&self.sent_in_loss.base.sats.height,
|
||||
&self.sent_in_loss.base.cents.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// SOPR EMAs (based on 24h window)
|
||||
self.sopr_24h_ema.compute_from_24h(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_1w_ago,
|
||||
&blocks.count.height_1m_ago,
|
||||
&self.sopr._24h.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Sell side risk EMAs (based on 24h window)
|
||||
// Extended-only: sell side risk EMAs
|
||||
self.sell_side_risk_ratio_24h_ema.compute_from_24h(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_1w_ago,
|
||||
@@ -647,57 +303,12 @@ impl RealizedBase {
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Realized profit/loss/net relative to realized cap
|
||||
self.realized_profit_rel_to_realized_cap
|
||||
.compute_binary::<Cents, Cents, RatioCentsBp32>(
|
||||
starting_indexes.height,
|
||||
&self.realized_profit.height,
|
||||
&self.realized_cap_cents.height,
|
||||
exit,
|
||||
)?;
|
||||
self.realized_loss_rel_to_realized_cap
|
||||
.compute_binary::<Cents, Cents, RatioCentsBp32>(
|
||||
starting_indexes.height,
|
||||
&self.realized_loss.height,
|
||||
&self.realized_cap_cents.height,
|
||||
exit,
|
||||
)?;
|
||||
self.net_realized_pnl_rel_to_realized_cap
|
||||
.compute_binary::<CentsSigned, Cents, RatioCentsSignedCentsBps32>(
|
||||
starting_indexes.height,
|
||||
&self.net_realized_pnl.height,
|
||||
&self.realized_cap_cents.height,
|
||||
exit,
|
||||
)?;
|
||||
// Source-only: peak_regret relative to realized cap
|
||||
self.peak_regret_rel_to_realized_cap
|
||||
.compute_binary::<Cents, Cents, RatioCentsBp32>(
|
||||
starting_indexes.height,
|
||||
&self.peak_regret.height,
|
||||
&self.realized_cap_cents.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Net realized PnL cumulative 30d delta
|
||||
self.net_pnl_change_1m.height.compute_rolling_change(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_1m_ago,
|
||||
&self.net_realized_pnl.cumulative.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.net_pnl_change_1m_rel_to_realized_cap
|
||||
.compute_binary::<CentsSigned, Cents, RatioCentsSignedCentsBps32>(
|
||||
starting_indexes.height,
|
||||
&self.net_pnl_change_1m.height,
|
||||
&self.realized_cap_cents.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.net_pnl_change_1m_rel_to_market_cap
|
||||
.compute_binary::<CentsSigned, Dollars, RatioCentsSignedDollarsBps32>(
|
||||
starting_indexes.height,
|
||||
&self.net_pnl_change_1m.height,
|
||||
height_to_market_cap,
|
||||
&self.complete.realized_cap_cents.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
|
||||
@@ -0,0 +1,328 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{
|
||||
BasisPointsSigned32, Bitcoin, Cents, CentsSigned, Dollars, Height, Indexes, StoredF64, Version,
|
||||
};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use vecdb::{
|
||||
AnyStoredVec, AnyVec, Exit, ReadableCloneableVec, ReadableVec, Rw, StorageMode, WritableVec,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
blocks,
|
||||
distribution::state::RealizedState,
|
||||
internal::{
|
||||
CentsPlus, CentsUnsignedToDollars, ComputedFromHeight, LazyFromHeight, PercentFromHeight,
|
||||
RatioCents64, RatioCentsSignedCentsBps32, RatioCentsSignedDollarsBps32, RollingEmas1w1m,
|
||||
RollingEmas2w, RollingWindows, ValueFromHeightCumulative,
|
||||
},
|
||||
prices,
|
||||
};
|
||||
|
||||
use crate::distribution::metrics::ImportConfig;
|
||||
|
||||
use super::CoreRealized;
|
||||
|
||||
#[derive(Deref, DerefMut, Traversable)]
|
||||
pub struct RealizedComplete<M: StorageMode = Rw> {
|
||||
#[deref]
|
||||
#[deref_mut]
|
||||
#[traversable(flatten)]
|
||||
pub core: CoreRealized<M>,
|
||||
|
||||
pub profit_value_created: ComputedFromHeight<Cents, M>,
|
||||
pub profit_value_destroyed: ComputedFromHeight<Cents, M>,
|
||||
pub loss_value_created: ComputedFromHeight<Cents, M>,
|
||||
pub loss_value_destroyed: ComputedFromHeight<Cents, M>,
|
||||
|
||||
pub value_created: ComputedFromHeight<Cents, M>,
|
||||
pub value_destroyed: ComputedFromHeight<Cents, M>,
|
||||
|
||||
pub capitulation_flow: LazyFromHeight<Dollars, Cents>,
|
||||
pub profit_flow: LazyFromHeight<Dollars, Cents>,
|
||||
|
||||
pub value_created_sum: RollingWindows<Cents, M>,
|
||||
pub value_destroyed_sum: RollingWindows<Cents, M>,
|
||||
|
||||
pub gross_pnl_sum: RollingWindows<Cents, M>,
|
||||
|
||||
pub net_pnl_change_1m: ComputedFromHeight<CentsSigned, M>,
|
||||
pub net_pnl_change_1m_rel_to_realized_cap: PercentFromHeight<BasisPointsSigned32, M>,
|
||||
pub net_pnl_change_1m_rel_to_market_cap: PercentFromHeight<BasisPointsSigned32, M>,
|
||||
|
||||
pub sopr: RollingWindows<StoredF64, M>,
|
||||
pub sopr_24h_ema: RollingEmas1w1m<StoredF64, M>,
|
||||
|
||||
pub sent_in_profit: ValueFromHeightCumulative<M>,
|
||||
pub sent_in_profit_ema: RollingEmas2w<M>,
|
||||
pub sent_in_loss: ValueFromHeightCumulative<M>,
|
||||
pub sent_in_loss_ema: RollingEmas2w<M>,
|
||||
}
|
||||
|
||||
impl RealizedComplete {
|
||||
pub(crate) fn forced_import(cfg: &ImportConfig) -> Result<Self> {
|
||||
let v0 = Version::ZERO;
|
||||
|
||||
let core = CoreRealized::forced_import(cfg)?;
|
||||
|
||||
let profit_value_created = cfg.import_computed("profit_value_created", v0)?;
|
||||
let profit_value_destroyed = cfg.import_computed("profit_value_destroyed", v0)?;
|
||||
let loss_value_created = cfg.import_computed("loss_value_created", v0)?;
|
||||
let loss_value_destroyed = cfg.import_computed("loss_value_destroyed", v0)?;
|
||||
let value_created = cfg.import_computed("value_created", v0)?;
|
||||
let value_destroyed = cfg.import_computed("value_destroyed", v0)?;
|
||||
|
||||
let capitulation_flow = LazyFromHeight::from_computed::<CentsUnsignedToDollars>(
|
||||
&cfg.name("capitulation_flow"),
|
||||
cfg.version,
|
||||
loss_value_destroyed.height.read_only_boxed_clone(),
|
||||
&loss_value_destroyed,
|
||||
);
|
||||
let profit_flow = LazyFromHeight::from_computed::<CentsUnsignedToDollars>(
|
||||
&cfg.name("profit_flow"),
|
||||
cfg.version,
|
||||
profit_value_destroyed.height.read_only_boxed_clone(),
|
||||
&profit_value_destroyed,
|
||||
);
|
||||
|
||||
let value_created_sum = cfg.import_rolling("value_created", Version::ONE)?;
|
||||
let value_destroyed_sum = cfg.import_rolling("value_destroyed", Version::ONE)?;
|
||||
let gross_pnl_sum = cfg.import_rolling("gross_pnl_sum", Version::ONE)?;
|
||||
|
||||
let sopr = cfg.import_rolling("sopr", Version::ONE)?;
|
||||
let sopr_24h_ema = cfg.import_emas_1w_1m("sopr_24h", Version::ONE)?;
|
||||
|
||||
Ok(Self {
|
||||
core,
|
||||
profit_value_created,
|
||||
profit_value_destroyed,
|
||||
loss_value_created,
|
||||
loss_value_destroyed,
|
||||
value_created,
|
||||
value_destroyed,
|
||||
capitulation_flow,
|
||||
profit_flow,
|
||||
value_created_sum,
|
||||
value_destroyed_sum,
|
||||
gross_pnl_sum,
|
||||
sopr,
|
||||
sopr_24h_ema,
|
||||
net_pnl_change_1m: cfg.import_computed("net_pnl_change_1m", Version::new(3))?,
|
||||
net_pnl_change_1m_rel_to_realized_cap: cfg
|
||||
.import_percent_bps32("net_pnl_change_1m_rel_to_realized_cap", Version::new(4))?,
|
||||
net_pnl_change_1m_rel_to_market_cap: cfg
|
||||
.import_percent_bps32("net_pnl_change_1m_rel_to_market_cap", Version::new(4))?,
|
||||
sent_in_profit: cfg.import_value_cumulative("sent_in_profit", v0)?,
|
||||
sent_in_profit_ema: cfg.import_emas_2w("sent_in_profit", v0)?,
|
||||
sent_in_loss: cfg.import_value_cumulative("sent_in_loss", v0)?,
|
||||
sent_in_loss_ema: cfg.import_emas_2w("sent_in_loss", v0)?,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn min_stateful_height_len(&self) -> usize {
|
||||
self.core
|
||||
.min_stateful_height_len()
|
||||
.min(self.profit_value_created.height.len())
|
||||
.min(self.profit_value_destroyed.height.len())
|
||||
.min(self.loss_value_created.height.len())
|
||||
.min(self.loss_value_destroyed.height.len())
|
||||
.min(self.sent_in_profit.base.sats.height.len())
|
||||
.min(self.sent_in_loss.base.sats.height.len())
|
||||
}
|
||||
|
||||
pub(crate) fn truncate_push(&mut self, height: Height, state: &RealizedState) -> Result<()> {
|
||||
self.core.truncate_push(height, state)?;
|
||||
self.profit_value_created
|
||||
.height
|
||||
.truncate_push(height, state.profit_value_created())?;
|
||||
self.profit_value_destroyed
|
||||
.height
|
||||
.truncate_push(height, state.profit_value_destroyed())?;
|
||||
self.loss_value_created
|
||||
.height
|
||||
.truncate_push(height, state.loss_value_created())?;
|
||||
self.loss_value_destroyed
|
||||
.height
|
||||
.truncate_push(height, state.loss_value_destroyed())?;
|
||||
self.sent_in_profit
|
||||
.base
|
||||
.sats
|
||||
.height
|
||||
.truncate_push(height, state.sent_in_profit())?;
|
||||
self.sent_in_loss
|
||||
.base
|
||||
.sats
|
||||
.height
|
||||
.truncate_push(height, state.sent_in_loss())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn collect_vecs_mut(&mut self) -> Vec<&mut dyn AnyStoredVec> {
|
||||
let mut vecs = self.core.collect_vecs_mut();
|
||||
vecs.push(&mut self.profit_value_created.height as &mut dyn AnyStoredVec);
|
||||
vecs.push(&mut self.profit_value_destroyed.height);
|
||||
vecs.push(&mut self.loss_value_created.height);
|
||||
vecs.push(&mut self.loss_value_destroyed.height);
|
||||
vecs.push(&mut self.sent_in_profit.base.sats.height);
|
||||
vecs.push(&mut self.sent_in_loss.base.sats.height);
|
||||
vecs
|
||||
}
|
||||
|
||||
pub(crate) fn compute_from_stateful(
|
||||
&mut self,
|
||||
starting_indexes: &Indexes,
|
||||
others: &[&Self],
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
let core_refs: Vec<&CoreRealized> = others.iter().map(|o| &o.core).collect();
|
||||
self.core
|
||||
.compute_from_stateful(starting_indexes, &core_refs, exit)?;
|
||||
|
||||
macro_rules! sum_others {
|
||||
($($field:tt).+) => {
|
||||
self.$($field).+.compute_sum_of_others(
|
||||
starting_indexes.height,
|
||||
&others.iter().map(|v| &v.$($field).+).collect::<Vec<_>>(),
|
||||
exit,
|
||||
)?
|
||||
};
|
||||
}
|
||||
|
||||
sum_others!(profit_value_created.height);
|
||||
sum_others!(profit_value_destroyed.height);
|
||||
sum_others!(loss_value_created.height);
|
||||
sum_others!(loss_value_destroyed.height);
|
||||
sum_others!(sent_in_profit.base.sats.height);
|
||||
sum_others!(sent_in_loss.base.sats.height);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn compute_rest_part1(
|
||||
&mut self,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.core.compute_rest_part1(starting_indexes, exit)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(crate) fn compute_rest_part2(
|
||||
&mut self,
|
||||
blocks: &blocks::Vecs,
|
||||
prices: &prices::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
height_to_supply: &impl ReadableVec<Height, Bitcoin>,
|
||||
height_to_market_cap: &impl ReadableVec<Height, Dollars>,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.core.compute_rest_part2(
|
||||
blocks,
|
||||
prices,
|
||||
starting_indexes,
|
||||
height_to_supply,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.value_created
|
||||
.compute_binary::<Cents, Cents, CentsPlus>(
|
||||
starting_indexes.height,
|
||||
&self.profit_value_created.height,
|
||||
&self.loss_value_created.height,
|
||||
exit,
|
||||
)?;
|
||||
self.value_destroyed
|
||||
.compute_binary::<Cents, Cents, CentsPlus>(
|
||||
starting_indexes.height,
|
||||
&self.profit_value_destroyed.height,
|
||||
&self.loss_value_destroyed.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
let window_starts = blocks.count.window_starts();
|
||||
self.value_created_sum.compute_rolling_sum(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
&self.value_created.height,
|
||||
exit,
|
||||
)?;
|
||||
self.value_destroyed_sum.compute_rolling_sum(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
&self.value_destroyed.height,
|
||||
exit,
|
||||
)?;
|
||||
self.gross_pnl_sum.compute_rolling_sum(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
&self.core.gross_pnl.cents.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
for ((sopr, vc), vd) in self
|
||||
.sopr
|
||||
.as_mut_array()
|
||||
.into_iter()
|
||||
.zip(self.value_created_sum.as_array())
|
||||
.zip(self.value_destroyed_sum.as_array())
|
||||
{
|
||||
sopr.compute_binary::<Cents, Cents, RatioCents64>(
|
||||
starting_indexes.height,
|
||||
&vc.height,
|
||||
&vd.height,
|
||||
exit,
|
||||
)?;
|
||||
}
|
||||
|
||||
self.sopr_24h_ema.compute_from_24h(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_1w_ago,
|
||||
&blocks.count.height_1m_ago,
|
||||
&self.sopr._24h.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.sent_in_profit_ema.compute(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_2w_ago,
|
||||
&self.sent_in_profit.base.sats.height,
|
||||
&self.sent_in_profit.base.cents.height,
|
||||
exit,
|
||||
)?;
|
||||
self.sent_in_loss_ema.compute(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_2w_ago,
|
||||
&self.sent_in_loss.base.sats.height,
|
||||
&self.sent_in_loss.base.cents.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.net_pnl_change_1m.height.compute_rolling_change(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_1m_ago,
|
||||
&self.core.net_realized_pnl.cumulative.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.net_pnl_change_1m_rel_to_realized_cap
|
||||
.compute_binary::<CentsSigned, Cents, RatioCentsSignedCentsBps32>(
|
||||
starting_indexes.height,
|
||||
&self.net_pnl_change_1m.height,
|
||||
&self.core.realized_cap_cents.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.net_pnl_change_1m_rel_to_market_cap
|
||||
.compute_binary::<CentsSigned, Dollars, RatioCentsSignedDollarsBps32>(
|
||||
starting_indexes.height,
|
||||
&self.net_pnl_change_1m.height,
|
||||
height_to_market_cap,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
287
crates/brk_computer/src/distribution/metrics/realized/core.rs
Normal file
287
crates/brk_computer/src/distribution/metrics/realized/core.rs
Normal file
@@ -0,0 +1,287 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{
|
||||
BasisPoints32, BasisPointsSigned32, Bitcoin, Cents, CentsSigned, Dollars, Height, Indexes,
|
||||
Sats, StoredF32, Version,
|
||||
};
|
||||
use vecdb::{
|
||||
AnyStoredVec, AnyVec, Exit, ReadableCloneableVec, ReadableVec, Rw, StorageMode, WritableVec,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
blocks,
|
||||
distribution::state::RealizedState,
|
||||
internal::{
|
||||
CentsUnsignedToDollars, ComputedFromHeight, ComputedFromHeightCumulative,
|
||||
ComputedFromHeightRatio, FiatFromHeight, Identity, LazyFromHeight,
|
||||
NegCentsUnsignedToDollars, PercentFromHeight, Price, RatioCentsBp32,
|
||||
RatioCentsSignedCentsBps32,
|
||||
},
|
||||
prices,
|
||||
};
|
||||
|
||||
use crate::distribution::metrics::ImportConfig;
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct CoreRealized<M: StorageMode = Rw> {
|
||||
pub realized_cap_cents: ComputedFromHeight<Cents, M>,
|
||||
pub realized_profit: ComputedFromHeightCumulative<Cents, M>,
|
||||
pub realized_loss: ComputedFromHeightCumulative<Cents, M>,
|
||||
|
||||
pub realized_cap: LazyFromHeight<Dollars, Cents>,
|
||||
pub realized_price: Price<ComputedFromHeight<Cents, M>>,
|
||||
pub realized_price_ratio: ComputedFromHeightRatio<M>,
|
||||
pub realized_cap_change_1m: ComputedFromHeight<CentsSigned, M>,
|
||||
|
||||
pub mvrv: LazyFromHeight<StoredF32>,
|
||||
|
||||
pub neg_realized_loss: LazyFromHeight<Dollars, Cents>,
|
||||
pub net_realized_pnl: ComputedFromHeightCumulative<CentsSigned, M>,
|
||||
pub net_realized_pnl_ema_1w: ComputedFromHeight<CentsSigned, M>,
|
||||
pub gross_pnl: FiatFromHeight<Cents, M>,
|
||||
|
||||
pub realized_profit_ema_1w: ComputedFromHeight<Cents, M>,
|
||||
pub realized_loss_ema_1w: ComputedFromHeight<Cents, M>,
|
||||
|
||||
pub realized_profit_rel_to_realized_cap: PercentFromHeight<BasisPoints32, M>,
|
||||
pub realized_loss_rel_to_realized_cap: PercentFromHeight<BasisPoints32, M>,
|
||||
pub net_realized_pnl_rel_to_realized_cap: PercentFromHeight<BasisPointsSigned32, M>,
|
||||
}
|
||||
|
||||
impl CoreRealized {
|
||||
pub(crate) fn forced_import(cfg: &ImportConfig) -> Result<Self> {
|
||||
let v0 = Version::ZERO;
|
||||
let v1 = Version::ONE;
|
||||
|
||||
let realized_cap_cents = cfg.import_computed("realized_cap_cents", v0)?;
|
||||
let realized_cap = LazyFromHeight::from_computed::<CentsUnsignedToDollars>(
|
||||
&cfg.name("realized_cap"),
|
||||
cfg.version,
|
||||
realized_cap_cents.height.read_only_boxed_clone(),
|
||||
&realized_cap_cents,
|
||||
);
|
||||
|
||||
let realized_profit = cfg.import_cumulative("realized_profit", v0)?;
|
||||
let realized_profit_ema_1w = cfg.import_computed("realized_profit_ema_1w", v0)?;
|
||||
let realized_loss = cfg.import_cumulative("realized_loss", v0)?;
|
||||
let realized_loss_ema_1w = cfg.import_computed("realized_loss_ema_1w", v0)?;
|
||||
|
||||
let neg_realized_loss = LazyFromHeight::from_height_source::<NegCentsUnsignedToDollars>(
|
||||
&cfg.name("neg_realized_loss"),
|
||||
cfg.version + Version::ONE,
|
||||
realized_loss.height.read_only_boxed_clone(),
|
||||
cfg.indexes,
|
||||
);
|
||||
|
||||
let net_realized_pnl = cfg.import_cumulative("net_realized_pnl", v0)?;
|
||||
let net_realized_pnl_ema_1w = cfg.import_computed("net_realized_pnl_ema_1w", v0)?;
|
||||
let gross_pnl = cfg.import_fiat("realized_gross_pnl", v0)?;
|
||||
|
||||
let realized_profit_rel_to_realized_cap =
|
||||
cfg.import_percent_bp32("realized_profit_rel_to_realized_cap", Version::new(2))?;
|
||||
let realized_loss_rel_to_realized_cap =
|
||||
cfg.import_percent_bp32("realized_loss_rel_to_realized_cap", Version::new(2))?;
|
||||
let net_realized_pnl_rel_to_realized_cap =
|
||||
cfg.import_percent_bps32("net_realized_pnl_rel_to_realized_cap", Version::new(2))?;
|
||||
|
||||
let realized_price = cfg.import_price("realized_price", v1)?;
|
||||
|
||||
let realized_price_ratio = cfg.import_ratio("realized_price", v1)?;
|
||||
let mvrv = LazyFromHeight::from_lazy::<Identity<StoredF32>, BasisPoints32>(
|
||||
&cfg.name("mvrv"),
|
||||
cfg.version,
|
||||
&realized_price_ratio.ratio,
|
||||
);
|
||||
|
||||
Ok(Self {
|
||||
realized_cap_cents,
|
||||
realized_cap,
|
||||
realized_price,
|
||||
realized_price_ratio,
|
||||
realized_cap_change_1m: cfg.import_computed("realized_cap_change_1m", v0)?,
|
||||
mvrv,
|
||||
realized_profit,
|
||||
realized_profit_ema_1w,
|
||||
realized_loss,
|
||||
realized_loss_ema_1w,
|
||||
neg_realized_loss,
|
||||
net_realized_pnl,
|
||||
net_realized_pnl_ema_1w,
|
||||
gross_pnl,
|
||||
realized_profit_rel_to_realized_cap,
|
||||
realized_loss_rel_to_realized_cap,
|
||||
net_realized_pnl_rel_to_realized_cap,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn min_stateful_height_len(&self) -> usize {
|
||||
self.realized_cap_cents
|
||||
.height
|
||||
.len()
|
||||
.min(self.realized_profit.height.len())
|
||||
.min(self.realized_loss.height.len())
|
||||
}
|
||||
|
||||
pub(crate) fn truncate_push(&mut self, height: Height, state: &RealizedState) -> Result<()> {
|
||||
self.realized_cap_cents
|
||||
.height
|
||||
.truncate_push(height, state.cap())?;
|
||||
self.realized_profit
|
||||
.height
|
||||
.truncate_push(height, state.profit())?;
|
||||
self.realized_loss
|
||||
.height
|
||||
.truncate_push(height, state.loss())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn collect_vecs_mut(&mut self) -> Vec<&mut dyn AnyStoredVec> {
|
||||
vec![
|
||||
&mut self.realized_cap_cents.height as &mut dyn AnyStoredVec,
|
||||
&mut self.realized_profit.height,
|
||||
&mut self.realized_loss.height,
|
||||
]
|
||||
}
|
||||
|
||||
pub(crate) fn compute_from_stateful(
|
||||
&mut self,
|
||||
starting_indexes: &Indexes,
|
||||
others: &[&Self],
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
macro_rules! sum_others {
|
||||
($($field:tt).+) => {
|
||||
self.$($field).+.compute_sum_of_others(
|
||||
starting_indexes.height,
|
||||
&others.iter().map(|v| &v.$($field).+).collect::<Vec<_>>(),
|
||||
exit,
|
||||
)?
|
||||
};
|
||||
}
|
||||
|
||||
sum_others!(realized_cap_cents.height);
|
||||
sum_others!(realized_profit.height);
|
||||
sum_others!(realized_loss.height);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn compute_rest_part1(
|
||||
&mut self,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.realized_profit
|
||||
.compute_rest(starting_indexes.height, exit)?;
|
||||
self.realized_loss
|
||||
.compute_rest(starting_indexes.height, exit)?;
|
||||
|
||||
self.net_realized_pnl
|
||||
.compute(starting_indexes.height, exit, |vec| {
|
||||
vec.compute_transform2(
|
||||
starting_indexes.height,
|
||||
&self.realized_profit.height,
|
||||
&self.realized_loss.height,
|
||||
|(i, profit, loss, ..)| {
|
||||
(
|
||||
i,
|
||||
CentsSigned::new(profit.inner() as i64 - loss.inner() as i64),
|
||||
)
|
||||
},
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
self.gross_pnl.cents.height.compute_add(
|
||||
starting_indexes.height,
|
||||
&self.realized_profit.height,
|
||||
&self.realized_loss.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn compute_rest_part2(
|
||||
&mut self,
|
||||
blocks: &blocks::Vecs,
|
||||
prices: &prices::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
height_to_supply: &impl ReadableVec<Height, Bitcoin>,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.realized_price.cents.height.compute_transform2(
|
||||
starting_indexes.height,
|
||||
&self.realized_cap_cents.height,
|
||||
height_to_supply,
|
||||
|(i, cap_cents, supply, ..)| {
|
||||
let cap = cap_cents.as_u128();
|
||||
let supply_sats = Sats::from(supply).as_u128();
|
||||
if supply_sats == 0 {
|
||||
(i, Cents::ZERO)
|
||||
} else {
|
||||
(i, Cents::from(cap * Sats::ONE_BTC_U128 / supply_sats))
|
||||
}
|
||||
},
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.realized_price_ratio.compute_ratio(
|
||||
starting_indexes,
|
||||
&prices.price.cents.height,
|
||||
&self.realized_price.cents.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.realized_cap_change_1m.height.compute_rolling_change(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_1m_ago,
|
||||
&self.realized_cap_cents.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.realized_profit_ema_1w.height.compute_rolling_ema(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_1w_ago,
|
||||
&self.realized_profit.height,
|
||||
exit,
|
||||
)?;
|
||||
self.realized_loss_ema_1w.height.compute_rolling_ema(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_1w_ago,
|
||||
&self.realized_loss.height,
|
||||
exit,
|
||||
)?;
|
||||
self.net_realized_pnl_ema_1w.height.compute_rolling_ema(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_1w_ago,
|
||||
&self.net_realized_pnl.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.realized_profit_rel_to_realized_cap
|
||||
.compute_binary::<Cents, Cents, RatioCentsBp32>(
|
||||
starting_indexes.height,
|
||||
&self.realized_profit.height,
|
||||
&self.realized_cap_cents.height,
|
||||
exit,
|
||||
)?;
|
||||
self.realized_loss_rel_to_realized_cap
|
||||
.compute_binary::<Cents, Cents, RatioCentsBp32>(
|
||||
starting_indexes.height,
|
||||
&self.realized_loss.height,
|
||||
&self.realized_cap_cents.height,
|
||||
exit,
|
||||
)?;
|
||||
self.net_realized_pnl_rel_to_realized_cap
|
||||
.compute_binary::<CentsSigned, Cents, RatioCentsSignedCentsBps32>(
|
||||
starting_indexes.height,
|
||||
&self.net_realized_pnl.height,
|
||||
&self.realized_cap_cents.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
mod adjusted;
|
||||
mod base;
|
||||
mod complete;
|
||||
mod core;
|
||||
mod extended;
|
||||
|
||||
mod with_adjusted;
|
||||
@@ -8,6 +10,8 @@ mod with_extended_adjusted;
|
||||
|
||||
pub use adjusted::*;
|
||||
pub use base::*;
|
||||
pub use complete::*;
|
||||
pub use core::*;
|
||||
pub use extended::*;
|
||||
|
||||
pub use with_adjusted::*;
|
||||
|
||||
@@ -1,25 +1,28 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{BasisPoints16, BasisPointsSigned32, Dollars, Height, Sats, StoredF32, Version};
|
||||
use vecdb::{Exit, ReadableCloneableVec, ReadableVec, Rw, StorageMode};
|
||||
use brk_types::{BasisPoints16, BasisPointsSigned32, Dollars, Height, Sats, Version};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use vecdb::{Exit, ReadableVec, Rw, StorageMode};
|
||||
|
||||
use crate::internal::{
|
||||
Bps32ToFloat, LazyFromHeight, NegRatioDollarsBps32, PercentFromHeight, RatioDollarsBp16,
|
||||
RatioDollarsBps32, RatioSatsBp16,
|
||||
};
|
||||
use crate::internal::{NegRatioDollarsBps32, PercentFromHeight, RatioDollarsBp16};
|
||||
|
||||
use crate::distribution::metrics::{ImportConfig, RealizedBase, UnrealizedBase};
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct RelativeBase<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>,
|
||||
use super::RelativeComplete;
|
||||
|
||||
pub unrealized_profit_rel_to_market_cap: PercentFromHeight<BasisPoints16, M>,
|
||||
pub unrealized_loss_rel_to_market_cap: PercentFromHeight<BasisPoints16, M>,
|
||||
/// Full relative metrics (Source/Extended tier).
|
||||
///
|
||||
/// Contains all Complete-tier fields (via Deref to RelativeComplete) plus:
|
||||
/// - Source-only: neg_unrealized_loss_rel_to_market_cap, invested_capital_in_profit/loss_rel_to_realized_cap
|
||||
#[derive(Deref, DerefMut, Traversable)]
|
||||
pub struct RelativeBase<M: StorageMode = Rw> {
|
||||
#[deref]
|
||||
#[deref_mut]
|
||||
#[traversable(flatten)]
|
||||
pub complete: RelativeComplete<M>,
|
||||
|
||||
// --- Source-only fields ---
|
||||
pub neg_unrealized_loss_rel_to_market_cap: PercentFromHeight<BasisPointsSigned32, 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>,
|
||||
@@ -27,35 +30,12 @@ pub struct RelativeBase<M: StorageMode = Rw> {
|
||||
|
||||
impl RelativeBase {
|
||||
pub(crate) fn forced_import(cfg: &ImportConfig) -> Result<Self> {
|
||||
let v1 = Version::ONE;
|
||||
let v2 = Version::new(2);
|
||||
|
||||
let net_unrealized_pnl_rel_to_market_cap =
|
||||
cfg.import_percent_bps32("net_unrealized_pnl_rel_to_market_cap", Version::new(3))?;
|
||||
|
||||
let nupl = LazyFromHeight::from_computed::<Bps32ToFloat>(
|
||||
&cfg.name("nupl"),
|
||||
cfg.version + Version::new(3),
|
||||
net_unrealized_pnl_rel_to_market_cap
|
||||
.bps
|
||||
.height
|
||||
.read_only_boxed_clone(),
|
||||
&net_unrealized_pnl_rel_to_market_cap.bps,
|
||||
);
|
||||
let complete = RelativeComplete::forced_import(cfg)?;
|
||||
|
||||
Ok(Self {
|
||||
supply_in_profit_rel_to_own_supply: cfg
|
||||
.import_percent_bp16("supply_in_profit_rel_to_own_supply", v1)?,
|
||||
supply_in_loss_rel_to_own_supply: cfg
|
||||
.import_percent_bp16("supply_in_loss_rel_to_own_supply", v1)?,
|
||||
unrealized_profit_rel_to_market_cap: cfg
|
||||
.import_percent_bp16("unrealized_profit_rel_to_market_cap", v2)?,
|
||||
unrealized_loss_rel_to_market_cap: cfg
|
||||
.import_percent_bp16("unrealized_loss_rel_to_market_cap", v2)?,
|
||||
complete,
|
||||
neg_unrealized_loss_rel_to_market_cap: cfg
|
||||
.import_percent_bps32("neg_unrealized_loss_rel_to_market_cap", Version::new(3))?,
|
||||
net_unrealized_pnl_rel_to_market_cap,
|
||||
nupl,
|
||||
invested_capital_in_profit_rel_to_realized_cap: cfg.import_percent_bp16(
|
||||
"invested_capital_in_profit_rel_to_realized_cap",
|
||||
Version::ZERO,
|
||||
@@ -76,34 +56,16 @@ impl RelativeBase {
|
||||
market_cap: &impl ReadableVec<Height, Dollars>,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.supply_in_profit_rel_to_own_supply
|
||||
.compute_binary::<Sats, Sats, RatioSatsBp16>(
|
||||
max_from,
|
||||
&unrealized.supply_in_profit.sats.height,
|
||||
supply_total_sats,
|
||||
exit,
|
||||
)?;
|
||||
self.supply_in_loss_rel_to_own_supply
|
||||
.compute_binary::<Sats, Sats, RatioSatsBp16>(
|
||||
max_from,
|
||||
&unrealized.supply_in_loss.sats.height,
|
||||
supply_total_sats,
|
||||
exit,
|
||||
)?;
|
||||
self.unrealized_profit_rel_to_market_cap
|
||||
.compute_binary::<Dollars, Dollars, RatioDollarsBp16>(
|
||||
max_from,
|
||||
&unrealized.unrealized_profit.usd.height,
|
||||
market_cap,
|
||||
exit,
|
||||
)?;
|
||||
self.unrealized_loss_rel_to_market_cap
|
||||
.compute_binary::<Dollars, Dollars, RatioDollarsBp16>(
|
||||
max_from,
|
||||
&unrealized.unrealized_loss.usd.height,
|
||||
market_cap,
|
||||
exit,
|
||||
)?;
|
||||
// Compute Complete-tier fields
|
||||
self.complete.compute(
|
||||
max_from,
|
||||
&unrealized.complete,
|
||||
supply_total_sats,
|
||||
market_cap,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Source-only
|
||||
self.neg_unrealized_loss_rel_to_market_cap
|
||||
.compute_binary::<Dollars, Dollars, NegRatioDollarsBps32>(
|
||||
max_from,
|
||||
@@ -111,13 +73,6 @@ impl RelativeBase {
|
||||
market_cap,
|
||||
exit,
|
||||
)?;
|
||||
self.net_unrealized_pnl_rel_to_market_cap
|
||||
.compute_binary::<Dollars, Dollars, RatioDollarsBps32>(
|
||||
max_from,
|
||||
&unrealized.net_unrealized_pnl.usd.height,
|
||||
market_cap,
|
||||
exit,
|
||||
)?;
|
||||
self.invested_capital_in_profit_rel_to_realized_cap
|
||||
.compute_binary::<Dollars, Dollars, RatioDollarsBp16>(
|
||||
max_from,
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{BasisPoints16, BasisPointsSigned32, Dollars, Height, Sats, StoredF32, Version};
|
||||
use vecdb::{Exit, ReadableCloneableVec, ReadableVec, Rw, StorageMode};
|
||||
|
||||
use crate::internal::{
|
||||
Bps32ToFloat, LazyFromHeight, PercentFromHeight, RatioDollarsBp16, RatioDollarsBps32,
|
||||
RatioSatsBp16,
|
||||
};
|
||||
|
||||
use crate::distribution::metrics::{ImportConfig, UnrealizedComplete};
|
||||
|
||||
/// Relative metrics for the Complete tier (~6 fields).
|
||||
///
|
||||
/// Excludes source-only fields (invested_capital_in_profit/loss_rel_to_realized_cap,
|
||||
/// neg_unrealized_loss_rel_to_market_cap).
|
||||
#[derive(Traversable)]
|
||||
pub struct RelativeComplete<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>,
|
||||
|
||||
pub unrealized_profit_rel_to_market_cap: PercentFromHeight<BasisPoints16, M>,
|
||||
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>,
|
||||
}
|
||||
|
||||
impl RelativeComplete {
|
||||
pub(crate) fn forced_import(cfg: &ImportConfig) -> Result<Self> {
|
||||
let v1 = Version::ONE;
|
||||
let v2 = Version::new(2);
|
||||
|
||||
let net_unrealized_pnl_rel_to_market_cap =
|
||||
cfg.import_percent_bps32("net_unrealized_pnl_rel_to_market_cap", Version::new(3))?;
|
||||
|
||||
let nupl = LazyFromHeight::from_computed::<Bps32ToFloat>(
|
||||
&cfg.name("nupl"),
|
||||
cfg.version + Version::new(3),
|
||||
net_unrealized_pnl_rel_to_market_cap
|
||||
.bps
|
||||
.height
|
||||
.read_only_boxed_clone(),
|
||||
&net_unrealized_pnl_rel_to_market_cap.bps,
|
||||
);
|
||||
|
||||
Ok(Self {
|
||||
supply_in_profit_rel_to_own_supply: cfg
|
||||
.import_percent_bp16("supply_in_profit_rel_to_own_supply", v1)?,
|
||||
supply_in_loss_rel_to_own_supply: cfg
|
||||
.import_percent_bp16("supply_in_loss_rel_to_own_supply", v1)?,
|
||||
unrealized_profit_rel_to_market_cap: cfg
|
||||
.import_percent_bp16("unrealized_profit_rel_to_market_cap", v2)?,
|
||||
unrealized_loss_rel_to_market_cap: cfg
|
||||
.import_percent_bp16("unrealized_loss_rel_to_market_cap", v2)?,
|
||||
net_unrealized_pnl_rel_to_market_cap,
|
||||
nupl,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn compute(
|
||||
&mut self,
|
||||
max_from: Height,
|
||||
unrealized: &UnrealizedComplete,
|
||||
supply_total_sats: &impl ReadableVec<Height, Sats>,
|
||||
market_cap: &impl ReadableVec<Height, Dollars>,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.supply_in_profit_rel_to_own_supply
|
||||
.compute_binary::<Sats, Sats, RatioSatsBp16>(
|
||||
max_from,
|
||||
&unrealized.supply_in_profit.sats.height,
|
||||
supply_total_sats,
|
||||
exit,
|
||||
)?;
|
||||
self.supply_in_loss_rel_to_own_supply
|
||||
.compute_binary::<Sats, Sats, RatioSatsBp16>(
|
||||
max_from,
|
||||
&unrealized.supply_in_loss.sats.height,
|
||||
supply_total_sats,
|
||||
exit,
|
||||
)?;
|
||||
self.unrealized_profit_rel_to_market_cap
|
||||
.compute_binary::<Dollars, Dollars, RatioDollarsBp16>(
|
||||
max_from,
|
||||
&unrealized.unrealized_profit.usd.height,
|
||||
market_cap,
|
||||
exit,
|
||||
)?;
|
||||
self.unrealized_loss_rel_to_market_cap
|
||||
.compute_binary::<Dollars, Dollars, RatioDollarsBp16>(
|
||||
max_from,
|
||||
&unrealized.unrealized_loss.usd.height,
|
||||
market_cap,
|
||||
exit,
|
||||
)?;
|
||||
self.net_unrealized_pnl_rel_to_market_cap
|
||||
.compute_binary::<Dollars, Dollars, RatioDollarsBps32>(
|
||||
max_from,
|
||||
&unrealized.net_unrealized_pnl.usd.height,
|
||||
market_cap,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
mod base;
|
||||
mod complete;
|
||||
mod extended_own_market_cap;
|
||||
mod extended_own_pnl;
|
||||
mod for_all;
|
||||
@@ -6,8 +7,10 @@ mod for_all;
|
||||
mod to_all;
|
||||
mod with_extended;
|
||||
mod with_rel_to_all;
|
||||
mod with_rel_to_all_complete;
|
||||
|
||||
pub use base::*;
|
||||
pub use complete::*;
|
||||
pub use extended_own_market_cap::*;
|
||||
pub use extended_own_pnl::*;
|
||||
pub use for_all::*;
|
||||
@@ -15,3 +18,4 @@ pub use for_all::*;
|
||||
pub use to_all::*;
|
||||
pub use with_extended::*;
|
||||
pub use with_rel_to_all::*;
|
||||
pub use with_rel_to_all_complete::*;
|
||||
|
||||
@@ -5,7 +5,7 @@ use vecdb::{Exit, ReadableVec, Rw, StorageMode};
|
||||
|
||||
use crate::internal::{PercentFromHeight, RatioSatsBp16};
|
||||
|
||||
use crate::distribution::metrics::{ImportConfig, UnrealizedBase};
|
||||
use crate::distribution::metrics::ImportConfig;
|
||||
|
||||
/// Relative-to-all metrics (not present for the "all" cohort itself).
|
||||
#[derive(Traversable)]
|
||||
@@ -30,7 +30,8 @@ impl RelativeToAll {
|
||||
pub(crate) fn compute(
|
||||
&mut self,
|
||||
max_from: Height,
|
||||
unrealized: &UnrealizedBase,
|
||||
supply_in_profit_sats: &impl ReadableVec<Height, Sats>,
|
||||
supply_in_loss_sats: &impl ReadableVec<Height, Sats>,
|
||||
supply_total_sats: &impl ReadableVec<Height, Sats>,
|
||||
all_supply_sats: &impl ReadableVec<Height, Sats>,
|
||||
exit: &Exit,
|
||||
@@ -45,14 +46,14 @@ impl RelativeToAll {
|
||||
self.supply_in_profit_rel_to_circulating_supply
|
||||
.compute_binary::<Sats, Sats, RatioSatsBp16>(
|
||||
max_from,
|
||||
&unrealized.supply_in_profit.sats.height,
|
||||
supply_in_profit_sats,
|
||||
all_supply_sats,
|
||||
exit,
|
||||
)?;
|
||||
self.supply_in_loss_rel_to_circulating_supply
|
||||
.compute_binary::<Sats, Sats, RatioSatsBp16>(
|
||||
max_from,
|
||||
&unrealized.supply_in_loss.sats.height,
|
||||
supply_in_loss_sats,
|
||||
all_supply_sats,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
@@ -56,7 +56,8 @@ impl RelativeWithExtended {
|
||||
)?;
|
||||
self.rel_to_all.compute(
|
||||
max_from,
|
||||
unrealized,
|
||||
&unrealized.supply_in_profit.sats.height,
|
||||
&unrealized.supply_in_loss.sats.height,
|
||||
supply_total_sats,
|
||||
all_supply_sats,
|
||||
exit,
|
||||
|
||||
@@ -49,7 +49,8 @@ impl RelativeWithRelToAll {
|
||||
)?;
|
||||
self.rel_to_all.compute(
|
||||
max_from,
|
||||
unrealized,
|
||||
&unrealized.supply_in_profit.sats.height,
|
||||
&unrealized.supply_in_loss.sats.height,
|
||||
supply_total_sats,
|
||||
all_supply_sats,
|
||||
exit,
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Dollars, Height, Sats};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use vecdb::{Exit, ReadableVec, Rw, StorageMode};
|
||||
|
||||
use crate::distribution::metrics::{ImportConfig, UnrealizedComplete};
|
||||
|
||||
use super::{RelativeComplete, RelativeToAll};
|
||||
|
||||
/// Complete relative metrics with rel_to_all.
|
||||
/// Used by CompleteCohortMetrics (epoch, class, min_age, max_age).
|
||||
#[derive(Deref, DerefMut, Traversable)]
|
||||
pub struct RelativeCompleteWithRelToAll<M: StorageMode = Rw> {
|
||||
#[deref]
|
||||
#[deref_mut]
|
||||
#[traversable(flatten)]
|
||||
pub base: RelativeComplete<M>,
|
||||
#[traversable(flatten)]
|
||||
pub rel_to_all: RelativeToAll<M>,
|
||||
}
|
||||
|
||||
impl RelativeCompleteWithRelToAll {
|
||||
pub(crate) fn forced_import(cfg: &ImportConfig) -> Result<Self> {
|
||||
Ok(Self {
|
||||
base: RelativeComplete::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: &UnrealizedComplete,
|
||||
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,
|
||||
supply_total_sats,
|
||||
market_cap,
|
||||
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(())
|
||||
}
|
||||
}
|
||||
@@ -1,32 +1,32 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Cents, CentsSats, CentsSigned, CentsSquaredSats, Height, Indexes, Version};
|
||||
use vecdb::{
|
||||
AnyStoredVec, AnyVec, BytesVec, Exit, ReadableCloneableVec, ReadableVec, Rw, StorageMode,
|
||||
WritableVec,
|
||||
};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use vecdb::{AnyStoredVec, AnyVec, BytesVec, Exit, ReadableVec, Rw, StorageMode, WritableVec};
|
||||
|
||||
use crate::{
|
||||
distribution::state::UnrealizedState,
|
||||
internal::{
|
||||
CentsSubtractToCentsSigned, FiatFromHeight, LazyFromHeight, NegCentsUnsignedToDollars,
|
||||
ValueFromHeight,
|
||||
},
|
||||
internal::{CentsSubtractToCentsSigned, FiatFromHeight},
|
||||
prices,
|
||||
};
|
||||
|
||||
use brk_types::Dollars;
|
||||
|
||||
use crate::distribution::metrics::ImportConfig;
|
||||
|
||||
#[derive(Traversable)]
|
||||
use super::UnrealizedComplete;
|
||||
|
||||
/// Full unrealized metrics (Source/Extended tier).
|
||||
///
|
||||
/// Contains all Complete-tier fields (via Deref to UnrealizedComplete) plus:
|
||||
/// - Source-only: invested_capital, raw BytesVecs, unrealized_gross_pnl
|
||||
/// - Extended-only: pain_index, greed_index, net_sentiment
|
||||
#[derive(Deref, DerefMut, Traversable)]
|
||||
pub struct UnrealizedBase<M: StorageMode = Rw> {
|
||||
pub supply_in_profit: ValueFromHeight<M>,
|
||||
pub supply_in_loss: ValueFromHeight<M>,
|
||||
|
||||
pub unrealized_profit: FiatFromHeight<Cents, M>,
|
||||
pub unrealized_loss: FiatFromHeight<Cents, M>,
|
||||
#[deref]
|
||||
#[deref_mut]
|
||||
#[traversable(flatten)]
|
||||
pub complete: UnrealizedComplete<M>,
|
||||
|
||||
// --- Source-only fields ---
|
||||
pub invested_capital_in_profit: FiatFromHeight<Cents, M>,
|
||||
pub invested_capital_in_loss: FiatFromHeight<Cents, M>,
|
||||
|
||||
@@ -35,24 +35,19 @@ pub struct UnrealizedBase<M: StorageMode = Rw> {
|
||||
pub investor_cap_in_profit_raw: M::Stored<BytesVec<Height, CentsSquaredSats>>,
|
||||
pub investor_cap_in_loss_raw: M::Stored<BytesVec<Height, CentsSquaredSats>>,
|
||||
|
||||
pub gross_pnl: FiatFromHeight<Cents, M>,
|
||||
|
||||
// --- Extended-only fields ---
|
||||
pub pain_index: FiatFromHeight<Cents, M>,
|
||||
pub greed_index: FiatFromHeight<Cents, M>,
|
||||
pub net_sentiment: FiatFromHeight<CentsSigned, M>,
|
||||
|
||||
pub neg_unrealized_loss: LazyFromHeight<Dollars, Cents>,
|
||||
|
||||
pub net_unrealized_pnl: FiatFromHeight<CentsSigned, M>,
|
||||
pub gross_pnl: FiatFromHeight<Cents, M>,
|
||||
}
|
||||
|
||||
impl UnrealizedBase {
|
||||
pub(crate) fn forced_import(cfg: &ImportConfig) -> Result<Self> {
|
||||
let v0 = Version::ZERO;
|
||||
let supply_in_profit = cfg.import_value("supply_in_profit", v0)?;
|
||||
let supply_in_loss = cfg.import_value("supply_in_loss", v0)?;
|
||||
|
||||
let unrealized_profit = cfg.import_fiat("unrealized_profit", v0)?;
|
||||
let unrealized_loss = cfg.import_fiat("unrealized_loss", v0)?;
|
||||
let complete = UnrealizedComplete::forced_import(cfg)?;
|
||||
|
||||
let invested_capital_in_profit = cfg.import_fiat("invested_capital_in_profit", v0)?;
|
||||
let invested_capital_in_loss = cfg.import_fiat("invested_capital_in_loss", v0)?;
|
||||
@@ -67,21 +62,10 @@ impl UnrealizedBase {
|
||||
let greed_index = cfg.import_fiat("greed_index", v0)?;
|
||||
let net_sentiment = cfg.import_fiat("net_sentiment", Version::ONE)?;
|
||||
|
||||
let neg_unrealized_loss = LazyFromHeight::from_computed::<NegCentsUnsignedToDollars>(
|
||||
&cfg.name("neg_unrealized_loss"),
|
||||
cfg.version,
|
||||
unrealized_loss.cents.height.read_only_boxed_clone(),
|
||||
&unrealized_loss.cents,
|
||||
);
|
||||
|
||||
let net_unrealized_pnl = cfg.import_fiat("net_unrealized_pnl", v0)?;
|
||||
let gross_pnl = cfg.import_fiat("unrealized_gross_pnl", v0)?;
|
||||
|
||||
Ok(Self {
|
||||
supply_in_profit,
|
||||
supply_in_loss,
|
||||
unrealized_profit,
|
||||
unrealized_loss,
|
||||
complete,
|
||||
invested_capital_in_profit,
|
||||
invested_capital_in_loss,
|
||||
invested_capital_in_profit_raw,
|
||||
@@ -91,20 +75,13 @@ impl UnrealizedBase {
|
||||
pain_index,
|
||||
greed_index,
|
||||
net_sentiment,
|
||||
neg_unrealized_loss,
|
||||
net_unrealized_pnl,
|
||||
gross_pnl,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn min_stateful_height_len(&self) -> usize {
|
||||
self.supply_in_profit
|
||||
.sats
|
||||
.height
|
||||
.len()
|
||||
.min(self.supply_in_loss.sats.height.len())
|
||||
.min(self.unrealized_profit.cents.height.len())
|
||||
.min(self.unrealized_loss.cents.height.len())
|
||||
self.complete
|
||||
.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())
|
||||
@@ -118,22 +95,8 @@ impl UnrealizedBase {
|
||||
height: Height,
|
||||
height_state: &UnrealizedState,
|
||||
) -> Result<()> {
|
||||
self.supply_in_profit
|
||||
.sats
|
||||
.height
|
||||
.truncate_push(height, height_state.supply_in_profit)?;
|
||||
self.supply_in_loss
|
||||
.sats
|
||||
.height
|
||||
.truncate_push(height, height_state.supply_in_loss)?;
|
||||
self.unrealized_profit
|
||||
.cents
|
||||
.height
|
||||
.truncate_push(height, height_state.unrealized_profit)?;
|
||||
self.unrealized_loss
|
||||
.cents
|
||||
.height
|
||||
.truncate_push(height, height_state.unrealized_loss)?;
|
||||
self.complete.truncate_push(height, height_state)?;
|
||||
|
||||
self.invested_capital_in_profit
|
||||
.cents
|
||||
.height
|
||||
@@ -164,20 +127,14 @@ impl UnrealizedBase {
|
||||
}
|
||||
|
||||
pub(crate) fn collect_vecs_mut(&mut self) -> Vec<&mut dyn AnyStoredVec> {
|
||||
vec![
|
||||
&mut self.supply_in_profit.base.sats.height as &mut dyn AnyStoredVec,
|
||||
&mut self.supply_in_profit.base.cents.height as &mut dyn AnyStoredVec,
|
||||
&mut self.supply_in_loss.base.sats.height as &mut dyn AnyStoredVec,
|
||||
&mut self.supply_in_loss.base.cents.height as &mut dyn AnyStoredVec,
|
||||
&mut self.unrealized_profit.cents.height,
|
||||
&mut self.unrealized_loss.cents.height,
|
||||
&mut self.invested_capital_in_profit.cents.height,
|
||||
&mut self.invested_capital_in_loss.cents.height,
|
||||
&mut self.invested_capital_in_profit_raw as &mut dyn AnyStoredVec,
|
||||
&mut self.invested_capital_in_loss_raw as &mut dyn AnyStoredVec,
|
||||
&mut self.investor_cap_in_profit_raw as &mut dyn AnyStoredVec,
|
||||
&mut self.investor_cap_in_loss_raw as &mut dyn AnyStoredVec,
|
||||
]
|
||||
let mut vecs = self.complete.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);
|
||||
vecs.push(&mut self.investor_cap_in_loss_raw as &mut dyn AnyStoredVec);
|
||||
vecs
|
||||
}
|
||||
|
||||
pub(crate) fn compute_from_stateful(
|
||||
@@ -186,6 +143,13 @@ impl UnrealizedBase {
|
||||
others: &[&Self],
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
// Delegate Complete-tier aggregation
|
||||
let complete_refs: Vec<&UnrealizedComplete> =
|
||||
others.iter().map(|o| &o.complete).collect();
|
||||
self.complete
|
||||
.compute_from_stateful(starting_indexes, &complete_refs, exit)?;
|
||||
|
||||
// Source-only: invested_capital
|
||||
macro_rules! sum_others {
|
||||
($($field:tt).+) => {
|
||||
self.$($field).+.compute_sum_of_others(
|
||||
@@ -196,14 +160,10 @@ impl UnrealizedBase {
|
||||
};
|
||||
}
|
||||
|
||||
sum_others!(supply_in_profit.sats.height);
|
||||
sum_others!(supply_in_loss.sats.height);
|
||||
sum_others!(unrealized_profit.cents.height);
|
||||
sum_others!(unrealized_loss.cents.height);
|
||||
sum_others!(invested_capital_in_profit.cents.height);
|
||||
sum_others!(invested_capital_in_loss.cents.height);
|
||||
|
||||
// Raw values for aggregation
|
||||
// Source-only: raw BytesVec aggregation
|
||||
let start = self
|
||||
.invested_capital_in_profit_raw
|
||||
.len()
|
||||
@@ -273,7 +233,10 @@ impl UnrealizedBase {
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
// Pain index: investor_price_of_losers - spot
|
||||
// Complete-tier: net_unrealized_pnl
|
||||
self.complete.compute_rest(starting_indexes, exit)?;
|
||||
|
||||
// Extended-only: Pain index (investor_price_of_losers - spot)
|
||||
self.pain_index.cents.height.compute_transform3(
|
||||
starting_indexes.height,
|
||||
&self.investor_cap_in_loss_raw,
|
||||
@@ -290,7 +253,7 @@ impl UnrealizedBase {
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Greed index: spot - investor_price_of_winners
|
||||
// Extended-only: Greed index (spot - investor_price_of_winners)
|
||||
self.greed_index.cents.height.compute_transform3(
|
||||
starting_indexes.height,
|
||||
&self.investor_cap_in_profit_raw,
|
||||
@@ -307,19 +270,11 @@ impl UnrealizedBase {
|
||||
exit,
|
||||
)?;
|
||||
|
||||
self.net_unrealized_pnl
|
||||
.cents
|
||||
.height
|
||||
.compute_binary::<Cents, Cents, CentsSubtractToCentsSigned>(
|
||||
starting_indexes.height,
|
||||
&self.unrealized_profit.cents.height,
|
||||
&self.unrealized_loss.cents.height,
|
||||
exit,
|
||||
)?;
|
||||
// Source-only: unrealized gross pnl
|
||||
self.gross_pnl.cents.height.compute_add(
|
||||
starting_indexes.height,
|
||||
&self.unrealized_profit.cents.height,
|
||||
&self.unrealized_loss.cents.height,
|
||||
&self.complete.unrealized_profit.cents.height,
|
||||
&self.complete.unrealized_loss.cents.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Cents, CentsSigned, Height, Indexes, Version};
|
||||
use vecdb::{AnyStoredVec, AnyVec, Exit, ReadableCloneableVec, Rw, StorageMode, WritableVec};
|
||||
|
||||
use crate::{
|
||||
distribution::state::UnrealizedState,
|
||||
internal::{
|
||||
CentsSubtractToCentsSigned, FiatFromHeight, LazyFromHeight, NegCentsUnsignedToDollars,
|
||||
ValueFromHeight,
|
||||
},
|
||||
};
|
||||
|
||||
use brk_types::Dollars;
|
||||
|
||||
use crate::distribution::metrics::ImportConfig;
|
||||
|
||||
/// Unrealized metrics for the Complete tier (~6 fields).
|
||||
///
|
||||
/// Excludes source-only fields (invested_capital, raw BytesVecs, unrealized_gross_pnl)
|
||||
/// and extended-only fields (pain_index, greed_index, net_sentiment).
|
||||
#[derive(Traversable)]
|
||||
pub struct UnrealizedComplete<M: StorageMode = Rw> {
|
||||
pub supply_in_profit: ValueFromHeight<M>,
|
||||
pub supply_in_loss: ValueFromHeight<M>,
|
||||
|
||||
pub unrealized_profit: FiatFromHeight<Cents, M>,
|
||||
pub unrealized_loss: FiatFromHeight<Cents, M>,
|
||||
|
||||
pub neg_unrealized_loss: LazyFromHeight<Dollars, Cents>,
|
||||
|
||||
pub net_unrealized_pnl: FiatFromHeight<CentsSigned, M>,
|
||||
}
|
||||
|
||||
impl UnrealizedComplete {
|
||||
pub(crate) fn forced_import(cfg: &ImportConfig) -> Result<Self> {
|
||||
let v0 = Version::ZERO;
|
||||
let supply_in_profit = cfg.import_value("supply_in_profit", v0)?;
|
||||
let supply_in_loss = cfg.import_value("supply_in_loss", v0)?;
|
||||
|
||||
let unrealized_profit = cfg.import_fiat("unrealized_profit", v0)?;
|
||||
let unrealized_loss = cfg.import_fiat("unrealized_loss", v0)?;
|
||||
|
||||
let neg_unrealized_loss = LazyFromHeight::from_computed::<NegCentsUnsignedToDollars>(
|
||||
&cfg.name("neg_unrealized_loss"),
|
||||
cfg.version,
|
||||
unrealized_loss.cents.height.read_only_boxed_clone(),
|
||||
&unrealized_loss.cents,
|
||||
);
|
||||
|
||||
let net_unrealized_pnl = cfg.import_fiat("net_unrealized_pnl", v0)?;
|
||||
|
||||
Ok(Self {
|
||||
supply_in_profit,
|
||||
supply_in_loss,
|
||||
unrealized_profit,
|
||||
unrealized_loss,
|
||||
neg_unrealized_loss,
|
||||
net_unrealized_pnl,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn min_stateful_height_len(&self) -> usize {
|
||||
self.supply_in_profit
|
||||
.sats
|
||||
.height
|
||||
.len()
|
||||
.min(self.supply_in_loss.sats.height.len())
|
||||
.min(self.unrealized_profit.cents.height.len())
|
||||
.min(self.unrealized_loss.cents.height.len())
|
||||
}
|
||||
|
||||
pub(crate) fn truncate_push(
|
||||
&mut self,
|
||||
height: Height,
|
||||
height_state: &UnrealizedState,
|
||||
) -> Result<()> {
|
||||
self.supply_in_profit
|
||||
.sats
|
||||
.height
|
||||
.truncate_push(height, height_state.supply_in_profit)?;
|
||||
self.supply_in_loss
|
||||
.sats
|
||||
.height
|
||||
.truncate_push(height, height_state.supply_in_loss)?;
|
||||
self.unrealized_profit
|
||||
.cents
|
||||
.height
|
||||
.truncate_push(height, height_state.unrealized_profit)?;
|
||||
self.unrealized_loss
|
||||
.cents
|
||||
.height
|
||||
.truncate_push(height, height_state.unrealized_loss)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn collect_vecs_mut(&mut self) -> Vec<&mut dyn AnyStoredVec> {
|
||||
vec![
|
||||
&mut self.supply_in_profit.base.sats.height as &mut dyn AnyStoredVec,
|
||||
&mut self.supply_in_profit.base.cents.height as &mut dyn AnyStoredVec,
|
||||
&mut self.supply_in_loss.base.sats.height as &mut dyn AnyStoredVec,
|
||||
&mut self.supply_in_loss.base.cents.height as &mut dyn AnyStoredVec,
|
||||
&mut self.unrealized_profit.cents.height,
|
||||
&mut self.unrealized_loss.cents.height,
|
||||
]
|
||||
}
|
||||
|
||||
pub(crate) fn compute_from_stateful(
|
||||
&mut self,
|
||||
starting_indexes: &Indexes,
|
||||
others: &[&Self],
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
macro_rules! sum_others {
|
||||
($($field:tt).+) => {
|
||||
self.$($field).+.compute_sum_of_others(
|
||||
starting_indexes.height,
|
||||
&others.iter().map(|v| &v.$($field).+).collect::<Vec<_>>(),
|
||||
exit,
|
||||
)?
|
||||
};
|
||||
}
|
||||
|
||||
sum_others!(supply_in_profit.sats.height);
|
||||
sum_others!(supply_in_loss.sats.height);
|
||||
sum_others!(unrealized_profit.cents.height);
|
||||
sum_others!(unrealized_loss.cents.height);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Compute derived metrics from stored values.
|
||||
pub(crate) fn compute_rest(
|
||||
&mut self,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.net_unrealized_pnl
|
||||
.cents
|
||||
.height
|
||||
.compute_binary::<Cents, Cents, CentsSubtractToCentsSigned>(
|
||||
starting_indexes.height,
|
||||
&self.unrealized_profit.cents.height,
|
||||
&self.unrealized_loss.cents.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
mod base;
|
||||
mod complete;
|
||||
|
||||
pub use base::*;
|
||||
pub use complete::*;
|
||||
|
||||
@@ -53,6 +53,9 @@ impl Vecs {
|
||||
+ 2.min(indexer.vecs.outputs.first_txoutindex.len()),
|
||||
);
|
||||
|
||||
let mut outputtypes_buf: Vec<OutputType> = Vec::new();
|
||||
let mut values_buf: Vec<Sats> = Vec::new();
|
||||
|
||||
// Iterate blocks
|
||||
for h in starting_height.to_usize()..=target_height.to_usize() {
|
||||
let height = Height::from(h);
|
||||
@@ -70,25 +73,24 @@ impl Vecs {
|
||||
let out_start = first_txoutindex.to_usize();
|
||||
let out_end = next_first_txoutindex.to_usize();
|
||||
|
||||
// Sum opreturn values — fold over both vecs without allocation
|
||||
let opreturn_value = indexer
|
||||
// Pre-collect both vecs into reusable buffers
|
||||
indexer
|
||||
.vecs
|
||||
.outputs
|
||||
.outputtype
|
||||
.fold_range_at(
|
||||
out_start,
|
||||
out_end,
|
||||
(Sats::ZERO, out_start),
|
||||
|(sum, vi), ot| {
|
||||
let new_sum = if ot == OutputType::OpReturn {
|
||||
sum + indexer.vecs.outputs.value.collect_one_at(vi).unwrap()
|
||||
} else {
|
||||
sum
|
||||
};
|
||||
(new_sum, vi + 1)
|
||||
},
|
||||
)
|
||||
.0;
|
||||
.collect_range_into_at(out_start, out_end, &mut outputtypes_buf);
|
||||
indexer
|
||||
.vecs
|
||||
.outputs
|
||||
.value
|
||||
.collect_range_into_at(out_start, out_end, &mut values_buf);
|
||||
|
||||
let mut opreturn_value = Sats::ZERO;
|
||||
for (ot, val) in outputtypes_buf.iter().zip(values_buf.iter()) {
|
||||
if *ot == OutputType::OpReturn {
|
||||
opreturn_value += *val;
|
||||
}
|
||||
}
|
||||
|
||||
height_vec.truncate_push(height, opreturn_value)?;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user