Crate trading_calendar

Crate trading_calendar 

Source
Expand description

Β§Trading Calendar

A comprehensive trading calendar for global financial markets, providing holidays, trading hours, and early close information.

Β§Features

  • 🌍 Multiple Markets: NYSE, NASDAQ, LSE, TSE, TSX
  • ⏰ Trading Hours: Regular, pre-market, and after-hours sessions
  • πŸ“… Holiday Detection: All market holidays with weekend adjustments
  • πŸ• Early Closes: Half-day schedules (Christmas Eve, Black Friday, etc.)
  • 🌐 Timezone Support: Automatic handling of market timezones
  • πŸš€ Performance: Efficient LRU caching
  • πŸ”’ Thread Safe: Concurrent access support
  • πŸ“† 2020-2030 Support: Comprehensive holiday calendars

Β§Quick Start

use trading_calendar::{TradingCalendar, Market};

fn main() -> trading_calendar::Result<()> {
    let nyse = TradingCalendar::new(Market::NYSE)?;
     
    // Check if market is open
    if nyse.is_open_now()? {
        println!("NYSE is open for trading!");
    }
     
    // Get next market open
    let next_open = nyse.next_open()?;
    println!("NYSE opens: {}", next_open);
     
    // Check specific date
    let christmas = chrono::NaiveDate::from_ymd_opt(2025, 12, 25).unwrap();
    if !nyse.is_trading_day(christmas)? {
        println!("Market closed on Christmas");
    }
     
    Ok(())
}

Β§Working with Different Markets

use trading_calendar::{TradingCalendar, Market};

// Create calendars for different markets
let nyse = TradingCalendar::new(Market::NYSE)?;
let lse = TradingCalendar::new(Market::LSE)?;
let tse = TradingCalendar::new(Market::TSE)?;

// Each market has its own timezone
assert_eq!(nyse.timezone().name(), "America/New_York");
assert_eq!(lse.timezone().name(), "Europe/London");
assert_eq!(tse.timezone().name(), "Asia/Tokyo");

Β§Handling Early Close Days

use trading_calendar::{TradingCalendar, Market};
use chrono::NaiveDate;

let nyse = TradingCalendar::new(Market::NYSE)?;
let christmas_eve = NaiveDate::from_ymd_opt(2025, 12, 24).unwrap();

let hours = nyse.trading_hours(christmas_eve);
if hours.is_early_close() {
    println!("Market closes early at {}", hours.market_close());
}

Β§Supported Markets

MarketRegular Hours (Local)Pre-MarketAfter-HoursStatus
NYSE9:30 AM - 4:00 PM ET4:00 AM - 9:30 AM4:00 PM - 8:00 PMβœ… Full Support
NASDAQ9:30 AM - 4:00 PM ET4:00 AM - 9:30 AM4:00 PM - 8:00 PMβœ… Full Support
LSE8:00 AM - 4:30 PM GMT--βœ… Full Support
TSE9:00 AM - 3:00 PM JST--βœ… Full Support
TSX9:30 AM - 4:00 PM ET--βœ… Full Support

Β§Thread Safety

The TradingCalendar is thread-safe and can be shared across threads:

use std::sync::Arc;
use trading_calendar::{TradingCalendar, Market};

fn main() -> trading_calendar::Result<()> {
    let calendar = Arc::new(TradingCalendar::new(Market::NYSE)?);

    // Share calendar across threads safely
    let cal_clone = Arc::clone(&calendar);
    std::thread::spawn(move || {
        let is_open = cal_clone.is_open_now().unwrap_or(false);
    });
     
    Ok(())
}

Β§Error Handling

The library uses proper error handling with Result types:

use trading_calendar::{TradingCalendar, Market, CalendarError};

fn main() -> trading_calendar::Result<()> {
    let calendar = TradingCalendar::new(Market::NYSE)?;

    // Check for unsupported years
    match calendar.is_trading_day(chrono::NaiveDate::from_ymd_opt(2019, 1, 1).unwrap()) {
        Ok(is_trading) => println!("Is trading day: {}", is_trading),
        Err(CalendarError::DateOutOfRange(date)) => println!("Date {} not supported", date),
        Err(e) => eprintln!("Error: {}", e),
    }
     
    Ok(())
}

Β§Performance

The library uses efficient caching to ensure optimal performance:

  • Holiday calculations are cached per year using LRU cache
  • Thread-safe concurrent access with proper eviction
  • Minimal allocations with optimized data structures

Β§License

Licensed under either of:

at your option.

Re-exportsΒ§

pub use calendar::TradingCalendar;
pub use error::CalendarError;
pub use error::Result;
pub use markets::Market;
pub use schedule::Session;
pub use schedule::TradingHours;

ModulesΒ§

calendar
Main trading calendar implementation
constants
Market-specific constants
error
Error types for the trading calendar
markets
Market definitions and implementations
schedule
Trading hours and session definitions
utils
Utility functions for calendar operations

StructsΒ§

DateTime
ISO 8601 combined date and time with time zone.
Holiday
Holiday information
NaiveDate
ISO 8601 calendar date without timezone. Allows for every proleptic Gregorian date from Jan 1, 262145 BCE to Dec 31, 262143 CE. Also supports the conversion from ISO 8601 ordinal and week date.
NaiveTime
ISO 8601 time without timezone. Allows for the nanosecond precision and optional leap second representation.
Utc
The UTC time zone. This is the most efficient time zone when you don’t need the local time. It is also used as an offset (which is also a dummy type).

ConstantsΒ§

MAX_YEAR
Maximum supported year
MIN_YEAR
Minimum supported year