Package: quant-invest-lab
Quant Invest LabQuant Invest Labis 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 featuresData: 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.InstallationTo installQuant Invest Labthrough pip, run the following command:pipinstallquant-invest-lab--upgradeYou can install it using poetry the same way :poetryaddquant-invest-labBasic examplesBacktest a basic EMA crossover strategyimportpandasaspdfromquant_invest_lab.backtestimportohlc_long_only_backtesterfromquant_invest_lab.data_providerimportdownload_crypto_historical_datasymbol="BTC-USDT"timeframe="4hour"df_BTC=download_crypto_historical_data(symbol,timeframe)# Define your indicatorsdf_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 functionsdefbuy_func(row:pd.Series,prev_row:pd.Series)->bool:returnTrueifrow.EMA20>row.EMA60elseFalsedefsell_func(row:pd.Series,prev_row:pd.Series,trading_days:int)->bool:returnTrueifrow.EMA20<row.EMA60elseFalse# Backtest your strategyohlc_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)fromquant_invest_lab.portfolioimportMonteCarloPortfolio,ConvexPortfolio,RiskParityPortfoliofromquant_invest_lab.data_providerimportbuild_multi_crypto_dataframesymbols=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 assetcvx_ptf.get_allocation()# ormc_ptf=MonteCarloPortfolio(returns)mc_ptf.fit(n_portfolios=20000,plot=True)mc_ptf.get_allocation("sharpe","max")# maximize sharpe ratioNext stepsCreate official docs and add more examplesShort, leverage and margin backtestingAdd more data provider (Stock, bonds...)Make montecarlo candle data generation process more realisticDisclaimerThis 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.