Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🔎 📈 🐍 💰 Backtest trading strategies in Python.

License

NotificationsYou must be signed in to change notification settings

kernc/backtesting.py

Repository files navigation

Backtesting.py

Build StatusCode CoverageSource lines of codeBacktesting on PyPIPyPI downloadsTotal downloadsGitHub Sponsors

Backtest trading strategies with Python.

Project website +Documentation   | YouTube

Installation

$ pip install backtesting

Usage

frombacktestingimportBacktest,Strategyfrombacktesting.libimportcrossoverfrombacktesting.testimportSMA,GOOGclassSmaCross(Strategy):definit(self):price=self.data.Closeself.ma1=self.I(SMA,price,10)self.ma2=self.I(SMA,price,20)defnext(self):ifcrossover(self.ma1,self.ma2):self.buy()elifcrossover(self.ma2,self.ma1):self.sell()bt=Backtest(GOOG,SmaCross,commission=.002,exclusive_orders=True)stats=bt.run()bt.plot()

Results in:

Start                     2004-08-19 00:00:00End                       2013-03-01 00:00:00Duration                   3116 days 00:00:00Exposure Time [%]                       94.27Equity Final [$]                     68935.12Equity Peak [$]                      68991.22Return [%]                             589.35Buy & Hold Return [%]                  703.46Return (Ann.) [%]                       25.42Volatility (Ann.) [%]                   38.43CAGR [%]                                16.80Sharpe Ratio                             0.66Sortino Ratio                            1.30Calmar Ratio                             0.77Alpha [%]                              450.62Beta                                     0.02Max. Drawdown [%]                      -33.08Avg. Drawdown [%]                       -5.58Max. Drawdown Duration      688 days 00:00:00Avg. Drawdown Duration       41 days 00:00:00# Trades                                   93Win Rate [%]                            53.76Best Trade [%]                          57.12Worst Trade [%]                        -16.63Avg. Trade [%]                           1.96Max. Trade Duration         121 days 00:00:00Avg. Trade Duration          32 days 00:00:00Profit Factor                            2.13Expectancy [%]                           6.91SQN                                      1.78Kelly Criterion                        0.6134_strategy              SmaCross(n1=10, n2=20)_equity_curve                          Equ..._trades                       Size  EntryB...dtype: object

plot of trading simulation

Find more usage examples in thedocumentation.

Features

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

xkcd.com/1570

Bugs

Before reporting bugs or posting to thediscussion board,please readcontributing guidelines, particularly the sectionabout crafting useful bug reports and```-fencing your code. We thank you!

Alternatives

Seealternatives.md for a list of alternative Pythonbacktesting frameworks and related packages.

Sponsor this project

 

[8]ページ先頭

©2009-2025 Movatter.jp