mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-27 16:19:59 -07:00
global: snapshot
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use brk_cohort::Filter;
|
||||
use brk_error::Result;
|
||||
use brk_types::{BasisPoints16, BasisPointsSigned16, Cents, Height, Version};
|
||||
use brk_types::{BasisPoints16, BasisPoints32, BasisPointsSigned32, Cents, Height, Version};
|
||||
use schemars::JsonSchema;
|
||||
use vecdb::{BytesVec, BytesVecValue, Database, ImportableVec};
|
||||
|
||||
@@ -87,11 +87,11 @@ impl<'a> ImportConfig<'a> {
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn import_percent_bps16(
|
||||
pub(crate) fn import_percent_bps32(
|
||||
&self,
|
||||
suffix: &str,
|
||||
offset: Version,
|
||||
) -> Result<PercentFromHeight<BasisPointsSigned16>> {
|
||||
) -> Result<PercentFromHeight<BasisPointsSigned32>> {
|
||||
PercentFromHeight::forced_import(
|
||||
self.db,
|
||||
&self.name(suffix),
|
||||
@@ -199,11 +199,24 @@ impl<'a> ImportConfig<'a> {
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn import_percent_rolling_bp16(
|
||||
pub(crate) fn import_percent_bp32(
|
||||
&self,
|
||||
suffix: &str,
|
||||
offset: Version,
|
||||
) -> Result<PercentRollingWindows<BasisPoints16>> {
|
||||
) -> Result<PercentFromHeight<BasisPoints32>> {
|
||||
PercentFromHeight::forced_import(
|
||||
self.db,
|
||||
&self.name(suffix),
|
||||
self.version + offset,
|
||||
self.indexes,
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn import_percent_rolling_bp32(
|
||||
&self,
|
||||
suffix: &str,
|
||||
offset: Version,
|
||||
) -> Result<PercentRollingWindows<BasisPoints32>> {
|
||||
PercentRollingWindows::forced_import(
|
||||
self.db,
|
||||
&self.name(suffix),
|
||||
@@ -225,11 +238,11 @@ impl<'a> ImportConfig<'a> {
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn import_percent_emas_1w_1m_bp16(
|
||||
pub(crate) fn import_percent_emas_1w_1m_bp32(
|
||||
&self,
|
||||
suffix: &str,
|
||||
offset: Version,
|
||||
) -> Result<PercentRollingEmas1w1m<BasisPoints16>> {
|
||||
) -> Result<PercentRollingEmas1w1m<BasisPoints32>> {
|
||||
PercentRollingEmas1w1m::forced_import(
|
||||
self.db,
|
||||
&self.name(suffix),
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{
|
||||
BasisPoints16, BasisPoints32, BasisPointsSigned16, Bitcoin, Cents, CentsSats, CentsSigned,
|
||||
BasisPoints16, BasisPoints32, BasisPointsSigned32, Bitcoin, Cents, CentsSats, CentsSigned,
|
||||
CentsSquaredSats, Dollars, Height, Indexes, Sats, StoredF32, StoredF64, Version,
|
||||
};
|
||||
use vecdb::{
|
||||
@@ -16,9 +16,9 @@ use crate::{
|
||||
CentsPlus, CentsUnsignedToDollars, ComputedFromHeight, ComputedFromHeightCumulative,
|
||||
ComputedFromHeightRatio, FiatFromHeight, Identity, LazyFromHeight,
|
||||
NegCentsUnsignedToDollars, PercentFromHeight, PercentRollingEmas1w1m,
|
||||
PercentRollingWindows, Price, RatioCents64, RatioCentsBp16, RatioCentsSignedCentsBps16,
|
||||
RatioCentsSignedDollarsBps16, RollingEmas1w1m, RollingEmas2w, RollingWindows,
|
||||
ValueFromHeightCumulative,
|
||||
PercentRollingWindows, Price, RatioCents64, RatioCentsBp16, RatioCentsBp32,
|
||||
RatioCentsSignedCentsBps32, RatioCentsSignedDollarsBps32, RollingEmas1w1m, RollingEmas2w,
|
||||
RollingWindows, ValueFromHeightCumulative,
|
||||
},
|
||||
prices,
|
||||
};
|
||||
@@ -55,7 +55,7 @@ pub struct RealizedBase<M: StorageMode = Rw> {
|
||||
|
||||
pub realized_profit_rel_to_realized_cap: PercentFromHeight<BasisPoints16, M>,
|
||||
pub realized_loss_rel_to_realized_cap: PercentFromHeight<BasisPoints16, M>,
|
||||
pub net_realized_pnl_rel_to_realized_cap: PercentFromHeight<BasisPointsSigned16, 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>,
|
||||
@@ -75,15 +75,15 @@ pub struct RealizedBase<M: StorageMode = Rw> {
|
||||
pub sopr_24h_ema: RollingEmas1w1m<StoredF64, M>,
|
||||
|
||||
pub gross_pnl_sum: RollingWindows<Cents, M>,
|
||||
pub sell_side_risk_ratio: PercentRollingWindows<BasisPoints16, M>,
|
||||
pub sell_side_risk_ratio_24h_ema: PercentRollingEmas1w1m<BasisPoints16, 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<BasisPointsSigned16, M>,
|
||||
pub net_pnl_change_1m_rel_to_market_cap: PercentFromHeight<BasisPointsSigned16, 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 peak_regret: ComputedFromHeightCumulative<Cents, M>,
|
||||
pub peak_regret_rel_to_realized_cap: PercentFromHeight<BasisPoints16, M>,
|
||||
pub peak_regret_rel_to_realized_cap: PercentFromHeight<BasisPoints32, M>,
|
||||
|
||||
pub sent_in_profit: ValueFromHeightCumulative<M>,
|
||||
pub sent_in_profit_ema: RollingEmas2w<M>,
|
||||
@@ -126,7 +126,7 @@ impl RealizedBase {
|
||||
let realized_loss_rel_to_realized_cap =
|
||||
cfg.import_percent_bp16("realized_loss_rel_to_realized_cap", v1)?;
|
||||
let net_realized_pnl_rel_to_realized_cap =
|
||||
cfg.import_percent_bps16("net_realized_pnl_rel_to_realized_cap", v1)?;
|
||||
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)?;
|
||||
@@ -169,15 +169,15 @@ impl RealizedBase {
|
||||
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_bp16("sell_side_risk_ratio", 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_24h_ema =
|
||||
cfg.import_percent_emas_1w_1m_bp16("sell_side_risk_ratio_24h", v1)?;
|
||||
cfg.import_percent_emas_1w_1m_bp32("sell_side_risk_ratio_24h", Version::new(2))?;
|
||||
|
||||
let peak_regret_rel_to_realized_cap =
|
||||
cfg.import_percent_bp16("realized_peak_regret_rel_to_realized_cap", v1)?;
|
||||
cfg.import_percent_bp32("realized_peak_regret_rel_to_realized_cap", Version::new(2))?;
|
||||
|
||||
Ok(Self {
|
||||
realized_cap_cents,
|
||||
@@ -220,9 +220,9 @@ impl RealizedBase {
|
||||
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_bps16("net_pnl_change_1m_rel_to_realized_cap", Version::new(3))?,
|
||||
.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_bps16("net_pnl_change_1m_rel_to_market_cap", Version::new(3))?,
|
||||
.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)?,
|
||||
@@ -585,7 +585,7 @@ impl RealizedBase {
|
||||
.into_iter()
|
||||
.zip(self.gross_pnl_sum.as_array())
|
||||
{
|
||||
ssrr.compute_binary::<Cents, Cents, RatioCentsBp16>(
|
||||
ssrr.compute_binary::<Cents, Cents, RatioCentsBp32>(
|
||||
starting_indexes.height,
|
||||
&rv.height,
|
||||
&self.realized_cap_cents.height,
|
||||
@@ -663,14 +663,14 @@ impl RealizedBase {
|
||||
exit,
|
||||
)?;
|
||||
self.net_realized_pnl_rel_to_realized_cap
|
||||
.compute_binary::<CentsSigned, Cents, RatioCentsSignedCentsBps16>(
|
||||
.compute_binary::<CentsSigned, Cents, RatioCentsSignedCentsBps32>(
|
||||
starting_indexes.height,
|
||||
&self.net_realized_pnl.height,
|
||||
&self.realized_cap_cents.height,
|
||||
exit,
|
||||
)?;
|
||||
self.peak_regret_rel_to_realized_cap
|
||||
.compute_binary::<Cents, Cents, RatioCentsBp16>(
|
||||
.compute_binary::<Cents, Cents, RatioCentsBp32>(
|
||||
starting_indexes.height,
|
||||
&self.peak_regret.height,
|
||||
&self.realized_cap_cents.height,
|
||||
@@ -686,7 +686,7 @@ impl RealizedBase {
|
||||
)?;
|
||||
|
||||
self.net_pnl_change_1m_rel_to_realized_cap
|
||||
.compute_binary::<CentsSigned, Cents, RatioCentsSignedCentsBps16>(
|
||||
.compute_binary::<CentsSigned, Cents, RatioCentsSignedCentsBps32>(
|
||||
starting_indexes.height,
|
||||
&self.net_pnl_change_1m.height,
|
||||
&self.realized_cap_cents.height,
|
||||
@@ -694,7 +694,7 @@ impl RealizedBase {
|
||||
)?;
|
||||
|
||||
self.net_pnl_change_1m_rel_to_market_cap
|
||||
.compute_binary::<CentsSigned, Dollars, RatioCentsSignedDollarsBps16>(
|
||||
.compute_binary::<CentsSigned, Dollars, RatioCentsSignedDollarsBps32>(
|
||||
starting_indexes.height,
|
||||
&self.net_pnl_change_1m.height,
|
||||
height_to_market_cap,
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{BasisPoints16, Cents, Dollars, Height, Indexes, StoredF64, Version};
|
||||
use brk_types::{BasisPoints32, Cents, Dollars, Height, Indexes, StoredF64, Version};
|
||||
use vecdb::{Exit, ReadableVec, Rw, StorageMode};
|
||||
|
||||
use crate::{
|
||||
blocks,
|
||||
internal::{
|
||||
ComputedFromHeightRatioExtension, PercentFromHeight, RatioCents64, RatioDollarsBp16,
|
||||
ComputedFromHeightRatioExtension, PercentFromHeight, RatioCents64, RatioDollarsBp32,
|
||||
RollingWindows,
|
||||
},
|
||||
};
|
||||
@@ -17,7 +17,7 @@ use super::RealizedBase;
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct RealizedExtended<M: StorageMode = Rw> {
|
||||
pub realized_cap_rel_to_own_market_cap: PercentFromHeight<BasisPoints16, M>,
|
||||
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>,
|
||||
@@ -32,7 +32,7 @@ impl RealizedExtended {
|
||||
pub(crate) fn forced_import(cfg: &ImportConfig) -> Result<Self> {
|
||||
Ok(RealizedExtended {
|
||||
realized_cap_rel_to_own_market_cap: cfg
|
||||
.import_percent_bp16("realized_cap_rel_to_own_market_cap", Version::ZERO)?,
|
||||
.import_percent_bp32("realized_cap_rel_to_own_market_cap", Version::ONE)?,
|
||||
realized_profit_sum: cfg.import_rolling("realized_profit", Version::ONE)?,
|
||||
realized_loss_sum: cfg.import_rolling("realized_loss", Version::ONE)?,
|
||||
realized_profit_to_loss_ratio: cfg
|
||||
@@ -78,7 +78,7 @@ impl RealizedExtended {
|
||||
|
||||
// Realized cap relative to own market cap
|
||||
self.realized_cap_rel_to_own_market_cap
|
||||
.compute_binary::<Dollars, Dollars, RatioDollarsBp16>(
|
||||
.compute_binary::<Dollars, Dollars, RatioDollarsBp32>(
|
||||
starting_indexes.height,
|
||||
&base.realized_cap.height,
|
||||
height_to_market_cap,
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{BasisPoints16, BasisPointsSigned16, Dollars, Height, Sats, StoredF32, Version};
|
||||
use brk_types::{BasisPoints16, BasisPointsSigned32, Dollars, Height, Sats, StoredF32, Version};
|
||||
use vecdb::{Exit, ReadableCloneableVec, ReadableVec, Rw, StorageMode};
|
||||
|
||||
use crate::internal::{
|
||||
Bps16ToFloat, LazyFromHeight, NegRatioDollarsBps16, PercentFromHeight, RatioDollarsBp16,
|
||||
RatioDollarsBps16, RatioSatsBp16,
|
||||
Bps32ToFloat, LazyFromHeight, NegRatioDollarsBps32, PercentFromHeight, RatioDollarsBp16,
|
||||
RatioDollarsBps32, RatioSatsBp16,
|
||||
};
|
||||
|
||||
use crate::distribution::metrics::{ImportConfig, RealizedBase, UnrealizedBase};
|
||||
@@ -17,9 +17,9 @@ pub struct RelativeBase<M: StorageMode = Rw> {
|
||||
|
||||
pub unrealized_profit_rel_to_market_cap: PercentFromHeight<BasisPoints16, M>,
|
||||
pub unrealized_loss_rel_to_market_cap: PercentFromHeight<BasisPoints16, M>,
|
||||
pub neg_unrealized_loss_rel_to_market_cap: PercentFromHeight<BasisPointsSigned16, M>,
|
||||
pub net_unrealized_pnl_rel_to_market_cap: PercentFromHeight<BasisPointsSigned16, M>,
|
||||
pub nupl: LazyFromHeight<StoredF32, BasisPointsSigned16>,
|
||||
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>,
|
||||
@@ -31,11 +31,11 @@ impl RelativeBase {
|
||||
let v2 = Version::new(2);
|
||||
|
||||
let net_unrealized_pnl_rel_to_market_cap =
|
||||
cfg.import_percent_bps16("net_unrealized_pnl_rel_to_market_cap", v2)?;
|
||||
cfg.import_percent_bps32("net_unrealized_pnl_rel_to_market_cap", Version::new(3))?;
|
||||
|
||||
let nupl = LazyFromHeight::from_computed::<Bps16ToFloat>(
|
||||
let nupl = LazyFromHeight::from_computed::<Bps32ToFloat>(
|
||||
&cfg.name("nupl"),
|
||||
cfg.version + v2,
|
||||
cfg.version + Version::new(3),
|
||||
net_unrealized_pnl_rel_to_market_cap
|
||||
.bps
|
||||
.height
|
||||
@@ -53,7 +53,7 @@ impl RelativeBase {
|
||||
unrealized_loss_rel_to_market_cap: cfg
|
||||
.import_percent_bp16("unrealized_loss_rel_to_market_cap", v2)?,
|
||||
neg_unrealized_loss_rel_to_market_cap: cfg
|
||||
.import_percent_bps16("neg_unrealized_loss_rel_to_market_cap", v2)?,
|
||||
.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(
|
||||
@@ -105,14 +105,14 @@ impl RelativeBase {
|
||||
exit,
|
||||
)?;
|
||||
self.neg_unrealized_loss_rel_to_market_cap
|
||||
.compute_binary::<Dollars, Dollars, NegRatioDollarsBps16>(
|
||||
.compute_binary::<Dollars, Dollars, NegRatioDollarsBps32>(
|
||||
max_from,
|
||||
&unrealized.unrealized_loss.usd.height,
|
||||
market_cap,
|
||||
exit,
|
||||
)?;
|
||||
self.net_unrealized_pnl_rel_to_market_cap
|
||||
.compute_binary::<Dollars, Dollars, RatioDollarsBps16>(
|
||||
.compute_binary::<Dollars, Dollars, RatioDollarsBps32>(
|
||||
max_from,
|
||||
&unrealized.net_unrealized_pnl.usd.height,
|
||||
market_cap,
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{BasisPoints16, BasisPointsSigned16, Dollars, Height, Version};
|
||||
use brk_types::{BasisPoints16, BasisPoints32, BasisPointsSigned32, Dollars, Height, Version};
|
||||
use vecdb::{Exit, ReadableVec, Rw, StorageMode};
|
||||
|
||||
use crate::internal::{
|
||||
NegRatioDollarsBps16, PercentFromHeight, RatioDollarsBp16, RatioDollarsBps16,
|
||||
NegRatioDollarsBps32, PercentFromHeight, RatioDollarsBp16, RatioDollarsBp32, RatioDollarsBps32,
|
||||
};
|
||||
|
||||
use crate::distribution::metrics::{ImportConfig, UnrealizedBase};
|
||||
@@ -13,9 +13,9 @@ use crate::distribution::metrics::{ImportConfig, UnrealizedBase};
|
||||
#[derive(Traversable)]
|
||||
pub struct RelativeExtendedOwnMarketCap<M: StorageMode = Rw> {
|
||||
pub unrealized_profit_rel_to_own_market_cap: PercentFromHeight<BasisPoints16, M>,
|
||||
pub unrealized_loss_rel_to_own_market_cap: PercentFromHeight<BasisPoints16, M>,
|
||||
pub neg_unrealized_loss_rel_to_own_market_cap: PercentFromHeight<BasisPointsSigned16, M>,
|
||||
pub net_unrealized_pnl_rel_to_own_market_cap: PercentFromHeight<BasisPointsSigned16, M>,
|
||||
pub unrealized_loss_rel_to_own_market_cap: PercentFromHeight<BasisPoints32, M>,
|
||||
pub neg_unrealized_loss_rel_to_own_market_cap: PercentFromHeight<BasisPointsSigned32, M>,
|
||||
pub net_unrealized_pnl_rel_to_own_market_cap: PercentFromHeight<BasisPointsSigned32, M>,
|
||||
}
|
||||
|
||||
impl RelativeExtendedOwnMarketCap {
|
||||
@@ -26,11 +26,11 @@ impl RelativeExtendedOwnMarketCap {
|
||||
unrealized_profit_rel_to_own_market_cap: cfg
|
||||
.import_percent_bp16("unrealized_profit_rel_to_own_market_cap", v2)?,
|
||||
unrealized_loss_rel_to_own_market_cap: cfg
|
||||
.import_percent_bp16("unrealized_loss_rel_to_own_market_cap", v2)?,
|
||||
.import_percent_bp32("unrealized_loss_rel_to_own_market_cap", Version::new(3))?,
|
||||
neg_unrealized_loss_rel_to_own_market_cap: cfg
|
||||
.import_percent_bps16("neg_unrealized_loss_rel_to_own_market_cap", v2)?,
|
||||
.import_percent_bps32("neg_unrealized_loss_rel_to_own_market_cap", Version::new(3))?,
|
||||
net_unrealized_pnl_rel_to_own_market_cap: cfg
|
||||
.import_percent_bps16("net_unrealized_pnl_rel_to_own_market_cap", v2)?,
|
||||
.import_percent_bps32("net_unrealized_pnl_rel_to_own_market_cap", Version::new(3))?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -49,21 +49,21 @@ impl RelativeExtendedOwnMarketCap {
|
||||
exit,
|
||||
)?;
|
||||
self.unrealized_loss_rel_to_own_market_cap
|
||||
.compute_binary::<Dollars, Dollars, RatioDollarsBp16>(
|
||||
.compute_binary::<Dollars, Dollars, RatioDollarsBp32>(
|
||||
max_from,
|
||||
&unrealized.unrealized_loss.usd.height,
|
||||
own_market_cap,
|
||||
exit,
|
||||
)?;
|
||||
self.neg_unrealized_loss_rel_to_own_market_cap
|
||||
.compute_binary::<Dollars, Dollars, NegRatioDollarsBps16>(
|
||||
.compute_binary::<Dollars, Dollars, NegRatioDollarsBps32>(
|
||||
max_from,
|
||||
&unrealized.unrealized_loss.usd.height,
|
||||
own_market_cap,
|
||||
exit,
|
||||
)?;
|
||||
self.net_unrealized_pnl_rel_to_own_market_cap
|
||||
.compute_binary::<Dollars, Dollars, RatioDollarsBps16>(
|
||||
.compute_binary::<Dollars, Dollars, RatioDollarsBps32>(
|
||||
max_from,
|
||||
&unrealized.net_unrealized_pnl.usd.height,
|
||||
own_market_cap,
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{BasisPoints16, BasisPointsSigned16, Dollars, Height, Version};
|
||||
use brk_types::{BasisPoints16, BasisPointsSigned32, Dollars, Height, Version};
|
||||
use vecdb::{Exit, Rw, StorageMode};
|
||||
|
||||
use crate::internal::{
|
||||
NegRatioDollarsBps16, PercentFromHeight, RatioDollarsBp16, RatioDollarsBps16,
|
||||
NegRatioDollarsBps32, PercentFromHeight, RatioDollarsBp16, RatioDollarsBps32,
|
||||
};
|
||||
|
||||
use crate::distribution::metrics::{ImportConfig, UnrealizedBase};
|
||||
@@ -14,14 +14,13 @@ use crate::distribution::metrics::{ImportConfig, UnrealizedBase};
|
||||
pub struct RelativeExtendedOwnPnl<M: StorageMode = Rw> {
|
||||
pub unrealized_profit_rel_to_own_gross_pnl: PercentFromHeight<BasisPoints16, M>,
|
||||
pub unrealized_loss_rel_to_own_gross_pnl: PercentFromHeight<BasisPoints16, M>,
|
||||
pub neg_unrealized_loss_rel_to_own_gross_pnl: PercentFromHeight<BasisPointsSigned16, M>,
|
||||
pub net_unrealized_pnl_rel_to_own_gross_pnl: PercentFromHeight<BasisPointsSigned16, M>,
|
||||
pub neg_unrealized_loss_rel_to_own_gross_pnl: PercentFromHeight<BasisPointsSigned32, M>,
|
||||
pub net_unrealized_pnl_rel_to_own_gross_pnl: PercentFromHeight<BasisPointsSigned32, M>,
|
||||
}
|
||||
|
||||
impl RelativeExtendedOwnPnl {
|
||||
pub(crate) fn forced_import(cfg: &ImportConfig) -> Result<Self> {
|
||||
let v1 = Version::ONE;
|
||||
let v2 = Version::new(2);
|
||||
|
||||
Ok(Self {
|
||||
unrealized_profit_rel_to_own_gross_pnl: cfg
|
||||
@@ -29,9 +28,9 @@ impl RelativeExtendedOwnPnl {
|
||||
unrealized_loss_rel_to_own_gross_pnl: cfg
|
||||
.import_percent_bp16("unrealized_loss_rel_to_own_gross_pnl", v1)?,
|
||||
neg_unrealized_loss_rel_to_own_gross_pnl: cfg
|
||||
.import_percent_bps16("neg_unrealized_loss_rel_to_own_gross_pnl", v1)?,
|
||||
.import_percent_bps32("neg_unrealized_loss_rel_to_own_gross_pnl", Version::new(2))?,
|
||||
net_unrealized_pnl_rel_to_own_gross_pnl: cfg
|
||||
.import_percent_bps16("net_unrealized_pnl_rel_to_own_gross_pnl", v2)?,
|
||||
.import_percent_bps32("net_unrealized_pnl_rel_to_own_gross_pnl", Version::new(3))?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -56,14 +55,14 @@ impl RelativeExtendedOwnPnl {
|
||||
exit,
|
||||
)?;
|
||||
self.neg_unrealized_loss_rel_to_own_gross_pnl
|
||||
.compute_binary::<Dollars, Dollars, NegRatioDollarsBps16>(
|
||||
.compute_binary::<Dollars, Dollars, NegRatioDollarsBps32>(
|
||||
max_from,
|
||||
&unrealized.unrealized_loss.usd.height,
|
||||
&unrealized.gross_pnl.usd.height,
|
||||
exit,
|
||||
)?;
|
||||
self.net_unrealized_pnl_rel_to_own_gross_pnl
|
||||
.compute_binary::<Dollars, Dollars, RatioDollarsBps16>(
|
||||
.compute_binary::<Dollars, Dollars, RatioDollarsBps32>(
|
||||
max_from,
|
||||
&unrealized.net_unrealized_pnl.usd.height,
|
||||
&unrealized.gross_pnl.usd.height,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
collections::{btree_map::Entry, BTreeMap},
|
||||
fs,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
@@ -9,7 +9,7 @@ use brk_types::{
|
||||
Cents, CentsCompact, CentsSats, CentsSquaredSats, CostBasisDistribution, Height, Sats,
|
||||
};
|
||||
use rustc_hash::FxHashMap;
|
||||
use vecdb::Bytes;
|
||||
use vecdb::{Bytes, unlikely};
|
||||
|
||||
use super::{CachedUnrealizedState, Percentiles, UnrealizedState};
|
||||
|
||||
@@ -187,24 +187,46 @@ impl CostBasisData {
|
||||
self.percentiles_dirty = true;
|
||||
let map = &mut self.state.as_mut().unwrap().base.map;
|
||||
for (cents, (inc, dec)) in self.pending.drain() {
|
||||
let entry = map.entry(cents).or_default();
|
||||
*entry += inc;
|
||||
if *entry < dec {
|
||||
panic!(
|
||||
"CostBasisData::apply_pending underflow!\n\
|
||||
Path: {:?}\n\
|
||||
Price: {}\n\
|
||||
Current + increments: {}\n\
|
||||
Trying to decrement by: {}",
|
||||
self.pathbuf,
|
||||
cents.to_dollars(),
|
||||
entry,
|
||||
dec
|
||||
);
|
||||
}
|
||||
*entry -= dec;
|
||||
if *entry == Sats::ZERO {
|
||||
map.remove(¢s);
|
||||
match map.entry(cents) {
|
||||
Entry::Occupied(mut e) => {
|
||||
*e.get_mut() += inc;
|
||||
if unlikely(*e.get() < dec) {
|
||||
panic!(
|
||||
"CostBasisData::apply_pending underflow!\n\
|
||||
Path: {:?}\n\
|
||||
Price: {}\n\
|
||||
Current + increments: {}\n\
|
||||
Trying to decrement by: {}",
|
||||
self.pathbuf,
|
||||
cents.to_dollars(),
|
||||
e.get(),
|
||||
dec
|
||||
);
|
||||
}
|
||||
*e.get_mut() -= dec;
|
||||
if *e.get() == Sats::ZERO {
|
||||
e.remove();
|
||||
}
|
||||
}
|
||||
Entry::Vacant(e) => {
|
||||
if unlikely(inc < dec) {
|
||||
panic!(
|
||||
"CostBasisData::apply_pending underflow (new entry)!\n\
|
||||
Path: {:?}\n\
|
||||
Price: {}\n\
|
||||
Increment: {}\n\
|
||||
Trying to decrement by: {}",
|
||||
self.pathbuf,
|
||||
cents.to_dollars(),
|
||||
inc,
|
||||
dec
|
||||
);
|
||||
}
|
||||
let val = inc - dec;
|
||||
if val != Sats::ZERO {
|
||||
e.insert(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -213,7 +235,7 @@ impl CostBasisData {
|
||||
state.cap_raw += self.pending_raw.cap_inc;
|
||||
|
||||
// Check for underflow before subtracting
|
||||
if state.cap_raw.inner() < self.pending_raw.cap_dec.inner() {
|
||||
if unlikely(state.cap_raw.inner() < self.pending_raw.cap_dec.inner()) {
|
||||
panic!(
|
||||
"CostBasisData::apply_pending cap_raw underflow!\n\
|
||||
Path: {:?}\n\
|
||||
@@ -231,7 +253,7 @@ impl CostBasisData {
|
||||
if has_investor_cap {
|
||||
state.investor_cap_raw += self.pending_raw.investor_cap_inc;
|
||||
|
||||
if state.investor_cap_raw.inner() < self.pending_raw.investor_cap_dec.inner() {
|
||||
if unlikely(state.investor_cap_raw.inner() < self.pending_raw.investor_cap_dec.inner()) {
|
||||
panic!(
|
||||
"CostBasisData::apply_pending investor_cap_raw underflow!\n\
|
||||
Path: {:?}\n\
|
||||
|
||||
Reference in New Issue
Block a user