os, matplotlib, pandas, numpy, tqdm, sklearn, sys, statsmodels.api
The main.py
file orchestrates the workflow for analyzing financial data, generating signals, and backtesting a trading strategy. It utilizes classes and methods defined in other files to carry out the analysis and backtesting over a specified date range.
- Data Preparation: Reads price and universe data.
- Signal Generation: Utilizes
SignalGenerator
to create trading signals based on predefined thresholds. - Analysis: Uses
FinancialAnalyzer
to perform various analyses, including computing s-scores and plotting results. - Backtesting: Employs
PortfolioBacktester
to simulate trading based on generated signals and evaluates performance.
- Date ranges for analysis and backtesting.
- Thresholds for signal generation.
- Tokens to include in the analysis.
Run the script directly to perform the entire workflow from data preparation to backtesting.
Defined in Backtest.py
, the PortfolioBacktester
class simulates trading based on input signals and evaluates the performance of a trading strategy.
backtest_portfolio
: Simulates trading over a given period based on trading signals and price data. It tracks portfolio value and records transactions.process_signal
: Processes individual trading signals and updates portfolio holdings and cash.plot_cumulative_return_and_calculate_metrics
: Plots cumulative returns of the portfolio and calculates performance metrics such as Sharpe ratio and maximum drawdown.
The portfolio is managed by executing trades based on signals and adjusting positions while considering transaction fees.
FinancialAnalyzer
and utils.py
contain functionalities for analyzing financial data, such as selecting common tokens, computing returns, and applying PCA for risk factor construction.
- Selection of Common Tokens: Identifies tokens with significant market presence over a specified time frame.
- Hourly Returns Calculation: Computes hourly returns for selected tokens.
- Risk Factor Construction: Uses PCA to identify principal components and constructs risk factors.
- s-Score Calculation: Estimates Ornstein-Uhlenbeck parameters and calculates s-scores for trading signal generation.
These scripts also provide methods for visualizing cumulative returns, eigenportfolio weights, and s-scores, aiding in the interpretation of analysis results.
Parameters that can be modified to generate different results include:
- Starting capital in the PortfolioBacktester class
- Transaction fees in the PortfolioBacktester class
- Maximum shares per stock in the PortfolioBacktester class
- Thresholds for generating trading signals in the FinancialAnalyzer class
- Ensure all dependencies are installed.
- Run the
main.py
script to execute the backtesting process. - Modify the parameters as needed in the script for different scenarios.
python main.py