quant-invest-lab


Namequant-invest-lab JSON
Version 0.2.11 PyPI version JSON
download
home_pagehttps://github.com/BaptisteZloch/Quant-Invest-Lab
SummaryQuant Invest Lab is a python package to help you to do some quantitative experiments, while trying to learn or build quantitative investment solutions. This project was initially my own set of functionnalities but I decided to build a package for that and sharing it as open source project.
upload_time2023-11-25 15:31:21
maintainerBaptisteZloch
docs_urlNone
authorBaptisteZloch
requires_python>=3.9,<3.12
licenseMIT
keywords trading backtest investments portfolio quantitative
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Quant Invest Lab
<p align="left">
<a href="https://pypi.org/project/quant-invest-lab/"><img alt="PyPI" src="https://img.shields.io/pypi/v/quant-invest-lab"></a>
<a><img alt="commit update" src="https://img.shields.io/github/last-commit/BaptisteZloch/Quant-Invest-Lab"></a>
<a href="https://github.com/psf/black"><img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
<a href="https://codeclimate.com/github/BaptisteZloch/Quant-Invest-Lab"><img alt="Code Climate" src="https://codeclimate.com/github/BaptisteZloch/Quant-Invest-Lab/badges/gpa.svg"></a>
<a href="https://github.com/BaptisteZloch/Quant-Invest-Lab/blob/master/.github/workflows/python-publish.yml"><img alt="GitHub Actions CI" src="https://github.com/BaptisteZloch/Quant-Invest-Lab/actions/workflows/python-publish.yml/badge.svg"></a>

