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 your own set of investments solution.
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 quantinvestlab.backtest import ohlclongonly_backtester from quantinvestlab.dataprovider import downloadcryptohistoricaldata
symbol = "BTC-USDT" timeframe = "4hour" dfBTC = downloadcryptohistoricaldata(symbol, timeframe)
Define your indicators
dfBTC["EMA20"] = dfBTC.Close.ewm(20).mean()
dfBTC["EMA60"] = dfBTC.Close.ewm(60).mean()
dfBTC = dfBTC.dropna()
Define your strategy entry and exit functions
def buyfunc(row: pd.Series, prevrow: pd.Series) -> bool:
return True if row.EMA20 > row.EMA60 else False
def sellfunc(row: pd.Series, prevrow: pd.Series, trading_days: int) -> bool: return True if row.EMA20 < row.EMA60 else False
Backtest your strategy
ohlclongonly_backtester(
df=df_BTC,
longentryfunction=buy_func,
longexitfunction=sell_func,
timeframe=timeframe,
initial_equity=1000,
)
Optimize a portfolio (mean-variance)
from quantinvestlab.portfolio import MonteCarloPortfolio, ConvexPortfolio, RiskParityPortfolio
from quantinvestlab.dataprovider import buildmulticryptodataframe
symbols = set( [ "BNB-USDT", "BTC-USDT", "NEAR-USDT", "ETH-USDT", "SOL-USDT", "EGLD-USDT", "ALGO-USDT", "FTM-USDT", "ADA-USDT", ] )
closes = buildmulticrypto_dataframe(symbols) returns = closes.pct_change().dropna()
cvx_ptf = ConvexPortfolio(returns)
cvxptf.fit("sharpe", "max", maxasset_weight=0.2) # maximize sharpe ratio with a max weight of 20% per asset
cvxptf.getallocation()
or
mc_ptf = MonteCarloPortfolio(returns)
mcptf.fit(nportfolios=20000, plot=True)
mcptf.getallocation("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