stock-pairs-trading is a python library for backtest with stock pairs trading using kalman filter on Python 3.8 and above.
Project description
stock-pairs-trading
stock-pairs-trading is a python library for backtest with stock pairs trading using kalman filter on Python 3.8 and above.
Installation
$ pip install stock-pairs-trading
Usage
find pairs
from stock_pairs_trading import StockPairsTrading
spt = StockPairsTrading(
start="2007-12-01",
end="2017-12-01",
)
r = spt.find_pairs(["AAPL", "ADBE", "MSFT", "IBM"])
print(r)
[('ADBE', 'MSFT')]
backtest
from pprint import pprint
from stock_pairs_trading import StockPairsTrading
spt = StockPairsTrading(
start="2007-12-01",
end="2017-12-01",
)
r = spt.backtest(('ADBE', 'MSFT'))
pprint(r)
{'cointegration': 0.0018311528816901195,
'correlation': 0.9858057442729853,
'maximum_drawdown': 34.801876068115234,
'profit_factor': 1.1214715644744209,
'riskreward_ratio': 0.8095390763424627,
'sharpe_ratio': 0.03606830691295276,
'total_profit': 35.97085762023926,
'total_trades': 520,
'win_rate': 0.5807692307692308}
latest signal
from pprint import pprint
from stock_pairs_trading import StockPairsTrading
spt = StockPairsTrading(
start="2007-12-01",
end="2017-12-01",
)
r = spt.latest_signal(("ADBE", "MSFT"))
pprint(r)
{'ADBE Adj Close': 299.5,
'ADBE Buy': True, # entry buy
'ADBE Cover': False, # exit buy
'ADBE Sell': False, # entry sell
'ADBE Short': False, # exit sell
'MSFT Adj Close': 244.74000549316406,
'MSFT Buy': False, # entry buy
'MSFT Cover': False, # exit buy
'MSFT Sell': True, # entry sell
'MSFT Short': False, # exit sell
'date': '2022-09-16',
'zscore': -36.830427514962274}
Advanced Usage
from stock_pairs_trading import StockPairsTrading
spt = StockPairsTrading(
start="2007-12-01",
end="2017-12-01",
outputs_dir_path = "outputs",
data_dir_path = "data",
column = "Adj Close",
window = 1,
transition_covariance = 0.01,
)
r = spt.backtest(('ADBE', 'MSFT'))
pprint(r)
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
Built Distribution
Close
Hashes for stock-pairs-trading-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f614378c55e093577efea232099b5362900e82730357328ef9cdfa797fbc38f |
|
MD5 | 134c739a40a329fbe754484acefd39aa |
|
BLAKE2b-256 | eb82d772beb2ab3b8c3416e3723b8e2b973101962d1056083100c18fb71b528b |
Close
Hashes for stock_pairs_trading-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ecedd36b835ff5ffec3698c2fa2ef9adbf9930cf1ebe2a4810f5e52992d937b |
|
MD5 | 040a6b472a726a2b0bf5fbfee01e1e97 |
|
BLAKE2b-256 | 6a0092a47d5dd079251302b54985e6ddfad0be9f33f88e7607a0af3e9c8bf42a |