stock-backtest is a python library for stock technical analysis backtest on Python 3.7 and above.
Project description
stock-backtest
stock-backtest is a python library for stock technical analysis backtest on Python 3.7 and above.
Installation
$ pip install stock-backtest
Usage
basic run
from stock_backtest import Backtest
class MyBacktest(Backtest):
def strategy(self):
fast_ma = self.sma(period=5)
slow_ma = self.sma(period=25)
# golden cross
self.sell_exit = self.buy_entry = (fast_ma > slow_ma) & (
fast_ma.shift() <= slow_ma.shift()
)
# dead cross
self.buy_exit = self.sell_entry = (fast_ma < slow_ma) & (
fast_ma.shift() >= slow_ma.shift()
)
MyBacktest("AAPL").run()
advanced run
from stock_backtest import Backtest
from pprint import pprint
class MyBacktest(Backtest):
def strategy(self):
rsi = self.rsi(period=10)
ema = self.ema(period=20)
atr = self.atr(period=20)
lower = ema - atr
upper = ema + atr
self.buy_entry = (rsi < 30) & (self.df.C < lower)
self.sell_entry = (rsi > 70) & (self.df.C > upper)
self.sell_exit = ema > self.df.C
self.buy_exit = ema < self.df.C
bt = MyBacktest(
"AAPL", # ticker
shares=100, # number of shares (default=1)
start="2010-01-01", # start date (default="")
end="2020-01-01", # end date (default="")
data_dir="data", # data directory (default=.)
)
pprint(bt.run(), sort_dicts=False)
{'total profit': -2779.465,
'total trades': 102,
'win rate': 66.667,
'profit factor': 0.641,
'maximum drawdown': 3147.5,
'recovery factor': -0.883,
'riskreward ratio': 0.321,
'sharpe ratio': -0.085,
'average return': -68.929,
'stop loss': 0,
'take profit': 0}
Supported indicators
- Simple Moving Average 'sma'
- Exponential Moving Average 'ema'
- Moving Average Convergence Divergence 'macd'
- Relative Strenght Index 'rsi'
- Bollinger Bands 'bbands'
- Stochastic Oscillator 'stoch'
- Average True Range 'atr'
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
stock-backtest-0.1.0.tar.gz
(5.9 kB
view hashes)
Built Distribution
Close
Hashes for stock_backtest-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67d2c62159956bbdb59416f00b843e75af6c4490dd7849fd8620a46a930b9dca |
|
MD5 | 277104f107e7377f6d2932d5a12ad68d |
|
BLAKE2b-256 | 998394d9c1e3a3239b681c897eb4ccc92f730f1802bbbc0c8a228e9473d002fb |