{"repo":"kernc/backtesting.py","free":true,"listed":false,"github":"https://github.com/kernc/backtesting.py","clone":"git clone https://github.com/kernc/backtesting.py.git","description":"🔎 📈 🐍 💰  Backtest trading strategies in Python.","language":"Python","stars":8858,"topics":["algo-trading","algorithmic-trading","backtesting","backtesting-engine","backtesting-frameworks","backtesting-trading-strategies","finance","financial-markets","forex","forex-trading","framework","hacktoberfest","investing","investment","investment-strategies","stocks","trading","trading-algorithms","trading-simulator","trading-strategies"],"license":"AGPL-3.0","category":"dev_tool","readme_excerpt":"[![](https://i.imgur.com/E8Kj69Y.png)](https://kernc.github.io/backtesting.py/)\n\nBacktesting.py\n==============\n[![Build Status](https://img.shields.io/github/actions/workflow/status/kernc/backtesting.py/ci.yml?branch=master&style=for-the-badge)](https://github.com/kernc/backtesting.py/actions)\n[![Code Coverage](https://img.shields.io/codecov/c/gh/kernc/backtesting.py.svg?style=for-the-badge&label=Covr)](https://codecov.io/gh/kernc/backtesting.py)\n[![Source lines of code](https://img.shields.io/endpoint?url=https%3A%2F%2Fghloc.vercel.app%2Fapi%2Fkernc%2Fbacktesting.py%2Fbadge?filter=.py%26format=human&style=for-the-badge&label=SLOC&color=skyblue)](https://ghloc.vercel.app/kernc/backtesting.py)\n[![Backtesting on PyPI](https://img.shields.io/pypi/v/backtesting.svg?color=blue&style=for-the-badge)](https://pypi.org/project/backtesting)\n[![PyPI downloads](https://img.shields.io/pypi/dd/backtesting.svg?style=for-the-badge&label=D/L&color=skyblue)](https://pypistats.org/packages/backtesting)\n[![Total downloads](https://img.shields.io/pepy/dt/backtesting?style=for-the-badge&label=%E2%88%91&color=skyblue)](https://pypistats.org/packages/backtesting)\n[![Stars](https://img.shields.io/github/stars/kernc/backtesting.py?color=silver&style=for-the-badge&label=%e2%ad%90)](https://github.com/kernc/backtesting.py)\n[![GitHub Sponsors](https://img.shields.io/github/sponsors/kernc?color=pink&style=for-the-badge&label=%E2%99%A5)](https://github.com/sponsors/kernc)\n\nBacktest trading strategies with Python.\n\n[**Project website**](https://kernc.github.io/backtesting.py) + [Documentation] &nbsp;&nbsp;|&nbsp; [YouTube]\n\n[Documentation]: https://kernc.github.io/backtesting.py/doc/backtesting/\n[YouTube]: https://www.youtube.com/results?q=%22backtesting.py%22\n\nInstallation\n------------\n\n    $ pip install backtesting\n\nOr if you prefer the bleeding edge:\n\n    $ pip install git+https://github.com/kernc/backtesting.py\n\n\nUsage\n-----\n```python\nfrom backtesting import Backtest, Strategy\nfrom backtesting.lib import crossover\n\nfrom backtesting.test import SMA, GOOG\n\n\nclass SmaCross(Strategy):\n    def init(self):\n        price = self.data.Close\n        self.ma1 = self.I(SMA, price, 10)\n        self.ma2 = self.I(SMA, price, 20)\n\n    def next(self):\n        if crossover(self.ma1, self.ma2):\n            self.buy()\n        elif crossover(self.ma2, self.ma1):\n            self.sell()\n\n\nbt = Backtest(GOOG, SmaCross, commission=.002,\n              exclusive_orders=True)\nstats = bt.run()\nbt.plot()\n```\n\nResults in:\n\n```text\nStart                     2004-08-19 00:00:00\nEnd                       2013-03-01 00:00:00\nDuration                   3116 days 00:00:00\nExposure Time [%]                       94.27\nEquity Final [$]                     68935.12\nEquity Peak [$]                      68991.22\nReturn [%]                             589.35\nBuy & Hold Return [%]                  703.46\nReturn (Ann.) [%]                       25.42\nVolatility (Ann.) [%]                   38.43\nCAGR [%]                                16.80\nSharpe Ratio                             0.66\nSortino Ratio                            1.30\nCalmar Ratio                             0.77\nAlpha [%]                              450.62\nBeta                                     0.02\nMax. Drawdown [%]                      -33.08\nAvg. Drawdown [%]                       -5.58\nMax. Drawdown Duration      688 days 00:00:00\nAvg. Drawdown Duration       41 days 00:00:00\n# Trades                                   93\nWin Rate [%]                            53.76\nBest Trade [%]                          57.12\nWorst Trade [%]                        -16.63\nAvg. Trade [%]                           1.96\nMax. Trade Duration         121 days 00:00:00\nAvg. Trade Duration          32 days 00:00:00\nProfit Factor                            2.13\nExpectancy [%]                           6.91\nSQN                                      1.78\nKelly Criterion                        0.6134\n_strategy              SmaCross(n1=10, n2=20)\n_equity_curve                          Equ...\n_trades                       Size  EntryB...\ndtype: object\n```\n[![plot of trading simulation](https://i.imgur.com/xRFNHfg.png)](https://kernc.github.io/backtesting.py/#example)\n\nFind more usage examples in the [documentation].\n\n\nFeatures\n--------\n* Simple, [well-documented API](https://kernc.github.io/backtesting.py/doc/backtesting/backtesting.html)\n* Blazing fast execution\n* Built-in [optimizer](https://kernc.github.io/backtesting.py/doc/examples/Quick%20Start%20User%20Guide.html#Optimization)\n  based on [SAMBO](https://sambo-optimization.github.io)\n* [Library of composable base strategies](https://kernc.github.io/backtesting.py/doc/examples/Strategies%20Library.html)\n  and related utilities\n* Indicator-library-agnostic (BYO)\n* Supports _any_ financial instrument with OHLC(V) candlestick data\n* [Detailed trade results](https://kernc.github.io/backtesting.py/doc/examples/Quick%20Start%20User%20Guide.html#Trade-data)\n  provided as simple Series/DataFrame objects\n* [Interactive visualizations](https://kernc.github.io/backtesting.py/#example)\n\n![xkcd.com/1570](https://imgs.xkcd.com/comics/engineer_syllogism.png)\n\n\nBugs\n----\nBefore reporting bugs or posting to the\n[discussion board](https://github.com/kernc/backtesting.py/discussions),\nplease read [contributing guidelines](CONTRIBUTING.md), particularly the section\nabout crafting useful bug reports and ```` ``` ````-fencing your code.\nThe maintainers thank you!\n\n\nAlternatives\n------------\nSee [alternatives.md] for a list of alternative Python\nbacktesting frameworks and related packages.\n\n[alternatives.md]: https://github.com/kernc/backtesting.py/blob/master/doc/alternatives.md\n","default_branch":"master","files":53,"tree":[".codecov.yml",".github/FUNDING.yml",".github/ISSUE_TEMPLATE/1-bug.yml",".github/ISSUE_TEMPLATE/2-enh.yml",".github/ISSUE_TEMPLATE/config.yml",".github/deploy-gh-pages.sh",".github/issue_template.md",".github/workflows/ci.yml",".github/workflows/deploy-docs.yml",".gitignore","CHANGELOG.md","CONTRIBUTING.md","LICENSE.md","MANIFEST.in","README.md","backtesting/__init__.py","backtesting/_plotting.py","backtesting/_stats.py","backtesting/_util.py","backtesting/autoscale_cb.js","backtesting/backtesting.py","backtesting/lib.py","backtesting/test/BTCUSD.csv","backtesting/test/EURUSD.csv","backtesting/test/GOOG.csv","backtesting/test/__init__.py","backtesting/test/__main__.py","backtesting/test/_test.py","doc/README.md","doc/alternatives.md","doc/build.sh","doc/examples/Multiple Time Frames.ipynb","doc/examples/Multiple Time Frames.py","doc/examples/Parameter Heatmap & Optimization.ipynb","doc/examples/Parameter Heatmap & Optimization.py","doc/examples/Quick Start User Guide.ipynb","doc/examples/Quick Start User Guide.py","doc/examples/Strategies Library.ipynb","doc/examples/Strategies Library.py","doc/examples/Trading with Machine Learning.ipynb","doc/examples/Trading with Machine Learning.py","doc/logo.png","doc/pdoc_template/config.mako","doc/pdoc_template/credits.mako","doc/pdoc_template/head.mako","doc/pdoc_template/logo.mako","doc/scripts/ipython_config.py","doc/scripts/logo.py","doc/scripts/strip_yaml.awk","pyproject.toml","requirements.txt","setup.cfg","setup.py"],"storefront":"/r/kernc","claimed":false,"request_supported":{"post":"https://gitbuyer.com/r/kernc/backtesting.py/request-supported","requests":0},"note":"indexed from public GitHub; nothing is for sale on this page. Clone it from GitHub. Paid listings live at /search."}