mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-22 16:38:11 -07:00
global: snapshot
This commit is contained in:
@@ -115,7 +115,7 @@ impl CoreCohortMetrics {
|
||||
.compute_rest_part1(blocks, starting_indexes, exit)?;
|
||||
|
||||
self.realized
|
||||
.compute_rest_part1(starting_indexes, exit)?;
|
||||
.compute_rest_part1(blocks, starting_indexes, exit)?;
|
||||
|
||||
self.unrealized.compute_rest(starting_indexes, exit)?;
|
||||
|
||||
|
||||
@@ -193,7 +193,7 @@ impl MinimalCohortMetrics {
|
||||
self.activity
|
||||
.compute_rest_part1(blocks, starting_indexes, exit)?;
|
||||
self.realized
|
||||
.compute_rest_part1(starting_indexes, exit)?;
|
||||
.compute_rest_part1(blocks, starting_indexes, exit)?;
|
||||
self.unrealized
|
||||
.compute_rest(prices, starting_indexes.height, exit)?;
|
||||
Ok(())
|
||||
|
||||
@@ -208,7 +208,7 @@ pub trait CohortMetricsBase: CohortMetricsState<Realized = RealizedState> + Send
|
||||
.compute_rest_part1(blocks, starting_indexes, exit)?;
|
||||
|
||||
self.realized_mut()
|
||||
.compute_rest_part1(starting_indexes, exit)?;
|
||||
.compute_rest_part1(blocks, starting_indexes, exit)?;
|
||||
|
||||
self.unrealized_mut()
|
||||
.compute_rest(prices, starting_indexes, exit)?;
|
||||
|
||||
@@ -5,8 +5,9 @@ use derive_more::{Deref, DerefMut};
|
||||
use vecdb::{AnyStoredVec, AnyVec, Exit, Rw, StorageMode, WritableVec};
|
||||
|
||||
use crate::{
|
||||
blocks,
|
||||
distribution::state::RealizedOps,
|
||||
internal::ComputedFromHeight,
|
||||
internal::{ComputedFromHeight, RollingWindow24h},
|
||||
};
|
||||
|
||||
use crate::distribution::metrics::ImportConfig;
|
||||
@@ -22,6 +23,9 @@ pub struct RealizedBase<M: StorageMode = Rw> {
|
||||
|
||||
pub sent_in_profit: ComputedFromHeight<Sats, M>,
|
||||
pub sent_in_loss: ComputedFromHeight<Sats, M>,
|
||||
|
||||
pub sent_in_profit_sum: RollingWindow24h<Sats, M>,
|
||||
pub sent_in_loss_sum: RollingWindow24h<Sats, M>,
|
||||
}
|
||||
|
||||
impl RealizedBase {
|
||||
@@ -32,6 +36,8 @@ impl RealizedBase {
|
||||
core: RealizedCore::forced_import(cfg)?,
|
||||
sent_in_profit: cfg.import("sent_in_profit", v1)?,
|
||||
sent_in_loss: cfg.import("sent_in_loss", v1)?,
|
||||
sent_in_profit_sum: cfg.import("sent_in_profit", v1)?,
|
||||
sent_in_loss_sum: cfg.import("sent_in_loss", v1)?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -78,9 +84,23 @@ impl RealizedBase {
|
||||
|
||||
pub(crate) fn compute_rest_part1(
|
||||
&mut self,
|
||||
blocks: &blocks::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.core.compute_rest_part1(starting_indexes, exit)
|
||||
self.core.compute_rest_part1(blocks, starting_indexes, exit)?;
|
||||
self.sent_in_profit_sum.compute_rolling_sum(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_24h_ago,
|
||||
&self.sent_in_profit.height,
|
||||
exit,
|
||||
)?;
|
||||
self.sent_in_loss_sum.compute_rolling_sum(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_24h_ago,
|
||||
&self.sent_in_loss.height,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,10 +117,11 @@ impl RealizedCore {
|
||||
|
||||
pub(crate) fn compute_rest_part1(
|
||||
&mut self,
|
||||
blocks: &blocks::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.minimal.compute_rest_part1(starting_indexes, exit)?;
|
||||
self.minimal.compute_rest_part1(blocks, starting_indexes, exit)?;
|
||||
|
||||
self.net_realized_pnl
|
||||
.compute(starting_indexes.height, exit, |vec| {
|
||||
|
||||
@@ -46,6 +46,11 @@ pub struct RealizedFull<M: StorageMode = Rw> {
|
||||
pub loss_value_created: ComputedFromHeight<Cents, M>,
|
||||
pub loss_value_destroyed: ComputedFromHeight<Cents, M>,
|
||||
|
||||
pub profit_value_created_sum: RollingWindows<Cents, M>,
|
||||
pub profit_value_destroyed_sum: RollingWindows<Cents, M>,
|
||||
pub loss_value_created_sum: RollingWindows<Cents, M>,
|
||||
pub loss_value_destroyed_sum: RollingWindows<Cents, M>,
|
||||
|
||||
pub capitulation_flow: LazyFromHeight<Dollars, Cents>,
|
||||
pub profit_flow: LazyFromHeight<Dollars, Cents>,
|
||||
|
||||
@@ -71,16 +76,16 @@ pub struct RealizedFull<M: StorageMode = Rw> {
|
||||
|
||||
pub realized_cap_rel_to_own_market_cap: PercentFromHeight<BasisPoints32, M>,
|
||||
|
||||
pub realized_profit_sum: RollingWindows<Cents, M>,
|
||||
pub realized_loss_sum: RollingWindows<Cents, M>,
|
||||
pub realized_profit_sum_extended: RollingWindowsFrom1w<Cents, M>,
|
||||
pub realized_loss_sum_extended: RollingWindowsFrom1w<Cents, M>,
|
||||
pub realized_profit_to_loss_ratio: RollingWindows<StoredF64, M>,
|
||||
|
||||
pub value_created_sum_extended: RollingWindowsFrom1w<Cents, M>,
|
||||
pub value_destroyed_sum_extended: RollingWindowsFrom1w<Cents, M>,
|
||||
pub sopr_extended: RollingWindowsFrom1w<StoredF64, M>,
|
||||
|
||||
pub sent_in_profit_sum: RollingWindows<Sats, M>,
|
||||
pub sent_in_loss_sum: RollingWindows<Sats, M>,
|
||||
pub sent_in_profit_sum_extended: RollingWindowsFrom1w<Sats, M>,
|
||||
pub sent_in_loss_sum_extended: RollingWindowsFrom1w<Sats, M>,
|
||||
|
||||
pub realized_price_ratio_percentiles: ComputedFromHeightRatioPercentiles<M>,
|
||||
pub realized_price_ratio_std_dev: ComputedFromHeightRatioStdDevBands<M>,
|
||||
@@ -103,6 +108,11 @@ impl RealizedFull {
|
||||
let loss_value_destroyed: ComputedFromHeight<Cents> =
|
||||
cfg.import("loss_value_destroyed", v0)?;
|
||||
|
||||
let profit_value_created_sum = cfg.import("profit_value_created", v1)?;
|
||||
let profit_value_destroyed_sum = cfg.import("profit_value_destroyed", v1)?;
|
||||
let loss_value_created_sum = cfg.import("loss_value_created", v1)?;
|
||||
let loss_value_destroyed_sum = cfg.import("loss_value_destroyed", v1)?;
|
||||
|
||||
let capitulation_flow = LazyFromHeight::from_computed::<CentsUnsignedToDollars>(
|
||||
&cfg.name("capitulation_flow"),
|
||||
cfg.version,
|
||||
@@ -159,6 +169,10 @@ impl RealizedFull {
|
||||
profit_value_destroyed,
|
||||
loss_value_created,
|
||||
loss_value_destroyed,
|
||||
profit_value_created_sum,
|
||||
profit_value_destroyed_sum,
|
||||
loss_value_created_sum,
|
||||
loss_value_destroyed_sum,
|
||||
capitulation_flow,
|
||||
profit_flow,
|
||||
gross_pnl_sum,
|
||||
@@ -178,15 +192,15 @@ impl RealizedFull {
|
||||
peak_regret_rel_to_realized_cap,
|
||||
realized_cap_rel_to_own_market_cap: cfg
|
||||
.import("realized_cap_rel_to_own_market_cap", v1)?,
|
||||
realized_profit_sum: cfg.import("realized_profit", v1)?,
|
||||
realized_loss_sum: cfg.import("realized_loss", v1)?,
|
||||
realized_profit_sum_extended: cfg.import("realized_profit", v1)?,
|
||||
realized_loss_sum_extended: cfg.import("realized_loss", v1)?,
|
||||
realized_profit_to_loss_ratio: cfg
|
||||
.import("realized_profit_to_loss_ratio", v1)?,
|
||||
value_created_sum_extended,
|
||||
value_destroyed_sum_extended,
|
||||
sopr_extended,
|
||||
sent_in_profit_sum: cfg.import("sent_in_profit", v1)?,
|
||||
sent_in_loss_sum: cfg.import("sent_in_loss", v1)?,
|
||||
sent_in_profit_sum_extended: cfg.import("sent_in_profit", v1)?,
|
||||
sent_in_loss_sum_extended: cfg.import("sent_in_loss", v1)?,
|
||||
realized_price_ratio_percentiles: ComputedFromHeightRatioPercentiles::forced_import(
|
||||
cfg.db,
|
||||
&realized_price_name,
|
||||
@@ -276,10 +290,11 @@ impl RealizedFull {
|
||||
|
||||
pub(crate) fn compute_rest_part1(
|
||||
&mut self,
|
||||
blocks: &blocks::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.base.compute_rest_part1(starting_indexes, exit)?;
|
||||
self.base.compute_rest_part1(blocks, starting_indexes, exit)?;
|
||||
self.peak_regret
|
||||
.compute_rest(starting_indexes.height, exit)?;
|
||||
Ok(())
|
||||
@@ -302,8 +317,9 @@ impl RealizedFull {
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Extended rolling windows (1w, 1m, 1y) for value_created/destroyed/sopr
|
||||
let window_starts = blocks.count.window_starts();
|
||||
|
||||
// Extended rolling windows (1w, 1m, 1y) for value_created/destroyed/sopr
|
||||
self.value_created_sum_extended.compute_rolling_sum(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
@@ -354,21 +370,46 @@ impl RealizedFull {
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Sent in profit/loss rolling sums
|
||||
let window_starts = blocks.count.window_starts();
|
||||
self.sent_in_profit_sum.compute_rolling_sum(
|
||||
// Sent in profit/loss extended rolling sums (1w, 1m, 1y)
|
||||
self.sent_in_profit_sum_extended.compute_rolling_sum(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
&self.base.sent_in_profit.height,
|
||||
exit,
|
||||
)?;
|
||||
self.sent_in_loss_sum.compute_rolling_sum(
|
||||
self.sent_in_loss_sum_extended.compute_rolling_sum(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
&self.base.sent_in_loss.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Profit/loss value created/destroyed rolling sums
|
||||
self.profit_value_created_sum.compute_rolling_sum(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
&self.profit_value_created.height,
|
||||
exit,
|
||||
)?;
|
||||
self.profit_value_destroyed_sum.compute_rolling_sum(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
&self.profit_value_destroyed.height,
|
||||
exit,
|
||||
)?;
|
||||
self.loss_value_created_sum.compute_rolling_sum(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
&self.loss_value_created.height,
|
||||
exit,
|
||||
)?;
|
||||
self.loss_value_destroyed_sum.compute_rolling_sum(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
&self.loss_value_destroyed.height,
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Gross PnL
|
||||
self.gross_pnl.cents.height.compute_add(
|
||||
starting_indexes.height,
|
||||
@@ -377,7 +418,6 @@ impl RealizedFull {
|
||||
exit,
|
||||
)?;
|
||||
|
||||
let window_starts = blocks.count.window_starts();
|
||||
self.gross_pnl_sum.compute_rolling_sum(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
@@ -471,15 +511,14 @@ impl RealizedFull {
|
||||
)?;
|
||||
}
|
||||
|
||||
// Extended: realized profit/loss rolling sums
|
||||
let window_starts = blocks.count.window_starts();
|
||||
self.realized_profit_sum.compute_rolling_sum(
|
||||
// Extended: realized profit/loss rolling sums (1w, 1m, 1y)
|
||||
self.realized_profit_sum_extended.compute_rolling_sum(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
&self.base.core.minimal.realized_profit.height,
|
||||
exit,
|
||||
)?;
|
||||
self.realized_loss_sum.compute_rolling_sum(
|
||||
self.realized_loss_sum_extended.compute_rolling_sum(
|
||||
starting_indexes.height,
|
||||
&window_starts,
|
||||
&self.base.core.minimal.realized_loss.height,
|
||||
@@ -496,12 +535,18 @@ impl RealizedFull {
|
||||
)?;
|
||||
|
||||
// Realized profit to loss ratios
|
||||
self.realized_profit_to_loss_ratio._24h.compute_binary::<Cents, Cents, RatioCents64>(
|
||||
starting_indexes.height,
|
||||
&self.base.core.minimal.realized_profit_sum._24h.height,
|
||||
&self.base.core.minimal.realized_loss_sum._24h.height,
|
||||
exit,
|
||||
)?;
|
||||
for ((ratio, profit), loss) in self
|
||||
.realized_profit_to_loss_ratio
|
||||
.as_mut_array()
|
||||
.as_mut_array_from_1w()
|
||||
.into_iter()
|
||||
.zip(self.realized_profit_sum.as_array())
|
||||
.zip(self.realized_loss_sum.as_array())
|
||||
.zip(self.realized_profit_sum_extended.as_array())
|
||||
.zip(self.realized_loss_sum_extended.as_array())
|
||||
{
|
||||
ratio.compute_binary::<Cents, Cents, RatioCents64>(
|
||||
starting_indexes.height,
|
||||
|
||||
@@ -6,10 +6,11 @@ use brk_types::{
|
||||
use vecdb::{AnyStoredVec, AnyVec, Exit, ReadableCloneableVec, ReadableVec, Rw, StorageMode, WritableVec};
|
||||
|
||||
use crate::{
|
||||
blocks,
|
||||
distribution::state::RealizedOps,
|
||||
internal::{
|
||||
CentsUnsignedToDollars, ComputedFromHeight, ComputedFromHeightCumulative,
|
||||
ComputedFromHeightRatio, Identity, LazyFromHeight, Price,
|
||||
ComputedFromHeightRatio, Identity, LazyFromHeight, Price, RollingWindow24h,
|
||||
},
|
||||
prices,
|
||||
};
|
||||
@@ -25,6 +26,9 @@ pub struct RealizedMinimal<M: StorageMode = Rw> {
|
||||
pub realized_price: Price<ComputedFromHeight<Cents, M>>,
|
||||
pub realized_price_ratio: ComputedFromHeightRatio<M>,
|
||||
pub mvrv: LazyFromHeight<StoredF32>,
|
||||
|
||||
pub realized_profit_sum: RollingWindow24h<Cents, M>,
|
||||
pub realized_loss_sum: RollingWindow24h<Cents, M>,
|
||||
}
|
||||
|
||||
impl RealizedMinimal {
|
||||
@@ -50,6 +54,9 @@ impl RealizedMinimal {
|
||||
&realized_price_ratio.ratio,
|
||||
);
|
||||
|
||||
let realized_profit_sum = cfg.import("realized_profit", Version::ONE)?;
|
||||
let realized_loss_sum = cfg.import("realized_loss", Version::ONE)?;
|
||||
|
||||
Ok(Self {
|
||||
realized_cap_cents,
|
||||
realized_profit,
|
||||
@@ -58,6 +65,8 @@ impl RealizedMinimal {
|
||||
realized_price,
|
||||
realized_price_ratio,
|
||||
mvrv,
|
||||
realized_profit_sum,
|
||||
realized_loss_sum,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -108,6 +117,7 @@ impl RealizedMinimal {
|
||||
|
||||
pub(crate) fn compute_rest_part1(
|
||||
&mut self,
|
||||
blocks: &blocks::Vecs,
|
||||
starting_indexes: &Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
@@ -115,6 +125,18 @@ impl RealizedMinimal {
|
||||
.compute_rest(starting_indexes.height, exit)?;
|
||||
self.realized_loss
|
||||
.compute_rest(starting_indexes.height, exit)?;
|
||||
self.realized_profit_sum.compute_rolling_sum(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_24h_ago,
|
||||
&self.realized_profit.height,
|
||||
exit,
|
||||
)?;
|
||||
self.realized_loss_sum.compute_rolling_sum(
|
||||
starting_indexes.height,
|
||||
&blocks.count.height_24h_ago,
|
||||
&self.realized_loss.height,
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ use brk_error::Result;
|
||||
use brk_types::{Height, Indexes};
|
||||
use vecdb::Exit;
|
||||
|
||||
use crate::distribution::state::RealizedState;
|
||||
use crate::{blocks, distribution::state::RealizedState};
|
||||
|
||||
/// Polymorphic dispatch for realized metric types.
|
||||
///
|
||||
@@ -26,7 +26,7 @@ pub trait RealizedLike: Send + Sync {
|
||||
fn as_base_mut(&mut self) -> &mut RealizedBase;
|
||||
fn min_stateful_height_len(&self) -> usize;
|
||||
fn truncate_push(&mut self, height: Height, state: &RealizedState) -> Result<()>;
|
||||
fn compute_rest_part1(&mut self, starting_indexes: &Indexes, exit: &Exit) -> Result<()>;
|
||||
fn compute_rest_part1(&mut self, blocks: &blocks::Vecs, starting_indexes: &Indexes, exit: &Exit) -> Result<()>;
|
||||
fn compute_from_stateful(
|
||||
&mut self,
|
||||
starting_indexes: &Indexes,
|
||||
@@ -42,8 +42,8 @@ impl RealizedLike for RealizedBase {
|
||||
fn truncate_push(&mut self, height: Height, state: &RealizedState) -> Result<()> {
|
||||
self.truncate_push(height, state)
|
||||
}
|
||||
fn compute_rest_part1(&mut self, starting_indexes: &Indexes, exit: &Exit) -> Result<()> {
|
||||
self.compute_rest_part1(starting_indexes, exit)
|
||||
fn compute_rest_part1(&mut self, blocks: &blocks::Vecs, starting_indexes: &Indexes, exit: &Exit) -> Result<()> {
|
||||
self.compute_rest_part1(blocks, starting_indexes, exit)
|
||||
}
|
||||
fn compute_from_stateful(&mut self, starting_indexes: &Indexes, others: &[&RealizedBase], exit: &Exit) -> Result<()> {
|
||||
self.compute_from_stateful(starting_indexes, others, exit)
|
||||
@@ -57,8 +57,8 @@ impl RealizedLike for RealizedFull {
|
||||
fn truncate_push(&mut self, height: Height, state: &RealizedState) -> Result<()> {
|
||||
self.truncate_push(height, state)
|
||||
}
|
||||
fn compute_rest_part1(&mut self, starting_indexes: &Indexes, exit: &Exit) -> Result<()> {
|
||||
self.compute_rest_part1(starting_indexes, exit)
|
||||
fn compute_rest_part1(&mut self, blocks: &blocks::Vecs, starting_indexes: &Indexes, exit: &Exit) -> Result<()> {
|
||||
self.compute_rest_part1(blocks, starting_indexes, exit)
|
||||
}
|
||||
fn compute_from_stateful(&mut self, starting_indexes: &Indexes, others: &[&RealizedBase], exit: &Exit) -> Result<()> {
|
||||
self.compute_from_stateful(starting_indexes, others, exit)
|
||||
|
||||
@@ -33,4 +33,8 @@ impl<A> Windows<A> {
|
||||
pub fn as_mut_array(&mut self) -> [&mut A; 4] {
|
||||
[&mut self._24h, &mut self._1w, &mut self._1m, &mut self._1y]
|
||||
}
|
||||
|
||||
pub fn as_mut_array_from_1w(&mut self) -> [&mut A; 3] {
|
||||
[&mut self._1w, &mut self._1m, &mut self._1y]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user