Expand description
Symbol types for unified symbol format Symbol Type Definitions for Unified Symbol Format
This module provides core type definitions for representing and working with unified trading pair symbols across different market types (Spot, Swap, Futures) and settlement currencies (Linear/Inverse).
§Overview
The types in this module form the foundation of the unified symbol system:
ParsedSymbol: The main structure representing a parsed trading pair symbolExpiryDate: Futures contract expiry date in YYMMDD formatSymbolMarketType: Enum for market types (Spot, Swap, Futures)ContractType: Enum for contract settlement types (Linear, Inverse)- [
SymbolError]: Error type for symbol-related operations
§Symbol Format
The unified symbol format follows the CCXT standard:
| Market Type | Format | Example | Description |
|---|---|---|---|
| Spot | BASE/QUOTE | BTC/USDT | Direct exchange of assets |
| Linear Swap | BASE/QUOTE:SETTLE | BTC/USDT:USDT | Perpetual, settled in quote |
| Inverse Swap | BASE/QUOTE:SETTLE | BTC/USD:BTC | Perpetual, settled in base |
| Futures | BASE/QUOTE:SETTLE-YYMMDD | BTC/USDT:USDT-241231 | Expiring contract |
§Creating Symbols
§Spot Symbols
use ccxt_core::types::symbol::ParsedSymbol;
let spot = ParsedSymbol::spot("BTC".to_string(), "USDT".to_string());
assert_eq!(spot.to_string(), "BTC/USDT");
assert!(spot.is_spot());
assert!(!spot.is_derivative());§Swap Symbols (Perpetuals)
use ccxt_core::types::symbol::{ParsedSymbol, ContractType};
// Linear swap (USDT-margined)
let linear = ParsedSymbol::linear_swap("BTC".to_string(), "USDT".to_string());
assert_eq!(linear.to_string(), "BTC/USDT:USDT");
assert_eq!(linear.contract_type(), Some(ContractType::Linear));
// Inverse swap (Coin-margined)
let inverse = ParsedSymbol::inverse_swap("BTC".to_string(), "USD".to_string());
assert_eq!(inverse.to_string(), "BTC/USD:BTC");
assert_eq!(inverse.contract_type(), Some(ContractType::Inverse));
// Custom swap with explicit settlement
let custom = ParsedSymbol::swap("ETH".to_string(), "USDT".to_string(), "USDT".to_string());
assert!(custom.is_swap());§Futures Symbols
use ccxt_core::types::symbol::{ParsedSymbol, ExpiryDate};
let expiry = ExpiryDate::new(24, 12, 31).unwrap();
let futures = ParsedSymbol::futures(
"BTC".to_string(),
"USDT".to_string(),
"USDT".to_string(),
expiry
);
assert_eq!(futures.to_string(), "BTC/USDT:USDT-241231");
assert!(futures.is_futures());§Analyzing Symbols
use ccxt_core::types::symbol::{ParsedSymbol, SymbolMarketType, ContractType};
let symbol = ParsedSymbol::linear_swap("ETH".to_string(), "USDT".to_string());
// Market type detection
assert_eq!(symbol.market_type(), SymbolMarketType::Swap);
assert!(symbol.is_swap());
assert!(symbol.is_derivative());
// Contract type detection
assert_eq!(symbol.contract_type(), Some(ContractType::Linear));
assert!(symbol.is_linear());
assert!(!symbol.is_inverse());§Expiry Date Handling
use ccxt_core::types::symbol::ExpiryDate;
use std::str::FromStr;
// Create with validation
let expiry = ExpiryDate::new(24, 12, 31).unwrap();
assert_eq!(expiry.to_string(), "241231");
// Parse from string
let parsed = ExpiryDate::from_str("250315").unwrap();
assert_eq!(parsed.year, 25);
assert_eq!(parsed.month, 3);
assert_eq!(parsed.day, 15);
// Invalid dates are rejected
assert!(ExpiryDate::new(24, 13, 1).is_err()); // month > 12
assert!(ExpiryDate::new(24, 1, 32).is_err()); // day > 31§Case Normalization
All currency codes are automatically normalized to uppercase:
use ccxt_core::types::symbol::ParsedSymbol;
let symbol = ParsedSymbol::spot("btc".to_string(), "usdt".to_string());
assert_eq!(symbol.base, "BTC");
assert_eq!(symbol.quote, "USDT");Structs§
- Expiry
Date - Represents a futures contract expiry date in YYMMDD format
- Parsed
Symbol - Represents a parsed unified symbol with all components
Enums§
- Contract
Type - Contract settlement type for derivatives
- Symbol
Error - Error type for symbol-related operations
- Symbol
Market Type - Market type derived from symbol structure