refactor(market): extract pure price-series math to data/market_series.h + tests
Move the no-I/O, no-ImGui series helpers (resampleHistory, bucketBySeconds, sparklineSeries, chartSeries) out of market_tab.cpp into an inline header under data/ (mirroring data/portfolio.h) and cover them with a testMarketSeries() group in test_phase4.cpp (resample block averaging + partial blocks, window bucketing + guards, sparkline fallback, chart time-window filtering + live timestamp synthesis). market_tab now calls data::sparklineSeries/chartSeries. No behavior change. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
99
src/data/market_series.h
Normal file
99
src/data/market_series.h
Normal file
@@ -0,0 +1,99 @@
|
||||
// DragonX Wallet - ImGui Edition
|
||||
// Copyright 2024-2026 The Hush Developers
|
||||
// Released under the GPLv3
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include <ctime>
|
||||
|
||||
#include "wallet_state.h" // MarketInfo
|
||||
|
||||
// Pure (no-I/O, no-ImGui) price-series math backing the market chart and the portfolio-group
|
||||
// sparklines. Kept header-only + inline so it can be unit-tested without the GUI (see
|
||||
// tests/test_phase4.cpp), mirroring data/portfolio.h.
|
||||
namespace dragonx {
|
||||
namespace data {
|
||||
|
||||
// Resample a ~1-sample/minute price history to the given interval by averaging each block of K
|
||||
// minute-samples into one point. interval: 0=min 1=hour 2=day 3=week 4=month.
|
||||
inline std::vector<double> resampleHistory(const std::vector<double>& hist, int interval)
|
||||
{
|
||||
static const int kMins[5] = {1, 60, 1440, 10080, 43200};
|
||||
int k = kMins[(interval >= 0 && interval < 5) ? interval : 0];
|
||||
if (k <= 1) return hist;
|
||||
std::vector<double> out;
|
||||
for (size_t i = 0; i < hist.size(); i += (size_t)k) {
|
||||
double sum = 0.0; size_t cnt = 0;
|
||||
for (size_t j = i; j < hist.size() && j < i + (size_t)k; ++j) { sum += hist[j]; ++cnt; }
|
||||
if (cnt) out.push_back(sum / (double)cnt);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Bucket a timestamped (unix-seconds, price) series into fixed-width time windows, averaging the
|
||||
// samples in each window into one point. Input is oldest->newest; output preserves that order.
|
||||
inline std::vector<double> bucketBySeconds(
|
||||
const std::vector<std::pair<std::time_t, double>>& series, long windowSec)
|
||||
{
|
||||
std::vector<double> out;
|
||||
if (series.empty() || windowSec <= 0) return out;
|
||||
long curBucket = 0; double sum = 0.0; int cnt = 0;
|
||||
for (const auto& sample : series) {
|
||||
long b = (long)(sample.first / windowSec);
|
||||
if (cnt > 0 && b != curBucket) { out.push_back(sum / cnt); sum = 0.0; cnt = 0; }
|
||||
curBucket = b;
|
||||
sum += sample.second; ++cnt;
|
||||
}
|
||||
if (cnt > 0) out.push_back(sum / cnt);
|
||||
return out;
|
||||
}
|
||||
|
||||
// Resolve the price series backing a group's sparkline for the chosen interval:
|
||||
// 0=minute -> the live in-session buffer; 1=hour -> intraday (5-min) data bucketed to hours;
|
||||
// 2=day / 3=week / 4=month -> the ~1yr daily series bucketed accordingly.
|
||||
// Falls back to the in-session buffer when the historical fetch hasn't populated yet.
|
||||
inline std::vector<double> sparklineSeries(const MarketInfo& m, int interval)
|
||||
{
|
||||
const long kDay = 86400;
|
||||
switch (interval) {
|
||||
case 1: { auto v = bucketBySeconds(m.price_chart_intraday, 3600); if (v.size() >= 2) return v; break; }
|
||||
case 2: { auto v = bucketBySeconds(m.price_chart_daily, kDay); if (v.size() >= 2) return v; break; }
|
||||
case 3: { auto v = bucketBySeconds(m.price_chart_daily, 7 * kDay); if (v.size() >= 2) return v; break; }
|
||||
case 4: { auto v = bucketBySeconds(m.price_chart_daily, 30 * kDay); if (v.size() >= 2) return v; break; }
|
||||
default: break; // minute interval, or no historical data yet -> live buffer below
|
||||
}
|
||||
return resampleHistory(m.price_history, interval);
|
||||
}
|
||||
|
||||
// Timestamped price series backing the main chart for the selected RANGE (Live/1H/1D/1W/1M),
|
||||
// ending at `now`. The interval buttons select a time window ending at `now`: 1H = last hour,
|
||||
// 1D = last 24h, 1W = last 7 days, 1M = last 30 days. 1H/1D come from the 5-minute intraday
|
||||
// series; 1W/1M from the daily series. Live (or an un-fetched/empty range) uses the minute buffer.
|
||||
inline std::vector<std::pair<std::time_t, double>> chartSeries(const MarketInfo& m, int interval,
|
||||
std::time_t now)
|
||||
{
|
||||
const long kDay = 86400;
|
||||
auto lastWindow = [now](const std::vector<std::pair<std::time_t, double>>& src, long rangeSec) {
|
||||
std::vector<std::pair<std::time_t, double>> out;
|
||||
std::time_t cutoff = now - (std::time_t)rangeSec;
|
||||
for (const auto& s : src) if (s.first >= cutoff) out.push_back(s);
|
||||
return out;
|
||||
};
|
||||
switch (interval) {
|
||||
case 1: { auto v = lastWindow(m.price_chart_intraday, 3600); if (v.size() >= 2) return v; break; } // 1H
|
||||
case 2: { auto v = lastWindow(m.price_chart_intraday, kDay); if (v.size() >= 2) return v; break; } // 1D
|
||||
case 3: { auto v = lastWindow(m.price_chart_daily, 7 * kDay); if (v.size() >= 2) return v; break; } // 1W
|
||||
case 4: { auto v = lastWindow(m.price_chart_daily, 30 * kDay); if (v.size() >= 2) return v; break; } // 1M
|
||||
default: break;
|
||||
}
|
||||
std::vector<std::pair<std::time_t, double>> out;
|
||||
const auto& h = m.price_history;
|
||||
for (size_t i = 0; i < h.size(); i++)
|
||||
out.push_back({ now - (std::time_t)((h.size() - 1 - i) * 60), h[i] });
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace data
|
||||
} // namespace dragonx
|
||||
Reference in New Issue
Block a user