Portfolio Manager API Reference¶
financelib.trading.portfolio_manager.PortfolioManager(initial_cash: float = 1000000, max_position_pct: float = 0.2, max_drawdown_pct: float = 0.15)
¶
Portfolio manager with position tracking and risk management.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial_cash
|
float
|
Starting cash balance. |
1000000
|
max_position_pct
|
float
|
Maximum single position as % of portfolio (0-1). |
0.2
|
max_drawdown_pct
|
float
|
Maximum drawdown before stopping (0-1). |
0.15
|
Example
pm = PortfolioManager(initial_cash=1000000) pm.open_position("THYAO.IS", 1000, 172.45) pm.update_price("THYAO.IS", 175.00) print(pm.summary())
Source code in financelib/trading/portfolio_manager.py
Attributes¶
current_drawdown: float
property
¶
Current drawdown from peak equity.
total_equity: float
property
¶
Total portfolio value (cash + positions).
Functions¶
close_position(symbol: str, price: float) -> Optional[Dict[str, Any]]
¶
Close an entire position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Security symbol. |
required |
price
|
float
|
Exit price. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
Trade result dict, or None if no position exists. |
Source code in financelib/trading/portfolio_manager.py
open_position(symbol: str, amount: float, price: float) -> bool
¶
Open a new position or add to existing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Security symbol. |
required |
amount
|
float
|
Number of shares/units. |
required |
price
|
float
|
Entry price. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if position was opened, False if risk limits prevent it. |
Source code in financelib/trading/portfolio_manager.py
record_equity() -> None
¶
Record current equity for the equity curve.
summary() -> Dict[str, Any]
¶
Generate a portfolio summary.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary with portfolio metrics. |
Source code in financelib/trading/portfolio_manager.py
update_price(symbol: str, price: float) -> None
¶
financelib.trading.portfolio_manager.Position(symbol: str, amount: float, entry_price: float, current_price: float = 0.0, entry_time: str = (lambda: datetime.now().isoformat())())
dataclass
¶
Represents an open position in a security.