fin-stream 2.4.2

Real-time market data streaming primitives — 100K+ ticks/second ingestion pipeline
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
//! Integration tests: cross-module pipelines and end-to-end scenarios.

use fin_stream::book::{BookDelta, BookSide, OrderBook};
use fin_stream::health::HealthMonitor;
use fin_stream::lorentz::{LorentzTransform, SpacetimePoint};
use fin_stream::norm::MinMaxNormalizer;
use fin_stream::ohlcv::{OhlcvAggregator, Timeframe};
use fin_stream::ring::SpscRing;
use fin_stream::session::{MarketSession, SessionAwareness, TradingStatus};
use fin_stream::tick::{Exchange, NormalizedTick, RawTick, TickNormalizer, TradeSide};
use fin_stream::ws::{ConnectionConfig, ReconnectPolicy, WsManager};
use fin_stream::StreamError;
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serde_json::json;
use std::str::FromStr;
use std::thread;
use std::time::Duration;

// ── Tick normalizer end-to-end ───────────────────────────────────────────────

#[test]
fn test_full_binance_pipeline() {
    let raw = RawTick {
        exchange: Exchange::Binance,
        symbol: "BTCUSDT".into(),
        payload: json!({ "p": "65000.50", "q": "0.002", "m": false, "t": 9999u64 }),
        received_at_ms: 1700000000000,
    };
    let tick = TickNormalizer::new().normalize(raw).unwrap();
    assert_eq!(tick.exchange, Exchange::Binance);
    assert_eq!(tick.price, Decimal::from_str("65000.50").unwrap());
    assert_eq!(tick.side, Some(TradeSide::Buy));
}

#[test]
fn test_all_exchanges_normalize_successfully() {
    let normalizer = TickNormalizer::new();
    let cases = vec![
        (
            Exchange::Binance,
            json!({ "p": "100", "q": "1", "m": true }),
        ),
        (
            Exchange::Coinbase,
            json!({ "price": "100", "size": "1", "side": "buy" }),
        ),
        (Exchange::Alpaca, json!({ "p": "100", "s": "1" })),
        (Exchange::Polygon, json!({ "p": "100", "s": "1" })),
    ];
    for (exchange, payload) in cases {
        let raw = RawTick {
            exchange,
            symbol: "SYM".into(),
            payload,
            received_at_ms: 0,
        };
        let result = normalizer.normalize(raw);
        assert!(
            result.is_ok(),
            "exchange {:?} failed: {:?}",
            exchange,
            result
        );
    }
}

// ── Order book pipeline ──────────────────────────────────────────────────────

#[test]
fn test_order_book_full_lifecycle() {
    let mut book = OrderBook::new("BTC-USD");
    book.reset(
        vec![
            fin_stream::book::PriceLevel::new(dec!(50000), dec!(5)),
            fin_stream::book::PriceLevel::new(dec!(49900), dec!(3)),
        ],
        vec![
            fin_stream::book::PriceLevel::new(dec!(50100), dec!(2)),
            fin_stream::book::PriceLevel::new(dec!(50200), dec!(1)),
        ],
    )
    .unwrap();
    assert_eq!(book.bid_depth(), 2);
    assert_eq!(book.ask_depth(), 2);
    book.apply(BookDelta::new(
        "BTC-USD",
        BookSide::Bid,
        dec!(50000),
        dec!(10),
    ))
    .unwrap();
    assert_eq!(book.best_bid().unwrap().quantity, dec!(10));
    book.apply(BookDelta::new(
        "BTC-USD",
        BookSide::Ask,
        dec!(50200),
        dec!(0),
    ))
    .unwrap();
    assert_eq!(book.ask_depth(), 1);
    assert_eq!(book.spread().unwrap(), dec!(100));
}

#[test]
fn test_order_book_top_levels() {
    let mut book = OrderBook::new("BTC-USD");
    for i in 0u32..5 {
        let price = dec!(50000) - Decimal::from(i * 100);
        book.apply(BookDelta::new("BTC-USD", BookSide::Bid, price, dec!(1)))
            .unwrap();
        let ask_price = dec!(50100) + Decimal::from(i * 100);
        book.apply(BookDelta::new("BTC-USD", BookSide::Ask, ask_price, dec!(1)))
            .unwrap();
    }
    let top3_bids = book.top_bids(3);
    assert_eq!(top3_bids.len(), 3);
    assert!(top3_bids[0].price > top3_bids[1].price);
}

