Files
foxhunt/data/examples/download_nq_fut.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

137 lines
5.2 KiB
Rust

//! Download NQ.FUT (Nasdaq-100 E-mini futures) historical data from Databento
//!
//! This downloads 1 day of OHLCV-1m data for NQ.FUT on 2024-01-02 (same date as ES.FUT)
use std::env;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
println!(" Databento NQ.FUT Historical Download");
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
println!();
// Check for API key
let api_key = env::var("DATABENTO_API_KEY")
.map_err(|_| "DATABENTO_API_KEY environment variable not set")?;
println!("✅ API Key found: {}...{}", &api_key[0..10], &api_key[api_key.len()-10..]);
println!();
// Test parameters
let symbol = "NQ.FUT";
let dataset = "GLBX.MDP3";
let schema = "ohlcv-1m";
let start_date = "2024-01-02";
let end_date = "2024-01-02";
println!("📋 Download Parameters:");
println!(" Symbol: {} (Nasdaq-100 E-mini Futures)", symbol);
println!(" Dataset: {} (CME Group MDP 3.0)", dataset);
println!(" Schema: {} (1-minute OHLCV bars)", schema);
println!(" Date: {} (single trading day)", start_date);
println!();
// Create HTTP client for Databento Historical API
let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(30))
.build()?;
// Build request URL
let url = format!(
"https://hist.databento.com/v0/timeseries.get_range?dataset={}&symbols={}&schema={}&start={}T00:00:00Z&end={}T23:59:59Z&encoding=dbn&stype_in=parent",
dataset, symbol, schema, start_date, end_date
);
println!("🔗 Request URL:");
println!(" {}", url);
println!();
println!("📥 Sending request...");
// Make request with Basic Authentication
let response = client
.get(&url)
.basic_auth(&api_key, Some(""))
.send()
.await?;
// Check status
let status = response.status();
println!("📊 Response Status: {}", status);
println!();
if !status.is_success() {
let error_text = response.text().await?;
eprintln!("❌ Request failed!");
eprintln!("Status: {}", status);
eprintln!("Response: {}", error_text);
return Err(format!("API returned error: {}", status).into());
}
// Get response body
let body = response.bytes().await?;
let size = body.len();
println!("✅ Download successful!");
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
println!(" Size: {} bytes ({:.2} KB)", size, size as f64 / 1024.0);
// Estimate cost (based on ES.FUT: 96KB cost ~$0.0002)
let size_gb = size as f64 / 1_073_741_824.0;
let cost_low = size_gb * 0.50;
let cost_high = size_gb * 2.00;
println!(" Size (GB): {:.10}", size_gb);
println!(" Estimated: ${:.6} - ${:.6}", cost_low, cost_high);
// Read current balance from COST_TRACKING.md
let cost_tracking_path = "COST_TRACKING.md";
let current_balance = if let Ok(content) = std::fs::read_to_string(cost_tracking_path) {
// Extract current credits from "**Current Credits**: ~$124.9998"
if let Some(line) = content.lines().find(|l| l.contains("**Current Credits**")) {
// Parse the number after "$"
if let Some(dollar_pos) = line.rfind('$') {
let balance_str = &line[dollar_pos+1..].trim();
balance_str.parse::<f64>().unwrap_or(125.0)
} else {
125.0
}
} else {
125.0
}
} else {
125.0
};
println!(" Credits Left: ~${:.4}", current_balance - cost_high);
println!();
// Save to file
let output_path = format!("test_data/real/databento/{}_{}_{}.dbn", symbol.replace("/", "_"), schema, start_date);
std::fs::create_dir_all("test_data/real/databento")?;
std::fs::write(&output_path, &body)?;
println!("💾 Saved to: {}", output_path);
println!();
// Verify file
if std::path::Path::new(&output_path).exists() {
let file_size = std::fs::metadata(&output_path)?.len();
println!("✅ File verified: {} bytes", file_size);
} else {
println!("⚠️ Warning: File not found after write");
}
println!();
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
println!(" Download Complete!");
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
println!();
println!("💡 Next Steps:");
println!(" 1. Validate data: cargo run -p backtesting_service --example validate_dbn_data -- test_data/real/databento/{}_{}_{}.dbn", symbol.replace("/", "_"), schema, start_date);
println!(" 2. Update COST_TRACKING.md with usage details");
Ok(())
}