[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat-square)](https://github.com/BaptisteZloch/Quant-Invest-Lab/issues)



**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:
```bash
pip install quant-invest-lab --upgrade
```
You can install it using poetry the same way :
```bash
poetry add quant-invest-lab
```

# Basic examples
## Backtest a basic EMA crossover strategy
```python
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)
```python
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.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/BaptisteZloch/Quant-Invest-Lab",
    "name": "quant-invest-lab",
    "maintainer": "BaptisteZloch",
    "docs_url": null,
    "requires_python": ">=3.9,<3.12",
    "maintainer_email": "bzloch@hotmail.fr",
    "keywords": "trading,backtest,investments,portfolio,quantitative",
    "author": "BaptisteZloch",
    "author_email": "bzloch@hotmail.fr",
    "download_url": "https://files.pythonhosted.org/packages/d7/60/f526b09fa020bb9aa543b4508e06c8c23906239d401cb02a89ce3f012beb/quant_invest_lab-0.2.11.tar.gz",
    "platform": null,
    "description": "# Quant Invest Lab\n<p align=\"left\">\n<a href=\"https://pypi.org/project/quant-invest-lab/\"><img alt=\"PyPI\" src=\"https://img.shields.io/pypi/v/quant-invest-lab\"></a>\n<a><img alt=\"commit update\" src=\"https://img.shields.io/github/last-commit/BaptisteZloch/Quant-Invest-Lab\"></a>\n<a href=\"https://github.com/psf/black\"><img alt=\"Code style: black\" src=\"https://img.shields.io/badge/code%20style-black-000000.svg\"></a>\n<a href=\"https://codeclimate.com/github/BaptisteZloch/Quant-Invest-Lab\"><img alt=\"Code Climate\" src=\"https://codeclimate.com/github/BaptisteZloch/Quant-Invest-Lab/badges/gpa.svg\"></a>\n<a href=\"https://github.com/BaptisteZloch/Quant-Invest-Lab/blob/master/.github/workflows/python-publish.yml\"><img alt=\"GitHub Actions CI\" src=\"https://github.com/BaptisteZloch/Quant-Invest-Lab/actions/workflows/python-publish.yml/badge.svg\"></a>\n\n[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat-square)](https://github.com/BaptisteZloch/Quant-Invest-Lab/issues)\n\n\n\n**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. \n\nInitially 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.\n## Main features\n- **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).\n- **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.\n- **Indicators**: a set of indicators to help you build your strategy.\n- **Portfolio**: a set of portfolio optimization tools to help you build your portfolio.\n- **Simulation**: simulate your data based on real data using statistics to get a better understanding of its behavior during backtesting.\n- **Metrics**: a set of metrics to help you evaluate your strategy through performances and risks.\n\n## Installation\nTo install **Quant Invest Lab** through pip, run the following command:\n```bash\npip install quant-invest-lab --upgrade\n```\nYou can install it using poetry the same way :\n```bash\npoetry add quant-invest-lab\n```\n\n# Basic examples\n## Backtest a basic EMA crossover strategy\n```python\nimport pandas as pd\n\nfrom quant_invest_lab.backtest import ohlc_long_only_backtester\nfrom quant_invest_lab.data_provider import download_crypto_historical_data\n\nsymbol = \"BTC-USDT\"\ntimeframe = \"4hour\"\ndf_BTC = download_crypto_historical_data(symbol, timeframe)\n\n# Define your indicators\ndf_BTC[\"EMA20\"] = df_BTC.Close.ewm(20).mean()\ndf_BTC[\"EMA60\"] = df_BTC.Close.ewm(60).mean()\n\ndf_BTC = df_BTC.dropna()\n\n# Define your strategy entry and exit functions\ndef buy_func(row: pd.Series, prev_row: pd.Series) -> bool:\n    return True if row.EMA20 > row.EMA60 else False\n\ndef sell_func(row: pd.Series, prev_row: pd.Series, trading_days: int) -> bool:\n    return True if row.EMA20 < row.EMA60 else False\n\n# Backtest your strategy\nohlc_long_only_backtester(\n    df=df_BTC,\n    long_entry_function=buy_func,\n    long_exit_function=sell_func,\n    timeframe=timeframe,\n    initial_equity=1000,\n)\n\n``` \n\n## Optimize a portfolio (mean-variance)\n```python\nfrom quant_invest_lab.portfolio import MonteCarloPortfolio, ConvexPortfolio, RiskParityPortfolio\nfrom quant_invest_lab.data_provider import build_multi_crypto_dataframe\n\nsymbols = set(\n    [\n        \"BNB-USDT\",\n        \"BTC-USDT\",\n        \"NEAR-USDT\",\n        \"ETH-USDT\",\n        \"SOL-USDT\",\n        \"EGLD-USDT\",\n        \"ALGO-USDT\",\n        \"FTM-USDT\",\n        \"ADA-USDT\",\n    ]\n)\n\ncloses = build_multi_crypto_dataframe(symbols)\nreturns = closes.pct_change().dropna()\n\ncvx_ptf = ConvexPortfolio(returns)\n\ncvx_ptf.fit(\"sharpe\", \"max\", max_asset_weight=0.2) # maximize sharpe ratio with a max weight of 20% per asset\n\ncvx_ptf.get_allocation()\n\n# or\nmc_ptf = MonteCarloPortfolio(returns)\n\nmc_ptf.fit(n_portfolios=20000, plot=True)\n\nmc_ptf.get_allocation(\"sharpe\", \"max\") # maximize sharpe ratio\n\n``` \n## Next steps\n- Create official docs and add more examples\n- Short, leverage and margin backtesting\n- Add more data provider (Stock, bonds...)\n- Make montecarlo candle data generation process more realistic\n## Disclaimer\nThis 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.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Quant Invest Lab is a python package to help you to do some quantitative experiments, while trying to learn or build quantitative investment solutions. This project was initially my own set of functionnalities but I decided to build a package for that and sharing it as open source project.",
    "version": "0.2.11",
    "project_urls": {
        "Bug Tracker": "https://github.com/BaptisteZloch/Quant-Invest-Lab/issues",
        "Homepage": "https://github.com/BaptisteZloch/Quant-Invest-Lab",
        "Repository": "https://github.com/BaptisteZloch/Quant-Invest-Lab"
    },
    "split_keywords": [
        "trading",
        "backtest",
        "investments",
        "portfolio",
        "quantitative"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "933e655e32add8d8a12a320fffbec1ae51f7363977794ed496d07940e58037c2",
                "md5": "86f6ecb702846a2e4f41c03b4e988dfe",
                "sha256": "052f58e30be676ebffe3c1a28df426a042f78989acfd7f0cd34349527d492c40"
            },
            "downloads": -1,
            "filename": "quant_invest_lab-0.2.11-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "86f6ecb702846a2e4f41c03b4e988dfe",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9,<3.12",
            "size": 43897,
            "upload_time": "2023-11-25T15:31:20",
            "upload_time_iso_8601": "2023-11-25T15:31:20.446733Z",
            "url": "https://files.pythonhosted.org/packages/93/3e/655e32add8d8a12a320fffbec1ae51f7363977794ed496d07940e58037c2/quant_invest_lab-0.2.11-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d760f526b09fa020bb9aa543b4508e06c8c23906239d401cb02a89ce3f012beb",
                "md5": "3588a02ff61c1eb2cb1afae091107a29",
                "sha256": "4ca221e7eb3b01b467a4cf2b8b6a4f8e2ae63c9cc6009462e4f19b7946ecb9bf"
            },
            "downloads": -1,
            "filename": "quant_invest_lab-0.2.11.tar.gz",
            "has_sig": false,
            "md5_digest": "3588a02ff61c1eb2cb1afae091107a29",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9,<3.12",
            "size": 40905,
            "upload_time": "2023-11-25T15:31:21",
            "upload_time_iso_8601": "2023-11-25T15:31:21.982105Z",
            "url": "https://files.pythonhosted.org/packages/d7/60/f526b09fa020bb9aa543b4508e06c8c23906239d401cb02a89ce3f012beb/quant_invest_lab-0.2.11.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-25 15:31:21",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "BaptisteZloch",
    "github_project": "Quant-Invest-Lab",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "quant-invest-lab"
}
        
Elapsed time: 1.85770s