Quant Invest Lab
Quant Invest Lab is a project aimed to provide a set of basic tools for quantitative experiments. By quantitative experiment I mean trying to build you own set of investments solution. The project is still in its early stage, but I hope it will grow in the future.
Initially this project was aimed to be a set of tools for my own experiments, but I decided to make it open source. Of courses it already exists some awesome packages, more detailed, better suited for some use cases. But I hope it will be useful for someone else (learn, practice, understand and create). Feel free to use it, modify it and contribute to it. This package is basically the package I wanted to find when I started to learn quantitative finance.
Main features
- Data: download data from external data provider without restriction on candle stick, the main provider is kucoin for now (currently only crypto data are supported).
- Backtesting: backtest your trading strategy (Long only for now but soon short and leverage) on historical data for different timeframe. Optimize you take profit, stop loss. Access full metrics of your strategy.
- Indicators: a set of indicators to help you build your strategy.
- Portfolio: a set of portfolio optimization tools to help you build your portfolio.
- Simulation: simulate your data based on real data using statistics to get a better understanding of its behavior during backtesting.
- Metrics: a set of metrics to help you evaluate your strategy through performances and risks.
Installation
To install Quant Invest Lab through pip, run the following command:
pip install quant-invest-lab --upgrade
You can install it using poetry the same way :
poetry add quant-invest-lab
Basic examples
Backtest a basic EMA crossover strategy
import pandas as pd
from quant_invest_lab.backtest import ohlc_long_only_backtester
from quant_invest_lab.data_provider import download_crypto_historical_data
symbol = "BTC-USDT"
timeframe = "4hour"
df_BTC = download_crypto_historical_data(symbol, timeframe)
# Define your indicators
df_BTC["EMA20"] = df_BTC.Close.ewm(20).mean()
df_BTC["EMA60"] = df_BTC.Close.ewm(60).mean()
df_BTC = df_BTC.dropna()
# Define your strategy entry and exit functions
def buy_func(row: pd.Series, prev_row: pd.Series) -> bool:
return True if row.EMA20 > row.EMA60 else False
def sell_func(row: pd.Series, prev_row: pd.Series, trading_days: int) -> bool:
return True if row.EMA20 < row.EMA60 else False
# Backtest your strategy
ohlc_long_only_backtester(
df=df_BTC,
long_entry_function=buy_func,
long_exit_function=sell_func,
timeframe=timeframe,
initial_equity=1000,
)
Optimize a portfolio (mean-variance)
from quant_invest_lab.portfolio import MonteCarloPortfolio, ConvexPortfolio, RiskParityPortfolio
from quant_invest_lab.data_provider import build_multi_crypto_dataframe
symbols = set(
[
"BNB-USDT",
"BTC-USDT",
"NEAR-USDT",
"ETH-USDT",
"SOL-USDT",
"EGLD-USDT",
"ALGO-USDT",
"FTM-USDT",
"ADA-USDT",
]
)
closes = build_multi_crypto_dataframe(symbols)
returns = closes.pct_change().dropna()
cvx_ptf = ConvexPortfolio(returns)
cvx_ptf.fit("sharpe", "max", max_asset_weight=0.2) # maximize sharpe ratio with a max weight of 20% per asset
cvx_ptf.get_allocation()
# or
mc_ptf = MonteCarloPortfolio(returns)
mc_ptf.fit(n_portfolios=20000, plot=True)
mc_ptf.get_allocation("sharpe", "max") # maximize sharpe ratio
Next steps
- Create official docs and add more examples
- Short, leverage and margin backtesting
- Add more data provider (Stock, bonds...)
- Make montecarlo candle data generation process more realistic
Disclaimer
This package is only for educational purpose or experimentation it is not intended to be used in production. I am not responsible for any loss of money you may have using this package. Use it at your own risk.
Metadata
Release files for quant-invest-lab 0.2.11
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| quant_invest_lab-0.2.11.tar.gz | 40.9 kB | Without provenance | Details for quant_invest_lab-0.2.11.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| quant_invest_lab-0.2.11-py3-none-any.whl | Python 3 | none | any | Details for quant_invest_lab-0.2.11-py3-none-any.whl |
Total release size: 84.8 kB
Release files / quant_invest_lab-0.2.11.tar.gz
| Download URL | quant_invest_lab-0.2.11.tar.gz |
|---|---|
| Size | 40.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4ca221e7eb3b01b467a4cf2b8b6a4f8e2ae63c9cc6009462e4f19b7946ecb9bf
|
|
BLAKE2b-256 checksum How to use checksums |
d760f526b09fa020bb9aa543b4508e06c8c23906239d401cb02a89ce3f012beb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.18
|
Release files / quant_invest_lab-0.2.11-py3-none-any.whl
| Download URL | quant_invest_lab-0.2.11-py3-none-any.whl |
|---|---|
| Size | 43.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
052f58e30be676ebffe3c1a28df426a042f78989acfd7f0cd34349527d492c40
|
|
BLAKE2b-256 checksum How to use checksums |
933e655e32add8d8a12a320fffbec1ae51f7363977794ed496d07940e58037c2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.18
|
Security for quant-invest-lab 0.2.11
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.