A small backtesting utility.
![image](https://user-images.githubusercontent.com/7386242/189368488-b0c30d48-9a1f-4362-9f78-10a451535682.png)
[![PyTest](https://github.com/crunchdao/backtest/actions/workflows/pytest.yml/badge.svg)](https://github.com/crunchdao/backtest/actions/workflows/pytest.yml)
- [Install](#install)
- [Usage](#usage)
- [Options](#options)
- [Exporters](#exporters)
- [Console](#console)
- [Dump](#dump)
- [QuantStats](#quantstats)
- [PDF](#pdf)
- [Specific Return](#specific-return)
- [Data Sources](#data-sources)
- [Yahoo](#yahoo)
- [CoinMarketCap](#coinmarketcap)
- [FactSet](#factset)
- [File](#file)
- [.parquet](#parquet)
# Install
```
pip install --upgrade bktest
```
# Usage
```bash
bktest [OPTIONS]
```
## Options
| Option | Value | Default | Format | Description |
| --- | --- | --- | --- | --- |
| `--start` | `<start date>` | `orders' first date` | `date` (ISO-8601) | The starting date of the backtesting. If the value is before the first ordering day, the value will be discarded. |
| `--end` | `<end date>` | `orders' last date` | `date` (ISO-8601) | The ending date of the backtesting. If the value is after today, the value will be discarded. |
| `--offset-before-trading` | `<days>` | `1` | `int` | Number of day to offset to push each date of the portfolio before trading it. |
| `--offset-before-ending` | `<days>` | `0` | `int` | Number of day to continue the backtest after every orders. |
| `--order-file` | `<file>` | | `path` | The single order file to use. The file must contain symbol, quantity and date information. |
| `--single-file-provider-column-date` | `<column>` | `date` | `string` | Change the date column name to use. |
| `--single-file-provider-column-symbol` | `<column>` | `symbol` | `string` | Change the symbol column name to use. |
| `--single-file-provider-column-quantity` | `<column>` | `quantity` | `string` | Change the quantity column name to use. |
| `--order-files` | `<directory>` | | `path` | The directory of order file to use. The filename must be a date. The file must contain symbol and quantity information. |
| `--order-files-extension` | `<extension>` | `csv` | `[csv, parquet, json]` | Change the file extension to use when listing for order files. |
| `--initial-cash` | `<amount>` | `100_000` | `number` | Change the initial cash to use for the backtesting. |
| `--quantity-mode` | `<mode>` | `percent` | `[percent, share]` | If the mode is `share`, all quantities will be interpreted as integers. If the mode is `percent`, all values will be multiplied by the current cash value. |
| `--weekends` | | `false` | | Enable ordering on weekends. |
| `--holidays` | | `false` | | Enable ordering on holidays. |
| `--symbol-mapping` | `<mapping>` | | `path` (.json) | Specify a custom symbol mapping file enabling vendor-id translation. |
| `--no-caching` | | `false` | | Disable prices caching. |
| `--fee-model` | `<model>` | | `expression` or `constant` | Specify a fee model to use. The value can be a `constant`. Or an expression that allow the usage of the `price` and `quantity` variable. <br /> Example: `abs(price * quantity) * 0.1` |
| `--holiday-provider` | `<name>` | `nyse` | `[legacy, nyse]` | Specify which holiday provider to use. |
| `--rfr-file` | `<directory>` | | `path` | The directory of rfr file to use. The file must contain a column with date information and a column with the rfr information in %. |
| `--rfr-file-column-date` | `<column>` | `date` | `string` | Change the date column name to use. |
### Exporters
Multiple exporters can be enabled at one time.
#### Console
The console exporter allows a quick look at the backtest.
| Option | Value | Default | Format | Description |
| --- | --- | --- | --- | --- |
| `--console` | | `false` | | Enable the console exporter. |
| `--console-format` | `<format>` | `text` | `[text, json]` | Change the output format. |
| `--console-file` | `<file>` | `out` | `[out, err]` | Change the output file. |
| `--console-hide-skips` | | `false` | | Do not the skipped days. |
| `--console-text-no-color` | | `false` | | Disable colors in the output. (only if the format is `text`) |
#### Dump
The dump exporter generate a dump of the portfolio at each day.
| Option | Value | Default | Format | Description |
| --- | --- | --- | --- | --- |
| `--dump` | | `false` | | Enable the dump exporter. |
| `--dump-output-file` | `<file>` | `dump.csv` | `path` | Specify the output file. |
| `--dump-auto-delete` | | `false` | | Automatically delete the previous dump file if it is present. |
#### QuantStats
Generate a tearsheet from the backtest data.
| Option | Value | Default | Format | Description |
| --- | --- | --- | --- | --- |
| `--quantstats` | | `false` | | Enable the quantstats exporter. |
| `--quantstats-output-file-html` | `<file>` | `report.html` | `path` | Specify the output file containing the tearsheet. |
| `--quantstats-output-file-csv` | `<file>` | `report.csv` | `path` | Specify the output file containing raw returns. |
| `--quantstats-benchmark-ticker` | `<ticker>` | `SPY` | `symbol` | Specify the ticker to use as a benchmark in the tearsheet. |
| `--quantstats-auto-delete` | | `false` | | Automatically delete the previous report files if they are present. |
#### PDF
Generate a tearsheet from a custom template.
| Option | Value | Default | Format | Description |
| --- | --- | --- | --- | --- |
| `--pdf` | | `false` | | Enable the pdf exporter. |
| `--pdf-template` | `<file>` | `tearsheet.sketch` | `path` | Specify the template file. |
| `--pdf-output-file` | `<file>` | `report.pdf` | `path` | Specify the output file. |
| `--pdf-auto-delete` | | `false` | | Automatically delete the previous report file if present. |
| `--pdf-debug` | | `false` | | Enable the pdf renderer's debugging tools. |
| `--pdf-variable` | `[<key> <value>]` | `[]` | `string` `string` | Add a custom variable. |
| `--pdf-user-script` | `[<file>]` | `[]` | `path` | Add a user script. |
#### Specific Return
Generate a tearsheet from the specific return backtest data.
| Option | Value | Default | Format | Description |
| --- | --- | --- | --- | --- |
| `--specific-return` | `<file>` | | `path` | Enable the specific return exporter by proving a file. |
| `--specific-return-column-date` | `<column>` | `date` | `string` | Specify the name of column containing the dates informations. |
| `--specific-return-column-symbol` | `<column>` | `symbol` | `string` | Specify the name of column containing the symbols informations. |
| `--specific-return-column-value` | `<column>` | `specific_return` | `string` | Specify the name of column containing the value informations. |
| `--specific-return-output-file-html` | `<file>` | `sr-report.html` | `path` | Specify the output file containing the tearsheet. |
| `--specific-return-output-file-csv` | `<file>` | `sr-report.csv` | `path` | Specify the output file containing raw returns. |
| `--specific-return-auto-delete` | | `false` | | Automatically delete the previous report files if they are present. |
### Data Sources
Only one data source can be used at once.
#### Yahoo
| Option | Value | Default | Format | Description |
| --- | --- | --- | --- | --- |
| `--yahoo` | | `false` | | Enable yahoo as the data source. |
#### CoinMarketCap
| Option | Value | Default | Format | Description |
| --- | --- | --- | --- | --- |
| `--coinmarketcap` | | `false` | | Enable coinmarketcap as the data source. |
| `--coinmarketcap-force-mapping-refresh` | | `false` | | Force a mapping refresh. This is usually only done automatically the first time of using this data source. |
| `--coinmarketcap-page-size` | `<size>` | `10_000` | `number` | Specify the page size while building the mapping. |
#### FactSet
| Option | Value | Default | Format | Description |
| --- | --- | --- | --- | --- |
| `--factset` | | `false` | | Enable factset as the data source. |
| `--factset-username-serial` | | `$FACTSET_USERNAME_SERIAL` | | Specify the factset's username serial to use. |
| `--factset-api-key` | | `$FACTSET_API_KEY` | | Specify the factset's api key to use. |
#### File
Use a static file as a price data source.
If another data source is specified, files sources will be used first in a delegated data source. Meaning that if the data are not available in the file, the next data source will be used.
##### .parquet
| Option | Value | Default | Format | Description |
| --- | --- | --- | --- | --- |
| `--file-parquet` | `<file>` | | `path` | Use a static `.parquet` file as a the data source. |
| `--file-parquet-column-date` | `<column>` | `date` | `string` | Specify the name of column containing the dates informations. |
| `--file-parquet-column-symbol` | `<column>` | `symbol` | `string` | Specify the name of column containing the symbols informations. |
| `--file-parquet-column-price` | `<column>` | `price` | `string` | Specify the name of column containing the prices informations. |
Raw data
{
"_id": null,
"home_page": "https://github.com/crunchdao/backtest",
"name": "bktest",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3",
"maintainer_email": null,
"keywords": "package development template",
"author": "Enzo CACERES",
"author_email": "enzo.caceres@crunchdao.com",
"download_url": "https://files.pythonhosted.org/packages/9c/43/36186b58a77e4bbb9f7f9af40ad513a159a3c71f91b6443560bc01408225/bktest-2.1.0.tar.gz",
"platform": null,
"description": "A small backtesting utility.\n\n![image](https://user-images.githubusercontent.com/7386242/189368488-b0c30d48-9a1f-4362-9f78-10a451535682.png)\n\n[![PyTest](https://github.com/crunchdao/backtest/actions/workflows/pytest.yml/badge.svg)](https://github.com/crunchdao/backtest/actions/workflows/pytest.yml)\n\n- [Install](#install)\n- [Usage](#usage)\n - [Options](#options)\n - [Exporters](#exporters)\n - [Console](#console)\n - [Dump](#dump)\n - [QuantStats](#quantstats)\n - [PDF](#pdf)\n - [Specific Return](#specific-return)\n - [Data Sources](#data-sources)\n - [Yahoo](#yahoo)\n - [CoinMarketCap](#coinmarketcap)\n - [FactSet](#factset)\n - [File](#file)\n - [.parquet](#parquet)\n\n# Install\n\n```\npip install --upgrade bktest\n```\n\n# Usage\n\n```bash\nbktest [OPTIONS]\n```\n\n## Options\n\n| Option | Value | Default | Format | Description |\n| --- | --- | --- | --- | --- |\n| `--start` | `<start date>` | `orders' first date` | `date` (ISO-8601) | The starting date of the backtesting. If the value is before the first ordering day, the value will be discarded. |\n| `--end` | `<end date>` | `orders' last date` | `date` (ISO-8601) | The ending date of the backtesting. If the value is after today, the value will be discarded. |\n| `--offset-before-trading` | `<days>` | `1` | `int` | Number of day to offset to push each date of the portfolio before trading it. |\n| `--offset-before-ending` | `<days>` | `0` | `int` | Number of day to continue the backtest after every orders. |\n| `--order-file` | `<file>` | | `path` | The single order file to use. The file must contain symbol, quantity and date information. |\n| `--single-file-provider-column-date` | `<column>` | `date` | `string` | Change the date column name to use. |\n| `--single-file-provider-column-symbol` | `<column>` | `symbol` | `string` | Change the symbol column name to use. |\n| `--single-file-provider-column-quantity` | `<column>` | `quantity` | `string` | Change the quantity column name to use. |\n| `--order-files` | `<directory>` | | `path` | The directory of order file to use. The filename must be a date. The file must contain symbol and quantity information. |\n| `--order-files-extension` | `<extension>` | `csv` | `[csv, parquet, json]` | Change the file extension to use when listing for order files. |\n| `--initial-cash` | `<amount>` | `100_000` | `number` | Change the initial cash to use for the backtesting. |\n| `--quantity-mode` | `<mode>` | `percent` | `[percent, share]` | If the mode is `share`, all quantities will be interpreted as integers. If the mode is `percent`, all values will be multiplied by the current cash value. |\n| `--weekends` | | `false` | | Enable ordering on weekends. |\n| `--holidays` | | `false` | | Enable ordering on holidays. |\n| `--symbol-mapping` | `<mapping>` | | `path` (.json) | Specify a custom symbol mapping file enabling vendor-id translation. |\n| `--no-caching` | | `false` | | Disable prices caching. |\n| `--fee-model` | `<model>` | | `expression` or `constant` | Specify a fee model to use. The value can be a `constant`. Or an expression that allow the usage of the `price` and `quantity` variable. <br /> Example: `abs(price * quantity) * 0.1` |\n| `--holiday-provider` | `<name>` | `nyse` | `[legacy, nyse]` | Specify which holiday provider to use. |\n| `--rfr-file` | `<directory>` | | `path` | The directory of rfr file to use. The file must contain a column with date information and a column with the rfr information in %. |\n| `--rfr-file-column-date` | `<column>` | `date` | `string` | Change the date column name to use. |\n\n### Exporters\n\nMultiple exporters can be enabled at one time.\n\n#### Console\n\nThe console exporter allows a quick look at the backtest.\n\n| Option | Value | Default | Format | Description |\n| --- | --- | --- | --- | --- |\n| `--console` | | `false` | | Enable the console exporter. |\n| `--console-format` | `<format>` | `text` | `[text, json]` | Change the output format. |\n| `--console-file` | `<file>` | `out` | `[out, err]` | Change the output file. |\n| `--console-hide-skips` | | `false` | | Do not the skipped days. |\n| `--console-text-no-color` | | `false` | | Disable colors in the output. (only if the format is `text`) |\n\n#### Dump\n\nThe dump exporter generate a dump of the portfolio at each day.\n\n| Option | Value | Default | Format | Description |\n| --- | --- | --- | --- | --- |\n| `--dump` | | `false` | | Enable the dump exporter. |\n| `--dump-output-file` | `<file>` | `dump.csv` | `path` | Specify the output file. |\n| `--dump-auto-delete` | | `false` | | Automatically delete the previous dump file if it is present. |\n\n#### QuantStats\n\nGenerate a tearsheet from the backtest data.\n\n| Option | Value | Default | Format | Description |\n| --- | --- | --- | --- | --- |\n| `--quantstats` | | `false` | | Enable the quantstats exporter. |\n| `--quantstats-output-file-html` | `<file>` | `report.html` | `path` | Specify the output file containing the tearsheet. |\n| `--quantstats-output-file-csv` | `<file>` | `report.csv` | `path` | Specify the output file containing raw returns. |\n| `--quantstats-benchmark-ticker` | `<ticker>` | `SPY` | `symbol` | Specify the ticker to use as a benchmark in the tearsheet. |\n| `--quantstats-auto-delete` | | `false` | | Automatically delete the previous report files if they are present. |\n\n#### PDF\n\nGenerate a tearsheet from a custom template.\n\n| Option | Value | Default | Format | Description |\n| --- | --- | --- | --- | --- |\n| `--pdf` | | `false` | | Enable the pdf exporter. |\n| `--pdf-template` | `<file>` | `tearsheet.sketch` | `path` | Specify the template file. |\n| `--pdf-output-file` | `<file>` | `report.pdf` | `path` | Specify the output file. |\n| `--pdf-auto-delete` | | `false` | | Automatically delete the previous report file if present. |\n| `--pdf-debug` | | `false` | | Enable the pdf renderer's debugging tools. |\n| `--pdf-variable` | `[<key> <value>]` | `[]` | `string` `string` | Add a custom variable. |\n| `--pdf-user-script` | `[<file>]` | `[]` | `path` | Add a user script. |\n\n#### Specific Return\n\nGenerate a tearsheet from the specific return backtest data.\n\n| Option | Value | Default | Format | Description |\n| --- | --- | --- | --- | --- |\n| `--specific-return` | `<file>` | | `path` | Enable the specific return exporter by proving a file. |\n| `--specific-return-column-date` | `<column>` | `date` | `string` | Specify the name of column containing the dates informations. |\n| `--specific-return-column-symbol` | `<column>` | `symbol` | `string` | Specify the name of column containing the symbols informations. |\n| `--specific-return-column-value` | `<column>` | `specific_return` | `string` | Specify the name of column containing the value informations. |\n| `--specific-return-output-file-html` | `<file>` | `sr-report.html` | `path` | Specify the output file containing the tearsheet. |\n| `--specific-return-output-file-csv` | `<file>` | `sr-report.csv` | `path` | Specify the output file containing raw returns. |\n| `--specific-return-auto-delete` | | `false` | | Automatically delete the previous report files if they are present. |\n\n### Data Sources\n\nOnly one data source can be used at once.\n\n#### Yahoo\n\n| Option | Value | Default | Format | Description |\n| --- | --- | --- | --- | --- |\n| `--yahoo` | | `false` | | Enable yahoo as the data source. |\n\n#### CoinMarketCap\n\n| Option | Value | Default | Format | Description |\n| --- | --- | --- | --- | --- |\n| `--coinmarketcap` | | `false` | | Enable coinmarketcap as the data source. |\n| `--coinmarketcap-force-mapping-refresh` | | `false` | | Force a mapping refresh. This is usually only done automatically the first time of using this data source. |\n| `--coinmarketcap-page-size` | `<size>` | `10_000` | `number` | Specify the page size while building the mapping. |\n\n#### FactSet\n\n| Option | Value | Default | Format | Description |\n| --- | --- | --- | --- | --- |\n| `--factset` | | `false` | | Enable factset as the data source. |\n| `--factset-username-serial` | | `$FACTSET_USERNAME_SERIAL` | | Specify the factset's username serial to use. |\n| `--factset-api-key` | | `$FACTSET_API_KEY` | | Specify the factset's api key to use. |\n\n#### File\n\nUse a static file as a price data source.\n\nIf another data source is specified, files sources will be used first in a delegated data source. Meaning that if the data are not available in the file, the next data source will be used.\n\n##### .parquet\n\n| Option | Value | Default | Format | Description |\n| --- | --- | --- | --- | --- |\n| `--file-parquet` | `<file>` | | `path` | Use a static `.parquet` file as a the data source. |\n| `--file-parquet-column-date` | `<column>` | `date` | `string` | Specify the name of column containing the dates informations. |\n| `--file-parquet-column-symbol` | `<column>` | `symbol` | `string` | Specify the name of column containing the symbols informations. |\n| `--file-parquet-column-price` | `<column>` | `price` | `string` | Specify the name of column containing the prices informations. |\n",
"bugtrack_url": null,
"license": null,
"summary": "bktest - A simple backtester by CrunchDAO",
"version": "2.1.0",
"project_urls": {
"Homepage": "https://github.com/crunchdao/backtest"
},
"split_keywords": [
"package",
"development",
"template"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "211e43629bb420c94df3360045c3e88ae226454d16fbfe33f6e171cb1e44f90f",
"md5": "f6f1fe8869981b46b0f00aa349434788",
"sha256": "d17ba428a2ad58af07769b4dbac93fdaeb735ca931a9586ea68c8e4394451bb3"
},
"downloads": -1,
"filename": "bktest-2.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f6f1fe8869981b46b0f00aa349434788",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3",
"size": 47109,
"upload_time": "2024-05-09T09:36:21",
"upload_time_iso_8601": "2024-05-09T09:36:21.052601Z",
"url": "https://files.pythonhosted.org/packages/21/1e/43629bb420c94df3360045c3e88ae226454d16fbfe33f6e171cb1e44f90f/bktest-2.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9c4336186b58a77e4bbb9f7f9af40ad513a159a3c71f91b6443560bc01408225",
"md5": "db931a9a23031f4d640b540d46b17608",
"sha256": "6a07bd06926b604bdfc3e50f7cc24e89add06beee44a27a5eb5f59bc8a850cd2"
},
"downloads": -1,
"filename": "bktest-2.1.0.tar.gz",
"has_sig": false,
"md5_digest": "db931a9a23031f4d640b540d46b17608",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3",
"size": 37947,
"upload_time": "2024-05-09T09:36:22",
"upload_time_iso_8601": "2024-05-09T09:36:22.761353Z",
"url": "https://files.pythonhosted.org/packages/9c/43/36186b58a77e4bbb9f7f9af40ad513a159a3c71f91b6443560bc01408225/bktest-2.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-09 09:36:22",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "crunchdao",
"github_project": "backtest",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "click",
"specs": [
[
">=",
"8.0"
],
[
"<",
"8.1.2"
]
]
},
{
"name": "pandas",
"specs": [
[
"<",
"2.0.0"
],
[
">=",
"1.5"
]
]
},
{
"name": "python-dateutil",
"specs": [
[
">=",
"2.8.2"
],
[
"<",
"2.8.3"
]
]
},
{
"name": "tqdm",
"specs": [
[
"<",
"4.65.0"
],
[
">=",
"4.48.0"
]
]
},
{
"name": "numpy",
"specs": [
[
"<",
"1.25.0"
],
[
">=",
"1.23.0"
]
]
},
{
"name": "py_expression_eval",
"specs": [
[
"<",
"0.3.14"
],
[
">=",
"0.3.9"
]
]
},
{
"name": "pyarrow",
"specs": [
[
">=",
"11.0"
],
[
"<",
"12.0"
]
]
},
{
"name": "quantstats",
"specs": [
[
"==",
"0.0.62"
]
]
},
{
"name": "pytest",
"specs": [
[
">=",
"7.1.0"
],
[
"<",
"7.3.0"
]
]
},
{
"name": "yfinance",
"specs": [
[
"<",
"0.2.27"
],
[
">=",
"0.2.24"
]
]
},
{
"name": "python-dotenv",
"specs": [
[
"<",
"1.0.0"
],
[
">=",
"0.20"
]
]
},
{
"name": "colorama",
"specs": [
[
">=",
"0.4.4"
],
[
"<",
"0.4.6"
]
]
},
{
"name": "ipython",
"specs": [
[
"==",
"8.15.0"
]
]
},
{
"name": "seaborn",
"specs": [
[
"<",
"0.13.0"
],
[
">=",
"0.12.0"
]
]
},
{
"name": "python-slugify",
"specs": []
},
{
"name": "cached-property",
"specs": []
},
{
"name": "fpdf2",
"specs": [
[
"==",
"2.7.4"
]
]
},
{
"name": "contexttimer",
"specs": []
},
{
"name": "watchdog",
"specs": []
},
{
"name": "fastparquet",
"specs": []
},
{
"name": "readwrite",
"specs": []
},
{
"name": "holidays",
"specs": []
}
],
"lcname": "bktest"
}