borsa-core 0.1.0

Core types, connector traits, and time-series utilities for the borsa financial data ecosystem.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
use chrono::Datelike;
use chrono::offset::Offset;
use chrono::{DateTime, NaiveDate, TimeZone, Timelike, Utc};
use paft::market::responses::history::{Candle, HistoryMeta};
use std::convert::TryFrom;
// For resolving local times around DST transitions
use chrono::offset::LocalResult;

const DAY: i64 = 86_400;

const fn week_start_day(day: i64) -> i64 {
    day - ((day + 3).rem_euclid(7))
}

const fn week_start_ts(ts: DateTime<Utc>) -> Option<DateTime<Utc>> {
    let day = ts.timestamp().div_euclid(DAY);
    let ws = week_start_day(day);
    DateTime::from_timestamp(ws * DAY, 0)
}

/// Generic resampler that groups sorted candles by a bucket function and
/// aggregates OHLCV within each bucket.
use crate::BorsaError;

fn resample_by<F>(mut candles: Vec<Candle>, bucket_of: F) -> Result<Vec<Candle>, BorsaError>
where
    F: Fn(DateTime<Utc>) -> Option<DateTime<Utc>>,
{
    if candles.is_empty() {
        return Ok(candles);
    }

    candles.sort_by_key(|c| c.ts);

    let mut out: Vec<Candle> = Vec::new();
    let mut series_currency: Option<paft::money::Currency> = None;

    let mut iter = candles.into_iter();
    let Some(first) = iter.find(|c| bucket_of(c.ts).is_some()) else {
        return Ok(Vec::new());
    };
    let mut cur_bucket = bucket_of(first.ts).unwrap();
    let mut open = first.open;
    let mut high = first.high;
    let mut low = first.low;
    let mut close = first.close;
    let mut vol_sum = first.volume.map(u128::from);

    for c in iter {
        let Some(bucket) = bucket_of(c.ts) else {
            continue;
        };
        if bucket == cur_bucket {
            if !(c.open.currency() == high.currency()
                && c.open.currency() == low.currency()
                && c.open.currency() == close.currency())
            {
                return Err(BorsaError::Data(format!(
                    "Mixed currencies in resample bucket at {}: open={:?} high={:?} low={:?} close={:?}",
                    cur_bucket,
                    c.open.currency(),
                    high.currency(),
                    low.currency(),
                    close.currency()
                )));
            }
            if c.high.amount() > high.amount() {
                high = c.high;
            }
            if c.low.amount() < low.amount() {
                low = c.low;
            }
            close = c.close;
            if let Some(v) = c.volume {
                vol_sum = Some(vol_sum.unwrap_or(0) + u128::from(v));
            }
        } else {
            finalize_bucket(
                &mut out,
                &mut series_currency,
                cur_bucket,
                BucketAgg {
                    open,
                    high,
                    low,
                    close,
                    vol_sum,
                },
            )?;
            cur_bucket = bucket;
            open = c.open;
            high = c.high;
            low = c.low;
            close = c.close;
            vol_sum = c.volume.map(u128::from);
        }
    }

    finalize_bucket(
        &mut out,
        &mut series_currency,
        cur_bucket,
        BucketAgg {
            open,
            high,
            low,
            close,
            vol_sum,
        },
    )?;

    Ok(out)
}

struct BucketAgg {
    open: paft::money::Money,
    high: paft::money::Money,
    low: paft::money::Money,
    close: paft::money::Money,
    vol_sum: Option<u128>,
}

fn finalize_bucket(
    out: &mut Vec<Candle>,
    series_currency: &mut Option<paft::money::Currency>,
    cur_bucket: DateTime<Utc>,
    agg: BucketAgg,
) -> Result<(), BorsaError> {
    if !(agg.open.currency() == agg.high.currency()
        && agg.open.currency() == agg.low.currency()
        && agg.open.currency() == agg.close.currency())
    {
        return Err(BorsaError::Data(format!(
            "Mixed currencies in resample bucket (finalize) at {}: open={:?} high={:?} low={:?} close={:?}",
            cur_bucket,
            agg.open.currency(),
            agg.high.currency(),
            agg.low.currency(),
            agg.close.currency()
        )));
    }
    if let Some(cur) = series_currency {
        if cur != agg.open.currency() {
            return Err(BorsaError::Data(format!(
                "Mixed currencies across resampled series at {}: expected {:?}, got {:?}",
                cur_bucket,
                cur,
                agg.open.currency()
            )));
        }
    } else if out.is_empty() {
        *series_currency = Some(agg.open.currency().clone());
    }
    out.push(Candle {
        ts: cur_bucket,
        open: agg.open,
        high: agg.high,
        low: agg.low,
        close: agg.close,
        close_unadj: None,
        volume: agg
            .vol_sum
            .and_then(|v| u64::try_from(v.min(u128::from(u64::MAX))).ok()),
    });
    Ok(())
}

