Implementation:51bitquant Ai hedge fund crypto Backtester calculate portfolio value
| Knowledge Sources | |
|---|---|
| Domains | Portfolio_Management, Backtesting |
| Last Updated | 2026-10-28 14:00 GMT |
Overview
Concrete tool for computing total portfolio mark-to-market value (cash + long market value − short market value) provided by the `Backtester` class.
Description
Backtester.calculate_portfolio_value sums `self.portfolio["cash"]` plus, for each ticker, `position["long"] * price` (long market value), and subtracts `position["short"] * price` (short market value liability). Returns the total as a float. It is called once per bar inside `run_backtest` after trades execute.
Usage
Called per-bar by `run_backtest` to record the daily portfolio value. You can also call it standalone to inspect the current portfolio value given a set of prices.
Code Reference
Source Location
- Repository: ai-hedge-fund-crypto
- File: src/backtest/backtester.py
Signature
class Backtester:
def calculate_portfolio_value(self, current_prices: Dict[str, float]) -> float:
Import
from src.backtest.backtester import Backtester
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| current_prices | Dict[str, float] | Yes | Per-ticker close price for the current bar |
Outputs
| Name | Type | Description |
|---|---|---|
| return | float | Total portfolio value (cash + long MV − short MV) |
Usage Examples
Per-Bar Valuation
# Inside run_backtest loop
total_value = self.calculate_portfolio_value(current_prices)
self.portfolio_values.append({"Date": current_time, "Portfolio Value": total_value})
Standalone Check
prices = {"BTCUSDT": 60000.0, "ETHUSDT": 3000.0}
value = bt.calculate_portfolio_value(prices)
print(f"Portfolio value: ${value:,.2f}")