dukascopy-fx
A production-ready Rust library for fetching historical forex data from Dukascopy, inspired by Python's yfinance.
Features
- yfinance-style API - Familiar
Tickerobject withhistory()method - Period strings - Use
"1d","1w","1mo","1y"for easy time ranges - Built-in time utilities - No need to add chrono separately
- Type-safe - Strong types for currency pairs, rates, and errors
- Automatic handling - JPY pairs, metals, weekends handled transparently
- Free data - No API keys required, data from 2003+
Installation
[]
= "0.3"
= { = "1", = ["full"] }
Quick Start
use ;
async
Usage
Ticker API (Recommended)
use ;
// Create tickers - multiple ways
let eur_usd = new;
let gold = xau_usd; // Convenience constructor
let ticker: Ticker = "GBP/JPY".parse?; // Parse from string
let ticker = ticker!; // Using macro
// Get historical data with period strings
let daily = ticker.history.await?; // Last 24 hours
let weekly = ticker.history.await?; // Last 7 days
let monthly = ticker.history.await?; // Last 30 days
let yearly = ticker.history.await?; // Last 365 days
// Custom date range
use ;
let history = ticker.history_range.await?;
// Change sampling interval (default: 1 hour)
use Duration;
let ticker_30min = new.interval;
let history = ticker_30min.history.await?; // ~48 records instead of ~24
Batch Download
use ;
let tickers = vec!;
let data = download.await?;
for in data
Simple Function API
use ;
use Duration;
// Single rate
let rate = get_rate.await?;
println!;
// Range of rates
let rates = get_rates_range.await?;
Time Utilities
No need to add chrono to your dependencies - we re-export everything you need:
use ;
use datetime;
// Convenient time helpers
let current = now;
let yesterday = days_ago;
let last_week = weeks_ago;
// datetime! macro - multiple formats
let ts = datetime!; // Hour and minute
let ts = datetime!; // With seconds
let ts = datetime!; // Midnight
Market Hours
use ;
let saturday = datetime!;
if is_weekend
if !is_market_open
match get_market_status
Error Handling
use ;
let ticker = new;
match ticker.rate_at.await
Advanced: Custom Client Configuration
use ;
let client = new
.cache_size // LRU cache entries (default: 100)
.timeout_secs // HTTP timeout (default: 30)
.with_instrument_config
.build;
Good to Know
Data Availability
- Historical depth: Major pairs available from 2003
- Latest data: ~1 hour delay (data is hourly)
- Weekends: No data from Friday 22:00 UTC to Sunday 22:00 UTC
Weekend Handling
Request data for Saturday? The library automatically returns Friday's last available rate:
let saturday = datetime!;
let rate = ticker.rate_at.await?;
// rate.timestamp will be Friday ~21:59 UTC, not Saturday
Price Precision
Different instruments have different decimal places - handled automatically:
| Instrument | Example Rate | Decimals |
|---|---|---|
| EUR/USD | 1.08505 | 5 |
| USD/JPY | 154.325 | 3 |
| XAU/USD | 2645.50 | 2-3 |
Caching
The library caches decompressed hourly data (LRU, 100 entries default). Requesting multiple timestamps within the same hour only fetches data once:
// These share the same cached hourly data file:
ticker.rate_at.await?;
ticker.rate_at.await?;
ticker.rate_at.await?;
Rate Limiting
Dukascopy may rate-limit aggressive requests. For bulk downloads, the library handles this gracefully, but consider adding delays for very large requests:
for ticker in tickers
Supported Instruments
| Type | Divisor | Decimals | Examples |
|---|---|---|---|
| Standard Forex | 100,000 | 5 | EUR/USD, GBP/USD, AUD/USD, USD/PLN |
| JPY Pairs | 1,000 | 3 | USD/JPY, EUR/JPY, GBP/JPY |
| Metals | 1,000 | 3 | XAU/USD (Gold), XAG/USD (Silver) |
| RUB Pairs | 1,000 | 3 | USD/RUB, EUR/RUB |
500+ instruments available - if Dukascopy has it, this library can fetch it.
Examples
Performance Tips
- Use period strings -
history("1w")is simpler than calculating dates - Batch similar requests - requests within same hour share cached data
- Check market hours - avoid unnecessary requests during weekends
- Reuse tickers -
Tickeris cheap to clone
Limitations
- Historical only - no real-time streaming
- ~1 hour delay - data organized by completed hours
- Weekend gaps - no data Friday close to Sunday open
- Rate limits - Dukascopy may throttle aggressive requests
License
MIT License - see LICENSE
Disclaimer
This library uses Dukascopy's publicly available API for research and educational purposes. Not affiliated with Dukascopy Bank SA. Data provided "as-is" without warranty.
Related Projects
- dukascopy-node - Node.js
- duka - Python