const fn bucket_day_with_offset(ts: DateTime<Utc>, offset_seconds: i64) -> Option<DateTime<Utc>> {
    let shifted = ts.timestamp() + offset_seconds;
    let day = shifted.div_euclid(DAY);
    let local_day_start = day * DAY - offset_seconds;
    DateTime::from_timestamp(local_day_start, 0)
}

const fn bucket_week_monday_with_offset(
    ts: DateTime<Utc>,
    offset_seconds: i64,
) -> Option<DateTime<Utc>> {
    let shifted_day = (ts.timestamp() + offset_seconds).div_euclid(DAY);
    let ws = week_start_day(shifted_day);
    let local_week_start = ws * DAY - offset_seconds;
    DateTime::from_timestamp(local_week_start, 0)
}

const fn bucket_minutes_with_offset(
    ts: DateTime<Utc>,
    minutes: i64,
    offset_seconds: i64,
) -> Option<DateTime<Utc>> {
    let step = minutes * 60;
    let shifted = ts.timestamp() + offset_seconds;
    let bucket = shifted - shifted.rem_euclid(step);
    let back = bucket - offset_seconds;
    DateTime::from_timestamp(back, 0)
}

fn local_midnight_utc_for_date(
    ts: DateTime<Utc>,
    date: NaiveDate,
    tz: chrono_tz::Tz,
) -> Option<DateTime<Utc>> {
    let naive_midnight = date.and_hms_opt(0, 0, 0)?;
    match tz.from_local_datetime(&naive_midnight) {
        LocalResult::Single(dt) => Some(dt.with_timezone(&Utc)),
        LocalResult::Ambiguous(dt1, _) => Some(dt1.with_timezone(&Utc)),
        LocalResult::None => {
            // Fallback: use UTC day start as a conservative default
            let day = ts.timestamp().div_euclid(DAY);
            DateTime::from_timestamp(day * DAY, 0)
        }
    }
}

fn bucket_day_with_tz(ts: DateTime<Utc>, tz: chrono_tz::Tz) -> Option<DateTime<Utc>> {
    let local = ts.with_timezone(&tz);
    let date = local.date_naive();
    local_midnight_utc_for_date(ts, date, tz)
}

fn bucket_week_monday_with_tz(ts: DateTime<Utc>, tz: chrono_tz::Tz) -> Option<DateTime<Utc>> {
    let local = ts.with_timezone(&tz);
    let date = local.date_naive();
    let days_from_monday = i64::from(local.weekday().num_days_from_monday());
    let week_start_date = date
        .checked_sub_signed(chrono::Duration::days(days_from_monday))
        .unwrap_or(date);
    local_midnight_utc_for_date(ts, week_start_date, tz)
}

fn bucket_minutes_with_tz(
    ts: DateTime<Utc>,
    minutes: i64,
    tz: chrono_tz::Tz,
) -> Option<DateTime<Utc>> {
    let step = minutes * 60;
    let local = ts.with_timezone(&tz);
    let date = local.date_naive();
    let seconds_since_midnight = i64::from(local.num_seconds_from_midnight());
    let bucket_sec = seconds_since_midnight - seconds_since_midnight.rem_euclid(step);
    // Build local midnight and add bucket offset in local time, then convert to UTC
    let naive_midnight = date.and_hms_opt(0, 0, 0)?;
    let local_bucket_naive = naive_midnight + chrono::Duration::seconds(bucket_sec);
    match tz.from_local_datetime(&local_bucket_naive) {
        LocalResult::Single(dt) => Some(dt.with_timezone(&Utc)),
        LocalResult::Ambiguous(dt1, dt2) => {
            // Choose the ambiguous mapping that matches the offset of the original ts in this tz.
            // This preserves distinct buckets across the fall-back overlap hour.
            let local_offset = local.offset().fix().local_minus_utc();
            let dt1_offset = dt1.offset().fix().local_minus_utc();
            if dt1_offset == local_offset {
                Some(dt1.with_timezone(&Utc))
            } else {
                Some(dt2.with_timezone(&Utc))
            }
        }
        LocalResult::None => {
            // Fallback: drop to nearest UTC bucket as conservative behavior
            let bucket = ts.timestamp() - ts.timestamp().rem_euclid(step);
            DateTime::from_timestamp(bucket, 0)
        }
    }
}

