Skip to main content

Stock Pair Cointegration

pip install stock_pair_cointegration

Introduction

Pair trading is based on one assumption Mean Reverting, that a over values stock the price will go down, and a under valued stock the price will go up in the end. Pair trading is to trade a pair of cointegrated stocks at the same time, that is to take a long position of the under valued stock and at the same time take a short position of the over valued stock.

This library is to help you to calculate the cointegration result of a pair of stocks.

How pair trading works

As we mentioned below, pair trading is to trade a pair of cointegrated stocks at the same time, for a pair in the market,

For example,

cointegrated example

The spread of the pair in long run, is mean reverting, that is, the spread will go back to the mean value, so if we trade when the spread is large and exit when the spread is small, it will be a gain.

Surely market is not a static system, spread, cointegration relation keep changing, but if we keeping attention on it, we could keep finding the cointegrated pairs and updated the hedge ratio.

  • We found Stock A and Stock B are highly cointegrated.
  • When the spread is smallert than -2 standard deviation, we take a long position of Stock A and a short position of Stock B.
  • When the spread is larger than 2 standard deviation, we take a short position of Stock A and a long position of Stock B.
  • When the spread is between -1 and 1 standard deviation, we exit the position.

Let's say we are short A and long B, there are only these possibilities:

# Scenario Strategy Performance
1 A is down, B is up +++, best case for the strategy
2 A and B both are down +, earned from A, lost from B
3 A and B both are up +, earned from B, lost from A
4 A is up, B is down -, loss, spread is going to be small, pair strategy will most exit soon if keep going the same direction

So basically, all dependes on probability, this is why we need to find the cointegrated stocks. Because they will be each other's hedge and signal, which is make it in high probability to fall into case 1 to 3, but low probability to fall into case 4.

In sum, it is a very old yet effective strategy, even in case 2, when both stocks are down, we still earn money or loss less than the market average.

Where does Pair Trading be used?

  • Gold and USD
    • Imagine you are a gold miner, you have a lot of gold, and you want to hedge the risk of gold price going down.
  • Target Currency and USD
    • Imagine your business is in Australis, but you have a lot of USD from businees, you want to hedge the risk of JPY going down.
  • Cointegrated Stock Pair
    • This is why this library is created, to help you to find the cointegrated stock pair.
  • Others

Metadata

Release files for stocks-pair-cointegration 0.0.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 stocks-pair-cointegration 0.0.1
File Size Uploaded
stocks_pair_cointegration-0.0.1.tar.gz 4.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for stocks-pair-cointegration 0.0.1
File Interpreter ABI Platform
stocks_pair_cointegration-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 9.0 kB

Release files / stocks_pair_cointegration-0.0.1.tar.gz

Download URL stocks_pair_cointegration-0.0.1.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
9783bc0fd6ab15902189433ace15169b7f6025d7d367d31a6945343e8abcabfb
BLAKE2b-256 checksum
How to use checksums
5d33efb3b56e12a5440a55da347875ceda9e57ba047fc0da1e47b0316feaa6b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.7 Darwin/22.6.0

Release files / stocks_pair_cointegration-0.0.1-py3-none-any.whl

Download URL stocks_pair_cointegration-0.0.1-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
58fcae384bb26c88c4e08cc97fd466ca89721d9f4a58814745fe785337693420
BLAKE2b-256 checksum
How to use checksums
cde5e07518e702b23892eef8a9a5e1deb4913a7844281d78fe4ff11571c73118
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.7 Darwin/22.6.0

Release history Release notifications | RSS feed

This release

0.0.1 This release

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