Implementation:Avhz RustQuant Portfolio
| Knowledge Sources | |
|---|---|
| Domains | Portfolio_Optimization, Quantitative_Finance |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Concrete tool for portfolio and position management provided by the RustQuant library.
Description
The module provides two generic structs parameterized over any type implementing the Instrument trait.
The Position struct represents a single holding, containing the instrument itself, a quantity (u64), a purchase price (per unit, f64), a current price (per unit, f64), and an optional Currency. It provides new() for construction, value() returning quantity * current_price, profit() returning value minus total cost, and mutable update methods update_price() and update_quantity().
The Portfolio struct is a collection of named positions stored in a HashMap<String, Position>. It provides new() for construction, value() summing the value of all positions, cost() summing the total purchase cost, profit() summing the profit/loss across all positions, update_price() and update_quantity() for modifying individual positions by name (panics if name not found), and position_weights() returning a HashMap<String, f32> of each position's weight (value / total portfolio value).
Usage
Use these types when tracking a collection of financial instrument positions, computing portfolio-level metrics such as total value, cost basis, profit/loss, and position weights for portfolio analysis and rebalancing.
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_portfolios/src/lib.rs
- Lines: 1-310
Signature
pub struct Portfolio<I: Instrument> {
pub positions: HashMap<String, Position<I>>,
}
pub struct Position<I: Instrument> {
pub instrument: I,
pub quantity: u64,
pub purchase_price: f64,
pub current_price: f64,
pub currency: Option<Currency>,
}
impl<I: Instrument> Position<I> {
pub fn new(instrument: I, quantity: u64, purchase_price: f64,
current_price: f64, currency: Option<Currency>) -> Self;
pub fn value(&self) -> f64;
pub fn profit(&self) -> f64;
pub fn update_price(&mut self, new_price: f64);
pub fn update_quantity(&mut self, new_quantity: u64);
}
impl<I: Instrument> Portfolio<I> {
pub const fn new(positions: HashMap<String, Position<I>>) -> Self;
pub fn value(&self) -> f64;
pub fn cost(&self) -> f64;
pub fn profit(&self) -> f64;
pub fn update_price(&mut self, instrument_name: &str, new_price: f64);
pub fn update_quantity(&mut self, instrument_name: &str, new_quantity: u64);
pub fn position_weights(&self) -> HashMap<String, f32>;
}
Import
use RustQuant::portfolios::{Portfolio, Position};
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| instrument | I: Instrument | Yes | The financial instrument held in the position |
| quantity | u64 | Yes | Number of units held |
| purchase_price | f64 | Yes | Price per unit at time of purchase |
| current_price | f64 | Yes | Current market price per unit |
| currency | Option<Currency> | No | Currency denomination of the instrument |
| positions | HashMap<String, Position> | Yes (for Portfolio) | Named collection of positions |
Outputs
| Name | Type | Description |
|---|---|---|
| f64 | value() | Current market value (quantity * current_price, summed for portfolio) |
| f64 | cost() | Total purchase cost (quantity * purchase_price, summed for portfolio) |
| f64 | profit() | Profit or loss (value - cost) |
| HashMap<String, f32> | position_weights() | Each position's fraction of total portfolio value |
Usage Examples
use RustQuant::portfolios::{Portfolio, Position};
use RustQuant::instruments::options::{BlackScholesMerton, TypeFlag};
use RustQuant::instruments::fx::USD;
use RustQuant::time::today;
use time::Duration;
use std::collections::HashMap;
// Create a position of 100 call options
let position = Position {
instrument: BlackScholesMerton::new(
0.08, 60.0, 65.0, 0.3, 0.08, None,
today() + Duration::days(91), TypeFlag::Call,
),
quantity: 100,
purchase_price: 2.10,
current_price: 3.50,
currency: Some(USD),
};
// Build a portfolio
let mut positions = HashMap::new();
positions.insert("Call Options".to_string(), position);
let portfolio = Portfolio::new(positions);
// Portfolio metrics
let value = portfolio.value(); // 100 * 3.50 = 350.0
let cost = portfolio.cost(); // 100 * 2.10 = 210.0
let profit = portfolio.profit(); // 350.0 - 210.0 = 140.0
// Position weights
let weights = portfolio.position_weights();