Files
foxhunt/services/backtesting_service/examples/export_dbn_to_csv.rs
jgrusewski e8a68ee39f Download 360 DBN files (36.3 MB) using Rust databento client
- Created data/examples/download_ml_training_data.rs using reqwest + Databento HTTP API
- Downloaded 90 days × 4 symbols (ES.FUT, NQ.FUT, ZN.FUT, 6E.FUT)
- Files saved to test_data/real/databento/ml_training/
- Total: 360 files, 15 MB compressed DBN format
- Used existing Rust pattern from download_nq_fut.rs
- API key loaded from .env file
- 100% success rate (360/360 files)
- Ready for ML training benchmarks

Next: Create simplified training benchmark for RTX 3050 Ti GPU measurements
2025-10-13 13:30:02 +02:00

108 lines
3.2 KiB
Rust

//! DBN to CSV Export Example
//!
//! Exports ES.FUT DBN data to CSV format for external analysis.
use anyhow::Result;
use backtesting_service::dbn_repository::DbnMarketDataRepository;
use backtesting_service::repositories::MarketDataRepository;
use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
#[tokio::main]
async fn main() -> Result<()> {
println!("ES.FUT DBN to CSV Exporter");
println!("==========================\n");
// Load data
let mut file_mapping = HashMap::new();
file_mapping.insert(
"ES.FUT".to_string(),
"test_data/real/databento/ES.FUT_ohlcv-1m_2024-01-02.dbn".to_string(),
);
let repo = DbnMarketDataRepository::new(file_mapping).await?;
let symbols = vec!["ES.FUT".to_string()];
let start_time = 1704153600_000_000_000i64; // 2024-01-02 00:00:00 UTC
let end_time = 1704240000_000_000_000i64; // 2024-01-03 00:00:00 UTC
println!("📂 Loading data from DBN file...");
let data = repo
.load_historical_data(&symbols, start_time, end_time)
.await?;
if data.is_empty() {
println!("❌ ERROR: No data loaded!");
return Ok(());
}
println!("✅ Loaded {} bars", data.len());
// Export to CSV
let output_path = "ES.FUT_2024-01-02.csv";
let mut file = File::create(output_path)?;
println!("\n📝 Writing CSV file: {}", output_path);
// Write header with detailed column descriptions
writeln!(
file,
"timestamp_utc,timestamp_unix_ns,symbol,open,high,low,close,volume"
)?;
// Write data
for bar in &data {
writeln!(
file,
"{},{},{},{},{},{},{},{}",
bar.timestamp.format("%Y-%m-%d %H:%M:%S"),
bar.timestamp.timestamp_nanos_opt().unwrap_or(0),
bar.symbol,
bar.open,
bar.high,
bar.low,
bar.close,
bar.volume
)?;
}
println!("✅ Successfully exported {} bars to {}", data.len(), output_path);
println!();
// Print sample rows
println!("📋 Sample Data (first 5 rows):");
println!(
"{:<20} {:<10} {:>8} {:>8} {:>8} {:>8} {:>10}",
"Timestamp", "Symbol", "Open", "High", "Low", "Close", "Volume"
);
println!("{}", "-".repeat(85));
for bar in data.iter().take(5) {
println!(
"{:<20} {:<10} {:>8.2} {:>8.2} {:>8.2} {:>8.2} {:>10.0}",
bar.timestamp.format("%Y-%m-%d %H:%M:%S"),
bar.symbol,
bar.open,
bar.high,
bar.low,
bar.close,
bar.volume
);
}
println!("\n💡 Usage Examples:");
println!(" • Python pandas: df = pd.read_csv('{}') ", output_path);
println!(" • R: data <- read.csv('{}') ", output_path);
println!(" • Excel: Open file directly");
println!(" • SQL: LOAD DATA INFILE '{}' INTO TABLE ...", output_path);
println!();
println!("📊 Analysis Tools:");
println!(" • Calculate moving averages");
println!(" • Plot candlestick charts");
println!(" • Compute technical indicators (RSI, MACD, Bollinger Bands)");
println!(" • Run statistical analysis");
Ok(())
}