global: snapshot

This commit is contained in:
k
2024-10-04 19:09:09 +02:00
parent 1c9d118ba2
commit 068bb07d6e
57 changed files with 1799 additions and 295 deletions

View File

@@ -2,7 +2,7 @@ use allocative::Allocative;
use crate::{
datasets::AnyDataset,
structs::{AnyHeightMap, Date, HeightMap},
structs::{AnyHeightMap, Date, HeightMap, Timestamp},
};
use super::{InsertData, MinInitialStates};
@@ -13,7 +13,7 @@ pub struct BlockMetadataDataset {
// Inserted
pub date: HeightMap<Date>,
pub timestamp: HeightMap<u32>,
pub timestamp: HeightMap<Timestamp>,
}
impl BlockMetadataDataset {
@@ -41,7 +41,7 @@ impl BlockMetadataDataset {
) {
self.timestamp.insert(height, timestamp);
self.date.insert(height, Date::from_timestamp(timestamp));
self.date.insert(height, timestamp.to_date());
}
}

View File

@@ -302,7 +302,7 @@ impl MiningDataset {
self.block_weight
.insert(height, block_weight as f32 / BYTES_IN_MB as f32);
self.block_vbytes.insert(height, block_vbytes);
self.block_interval.insert(height, block_interval);
self.block_interval.insert(height, *block_interval);
if is_date_last_block {
self.coinbase.date_insert_sum_range(date, date_blocks_range);

View File

@@ -45,7 +45,7 @@ use crate::{
// UTXOCohortsReceivedStates,
UTXOCohortsSentStates,
},
structs::{Amount, Date, Height, Price},
structs::{Amount, Date, Height, Price, Timestamp},
};
pub struct InsertData<'a> {
@@ -53,7 +53,7 @@ pub struct InsertData<'a> {
pub address_cohorts_one_shot_states: &'a Option<AddressCohortsOneShotStates>,
pub address_cohorts_realized_states: &'a Option<AddressCohortsRealizedStates>,
pub amount_sent: Amount,
pub block_interval: u32,
pub block_interval: Timestamp,
pub block_price: Price,
pub block_size: usize,
pub block_vbytes: u64,
@@ -71,7 +71,7 @@ pub struct InsertData<'a> {
pub satblocks_destroyed: Amount,
pub satdays_destroyed: Amount,
pub states: &'a States,
pub timestamp: u32,
pub timestamp: Timestamp,
pub transaction_count: usize,
pub utxo_cohorts_one_shot_states: &'a UTXOCohortsOneShotStates,
// pub utxo_cohorts_received_states: &'a UTXOCohortsReceivedStates,

View File

@@ -3,17 +3,16 @@ mod ohlc;
use std::collections::BTreeMap;
use allocative::Allocative;
use chrono::{Days, NaiveDateTime, NaiveTime, TimeZone, Timelike, Utc};
use chrono::Days;
use color_eyre::eyre::Error;
pub use ohlc::*;
use crate::{
log,
price::{Binance, Kibo, Kraken},
structs::{
AnyBiMap, AnyDateMap, BiMap, Date, DateMap, DateMapChunkId, Height, HeightMapChunkId,
MapKey,
Amount, AnyBiMap, AnyDateMap, BiMap, Date, DateMap, DateMapChunkId, Height,
HeightMapChunkId, MapKey, Timestamp,
},
utils::{ONE_MONTH_IN_DAYS, ONE_WEEK_IN_DAYS, ONE_YEAR_IN_DAYS},
};
@@ -76,9 +75,11 @@ pub struct PriceDatasets {
pub price_10y_total_return: DateMap<f32>,
pub price_4y_compound_return: DateMap<f32>,
// projection via lowest 4y compound value
pub all_time_high: BiMap<f32>,
pub market_price_to_all_time_high_ratio: BiMap<f32>,
pub drawdown: BiMap<f32>,
pub sats_per_dollar: BiMap<f32>,
// volatility
// drawdown
// sats per dollar
}
impl PriceDatasets {
@@ -138,6 +139,13 @@ impl PriceDatasets {
price_8y_total_return: DateMap::new_bin(1, &f("price_8y_total_return")),
price_10y_total_return: DateMap::new_bin(1, &f("price_10y_total_return")),
price_4y_compound_return: DateMap::new_bin(1, &f("price_4y_compound_return")),
all_time_high: BiMap::new_bin(1, &f("all_time_high")),
market_price_to_all_time_high_ratio: BiMap::new_bin(
1,
&f("market_price_to_all_time_high_ratio"),
),
drawdown: BiMap::new_bin(1, &f("drawdown")),
sats_per_dollar: BiMap::new_bin(1, &f("sats_per_dollar")),
};
s.min_initial_states
@@ -306,6 +314,26 @@ impl PriceDatasets {
.compute(compute_data, &mut self.closes, &mut self.price_144d_sma);
self.price_200w_sma_ratio
.compute(compute_data, &mut self.closes, &mut self.price_200w_sma);
self.all_time_high
.multi_insert_max(heights, dates, &mut self.closes);
self.market_price_to_all_time_high_ratio
.multi_insert_percentage(heights, dates, &mut self.closes, &mut self.all_time_high);
self.drawdown.multi_insert_simple_transform(
heights,
dates,
&mut self.market_price_to_all_time_high_ratio,
&|v| -(100.0 - v),
);
self.sats_per_dollar.multi_insert_simple_transform(
heights,
dates,
&mut self.closes,
&|price| Amount::ONE_BTC_F32 / price,
);
}
pub fn get_date_ohlc(&mut self, date: Date) -> color_eyre::Result<OHLC> {
@@ -396,31 +424,20 @@ impl PriceDatasets {
pub fn get_height_ohlc(
&mut self,
height: Height,
timestamp: u32,
previous_timestamp: Option<u32>,
timestamp: Timestamp,
previous_timestamp: Option<Timestamp>,
) -> color_eyre::Result<OHLC> {
if let Some(ohlc) = self.ohlcs.height.get(&height) {
return Ok(ohlc);
}
let clean_timestamp = |timestamp| {
let date_time = Utc.timestamp_opt(i64::from(timestamp), 0).unwrap();
NaiveDateTime::new(
date_time.date_naive(),
NaiveTime::from_hms_opt(date_time.hour(), date_time.minute(), 0).unwrap(),
)
.and_utc()
.timestamp() as u32
};
let timestamp = clean_timestamp(timestamp);
let timestamp = timestamp.to_floored_seconds();
if previous_timestamp.is_none() && !height.is_first() {
panic!("Shouldn't be possible");
}
let previous_timestamp = previous_timestamp.map(clean_timestamp);
let previous_timestamp = previous_timestamp.map(|t| t.to_floored_seconds());
let ohlc = self
.get_from_1mn_kraken(timestamp, previous_timestamp)
@@ -430,7 +447,7 @@ impl PriceDatasets {
self.get_from_har_binance(timestamp, previous_timestamp)
.unwrap_or_else(|_| {
self.get_from_height_kibo(&height).unwrap_or_else(|_| {
let date = Date::from_timestamp(timestamp);
let date = timestamp.to_date();
panic!(
"Can't find the price for: height: {height} - date: {date}
@@ -479,8 +496,8 @@ How to fix this:
fn get_from_1mn_kraken(
&mut self,
timestamp: u32,
previous_timestamp: Option<u32>,
timestamp: Timestamp,
previous_timestamp: Option<Timestamp>,
) -> color_eyre::Result<OHLC> {
if self.kraken_1mn.is_none()
|| self
@@ -500,8 +517,8 @@ How to fix this:
fn get_from_1mn_binance(
&mut self,
timestamp: u32,
previous_timestamp: Option<u32>,
timestamp: Timestamp,
previous_timestamp: Option<Timestamp>,
) -> color_eyre::Result<OHLC> {
if self.binance_1mn.is_none()
|| self
@@ -526,8 +543,8 @@ How to fix this:
fn get_from_har_binance(
&mut self,
timestamp: u32,
previous_timestamp: Option<u32>,
timestamp: Timestamp,
previous_timestamp: Option<Timestamp>,
) -> color_eyre::Result<OHLC> {
if self.binance_har.is_none() {
self.binance_har
@@ -544,8 +561,8 @@ How to fix this:
fn find_height_ohlc(
tree: &Option<BTreeMap<u32, OHLC>>,
timestamp: u32,
previous_timestamp: Option<u32>,
timestamp: Timestamp,
previous_timestamp: Option<Timestamp>,
name: &str,
) -> color_eyre::Result<OHLC> {
let tree = tree.as_ref().unwrap();
@@ -572,12 +589,12 @@ How to fix this:
close: previous_ohlc.close,
};
let start = previous_timestamp.unwrap_or(0);
let start = previous_timestamp.unwrap_or_default();
let end = timestamp;
// Otherwise it's a re-org
if start < end {
tree.range(&start..=&end).skip(1).for_each(|(_, ohlc)| {
tree.range(&*start..=&*end).skip(1).for_each(|(_, ohlc)| {
if ohlc.high > final_ohlc.high {
final_ohlc.high = ohlc.high
}
@@ -624,6 +641,10 @@ impl AnyDataset for PriceDatasets {
&self.price_89d_sma,
&self.price_144d_sma,
&self.price_200w_sma,
&self.all_time_high,
&self.market_price_to_all_time_high_ratio,
&self.drawdown,
&self.sats_per_dollar,
];
v.append(&mut self.price_1w_sma_ratio.to_computed_bi_map_vec());
@@ -660,6 +681,10 @@ impl AnyDataset for PriceDatasets {
&mut self.price_89d_sma,
&mut self.price_144d_sma,
&mut self.price_200w_sma,
&mut self.all_time_high,
&mut self.market_price_to_all_time_high_ratio,
&mut self.drawdown,
&mut self.sats_per_dollar,
];
v.append(&mut self.price_1w_sma_ratio.to_computed_mut_bi_map_vec());

View File

@@ -66,7 +66,7 @@ impl CapitalizationDataset {
let realized_cap = self
.realized_cap
.height
.insert(height, state.realized_cap.to_dollar() as f32);
.insert(height, state.realized_cap().to_dollar() as f32);
if is_date_last_block {
self.realized_cap.date.insert(date, realized_cap);

View File

@@ -51,9 +51,12 @@ impl InputSubDataset {
}: &InsertData,
state: &InputState,
) {
let count = self.count.height.insert(height, state.count.round() as u64);
let count = self
.count
.height
.insert(height, state.count().round() as u64);
self.volume.height.insert(height, state.volume.to_btc());
self.volume.height.insert(height, state.volume().to_btc());
if is_date_last_block {
self.count.date.insert(date, count);

View File

@@ -83,28 +83,25 @@ impl PricePaidSubDataset {
}: &InsertData,
state: &PricePaidState,
) {
let PricePaidState {
pp_05p,
pp_10p,
pp_15p,
pp_20p,
pp_25p,
pp_30p,
pp_35p,
pp_40p,
pp_45p,
pp_median,
pp_55p,
pp_60p,
pp_65p,
pp_70p,
pp_75p,
pp_80p,
pp_85p,
pp_90p,
pp_95p,
..
} = state;
let pp_05p = state.pp_05p();
let pp_10p = state.pp_10p();
let pp_15p = state.pp_15p();
let pp_20p = state.pp_20p();
let pp_25p = state.pp_25p();
let pp_30p = state.pp_30p();
let pp_35p = state.pp_35p();
let pp_40p = state.pp_40p();
let pp_45p = state.pp_45p();
let pp_median = state.pp_median();
let pp_55p = state.pp_55p();
let pp_60p = state.pp_60p();
let pp_65p = state.pp_65p();
let pp_70p = state.pp_70p();
let pp_75p = state.pp_75p();
let pp_80p = state.pp_80p();
let pp_85p = state.pp_85p();
let pp_90p = state.pp_90p();
let pp_95p = state.pp_95p();
// Check if iter was empty
if pp_05p.is_none() {

View File

@@ -15,9 +15,12 @@ pub struct RealizedSubDataset {
// Inserted
realized_profit: BiMap<f32>,
realized_loss: BiMap<f32>,
value_destroyed: BiMap<f32>,
value_created: BiMap<f32>,
adjusted_value_created: BiMap<f32>,
value_destroyed: BiMap<f32>,
adjusted_value_destroyed: BiMap<f32>,
spent_output_profit_ratio: BiMap<f32>,
adjusted_spent_output_profit_ratio: BiMap<f32>,
// Computed
negative_realized_loss: BiMap<f32>,
@@ -47,8 +50,14 @@ impl RealizedSubDataset {
realized_profit: BiMap::new_bin(1, &f("realized_profit")),
realized_loss: BiMap::new_bin(1, &f("realized_loss")),
value_created: BiMap::new_bin(1, &f("value_created")),
adjusted_value_created: BiMap::new_bin(1, &f("adjusted_value_created")),
value_destroyed: BiMap::new_bin(1, &f("value_destroyed")),
adjusted_value_destroyed: BiMap::new_bin(1, &f("adjusted_value_destroyed")),
spent_output_profit_ratio: BiMap::new_bin(2, &f("spent_output_profit_ratio")),
adjusted_spent_output_profit_ratio: BiMap::new_bin(
2,
&f("adjusted_spent_output_profit_ratio"),
),
negative_realized_loss: BiMap::new_bin(2, &f("negative_realized_loss")),
net_realized_profit_and_loss: BiMap::new_bin(1, &f("net_realized_profit_and_loss")),
@@ -89,29 +98,51 @@ impl RealizedSubDataset {
) {
self.realized_profit
.height
.insert(height, height_state.realized_profit.to_dollar() as f32);
.insert(height, height_state.realized_profit().to_dollar() as f32);
self.realized_loss
.height
.insert(height, height_state.realized_loss.to_dollar() as f32);
.insert(height, height_state.realized_loss().to_dollar() as f32);
self.value_created
.height
.insert(height, height_state.value_created.to_dollar() as f32);
.insert(height, height_state.value_created().to_dollar() as f32);
self.adjusted_value_created.height.insert(
height,
height_state.adjusted_value_created().to_dollar() as f32,
);
self.value_destroyed
.height
.insert(height, height_state.value_destroyed.to_dollar() as f32);
.insert(height, height_state.value_destroyed().to_dollar() as f32);
self.adjusted_value_destroyed.height.insert(
height,
height_state.adjusted_value_destroyed().to_dollar() as f32,
);
self.spent_output_profit_ratio.height.insert(height, {
if height_state.value_destroyed > Price::ZERO {
(height_state.value_created.to_cent() as f64
/ height_state.value_destroyed.to_cent() as f64) as f32
if height_state.value_destroyed() > Price::ZERO {
(height_state.value_created().to_cent() as f64
/ height_state.value_destroyed().to_cent() as f64) as f32
} else {
1.0
}
});
self.adjusted_spent_output_profit_ratio
.height
.insert(height, {
if height_state.adjusted_value_destroyed() > Price::ZERO {
(height_state.adjusted_value_created().to_cent() as f64
/ height_state.adjusted_value_destroyed().to_cent() as f64)
as f32
} else {
1.0
}
});
if is_date_last_block {
self.realized_profit
.date_insert_sum_range(date, date_blocks_range);
@@ -122,14 +153,31 @@ impl RealizedSubDataset {
self.value_created
.date_insert_sum_range(date, date_blocks_range);
self.adjusted_value_created
.date_insert_sum_range(date, date_blocks_range);
self.value_destroyed
.date_insert_sum_range(date, date_blocks_range);
self.adjusted_value_destroyed
.date_insert_sum_range(date, date_blocks_range);
self.spent_output_profit_ratio.date.insert(
date,
self.value_created.height.sum_range(date_blocks_range)
/ self.value_destroyed.height.sum_range(date_blocks_range),
);
self.adjusted_spent_output_profit_ratio.date.insert(
date,
self.adjusted_value_created
.height
.sum_range(date_blocks_range)
/ self
.adjusted_value_destroyed
.height
.sum_range(date_blocks_range),
);
}
}
@@ -208,8 +256,11 @@ impl AnyDataset for RealizedSubDataset {
&self.realized_loss,
&self.realized_profit,
&self.value_created,
&self.adjusted_value_created,
&self.value_destroyed,
&self.adjusted_value_destroyed,
&self.spent_output_profit_ratio,
&self.adjusted_spent_output_profit_ratio,
]
}
@@ -218,8 +269,11 @@ impl AnyDataset for RealizedSubDataset {
&mut self.realized_loss,
&mut self.realized_profit,
&mut self.value_created,
&mut self.adjusted_value_created,
&mut self.value_destroyed,
&mut self.adjusted_value_destroyed,
&mut self.spent_output_profit_ratio,
&mut self.adjusted_spent_output_profit_ratio,
]
}

View File

@@ -254,99 +254,3 @@ where
v
}
}
// impl<Key, Value, ChunkId, Serialized> AnyDataset for RecapDataset<Key, Value, ChunkId, Serialized>
// where
// Value: MapValue,
// ChunkId: MapChunkId,
// Key: MapKey<ChunkId>,
// Serialized: MapSerialized<Key, Value, ChunkId>,
// {
// fn get_min_initial_states(&self) -> &MinInitialStates {
// &self.min_initial_states
// }
// fn to_computed_date_map_vec(&self) -> Vec<&(dyn AnyDateMap + Send + Sync)> {
// let mut v: Vec<&(dyn AnyDateMap + Send + Sync)> = vec![];
// if let Some(min) = self.min.as_ref() {
// v.push(min);
// }
// if let Some(max) = self.max.as_ref() {
// v.push(max);
// }
// if let Some(median) = self.median.as_ref() {
// v.push(median);
// }
// if let Some(average) = self.average.as_ref() {
// v.push(average);
// }
// if let Some(sum) = self.sum.as_ref() {
// v.push(sum);
// }
// if let Some(_90p) = self._90p.as_ref() {
// v.push(_90p);
// }
// if let Some(_75p) = self._75p.as_ref() {
// v.push(_75p);
// }
// if let Some(_25p) = self._25p.as_ref() {
// v.push(_25p);
// }
// if let Some(_10p) = self._10p.as_ref() {
// v.push(_10p);
// }
// v
// }
// fn to_computed_mut_date_map_vec(&mut self) -> Vec<&mut dyn AnyDateMap> {
// let mut v: Vec<&mut dyn AnyDateMap> = vec![];
// if let Some(min) = self.min.as_mut() {
// v.push(min);
// }
// if let Some(max) = self.max.as_mut() {
// v.push(max);
// }
// if let Some(median) = self.median.as_mut() {
// v.push(median);
// }
// if let Some(average) = self.average.as_mut() {
// v.push(average);
// }
// if let Some(sum) = self.sum.as_mut() {
// v.push(sum);
// }
// if let Some(_90p) = self._90p.as_mut() {
// v.push(_90p);
// }
// if let Some(_75p) = self._75p.as_mut() {
// v.push(_75p);
// }
// if let Some(_25p) = self._25p.as_mut() {
// v.push(_25p);
// }
// if let Some(_10p) = self._10p.as_mut() {
// v.push(_10p);
// }
// v
// }
// }

View File

@@ -60,7 +60,7 @@ impl SupplySubDataset {
}: &InsertData,
state: &SupplyState,
) {
let total_supply = self.supply.height.insert(height, state.supply.to_btc());
let total_supply = self.supply.height.insert(height, state.supply().to_btc());
if is_date_last_block {
self.supply.date.insert(date, total_supply);

View File

@@ -86,30 +86,30 @@ impl UnrealizedSubDataset {
) {
self.supply_in_profit
.height
.insert(height, block_state.supply_in_profit.to_btc());
.insert(height, block_state.supply_in_profit().to_btc());
self.unrealized_profit
.height
.insert(height, block_state.unrealized_profit.to_dollar() as f32);
.insert(height, block_state.unrealized_profit().to_dollar() as f32);
self.unrealized_loss
.height
.insert(height, block_state.unrealized_loss.to_dollar() as f32);
.insert(height, block_state.unrealized_loss().to_dollar() as f32);
if is_date_last_block {
let date_state = date_state.as_ref().unwrap();
self.supply_in_profit
.date
.insert(date, date_state.supply_in_profit.to_btc());
.insert(date, date_state.supply_in_profit().to_btc());
self.unrealized_profit
.date
.insert(date, date_state.unrealized_profit.to_dollar() as f32);
.insert(date, date_state.unrealized_profit().to_dollar() as f32);
self.unrealized_loss
.date
.insert(date, date_state.unrealized_loss.to_dollar() as f32);
.insert(date, date_state.unrealized_loss().to_dollar() as f32);
}
}

View File

@@ -46,7 +46,7 @@ impl UTXOSubDataset {
}: &InsertData,
state: &UTXOState,
) {
let count = self.count.height.insert(height, state.count);
let count = self.count.height.insert(height, state.count());
if is_date_last_block {
self.count.date.insert(date, count);