QuantLib-Python
QuantLib-Python provides official Python bindings for the open-source Quantitative Finance Library (QuantLib C++). It offers a comprehensive suite of models and tools for pricing, risk management, and analysis of financial instruments. As of version 1.41, the library is actively maintained with regular updates, making advanced financial analytics accessible in Python.
Warnings
- gotcha QuantLib uses its own robust date, calendar, and day counter system (e.g., `ql.Date`, `ql.Calendar`). Mixing directly with Python's `datetime` objects without proper conversion or misunderstanding QuantLib's specific business day conventions can lead to incorrect or unexpected results.
- gotcha QuantLib heavily relies on an 'observable' pattern for dependency management (e.g., an instrument observing a yield curve). If underlying data or objects change, dependent objects might not automatically update their calculated values unless explicitly refreshed or properly observing these changes.
- gotcha While robust, the Python bindings introduce a performance overhead compared to direct C++ QuantLib usage. For very high-frequency calculations, large-scale Monte Carlo simulations, or iterative optimizations, this overhead can become significant.
Install
-
pip install QuantLib-Python
Imports
- QuantLib
import QuantLib as ql
Quickstart
import QuantLib as ql
# Set a global evaluation date (crucial for many QuantLib calculations)
todaysDate = ql.Date(15, ql.May, 2023)
ql.Settings.instance().evaluationDate = todaysDate
# Create another QuantLib Date object
another_date = ql.Date(15, ql.May, 2024)
# Print dates and check a simple calendar function (e.g., TARGET)
print(f"Evaluation date: {todaysDate}")
print(f"Another date: {another_date}")
print(f"Is evaluation date a business day (TARGET calendar)? {ql.TARGET().isBusinessDay(todaysDate)}")