fn choose_bucket_day(ts: DateTime<Utc>, meta: Option<&HistoryMeta>) -> Option<DateTime<Utc>> {
    if let Some(m) = meta {
        if let Some(tz) = m.timezone {
            return bucket_day_with_tz(ts, tz);
        }
        if let Some(off) = m.utc_offset_seconds {
            return bucket_day_with_offset(ts, off);
        }
    }
    let day = ts.timestamp().div_euclid(DAY);
    DateTime::from_timestamp(day * DAY, 0)
}

fn choose_bucket_week(ts: DateTime<Utc>, meta: Option<&HistoryMeta>) -> Option<DateTime<Utc>> {
    if let Some(m) = meta {
        if let Some(tz) = m.timezone {
            return bucket_week_monday_with_tz(ts, tz);
        }
        if let Some(off) = m.utc_offset_seconds {
            return bucket_week_monday_with_offset(ts, off);
        }
    }
    week_start_ts(ts)
}

fn choose_bucket_minutes(
    ts: DateTime<Utc>,
    minutes: i64,
    meta: Option<&HistoryMeta>,
) -> Option<DateTime<Utc>> {
    if let Some(m) = meta {
        if let Some(tz) = m.timezone {
            return bucket_minutes_with_tz(ts, minutes, tz);
        }
        if let Some(off) = m.utc_offset_seconds {
            return bucket_minutes_with_offset(ts, minutes, off);
        }
    }
    let step = minutes * 60;
    let bucket = ts.timestamp() - (ts.timestamp().rem_euclid(step));
    DateTime::from_timestamp(bucket, 0)
}

/// Resample arbitrary-interval candles into UTC daily OHLCV.
///
/// - Groups by UTC day (floor(ts / `86_400`)).
/// - Open = first open of the day (earliest ts)
/// - High = max high
/// - Low  = min low
/// - Close = last close of the day (latest ts)
/// - Volume = sum of volumes (ignores `None`; if all `None`, result is `None`)
/// - Output candles have `ts` at the **day start** (00:00:00 UTC).
///
/// # Errors
/// Returns `Err(BorsaError::Data)` if mixed currencies are detected within a
/// bucket or across the resampled output series. All OHLC in every bucket must
/// share the same currency, and all buckets in the output series must share a
/// single currency.
///
/// ```
/// use borsa_core::{resample_to_daily, Candle, Money, Currency, IsoCurrency};
/// use chrono::{DateTime, Utc};
/// use rust_decimal::Decimal;
/// fn t(sec: i64) -> DateTime<Utc> { DateTime::from_timestamp(sec, 0).unwrap() }
/// fn m(v: f64, usd: bool) -> Money { Money::new(Decimal::from_f64_retain(v).unwrap(), if usd { Currency::Iso(IsoCurrency::USD) } else { Currency::Iso(IsoCurrency::EUR) }).unwrap() }
/// let c = |ts: i64, usd: bool| Candle { ts: t(ts), open: m(1.0, usd), high: m(1.0, usd), low: m(1.0, usd), close: m(1.0, usd), close_unadj: None, volume: None };
/// // Two different days with different currencies → panic on finalize of second bucket
/// let res = resample_to_daily(vec![c(0, true), c(86_400, false)]);
/// assert!(res.is_err());
/// ```
pub fn resample_to_daily(candles: Vec<Candle>) -> Result<Vec<Candle>, BorsaError> {
    resample_by(candles, |ts| {
        let day = ts.timestamp().div_euclid(DAY);
        DateTime::from_timestamp(day * DAY, 0)
    })
}

/// Resample to daily buckets using `HistoryMeta` timezone/offset when provided.
///
/// # Errors
/// Returns `Err(BorsaError::Data)` if mixed currencies are detected within a
/// bucket or across the resampled output series.
pub fn resample_to_daily_with_meta(
    candles: Vec<Candle>,
    meta: Option<&HistoryMeta>,
) -> Result<Vec<Candle>, BorsaError> {
    resample_by(candles, move |ts| choose_bucket_day(ts, meta))
}

