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.
  • 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.

Usage Example

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

# 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)

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.1.tar.gz (14.7 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.1-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: vartools-0.0.1.tar.gz
  • Upload date:
  • Size: 14.7 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.1.tar.gz
Algorithm Hash digest
SHA256 af89298a1722dbc4a64a389d335ace57f1f211063611c455aa2691b68d09b990
MD5 6bc4ee2e4d487c72f4ffefc496a58b23
BLAKE2b-256 f32512746f517021c67abaa7cb228a12e17ce2bf0d9209c9980f3779cf2bd375

See more details on using hashes here.

File details

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

File metadata

  • Download URL: vartools-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 15.3 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 51e402184bef12f731551ba0c7bde8f444618b160a76501977d4dcba8f747607
MD5 df6d582ce5f8258afb16b3e84007425c
BLAKE2b-256 217cc41978bce8bf1121f0802b553780baef2c7217bbb2779aba1c6989da2c12

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