Skip to main content

A Python library for Value at Risk (VaR) calculations

Project description

VaR Calculation Library

Overview

This Python library provides functions to calculate the Value at Risk (VaR) and Conditional Value at Risk (cVaR) for financial portfolios, including stock and forex portfolios. These risk measures help in understanding potential losses under given confidence levels.

Features

  • Calculate VaR and cVaR for a stock portfolio.
  • Calculate VaR and cVaR for a forex portfolio.
  • Rebalance a stock portfolio.
  • Supports both long and short positions.
  • Outputs results in both percentage and cash value.

Installation

Ensure you have the required dependencies installed:

pip install numpy 
pip install pandas

Functions

var_stocks(data, stocks, n_stocks, conf, long)

Calculates the VaR and cVaR for a stock portfolio.

Parameters:

  • data (pd.DataFrame): DataFrame containing stock prices.
  • stocks (list): List of stock tickers.
  • n_stocks (list): Number of stocks per ticker.
  • conf (float): Confidence level (e.g., 95 for 95%).
  • long (bool): True for long position, False for short position.

Returns:

A DataFrame with the following columns:

  • Métrica: "VaR" and "cVaR".
  • Porcentaje: The percentage value of risk.
  • Cash: The risk in monetary terms.

var_forex(data, currencies, positions, conf, long)

Calculates the VaR and cVaR for a forex portfolio.

Parameters:

  • data (pd.DataFrame): DataFrame containing forex currency pair prices.
  • currencies (list): List of currency pairs.
  • positions (list): Number of units per currency pair.
  • conf (float): Confidence level (e.g., 95 for 95%).
  • long (bool): True for long position, False for short position.

Returns:

A DataFrame with the following columns:

  • Métrica: "VaR" and "cVaR".
  • Porcentual: The percentage value of risk.
  • Cash: The risk in monetary terms.

rebalance_stocks(w_original, target_weights, data, stocks, portfolio_value)

Calculates the number of shres to buy/sell to rebalance a stock portfolio..

Parameters:

  • w_original: list of floats representing the original weights of each asset in the portfolio.
  • target_weights: list of floats representing the target weights of each asset in the portfolio.
  • data: pd.DataFrame with historical stock prices, where columns represent different stocks.
  • stocks: list of stock tickers (column names in the data DataFrame).
  • portfolio_value: float representing the total value of the portfolio.

Returns:

  • A pd.DataFrame showing the original weights, target weights, and the number of shares to buy or sell for each asset to rebalance the portfolio.

Usage Example

import pandas as pd
import numpy as np
import vartools as vt #from vartools import var_stocks, var_forex, rebalance_stocks

# Example data
stock_data = pd.DataFrame({...})  # Stock price data, you can use yfinance
tickers = ['AAPL', 'GOOGL', 'MSFT']
n_shares = [10, 5, 8]
confidence = 95
is_long = True

# Calculate stock portfolio VaR
stock_var = var_stocks(stock_data, tickers, n_shares, confidence, is_long)
print(stock_var)

# Example forex data
forex_data = pd.DataFrame({...})  # Forex currency pair data, you can use yfinance
currency_pairs = ['EUR/USD', 'GBP/USD']
positions = [10000, 5000]

# Calculate forex portfolio VaR
forex_var = var_forex(forex_data, currency_pairs, positions, confidence, is_long)
print(forex_var)

# Rebalance your portfolio
w_original = np.array([0.4, 0.3, 0.3])
target_weights = np.array([0.5, 0.25, 0.25])
data = pd.DataFrame({...})  # Stock price data
stocks = ["AAPL", "MSFT", "GOOGL"]
portfolio_value = 100000

rebalance_df = rebalance_stocks(w_original, target_weights, data, stocks, portfolio_value)
print(rebalance_df)

License

This project is licensed under the GPL-3.0 license.

Author

Luis Fernando Márquez Bañuelos

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vartools-0.0.2.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

vartools-0.0.2-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file vartools-0.0.2.tar.gz.

File metadata

  • Download URL: vartools-0.0.2.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for vartools-0.0.2.tar.gz
Algorithm Hash digest
SHA256 a1f40bc1754d6cc98fba7480aff97c340c8666768d711ee90b55abbc672aa745
MD5 c757b1cef64e57fa01ad8a5bf3cd8557
BLAKE2b-256 ea8c323cae2366ce9212d8bf6b68c88ed57ee3bfd0de64690a8fac40a46003b7

See more details on using hashes here.

File details

Details for the file vartools-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: vartools-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for vartools-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 728abed805cbc6e4ee4097ceb03c9b3e0a6ce91376913f8a1e3ea293ea22dbf5
MD5 c81bda2f22b687cc107f5eb8010bbac1
BLAKE2b-256 9e5c21ee444af646bdb039e76b35f28c2e72ea6288300a9963751c12a4f0d88a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page