GithubHelp home page GithubHelp logo

alf28dev / statistical_arbitrage Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 1.0 23 KB

A project which performs a basic statistical arbitrage trading strategy based on the spread of two equites. Positions are opened and closed based on the period 15 lower Bollinger band.

Python 100.00%
arbitrage matplolib pandas-python python stock-market

statistical_arbitrage's Introduction

Statistical Arbitrage

I recently became interested in statistical arbitrage after reading the chapter about arbitrage in the book "Machine Learning For Algorithmic Trading". I have designed a basic strategy based on the correlation of two stocks. If two stocks have a high correlation, they are more likely to move in the same direction or the same patterns.

To profit from this strategy, we must track the spread between the stocks. We can trade the spread by buying and selling the stocks within the pair. An example spread is shown below with a bollinger band indicator.

Stat_Arb_Pairs

To identify pairs of stocks, I measured the level of correlation between two stock price time series. A basic scatter chart and linear regression are shown below, demonstrating a pair of stocks with a high correlation level.

Screenshot 2022-05-30 at 16 10 07

A key component of the statistical arbitrage strategy is knowing when to open and close positions. For my strategy, I chose to utilise a period 15 bollinger band. Whenever the spread drops below the lower band, I will open a position. The position will then be sold when the spread rises above the upper band. This strategy will only place long positions that profit when the spread widens (long bias).

Strategy Backtesting

Screenshot 2022-05-29 at 15 48 55

To test this strategy, I designed and developed my own backtest engine, which can take in data from the yfinance API and open/close trades as well as track/monitor positions and cumulative returns. The image above shows one of the output charts produced by my backtest engine. The entries and exits are clearly shown by the green (entries) and red (exits) triangles.

The cumulative returns of the strategy above are shown in the chart below. The system does not place a lot of trades; however it does seem to have a reasonably high win rate. I have also noticed that the win rate will increase dramatically based on the amount of correlation between the pairs. This becomes evident when using the strategy on Indices and DLC's.

Screenshot 2022-05-29 at 15 49 19

Index and DLC Statistical Arbitrage

Arbitrage using DLC'S

A DLC is a Dual-Listed Company. This is a company with a stock listed on more than one exchange. As exchanges are independent and markets are not perfect, price discrepancies will arise. A typical example of a dual-listed stock is GOOG and GOOGL. Below is the performance of my strategy on the GOOG and GOOGL pair. I have shown the spread, positions and returns of the backtest.

Screenshot 2022-05-30 at 21 33 25

Screenshot 2022-05-30 at 21 33 41

Screenshot 2022-05-30 at 21 33 51

Arbitrage using Indices

Indices are portfolios of stocks that move based on the portfolio's overall performance. Different indices can contain the same stocks. Therefore, they are likely to have the same performance over time. As indices have the same stocks, they have a high level of correlation, meaning it is potentially possible to profit when the indices diverge.

Furthur improvements

  • Research cointegration
  • Develop platform to manage statarb positions
  • Research sharpe ratios to measure performance

statistical_arbitrage's People

Contributors

alf28dev avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

hcyrine

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.