// ── OHLCV aggregation ────────────────────────────────────────────────────────

fn make_tick(symbol: &str, price: Decimal, qty: Decimal, ts_ms: u64) -> NormalizedTick {
    NormalizedTick {
        exchange: Exchange::Binance,
        symbol: symbol.to_string(),
        price,
        quantity: qty,
        side: None,
        trade_id: None,
        exchange_ts_ms: None,
        received_at_ms: ts_ms,
    }
}

#[test]
fn test_ohlcv_multi_bar_sequence() {
    let mut agg = OhlcvAggregator::new("BTC-USD", Timeframe::Minutes(1)).unwrap();
    agg.feed(&make_tick("BTC-USD", dec!(50000), dec!(1), 60_000))
        .unwrap();
    agg.feed(&make_tick("BTC-USD", dec!(50500), dec!(2), 60_500))
        .unwrap();
    let mut bars = agg
        .feed(&make_tick("BTC-USD", dec!(51000), dec!(1), 120_000))
        .unwrap();
    assert_eq!(bars.len(), 1);
    let completed = bars.remove(0);
    assert!(completed.is_complete);
    assert_eq!(completed.open, dec!(50000));
    assert_eq!(completed.close, dec!(50500));
    assert_eq!(completed.high, dec!(50500));
    assert_eq!(completed.volume, dec!(3));
    let current = agg.current_bar().unwrap();
    assert_eq!(current.open, dec!(51000));
}

#[test]
fn test_ohlcv_flush_returns_partial_bar() {
    let mut agg = OhlcvAggregator::new("ETH-USD", Timeframe::Hours(1)).unwrap();
    agg.feed(&make_tick("ETH-USD", dec!(3000), dec!(5), 3_600_000))
        .unwrap();
    agg.feed(&make_tick("ETH-USD", dec!(3100), dec!(3), 3_601_000))
        .unwrap();
    let flushed = agg.flush().unwrap();
    assert!(flushed.is_complete);
    assert_eq!(flushed.trade_count, 2);
    assert_eq!(flushed.volume, dec!(8));
}

// ── Health monitor ───────────────────────────────────────────────────────────

#[test]
fn test_health_monitor_multi_feed_scenario() {
    let monitor = HealthMonitor::new(5_000);
    monitor.register("BTC-USD", None);
    monitor.register("ETH-USD", Some(2_000));

    monitor.heartbeat("BTC-USD", 1_000_000).unwrap();
    monitor.heartbeat("ETH-USD", 1_000_000).unwrap();

    let errors = monitor.check_all(1_003_000);
    assert_eq!(errors.len(), 1);
    assert!(errors[0].1.to_string().contains("ETH-USD"));

    assert_eq!(monitor.healthy_count(), 1);
    assert_eq!(monitor.stale_count(), 1);
}

// ── Session awareness ────────────────────────────────────────────────────────

const SAT_UTC_MS: u64 = 1705147200000; // 2024-01-13 12:00 UTC = 07:00 EST (Saturday in ET)
const MON_OPEN_UTC_MS: u64 = 1704724200000; // Mon Jan 08 2024 14:30 UTC = 09:30 ET

#[test]
fn test_session_crypto_always_open_any_time() {
    let sa = SessionAwareness::new(MarketSession::Crypto);
    for ts in [0u64, 1_000_000, 1_700_000_000_000, SAT_UTC_MS] {
        assert_eq!(sa.status(ts).unwrap(), TradingStatus::Open);
    }
}

#[test]
fn test_session_us_equity_weekend_closed() {
    let sa = SessionAwareness::new(MarketSession::UsEquity);
    assert_eq!(sa.status(SAT_UTC_MS).unwrap(), TradingStatus::Closed);
}