/// Resample arbitrary-interval candles into UTC weekly OHLCV.
///
/// Weeks start Monday 00:00 UTC.
///
/// - Group key is the Monday-start timestamp: floor(ts / `86_400`) -> day,
///   `week_start_day` = day - ((day + 3) % 7), since 1970-01-01 is Thursday.
///   The output candle `ts` is `week_start_day * 86_400`.
/// - Open  = first open of the week (earliest ts)
/// - High  = max high
/// - Low   = min low
/// - Close = last close of the week (latest ts)
/// - Volume = sum of volumes (ignores `None`; if all `None`, result is `None`)
/// # Errors
/// Returns `Err(BorsaError::Data)` if mixed currencies are detected within a
/// bucket or across the resampled output series.
///
/// ```
/// use borsa_core::{resample_to_weekly, Candle, Money, Currency, IsoCurrency};
/// use chrono::{DateTime, Utc};
/// use rust_decimal::Decimal;
/// fn t(sec: i64) -> DateTime<Utc> { DateTime::from_timestamp(sec, 0).unwrap() }
/// fn m(v: f64, usd: bool) -> Money { Money::new(Decimal::from_f64_retain(v).unwrap(), if usd { Currency::Iso(IsoCurrency::USD) } else { Currency::Iso(IsoCurrency::EUR) }).unwrap() }
/// let c = |ts: i64, usd: bool| Candle { ts: t(ts), open: m(1.0, usd), high: m(1.0, usd), low: m(1.0, usd), close: m(1.0, usd), close_unadj: None, volume: None };
/// // Different weeks and currencies → panic
/// let res = resample_to_weekly(vec![c(0, true), c(7*86_400, false)]);
/// assert!(res.is_err());
/// ```
pub fn resample_to_weekly(candles: Vec<Candle>) -> Result<Vec<Candle>, BorsaError> {
    resample_by(candles, week_start_ts)
}

/// Resample to weekly buckets (Monday start) in market local time using `HistoryMeta` when provided.
///
/// # Errors
/// Returns `Err(BorsaError::Data)` if mixed currencies are detected within a
/// bucket or across the resampled output series.
pub fn resample_to_weekly_with_meta(
    candles: Vec<Candle>,
    meta: Option<&HistoryMeta>,
) -> Result<Vec<Candle>, BorsaError> {
    resample_by(candles, move |ts| choose_bucket_week(ts, meta))
}

/// Resample subdaily candles to an arbitrary minute bucket (e.g., 2m, 90m).
///
/// Assumes input is subdaily. Candles are grouped by `floor(ts / (minutes*60))`.
///
/// # Errors
/// Returns `Err(BorsaError::Data)` if mixed currencies are detected within a
/// bucket or across the resampled output series.
///
/// ```
/// use borsa_core::{resample_to_minutes, Candle, Money, Currency, IsoCurrency};
/// use chrono::{DateTime, Utc};
/// use rust_decimal::Decimal;
/// fn t(sec: i64) -> DateTime<Utc> { DateTime::from_timestamp(sec, 0).unwrap() }
/// fn m(v: f64, usd: bool) -> Money { Money::new(Decimal::from_f64_retain(v).unwrap(), if usd { Currency::Iso(IsoCurrency::USD) } else { Currency::Iso(IsoCurrency::EUR) }).unwrap() }
/// let c = |ts: i64, usd: bool| Candle { ts: t(ts), open: m(1.0, usd), high: m(1.0, usd), low: m(1.0, usd), close: m(1.0, usd), close_unadj: None, volume: None };
/// // Two minute buckets, different currencies → panic
/// let res = resample_to_minutes(vec![c(0, true), c(120, false)], 1);
/// assert!(res.is_err());
/// ```
pub fn resample_to_minutes(candles: Vec<Candle>, minutes: i64) -> Result<Vec<Candle>, BorsaError> {
    if candles.is_empty() || minutes <= 0 {
        return Ok(candles);
    }
    let step = minutes * 60;
    resample_by(candles, move |ts| {
        let bucket = ts.timestamp() - (ts.timestamp().rem_euclid(step));
        DateTime::from_timestamp(bucket, 0)
    })
}

/// Resample to minute buckets using market local time when `HistoryMeta` is provided.
///
/// # Errors
/// Returns `Err(BorsaError::Data)` if mixed currencies are detected within a
/// bucket or across the resampled output series.
pub fn resample_to_minutes_with_meta(
    candles: Vec<Candle>,
    minutes: i64,
    meta: Option<&HistoryMeta>,
) -> Result<Vec<Candle>, BorsaError> {
    if candles.is_empty() || minutes <= 0 {
        return Ok(candles);
    }
    resample_by(candles, move |ts| choose_bucket_minutes(ts, minutes, meta))
}