Skip to main content

Get the cointegration result of a pair of stocks.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stocks_pair_cointegration-0.0.1.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stocks_pair_cointegration-0.0.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file stocks_pair_cointegration-0.0.1.tar.gz.

File metadata

  • Download URL: stocks_pair_cointegration-0.0.1.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.7 Darwin/22.6.0

File hashes

Hashes for stocks_pair_cointegration-0.0.1.tar.gz
Algorithm Hash digest
SHA256 9783bc0fd6ab15902189433ace15169b7f6025d7d367d31a6945343e8abcabfb
MD5 d7244957d4d4379bb2014ae3cd7f9ff4
BLAKE2b-256 5d33efb3b56e12a5440a55da347875ceda9e57ba047fc0da1e47b0316feaa6b1

See more details on using hashes here.

File details

Details for the file stocks_pair_cointegration-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for stocks_pair_cointegration-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 58fcae384bb26c88c4e08cc97fd466ca89721d9f4a58814745fe785337693420
MD5 609f14cc19e0534886ce0f97c2a1ad93
BLAKE2b-256 cde5e07518e702b23892eef8a9a5e1deb4913a7844281d78fe4ff11571c73118

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page