Your question is Implement Value at Risk (VaR). Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
In an EY Oman Quantitative Advisory analysis, calculate the historical Value at Risk for a portfolio of assets. Use each asset's historical daily price changes to create portfolio profit-and-loss scenarios, then return the loss percentile at the requested confidence level.
For each asset, apply its historical percentage return to its current market value, where current market value is quantity * latest_price. Sum the resulting P&L across all assets for each historical day. Portfolio loss is the negative of portfolio P&L.
Use the nearest-rank percentile rule: sort the losses and select the element at zero-based index ceil(confidence * number_of_scenarios) - 1. Return the VaR as a numeric value in portfolio currency. A negative result is valid when the selected percentile represents a gain.
Implement calculate_var(positions, confidence). positions is a list of dictionaries, each containing quantity and prices. Every prices list contains at least two positive daily prices and all assets have the same number of observations. confidence is a decimal strictly between 0 and 1. Return a floating-point VaR.
def calculate_var(positions, confidence):