Price Fetching
Basic Usage
Command-Line Interface
# Fetch latest prices for all assets
bean-price ledger.beancount
# Fetch historical prices
bean-price --date=2024-01-15 ledger.beancount
# Fetch specific symbols
bean-price -e "USD:google/NASDAQ:AAPL" "USD:yahoo/VTSAX"
Price Source Configuration
Commodity Declarations
; Stock with single price source
2024-01-01 commodity AAPL
price: "USD:google/NASDAQ:AAPL"
; Currency with multiple sources
2024-01-01 commodity EUR
price: "USD:google/CURRENCY:EURUSD,yahoo/EURUSD"
; Asset with multiple quote currencies
2024-01-01 commodity GBP
price: "USD:yahoo/GBPUSD CAD:yahoo/GBPCAD"
Source String Format
<quote-currency>:<module>/[^]<symbol>
Components:
- quote-currency: Currency for price (USD, EUR, etc.)
- module: Price fetcher implementation
- symbol: Exchange-specific identifier
Price Fetching Methods
Direct Sources
; Google Finance
USD:google/NASDAQ:AAPL # Stock
USD:google/NYSEARCA:VTI # ETF
USD:google/CURRENCY:EURUSD # Currency
; Yahoo Finance
USD:yahoo/AAPL # Stock
USD:yahoo/VTSAX # Mutual Fund
Inverted Sources
; Invert fetched price
USD:google/^CURRENCY:USDCAD # CAD price from USD/CAD rate
; With --swap-inverted option
2024-01-15 price USD 1.32759 CAD # Instead of CAD 0.75324 USD
Fallback Sources
; Try multiple sources in order
2024-01-01 commodity GBP
price: "USD:google/CURRENCY:GBPUSD,yahoo/GBPUSD"
Cache Management
# Disable caching
bean-price --no-cache ledger.beancount
# Clear existing cache
bean-price --clear-cache ledger.beancount
Price Fetching Modes
Commodity Selection
# Default: Only active holdings
bean-price ledger.beancount
# Include inactive commodities
bean-price --inactive ledger.beancount
# Include undeclared commodities
bean-price --undeclared ledger.beancount
# Include everything
bean-price --all ledger.beancount
Diagnostic Options
# Show what would be fetched
bean-price --dry-run ledger.beancount
# Detailed logging
bean-price -vv ledger.beancount
Custom Source Implementation
Source Class Interface
from beancount.prices import source
class Source(source.Source):
def get_latest_price(self, ticker):
"""Fetch latest price."""
# Implementation
def get_historical_price(self, ticker, time):
"""Fetch historical price."""
# Implementation
Source Registration
2024-01-01 commodity CUSTOM
price: "USD:my.price.module/SYMBOL"
Best Practices
- Price Organization
; Group price sources by type
2024-01-01 commodity STOCK1
name: "Public Stock"
price: "USD:google/NASDAQ:STOCK1"
2024-01-01 commodity FUND1
name: "Mutual Fund"
price: "USD:yahoo/FUND1"
- Price Validation
; Add balance assertions after important prices
2024-01-15 price STOCK1 100.00 USD
2024-01-15 balance Assets:Investment:STOCK1 10 STOCK1 @ 100.00 USD
- Error Handling
; Use multiple sources for reliability
2024-01-01 commodity MAJOR_STOCK
price: "USD:google/NASDAQ:STOCK,yahoo/STOCK,custom/STOCK"
This guide provides a comprehensive overview of Beancount's price fetching capabilities while maintaining technical accuracy.