#[test]
fn test_session_us_equity_open_during_market_hours() {
    let sa = SessionAwareness::new(MarketSession::UsEquity);
    assert_eq!(sa.status(MON_OPEN_UTC_MS).unwrap(), TradingStatus::Open);
}

// ── WsManager / reconnect ────────────────────────────────────────────────────

#[test]
fn test_ws_manager_reconnect_policy_integration() {
    let policy =
        ReconnectPolicy::new(4, Duration::from_millis(50), Duration::from_secs(5), 2.0).unwrap();
    let config = ConnectionConfig::new("wss://feed.example.com/ws", 512)
        .unwrap()
        .with_reconnect(policy);
    let mut mgr = WsManager::new(config);

    mgr.connect_simulated();
    assert!(mgr.is_connected());

    mgr.disconnect_simulated();
    assert!(!mgr.is_connected());

    let b0 = mgr.next_reconnect_backoff().unwrap();
    let b1 = mgr.next_reconnect_backoff().unwrap();
    let b2 = mgr.next_reconnect_backoff().unwrap();
    assert!(b1 >= b0);
    assert!(b2 >= b1);

    let result = mgr.next_reconnect_backoff();
    assert!(matches!(
        result,
        Err(StreamError::ReconnectExhausted { .. })
    ));
}

// ── Cross-module: tick -> OHLCV pipeline ────────────────────────────────────

#[test]
fn test_tick_to_ohlcv_end_to_end() {
    let normalizer = TickNormalizer::new();
    let mut agg = OhlcvAggregator::new("BTCUSDT", Timeframe::Seconds(30)).unwrap();

    let base_ts = 30_000u64;
    let payloads = vec![
        (dec!(50000), dec!(1)),
        (dec!(50100), dec!(2)),
        (dec!(49900), dec!(0.5)),
    ];

    for (i, (price, qty)) in payloads.iter().enumerate() {
        let raw = RawTick {
            exchange: Exchange::Binance,
            symbol: "BTCUSDT".into(),
            payload: json!({ "p": price.to_string(), "q": qty.to_string(), "m": false }),
            received_at_ms: base_ts + i as u64 * 100,
        };
        let tick = normalizer.normalize(raw).unwrap();
        agg.feed(&tick).unwrap();
    }

    let bar = agg.current_bar().unwrap();
    assert_eq!(bar.open, dec!(50000));
    assert_eq!(bar.high, dec!(50100));
    assert_eq!(bar.low, dec!(49900));
    assert_eq!(bar.close, dec!(49900));
    assert_eq!(bar.trade_count, 3);
}

// ── SPSC ring buffer pipeline tests ─────────────────────────────────────────

/// Feed ticks through an SPSC ring buffer and verify no items are lost.
#[test]
fn test_ring_buffer_tick_pipeline() {
    let ring: SpscRing<NormalizedTick, 64> = SpscRing::new();
    let normalizer = TickNormalizer::new();

    for i in 0..10u64 {
        let raw = RawTick {
            exchange: Exchange::Binance,
            symbol: "BTCUSDT".into(),
            payload: json!({ "p": "50000", "q": "1", "m": false }),
            received_at_ms: i * 100,
        };
        let tick = normalizer.normalize(raw).unwrap();
        ring.push(tick).unwrap();
    }

    let mut count = 0;
    while let Ok(_tick) = ring.pop() {
        count += 1;
    }
    assert_eq!(count, 10);
}

/// Ring buffer full condition: pushing beyond capacity returns RingBufferFull.
#[test]
fn test_ring_buffer_full_error_propagation() {
    let ring: SpscRing<u32, 4> = SpscRing::new(); // capacity = 3
    ring.push(1).unwrap();
    ring.push(2).unwrap();
    ring.push(3).unwrap();
    let err = ring.push(4).unwrap_err();
    assert!(matches!(err, StreamError::RingBufferFull { .. }));
}

/// Ring buffer empty condition: popping from empty returns RingBufferEmpty.
#[test]
fn test_ring_buffer_empty_error_propagation() {
    let ring: SpscRing<u32, 8> = SpscRing::new();
    let err = ring.pop().unwrap_err();
    assert!(matches!(err, StreamError::RingBufferEmpty));
}

