rangebar/
lib.rs

1//! Non-lookahead range bar construction for cryptocurrency and forex trading.
2//!
3//! [![Crates.io](https://img.shields.io/crates/v/rangebar.svg)](https://crates.io/crates/rangebar)
4//! [![Documentation](https://docs.rs/rangebar/badge.svg)](https://docs.rs/rangebar)
5//! [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6//!
7//! This crate provides algorithms for constructing range bars from tick data
8//! with temporal integrity guarantees, ensuring no lookahead bias in financial backtesting.
9//!
10//! ## Installation
11//!
12//! Add to your `Cargo.toml`:
13//!
14//! ```toml
15//! [dependencies]
16//! rangebar = "5.2"
17//! ```
18//!
19//! ## Meta-Crate
20//!
21//! This is a meta-crate that re-exports all rangebar sub-crates for backward compatibility
22//! with v4.0.0. New code should depend on specific sub-crates directly:
23//!
24//! - `rangebar-core` - Core algorithm and types
25//! - `rangebar-providers` - Data providers (Binance, Exness)
26//! - `rangebar-config` - Configuration management
27//! - `rangebar-io` - I/O operations and Polars integration
28//! - `rangebar-streaming` - Real-time streaming processor
29//! - `rangebar-batch` - Batch analytics engine
30//! - `rangebar-cli` - Command-line tools
31//!
32//! ## Features
33//!
34//! - `core` - Core algorithm (always enabled)
35//! - `providers` - Data providers (Binance, Exness)
36//! - `config` - Configuration management
37//! - `io` - I/O operations and Polars integration
38//! - `streaming` - Real-time streaming processor
39//! - `batch` - Batch analytics engine
40//! - `full` - Enable all features
41//!
42//! ## Basic Usage
43//!
44//! ```rust
45//! use rangebar::{RangeBarProcessor, AggTrade, FixedPoint};
46//!
47//! // Create processor with 250 basis points threshold (2.5%)
48//! let mut processor = RangeBarProcessor::new(250).unwrap();
49//!
50//! // Create sample aggTrade
51//! let trade = AggTrade {
52//!     agg_trade_id: 1,
53//!     price: FixedPoint::from_str("50000.0").unwrap(),
54//!     volume: FixedPoint::from_str("1.0").unwrap(),
55//!     first_trade_id: 1,
56//!     last_trade_id: 1,
57//!     timestamp: 1609459200000,
58//!     is_buyer_maker: false,
59//!     is_best_match: None,
60//! };
61//!
62//! // Process aggTrade records into range bars
63//! let agg_trade_records = vec![trade];
64//! let bars = processor.process_agg_trade_records(&agg_trade_records).unwrap();
65//! ```
66//!
67//! ## Dual-Path Architecture
68//!
69//! ### Streaming Mode (Real-time)
70//! ```rust
71//! # #[cfg(feature = "streaming")]
72//! # {
73//! use rangebar::streaming::StreamingProcessor;
74//!
75//! let threshold_bps = 25; // 0.25% range bars
76//! let processor = StreamingProcessor::new(threshold_bps);
77//! // Real-time processing with bounded memory
78//! # }
79//! ```
80//!
81//! ### Batch Mode (Analytics)
82//! ```rust
83//! # #[cfg(feature = "batch")]
84//! # {
85//! use rangebar::batch::BatchAnalysisEngine;
86//! use rangebar::core::types::RangeBar;
87//!
88//! let range_bars: Vec<RangeBar> = vec![]; // Your range bar data
89//! let engine = BatchAnalysisEngine::new();
90//! // let result = engine.analyze_single_symbol(&range_bars, "BTCUSDT").unwrap();
91//! # }
92//! ```
93//!
94//! ## Links
95//!
96//! - [GitHub Repository](https://github.com/terrylica/rangebar)
97//! - [API Documentation](https://docs.rs/rangebar)
98//! - [Changelog](https://github.com/terrylica/rangebar/blob/main/CHANGELOG.md)
99//! - [Algorithm Specification](https://github.com/terrylica/rangebar/blob/main/docs/specifications/algorithm-spec.md)
100
101// Re-export core (always available)
102pub use rangebar_core as core;
103
104// Re-export optional crates
105#[cfg(feature = "providers")]
106pub use rangebar_providers as providers;
107
108#[cfg(feature = "config")]
109pub use rangebar_config as config;
110
111#[cfg(feature = "io")]
112pub use rangebar_io as io;
113
114#[cfg(feature = "streaming")]
115pub use rangebar_streaming as streaming;
116
117#[cfg(feature = "batch")]
118pub use rangebar_batch as batch;
119
120// Legacy compatibility modules for v4.0.0 API
121pub mod fixed_point {
122    //! Fixed-point arithmetic module (legacy compatibility)
123    pub use rangebar_core::fixed_point::*;
124}
125
126pub mod range_bars {
127    //! Range bar processor module (legacy compatibility)
128    pub use rangebar_core::processor::*;
129}
130
131pub mod types {
132    //! Core types module (legacy compatibility)
133    pub use rangebar_core::types::*;
134}
135
136#[cfg(feature = "providers")]
137pub mod tier1 {
138    //! Tier-1 symbol discovery (legacy compatibility)
139    pub use rangebar_providers::binance::symbols::*;
140}
141
142#[cfg(feature = "providers")]
143pub mod data {
144    //! Historical data loading (legacy compatibility)
145    pub use rangebar_providers::binance::HistoricalDataLoader;
146}
147
148#[cfg(feature = "config")]
149pub mod infrastructure {
150    //! Infrastructure modules (legacy compatibility)
151
152    #[cfg(feature = "config")]
153    pub use rangebar_config as config;
154
155    #[cfg(feature = "io")]
156    pub use rangebar_io as io;
157}
158
159#[cfg(feature = "streaming")]
160pub mod engines {
161    //! Engine modules (legacy compatibility)
162
163    #[cfg(feature = "streaming")]
164    pub use rangebar_streaming as streaming;
165
166    #[cfg(feature = "batch")]
167    pub use rangebar_batch as batch;
168}
169
170// Re-export commonly used types at crate root for convenience
171pub use rangebar_core::{
172    AggTrade, ExportRangeBarProcessor, FixedPoint, ProcessingError, RangeBar, RangeBarProcessor,
173};
174
175#[cfg(feature = "config")]
176pub use rangebar_config::Settings;
177
178#[cfg(feature = "providers")]
179pub use rangebar_providers::binance::{
180    TIER1_SYMBOLS, get_tier1_symbols, get_tier1_usdt_pairs, is_tier1_symbol,
181};
182
183#[cfg(feature = "streaming")]
184pub use rangebar_streaming::processor::{
185    StreamingError, StreamingMetrics, StreamingProcessor, StreamingProcessorConfig,
186};
187
188#[cfg(feature = "batch")]
189pub use rangebar_batch::{AnalysisReport, BatchAnalysisEngine, BatchConfig, BatchResult};
190
191// Re-export I/O types when feature is enabled
192#[cfg(feature = "io")]
193pub use rangebar_io::{ArrowExporter, ParquetExporter, PolarsExporter, StreamingCsvExporter};
194
195/// Version information
196pub const VERSION: &str = env!("CARGO_PKG_VERSION");
197pub const NAME: &str = env!("CARGO_PKG_NAME");
198pub const DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION");
199
200/// Library initialization and configuration
201pub fn init() {
202    // Future: Initialize logging, metrics, etc.
203}
204
205#[cfg(test)]
206mod tests {
207    use super::*;
208
209    #[test]
210    #[allow(clippy::const_is_empty)]
211    fn test_version() {
212        assert!(!VERSION.is_empty());
213        assert!(!NAME.is_empty());
214        assert!(!DESCRIPTION.is_empty());
215    }
216
217    #[test]
218    fn test_types_export() {
219        // Test that we can create and use exported types
220        let fp = FixedPoint::from_str("123.456").unwrap();
221        assert_eq!(fp.to_string(), "123.45600000");
222    }
223
224    #[test]
225    fn test_legacy_fixed_point_module() {
226        // Test backward compatibility via legacy module path
227        let fp = fixed_point::FixedPoint::from_str("100.0").unwrap();
228        assert_eq!(fp.to_string(), "100.00000000");
229    }
230
231    #[test]
232    fn test_legacy_types_module() {
233        // Test backward compatibility via legacy types module
234        use types::DataSource;
235        let _data_source = DataSource::BinanceSpot;
236    }
237
238    #[cfg(feature = "providers")]
239    #[test]
240    fn test_tier1_symbols_export() {
241        // Test that tier1 symbols are accessible
242        let symbols = get_tier1_symbols();
243        assert!(!symbols.is_empty());
244        assert!(symbols.contains(&"BTC".to_string()));
245    }
246
247    #[cfg(feature = "config")]
248    #[test]
249    fn test_settings_export() {
250        // Test that Settings type is accessible
251        let settings = Settings::default();
252        assert!(!settings.app.name.is_empty());
253    }
254
255    #[cfg(feature = "providers")]
256    #[test]
257    fn test_data_module_export() {
258        // Test backward compatibility via legacy data module path
259        // This verifies that `use rangebar::data::HistoricalDataLoader` works
260        use data::HistoricalDataLoader;
261
262        // Just verify the type is accessible
263        let _loader = HistoricalDataLoader::new("BTCUSDT");
264    }
265}