Moody's gave you a Python script that is supposed to value a bond-and-equity portfolio using boilerplate holdings, price, and FX data, and asked you to review it before it gets used for a client report.
holdings = [
{'symbol': 'AAPL', 'quantity': 500, 'currency': 'USD', 'asset_type': 'equity'},
{'symbol': 'GOOGL', 'quantity': -100, 'currency': 'USD', 'asset_type': 'equity'},
{'symbol': 'BUND10Y', 'quantity': 2000, 'currency': 'EUR', 'asset_type': 'bond'},
]
prices = {'AAPL': 190.20, 'GOOGL': 141.80, 'BUND10Y': 98.50}
fx_rates = {'USD': 1.0, 'EUR': 1.09}
def calculate_portfolio_value(holdings, prices, fx_rates=None):
if fx_rates is None:
fx_rates = {}
total = 0
for h in holdings:
qty = abs(h['quantity'])
price = prices.get(h['symbol'], 0)
total += qty * price
return total
print(calculate_portfolio_value(holdings, prices, fx_rates))
Explain, in your own words, everything wrong with how this script computes portfolio value and what you would change so the printed number is actually correct.
Write a SQL query that produces the corrected valuation for every portfolio in the supplied data. Use signed quantities, convert each valued position to USD, and expose positions that cannot be valued because a price or FX rate is missing.
portfolio_id, portfolio_value_usd, valued_positions, and missing_valuation_inputs.portfolio_id ascending.| Column | Type | Description |
|---|---|---|
| holding_idPK | INT | Unique holding identifier |
| portfolio_id | VARCHAR(30) | Portfolio identifier |
| symbol | VARCHAR(20) | Security symbol |
| quantity | NUMERIC(18,4) | Signed position quantity |
| currency | VARCHAR(10) | Position currency |
| asset_type | VARCHAR(30) | Asset classification |
| Column | Type | Description |
|---|---|---|
| symbolPK | VARCHAR(20) | Security symbol |
| price | NUMERIC(18,4) | Latest local-currency price |
| Column | Type | Description |
|---|---|---|
| currencyPK | VARCHAR(10) | Currency code |
| usd_rate | NUMERIC(18,8) | USD value of one unit of currency |