Skip to main content

stock-pairs-trading

PyPI License: MIT codecov Build Status PyPI - Python Version Downloads

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')]

pairs.png

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}

performance.png

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 pprint import pprint
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)

Metadata

Release files for stock-pairs-trading 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stock-pairs-trading 0.1.1
File Size Uploaded
stock-pairs-trading-0.1.1.tar.gz 5.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for stock-pairs-trading 0.1.1
File Interpreter ABI Platform
stock_pairs_trading-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.9 kB

Release files / stock-pairs-trading-0.1.1.tar.gz

Download URL stock-pairs-trading-0.1.1.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
d11fe6cf551b9597b87e612dce46804597f8dc79f556df1dd8691dc0b12f7e55
BLAKE2b-256 checksum
How to use checksums
ace6a88ded68eab43cf6e774d09dc929fc0c3381f813bf584e80f5f84c37d7c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release files / stock_pairs_trading-0.1.1-py3-none-any.whl

Download URL stock_pairs_trading-0.1.1-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
00264ada4d452e6ed230dc3a2736b81a59a16823e2dce2dafa32fedafede9f2e
BLAKE2b-256 checksum
How to use checksums
4060ef7f9da39391c07e7c88d0d62b0671f03c8af12432c714ba1019952823af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page