Commons Library
A core utility library providing shared functionality across the platform's services. This library implements common data models, utilities, and infrastructure components used throughout the trading system.
Key Components
Data Models
- Exchange Models: Implementations for order books, trades, and market data structures
- Instrument Models: Definitions for various trading instruments (options, perpetuals, etc.)
- Account Models: Portfolio and position tracking models
- Redis Models: Data models for Redis storage and caching
Estimators
- Realized Kernel Volatility (RKV): Statistical estimators for computing realized volatility from market data
Store
- Exchange Store: Redis-based storage implementation for maintaining exchange state, market data, and order books
Utilities
- Chronos: Date/time utilities specific to financial markets
- HTTP: Async HTTP client utilities
- Validation: Common validation logic for trading symbols and instruments
Key Features
- Type-safe implementations using Pydantic models
- Async-first design with coroutines for I/O operations
- Comprehensive test coverage
- Redis integration for market data caching
- Support for various financial instruments:
- Linear/Inverse Options
- Perpetual Futures
- Spot Markets
Installation
The package can be installed via poetry:
Usage Example
from commons.store import ExchangeStore
from commons.models.exchange import Trade
# Initialize store
store = ExchangeStore(chain_id=1, redis_client=redis_client)
# Add trading pair
store.add_pair("ETH_USDC")
# Update market data
store.update_index_price("ETH_USDC", TimeObservable(
timestamp=1000,
value=2000.0
))
# Get exchange state
state = store.state("ETH_USDC")
print(state.index_price) # 2000.0
Contributing
When contributing to this library, please:
- Add tests for any new functionality
- Ensure all tests pass:
poetry run pytest
- Follow the existing code style and type hints
- Document any new public APIs
Dependencies
- Python >= 3.9, < 3.11
- pydantic
- pandas
- web3
- redis
This documentation is generated from the service README. For the most up-to-date information, refer to the original README