https://github.com/kernc/backtesting.py

πŸ”Ž πŸ“ˆ 🐍 πŸ’° Backtest trading strategies in Python.

https://github.com/kernc/backtesting.py

Science Score: 36.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • β—‹
    CITATION.cff file
  • βœ“
    codemeta.json file
    Found codemeta.json file
  • βœ“
    .zenodo.json file
    Found .zenodo.json file
  • β—‹
    DOI references
  • β—‹
    Academic publication links
  • βœ“
    Committers with academic emails
    1 of 35 committers (2.9%) from academic institutions
  • β—‹
    Institutional organization owner
  • β—‹
    JOSS paper metadata
  • β—‹
    Scientific vocabulary similarity
    Low similarity (9.4%) to scientific vocabulary

Keywords

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
Last synced: 5 months ago · JSON representation

Repository

πŸ”Ž πŸ“ˆ 🐍 πŸ’° Backtest trading strategies in Python.

Basic Info
Statistics
  • Stars: 7,084
  • Watchers: 126
  • Forks: 1,276
  • Open Issues: 52
  • Releases: 0
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
Created about 7 years ago · Last pushed 7 months ago
Metadata Files
Readme Changelog Contributing Funding License

README.md

Backtesting.py

Build Status Code Coverage Source lines of code Backtesting on PyPI PyPI downloads Total downloads GitHub Sponsors

Backtest trading strategies with Python.

Project website + Documentation   |  YouTube

Installation

$ pip install backtesting

Usage

```python from backtesting import Backtest, Strategy from backtesting.lib import crossover

from backtesting.test import SMA, GOOG

class SmaCross(Strategy): def init(self): price = self.data.Close self.ma1 = self.I(SMA, price, 10) self.ma2 = self.I(SMA, price, 20)

def next(self):
    if crossover(self.ma1, self.ma2):
        self.buy()
    elif crossover(self.ma2, self.ma1):
        self.sell()

bt = Backtest(GOOG, SmaCross, commission=.002, exclusive_orders=True) stats = bt.run() bt.plot() ```

Results in:

```text Start 2004-08-19 00:00:00 End 2013-03-01 00:00:00 Duration 3116 days 00:00:00 Exposure Time [%] 94.27 Equity Final [$] 68935.12 Equity Peak [$] 68991.22 Return [%] 589.35 Buy & Hold Return [%] 703.46 Return (Ann.) [%] 25.42 Volatility (Ann.) [%] 38.43 CAGR [%] 16.80 Sharpe Ratio 0.66 Sortino Ratio 1.30 Calmar Ratio 0.77 Alpha [%] 450.62 Beta 0.02 Max. Drawdown [%] -33.08 Avg. Drawdown [%] -5.58 Max. Drawdown Duration 688 days 00:00:00 Avg. Drawdown Duration 41 days 00:00:00

Trades 93

Win Rate [%] 53.76 Best Trade [%] 57.12 Worst Trade [%] -16.63 Avg. Trade [%] 1.96 Max. Trade Duration 121 days 00:00:00 Avg. Trade Duration 32 days 00:00:00 Profit Factor 2.13 Expectancy [%] 6.91 SQN 1.78 Kelly Criterion 0.6134 strategy SmaCross(n1=10, n2=20) _equitycurve Equ... _trades Size EntryB... dtype: object ``` plot of trading simulation

Find more usage examples in the documentation.

Features

  • Simple, well-documented API
  • Blazing fast execution
  • Built-in optimizer
  • Library of composable base strategies and utilities
  • Indicator-library-agnostic
  • Supports any financial instrument with candlestick data
  • Detailed results
  • Interactive visualizations

xkcd.com/1570

Bugs

