Git Product home page Git Product logo

portfolio-rebalance-methods's Introduction

๐Ÿ“ˆ๐Ÿ’ผ Portfolio Rebalance Methods

Portfolio Rebalance Methods: Explore and implement diverse rebalancing strategies, including Static Weights, Equal Weights, Risk Parity, and Markowitz, to optimize and fine-tune your investment portfolios.

๐ŸŒ Overview

This repository contains Python scripts for portfolio analysis with various rebalancing methods. The primary script, portfolio_rebalancer.py, computes portfolio metrics based on synchronized asset time series data, while the secondary script, main.py, demonstrates its usage with example data.

๐Ÿš€ Usage

Requirements

  • Python 3.10.9
  • Pandas library 2.0.1

Example

The main.py script serves as an example of how to use the portfolio_rebalancer script with sample data.

To use the portfolio rebalancer, create an instance of the PortfolioRebalancer class and specify the required parameters. The following example demonstrates how to use it:

  1. Read asset time series data from a CSV file.
  2. Define needed parameters.
  3. Specify the weights type (risk parity or static).
  4. Create a PortfolioRebalancer instance.
  5. Calculate portfolio metrics.
  6. Save results to CSV files.
  7. Print the results.
import pandas as pd
from portfolio_rebalancer import PortfolioRebalancer

if __name__ == "__main__":
    # Read asset time series data from CSV
    asset_ts = pd.read_csv('data/example-data.csv', index_col=0, parse_dates=['timestamp'])

    # Define initial allocation weights and rebalance frequency
    allocation = [0.333, 0.333, 0.334]
    reb_freq = 'Q'
    
    # Specify weights type
    weights_type = 'static'  # Change this to 'risk_parity' if needed

    # Create PortfolioRebalancer instance
    rebalancer = PortfolioRebalancer(asset_prices=asset_ts, weights_type=weights_type, rebalance_frequency=reb_freq, static_weights=allocation)

    # Calculate portfolio metrics
    result_equity, result_allocations = rebalancer.calculate_portfolio()

    # Save results to CSV files with weights type in filenames
    equity_filename = f'equity_{weights_type}.csv'
    allocations_filename = f'allocations_{weights_type}.csv'
    
    result_equity.to_csv(equity_filename, sep=';')
    result_allocations.to_csv(allocations_filename, sep=';')

    # Print results
    print("Cumulative Equity:")
    print(result_equity)

    print("\nAsset Allocations:")
    print(result_allocations)

๐Ÿ“š Documentation

PortfolioRebalancer Class

__init__(self, asset_prices, weights_type='static', rebalance_frequency='Q', static_weights=None, start_deposit=100.0)

Initialize PortfolioRebalancer.

  • asset_prices (pd.DataFrame): A DataFrame with synchronized asset prices over time.
  • weights_type (Literal['risk_parity', 'static']): The type of weights to use ('risk_parity' or 'static').
  • rebalance_frequency (str): The rebalancing frequency as a pandas frequency string (e.g., 'Q' for quarterly, 'M' for monthly).
  • static_weights (Optional[List[float]]): A list of static weights for each asset. If not provided and weights_type is 'static', it defaults to equal weights.
  • start_deposit (float): The initial deposit or starting amount for the portfolio.

Function: get_allocation_weights(self, timestamp) -> pd.Series

Calculate allocation weights based on the specified type.

Returns:

  • pd.Series[float]: A Pandas Series of weights for each asset.

Function: calculate_portfolio(self) -> Tuple[pd.DataFrame, pd.DataFrame]

Calculate portfolio metrics based on synchronized asset time series data using specified parameters.

Returns:

  • Tuple[pd.DataFrame, pd.DataFrame]:
    • equity: DataFrame with cumulative equity.
    • asset_allocations: DataFrame with daily asset allocations.

๐Ÿ“„ License

This project is licensed under the MIT License. See the LICENSE file for details.

portfolio-rebalance-methods's People

Contributors

py310 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

larafgarcia

portfolio-rebalance-methods's Issues

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.