Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork1.2k
🔎 📈 🐍 💰 Backtest trading strategies in Python.
License
kernc/backtesting.py
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Backtest trading strategies with Python.
Project website +Documentation | YouTube
$ pip install backtesting
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
Find more usage examples in thedocumentation.
- 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
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!
Seealternatives.md for a list of alternative Pythonbacktesting frameworks and related packages.
About
🔎 📈 🐍 💰 Backtest trading strategies in Python.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.