Before reporting bugs or posting to the discussion board, please read contributing guidelines, particularly the section about crafting useful bug reports and ```-fencing your code. We thank you!

Alternatives

See alternatives.md for a list of alternative Python backtesting frameworks and related packages.

Owner

  • Login: kernc
  • Kind: user

Committers

Last synced: 9 months ago

All Time
  • Total Commits: 410
  • Total Committers: 35
  • Avg Commits per committer: 11.714
  • Development Distribution Score (DDS): 0.1
Past Year
  • Commits: 132
  • Committers: 17
  • Avg Commits per committer: 7.765
  • Development Distribution Score (DDS): 0.144
Top Committers
Name Email Commits
Kernc k****e@g****m 369
Billy Cao a****o@g****m 2
Eric Ervin e****n@r****m 2
Ivan Vaigult i****t@g****m 2
NEMi t****k@g****m 2
TankorSmash t****b@g****m 2
crazy25000 c****0 2
jensnesten 4****n 2
Diego Lovison d****n@g****m 1
CtrlX 7****x 1
Chuan c****i@g****m 1
BΓ©nouare 2****e 1
Andrew Schwartz a****w@m****m 1
Andrew q****e 1
Andre Miras A****s 1
Ali Gheshlaghi a****7@g****m 1
tani3010 t****0@g****m 1
Jack McPherson j****b@g****m 1
t t****s@t****m 1
paklong p****6@g****m 1
lachyn21 7****1 1
arkershaw a****w 1
Zeel Patel z****l 1
Valentin Mikhaylenko v****o@g****m 1
Tony 3****n 1
Tomasz Łakota 4****a 1
Syed Kawish Qayyum 8****m 1
Sean Kernitsman s****l@g****m 1
S. Alexander Zaman a****n@c****m 1
Murat Saglam 4****m 1
and 5 more...
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 275
  • Total pull requests: 114
  • Average time to close issues: about 1 year
  • Average time to close pull requests: 11 months
  • Total issue authors: 213
  • Total pull request authors: 54
  • Average comments per issue: 2.57
  • Average comments per pull request: 1.17
  • Merged pull requests: 45
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 56
  • Pull requests: 38
  • Average time to close issues: 13 days
  • Average time to close pull requests: 20 days
  • Issue authors: 42
  • Pull request authors: 15
  • Average comments per issue: 0.75
  • Average comments per pull request: 0.74
  • Merged pull requests: 18
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • ironhak (8)
  • bravegag (6)
  • kernc (6)
  • rusty-peilin (4)
  • zeshansari (4)
  • ck0099 (4)
  • trueToastedCode (4)
  • vladiscripts (3)
  • kbs-code (3)
  • gypsyzz (3)
  • jsulopzs (3)
  • zha0yangchen (3)
  • Ish2K (2)
  • efeint01 (2)
  • eromoe (2)
Pull Request Authors
  • kernc (10)
  • jensnesten (6)
  • zlpatel (6)
  • abhirathmahipal (5)
  • ivaigult (4)
  • tani3010 (4)
  • gitctrlx (2)
  • lachyn21 (2)
  • themantalope (2)
  • abd1rahmane (2)
  • arkershaw (2)
  • jmcph4 (2)
  • Benouare (2)
  • diegolovison (2)
  • IperGiove (2)
Top Labels
Issue Labels
bug (42) invalid (39) question (34) enhancement (22) duplicate (17) help wanted (9) wontfix (7) Hacktoberfest (7) top-choice (6) good first issue (6) upstream (4) API (2)
Pull Request Labels
good first issue (1) Hacktoberfest (1)

Packages

  • Total packages: 6
  • Total downloads:
    • pypi 78,830 last-month
  • Total dependent packages: 2
    (may contain duplicates)
  • Total dependent repositories: 22
    (may contain duplicates)
  • Total versions: 59
  • Total maintainers: 4
pypi.org: backtesting

Backtest trading strategies in Python

  • Versions: 26
  • Dependent Packages: 2
  • Dependent Repositories: 21
  • Downloads: 77,982 Last month
Rankings
Stargazers count: 1.1%
Forks count: 1.5%
Downloads: 2.6%
Average: 2.6%
Dependent repos count: 3.2%
Dependent packages count: 4.7%
Maintainers (1)
Last synced: 6 months ago
pypi.org: foxbt

Backtest trading strategies in Python

  • Versions: 11
  • Dependent Packages: 0
  • Dependent Repositories: 1
  • Downloads: 17 Last month
Rankings
Dependent packages count: 10.0%
Dependent repos count: 21.7%
Average: 25.8%
Downloads: 28.5%
Forks count: 29.8%
Stargazers count: 38.8%
Maintainers (1)
Last synced: 5 months ago
pypi.org: multibacktesting2

Backtest trading strategies in Python

  • Versions: 5
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 42 Last month
Rankings
Dependent packages count: 10.3%
Average: 34.3%
Dependent repos count: 58.2%
Maintainers (1)
Last synced: 6 months ago
pypi.org: multiplebacktesting2

Backtest trading strategies in Python

  • Versions: 11
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 751 Last month
Rankings
Dependent packages count: 10.5%
Average: 34.7%
Dependent repos count: 59.0%
Maintainers (1)
Last synced: about 1 year ago
pypi.org: multiple-backtesting

Backtest trading strategies in Python

  • Versions: 2
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 15 Last month
Rankings
Dependent packages count: 10.7%
Average: 35.4%
Dependent repos count: 60.1%
Maintainers (1)
Last synced: 5 months ago
pypi.org: multibacktesting

Backtest trading strategies in Python

  • Versions: 4
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 23 Last month
Rankings
Dependent packages count: 10.7%
Average: 35.4%
Dependent repos count: 60.1%
Maintainers (1)
Last synced: 6 months ago

Dependencies

setup.py pypi
  • bokeh *
  • numpy *
  • pandas *
.github/workflows/ci.yml actions
  • actions/cache v3 composite
  • actions/checkout v3 composite
  • actions/setup-python v4 composite
.github/workflows/deploy-docs.yml actions
  • actions/cache v2 composite
  • actions/checkout v2 composite
  • actions/setup-python v2 composite
pyproject.toml pypi
requirements.txt pypi