Simple Python snippets to get started with the Unravel API for portfolio backtesting and live weight retrieval.
This repository provides transparent, easy-to-understand code examples for:
- Getting Started: Quick setup and basic usage of the Unravel API
- Backtest Validation: Transparent backtesting code to validate portfolio performance
- Live Weights: Simple access to current portfolio allocations
replicate_portfolio_backtest.py- Transparent backtesting implementation with transaction costs
- Historical portfolio weights retrieval
- Performance visualization
get_live_weights.py: Real-time portfolio weight access for current allocations
- Basic factor analysis using AlphaLens
- Portfolio factor historical data
factor_analysis_altair.py: Factor analysis for the Altair portfolio using AlphaLensfactor_analysis_carry_enhanced.py: Factor analysis for the Carry Enhanced portfoliofactor_analysis_retail_flow.py: Factor analysis for the Retail Flow portfoliofactor_returns_correlation.py: Cross-sectional returns correlation analysis between multiple portfolios
convert_to_notebooks.py: Script to convert all Python files to Jupyter notebooksanalysis/: Utility modules for backtesting, plotting, price data, and factor analysis
# Clone the repository
git clone https://github.com/unravel-finance/api-snippets.git
cd api-snippets
# Install the unravel-client package
pip install unravel-client
# Install other dependencies
pip install -r requirements.txt
# Set up environment variables
export UNRAVEL_API_KEY="your_api_key_here"- Visit Unravel Finance and sign up for an account
- Navigate to your API settings to generate an API key
- Set the environment variable as shown above, or create a
.envfile in the project root:
echo "UNRAVEL_API_KEY=your_api_key_here" > .envunravel-client: Official Unravel API client packagepandas: Data manipulationmatplotlib: Visualizationrequests: API callsalphalens-reloaded: Factor analysisfinml-utils: Utilities
The Python scripts use the unravel-client package. Make sure to install the package before running the scripts.
Run the complete portfolio backtesting example:
jupyter notebook replicate_portfolio_backtest.ipynbThis script demonstrates:
- Fetching historical portfolio weights
- Getting underlying asset prices
- Running backtests with transaction costs
- Plotting performance results
Get current portfolio allocations:
jupyter notebook get_live_weights.ipynbAnalyze portfolio factors:
jupyter notebook factor_analysis_altair.ipynb
jupyter notebook factor_analysis_carry_enhanced.ipynb
jupyter notebook factor_analysis_retail_flow.ipynbAnalyze correlations between portfolio returns:
jupyter notebook factor_returns_correlation.ipynbFor a complete list of available portfolios and their parameters, visit Unravel Portfolios.
These snippets are licensed under the MIT License - see the LICENSE file for details.