Python Trading Strategy
Project description
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
-
To run this module, execute the following command in your terminal:
$ python executor.py
-
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).
- Generating Trading Signals: use the
TradingSignalGeneratorclass insignal_generator.pyto 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rv_strategy_mb-0.5.1.tar.gz.
File metadata
- Download URL: rv_strategy_mb-0.5.1.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.0 CPython/3.11.8 Darwin/20.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a01a22e4981a14b89763ea5769e052eaa09038c5b93088b75cf39cb74388c7b8
|
|
| MD5 |
748b2cbbbd48049855eba46b73df239a
|
|
| BLAKE2b-256 |
2eadaa66c912bc23b66d7bce234c9e671b3aa4f8c7761e4853f6e311dfe3b73e
|
File details
Details for the file rv_strategy_mb-0.5.1-py3-none-any.whl.
File metadata
- Download URL: rv_strategy_mb-0.5.1-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.0 CPython/3.11.8 Darwin/20.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5fd66fc2e7a4693d9e861e91ea909640e2e8cafa8b51b0e121ed7c79b8f76637
|
|
| MD5 |
f676e6937fe9e3b9428f942bdac80b32
|
|
| BLAKE2b-256 |
2dfe9e71219ee68a5a11b94ed3d45b359a12637d98451e56097b4b4db78a72b7
|