// ── End-to-end: tick -> OHLCV -> normalized pipeline ─────────────────────────

/// Full pipeline: ingest ticks via the ring buffer, aggregate to OHLCV, then
/// normalize the close price with a rolling window.
#[test]
fn test_tick_to_ohlcv_to_normalized_pipeline() {
    let ring: SpscRing<NormalizedTick, 32> = SpscRing::new();
    let (prod, cons) = ring.split();

    // Produce 5 ticks in the same 1-minute window.
    let prices: &[f64] = &[100.0, 102.0, 98.0, 103.0, 101.0];
    for (i, &p) in prices.iter().enumerate() {
        let tick = NormalizedTick {
            exchange: Exchange::Binance,
            symbol: "BTC-USD".into(),
            price: Decimal::try_from(p).unwrap(),
            quantity: dec!(1),
            side: None,
            trade_id: None,
            exchange_ts_ms: None,
            received_at_ms: 60_000 + i as u64 * 100,
        };
        prod.push(tick).unwrap();
    }

    // Consume and aggregate.
    let mut agg = OhlcvAggregator::new("BTC-USD", Timeframe::Minutes(1)).unwrap();
    while let Ok(tick) = cons.pop() {
        agg.feed(&tick).unwrap();
    }

    let bar = agg.current_bar().unwrap();
    assert_eq!(bar.trade_count, 5);

    // Normalize the close price.
    let mut norm = MinMaxNormalizer::new(10).unwrap();
    for &p in prices {
        norm.update(Decimal::try_from(p).unwrap());
    }
    let normalized = norm.normalize(bar.close).unwrap();
    assert!((0.0..=1.0).contains(&normalized));
}

// ── Lorentz transform pipeline integration ───────────────────────────────────

/// Verify that Lorentz-transformed OHLCV timestamps round-trip correctly.
#[test]
fn test_lorentz_transform_on_ohlcv_timestamps() {
    let lt = LorentzTransform::new(0.5).unwrap();
    let bar_starts: &[f64] = &[60_000.0, 120_000.0, 180_000.0];

    for &t in bar_starts {
        let p = SpacetimePoint::new(t, 50_000.0);
        let transformed = lt.transform(p);
        let recovered = lt.inverse_transform(transformed);
        assert!(
            (recovered.t - t).abs() < 1e-6,
            "round-trip failed for t={t}"
        );
    }
}

/// Pipeline: normalize prices, then apply Lorentz transform to (time, price).
#[test]
fn test_normalize_then_lorentz_pipeline() {
    let mut norm = MinMaxNormalizer::new(5).unwrap();
    let prices: &[f64] = &[100.0, 105.0, 95.0, 110.0, 90.0];
    for &p in prices {
        norm.update(Decimal::try_from(p).unwrap());
    }

    let lt = LorentzTransform::new(0.3).unwrap();

    for (i, &p) in prices.iter().enumerate() {
        let x = norm.normalize(Decimal::try_from(p).unwrap()).unwrap(); // in [0, 1]
        let t = i as f64;
        let pt = SpacetimePoint::new(t, x);
        let transformed = lt.transform(pt);
        // Transformed point should be finite and well-defined.
        assert!(transformed.t.is_finite());
        assert!(transformed.x.is_finite());
    }
}

// ── Concurrent SPSC ring buffer ───────────────────────────────────────────────

/// Concurrent producer/consumer with 50 000 ticks -- integration smoke test.
#[test]
fn test_concurrent_tick_ring_buffer_integration() {
    const N: usize = 50_000;
    let ring: SpscRing<u64, 512> = SpscRing::new();
    let (prod, cons) = ring.split();

    let producer = thread::spawn(move || {
        let mut sent = 0;
        while sent < N {
            if prod.push(sent as u64).is_ok() {
                sent += 1;
            }
        }
    });

    let consumer = thread::spawn(move || {
        let mut received = 0usize;
        while received < N {
            if cons.pop().is_ok() {
                received += 1;
            }
        }
        received
    });

    producer.join().unwrap();
    let count = consumer.join().unwrap();
    assert_eq!(count, N);
}