Skip to main content

rv_strategy_mb

Python Relative Value Trading Strategy

Package Documentation

Overview

This package is designed to execute relative value trades between two stock indices.

Users can specify two indices to analyse along with an observation period. The tool performs a simple Ordinary Least Squares (OLS) linear regression between the indices to extract the beta coefficient and calculate a z-score.

The package also uses the pybacktestchain package for better data retrieval, backtesting and trading signal generation.

Key Concepts

The underlying strategy relies on the assumption that asset prices or their ratios will revert to a historical mean. Significant deviations of the z-score from this mean, as indicated by the standard deviation, suggest potential mispricing between the two assets.

This presents an opportunity to trade in anticipation of a return to the mean.

Functionalities

1. Input parameters:

Two stock indices to analyse. An observation period for the regression analysis. A maximum budget for the trade.

2. Regression analysis:

The package runs an OLS linear regression to create a model, giving the mean and standard deviation of the residuals.

3. Z-score computation:

A z-score is calculated along with its mean and standard deviation over the specified period.

By looking at the z-score for trading signals, users can make informed decisions based on statistical indicators that reflect market efficiencies and mispricings.

If the current z-score exceeds 2 standard deviations from the mean, it suggests that Index 1 is overvalued relative to Index 2. Likewise, if it falls below -2 standard deviations, it indicates that Index 1 is undervalued.

4. Trading signals:

A "sell Index 1 and buy Index 2" signal is generated when the z-score is above 2 standard deviations.

The package provides recommendations for the quantities to buy or sell based on the maximum budget specified by the user and beta-adjusted from the one derived from the regression.

5. Blockchain integration:

All trading activities are recorded in a blockchain for traceability.

6. Backtesting:

The backtest class retrieves historical data to evaluate trading scenarios, giving the final portfolio value after potential trades.

Results from the backtest are also stored in the blockchain for future reference.

Modules

This package contains several modules:

  • executor.py: it is the main entry point for the application. It handles user input, retrieves historical stock data, and performs regression analysis to generate trading signals.
  • backtest.py: it executes the backtesting of trading strategies based on the generated signals. It calculates portfolio values over time based on buy/sell signals and plots the results.
  • signal_generator.py: it is responsible for generating trading signals based on the regression analysis results. It calculates Z-scores and determines buy, sell, or hold actions as well as suggests quantities to trade.

Installation

$ pip install rv_strategy_mb

Usage

  1. To run this module, execute the following command in your terminal:

    $ python executor.py

  2. Follow the prompt to input the indices, date range, and investment budget.

Performing Backtests: use the Backtest class in the backtest.py module to test the trading strategy.

Ensure that you are inserting the indices in a ticker format (i.e. ^DJI for the Dow Jones Industrial Average).

  1. Generating Trading Signals: use the TradingSignalGenerator class in signal_generator.py to perform regression analysis and generate signals.
from rv_strategy_mb.signal_generator import TradingSignalGenerator
from pybacktestchain.data_module import get_stocks_data
from pybacktestchain.broker import Broker
from datetime import datetime
import logging

#Set verbosity for logging
logging.basicConfig(level=logging.INFO)

#Define the parameters for the analysis
index1 = '^FTSE'  #First stock index
index2 = '^DJI'   #Second stock index
start_date = datetime(2020, 1, 1)  #Start date for historical data
end_date = datetime(2021, 1, 1)    #End date for historical data
portfolio_budget = 100000  #Budget for trading operations

#Initialise the signal generator
signal_generator = TradingSignalGenerator(
    index1=index1,
    index2=index2,
    start_date=start_date,
    end_date=end_date,
    portfolio_budget=portfolio_budget
)

#Get data and perform regression analysis
slope, intercept = signal_generator.perform_regression()

#Generate trading signals based on the regression output
signals, quantities = signal_generator.generate_signals(slope, intercept)

#Set up the broker to execute trades based on the generated signals
broker = Broker(cash=portfolio_budget, verbose=True)  #Specify initial cash and enable logging

#Run backtesting using the generated signals
for date, signal in signals.items():
    if signal == "Buy":
        broker.execute_portfolio(portfolio_budget // signal_generator.data[signal_generator.index1 + '_Close'][date], 
                                  signal_generator.data[signal_generator.index1 + '_Close'][date], date)
    elif signal == "Sell":
        broker.execute_portfolio(-portfolio_budget // signal_generator.data[signal_generator.index2 + '_Close'][date], 
                                 signal_generator.data[signal_generator.index2 + '_Close'][date], date)

#Display trading signals and quantities
print(f"Generated Trading Signals: {signals}")
print(f"Trade Quantities: {quantities}")

**Optional: Plot the Z-score to visualise trading strategy performance**
signal_generator.plot_z_score()

Requirements

Python 3.x

Required packages: pandas numpy matplotlib statsmodels numba logging yfinance

Dependencies

This package uses classes and modules from the pybacktestchain package, providing essential functionalities for data retrieval, backtesting, and trading signal generation.

Dependencies are managed with Poetry.

Testing

Running Tests The package includes unit tests to ensure the functionality of various components. To run the tests, you can use a testing framework such as unittest or pytest. Ensure you have pytest installed:

$ pip install pytest

Then, you can run the tests by executing the following command in your terminal from the root directory of the package:

$ pytest

Test files

The test files are designed to ensure that the different modules work together as intended and that updates to the code do not break existing functionalities.

Logging

The package uses the logging module to provide information, warnings, and error messages throughout execution.

License

rv_strategy_mb was created by Marion BIGOTTE. It is licensed under the terms of the MIT license.

Credits

rv_strategy_mb was created with cookiecutter and the py-pkgs-cookiecutter template.

Metadata

Release files for rv-strategy-mb 0.5.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 rv-strategy-mb 0.5.1
File Size Uploaded Provenance Actions
rv_strategy_mb-0.5.1.tar.gz 11.2 kB Without provenance Details for rv_strategy_mb-0.5.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for rv-strategy-mb 0.5.1
File Interpreter ABI Platform Actions
rv_strategy_mb-0.5.1-py3-none-any.whl Python 3 none any Details for rv_strategy_mb-0.5.1-py3-none-any.whl

Total release size: 22.2 kB

Release files / rv_strategy_mb-0.5.1.tar.gz

Download URL rv_strategy_mb-0.5.1.tar.gz
Size 11.2 kB
Tags Source
SHA-256 checksum
How to use checksums
a01a22e4981a14b89763ea5769e052eaa09038c5b93088b75cf39cb74388c7b8
BLAKE2b-256 checksum
How to use checksums
2eadaa66c912bc23b66d7bce234c9e671b3aa4f8c7761e4853f6e311dfe3b73e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.0 CPython/3.11.8 Darwin/20.6.0

Release files / rv_strategy_mb-0.5.1-py3-none-any.whl

Download URL rv_strategy_mb-0.5.1-py3-none-any.whl
Size 11.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5fd66fc2e7a4693d9e861e91ea909640e2e8cafa8b51b0e121ed7c79b8f76637
BLAKE2b-256 checksum
How to use checksums
2dfe9e71219ee68a5a11b94ed3d45b359a12637d98451e56097b4b4db78a72b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.0 CPython/3.11.8 Darwin/20.6.0

Security for rv-strategy-mb 0.5.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.5.1 This release

2 release files No provenance

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