QuantLib-Python

raw JSON →
1.41 verified Fri May 15 auth: no 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.

pip install QuantLib-Python
error ModuleNotFoundError: No module named 'QuantLib'
cause The QuantLib-Python package is not installed or the Python environment cannot locate it.
fix
Ensure that QuantLib-Python is installed in your environment by running 'pip install QuantLib'.
error ModuleNotFoundError: No module named '_QuantLib'
cause The '_QuantLib' module, a core component of QuantLib-Python, is missing or not properly installed.
fix
Verify that QuantLib-Python is correctly installed and that your Python environment is set up to locate the '_QuantLib' module.
error ImportError: DLL load failed: The specified module could not be found.
cause A required DLL for QuantLib-Python is missing or not accessible.
fix
Ensure that all necessary dependencies, such as the Microsoft Visual C++ Redistributable, are installed and that the system's PATH includes the directory containing the required DLLs.
error ImportError: DLL load failed while importing gurobipy: The specified module could not be found.
cause The 'gurobipy' module is missing a required DLL or the DLL is not in the system's PATH.
fix
Add the directory containing the 'gurobi90.dll' to the system's PATH or copy the DLL to the 'gurobipy' package directory.
error ImportError: DLL load failed while importing shell: The specified module could not be found.
cause The 'shell' module is missing a required DLL or the DLL is not in the system's PATH.
fix
Ensure that all necessary dependencies are installed and that the system's PATH includes the directory containing the required DLLs.
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.
fix Always use `QuantLib.Date` and `QuantLib.Calendar` for internal calculations, converting to/from `datetime` explicitly only when interacting with external Python libraries or data sources. Consult QuantLib documentation for specific date arithmetic and calendar rules.
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.
fix Ensure proper observer registration for dependent objects. In scenarios where values seem stale, explicit calls to `update()` on observable objects might be necessary to trigger recalculations or verify that the observation chain is correctly configured.
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.
fix Profile critical sections of your code to identify performance bottlenecks. For highly performance-sensitive tasks, consider offloading computations to a C++ extension or pre-calculating results where feasible, or explore alternative pure-Python libraries for less complex tasks.
python os / libc status wheel install import disk mem side effects
3.10 alpine (musl) wheel - 0.27s 104.7M 11.3M clean
3.10 slim (glibc) wheel 2.6s 0.14s 98M 11.3M clean
3.11 alpine (musl) wheel - 2.83s 107.4M 12.5M clean
3.11 slim (glibc) wheel 2.4s 2.34s 101M 12.5M clean
3.12 alpine (musl) wheel - 1.20s 99.1M 12.3M clean
3.12 slim (glibc) wheel 2.3s 1.25s 93M 12.3M clean
3.13 alpine (musl) wheel - 1.06s 98.9M 12.9M clean
3.13 slim (glibc) wheel 2.4s 1.17s 92M 12.9M clean
3.9 alpine (musl) wheel - 0.15s 104.4M 12.0M clean
3.9 slim (glibc) wheel 2.8s 0.14s 98M 11.9M clean

This quickstart demonstrates how to import QuantLib, set a global evaluation date, create QuantLib Date objects, and use a calendar function. Setting the evaluation date is a fundamental first step for most QuantLib applications.

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)}")