Loading ...

Value at Risk (VaR) for Algorithmic Trading Risk Management

Estimating the risk of loss to an algorithmic trading strategy, or portfolio of strategies, is of extreme importance for long-term capital growth. Many techniques for risk management have been developed for use in institutional settings. One technique in particular, known as Value at Risk or VaR, will be the topic of this article.

We will be applying the concept of VaR to a single strategy or a set of strategies in order to help us quantify risk in our trading portfolio. The definition of VaR is as follows:

**VaR provides an estimate, under a given degree of confidence, of the size of a loss from a portfolio over a given time period.**

In this instance “portfolio” can refer to a single strategy, a group of strategies, a trader’s book, a prop desk, a hedge fund or an entire investment bank. The “given degree of confidence” will be a value of, say, 95% or 99%. The “given time period” will be chosen to reflect one that would lead to a minimal market impact if a portfolio were to be liquidated.

For example, a VaR equal to 500,000 USD at 95% confidence level for a time period of a day would simply state that there is a 95% probability of losing no more than 500,000 USD in the following day. Mathematically this is stated as:

P(L≤−5.0×10^5)=0.05 Or, more generally, for loss L exceeding a value VaR with a confidence level c we have:

P(L≤−VaR)=1−c The “standard” calculation of VaR makes the following assumptions:

- Standard Market Conditions - VaR is not supposed to consider extreme events or “tail risk”, rather it is supposed to provide the expectation of a loss under normal “day-to-day” operation.
- Volatilities and Correlations - VaR requires the volatilities of the assets under consideration, as well as their respective correlations. These two quantities are tricky to estimate and are subject to continual change.
- Normality of Returns - VaR, in its standard form, assumes the returns of the asset or portfolio are normally distributed. This leads to more straightforward analytical calculation, but it is quite unrealistic for most assets.

VaR is pervasive in the financial industry, hence you should be familiar with the benefits and drawbacks of the technique. Some of the advantages of VaR are as follows:

- VaR is very straightforward to calculate for individual assets, algo strategies, quant portfolios, hedge funds or even bank prop desks.
- The time period associated with the VaR can be modified for multiple trading strategies that have different time horizons.
- Different values of VaR can be associated with different forms of risk, say broken down by asset class or instrument type. This makes it easy to interpret where the majority of portfolio risk may be clustered, for instance.
- Individual strategies can be constrained as can entire portfolios based on their individual VaR.
- VaR is straightforward to interpret by (potentially) non-technical external investors and fund managers.

However, VaR is not without its disadvantages:

- VaR does not discuss the magnitude of the expected loss beyond the value of VaR, i.e. it will tell us that we are likely to see a loss exceeding a value, but not how much it exceeds it.
- It does not take into account extreme events, but only typical market conditions.
- Since it uses historical data (it is rearward-looking) it will not take into account future market regime shifts that can change volatilities and correlations of assets.

VaR should not be used in isolation. It should always be used with a suite of risk management techniques, such as diversification, optimal portfolio allocation and prudent use of leverage.

As of yet we have not discussed the actual calculation of VaR, either in the general case or a concrete trading example. There are three techniques that will be of interest to us. The first is the variance-covariance method (using normality assumptions), the second is a Monte Carlo method (based on an underlying, potentially non-normal, distribution) and the third is known as historical bootstrapping, which makes use of historical returns information for assets under consideration.

In this article we will concentrate on the Variance-Covariance Method and in later articles will consider the Monte Carlo and Historical Bootstrap methods.

Consider a portfolio of P dollars, with a confidence level c. We are considering daily returns, with asset (or strategy) historical standard deviation σ and mean μ. Then the daily VaR, under the variance-covariance method for a single asset (or strategy) is calculated as:

P−(P(α(1−c)+1)) Where α is the inverse of the cumulative distribution function of a normal distribution with mean μ and standard deviation σ.

We can use the SciPy and pandas libraries from Python in order to calculate these values. If we set P=106 and c=0.99, we can use the SciPy ppf method to generate the values for the inverse cumulative distribution function to a normal distribution with μ and σ obtained from some real financial data, in this case the historical daily returns of CitiGroup (we could easily substitute the returns of an algorithmic strategy in here):

```
# var.py
import datetime
import numpy as np
import pandas.io.data as web
from scipy.stats import norm
def var_cov_var(P, c, mu, sigma):
"""
Variance-Covariance calculation of daily Value-at-Risk
using confidence level c, with mean of returns mu
and standard deviation of returns sigma, on a portfolio
of value P.
"""
alpha = norm.ppf(1-c, mu, sigma)
return P - P*(alpha + 1)
if __name__ == "__main__":
start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2014, 1, 1)
citi = web.DataReader("C", 'yahoo', start, end)
citi["rets"] = citi["Adj Close"].pct_change()
P = 1e6 # 1,000,000 USD
c = 0.99 # 99% confidence interval
mu = np.mean(citi["rets"])
sigma = np.std(citi["rets"])
var = var_cov_var(P, c, mu, sigma)
print "Value-at-Risk: $%0.2f" % var
```

The calculated value of VaR is given by:

Value-at-Risk: $56510.29 VaR is an extremely useful and pervasive technique in all areas of financial management, but it is not without its flaws. We have yet to discuss the actual value of what could be lost in a portfolio, rather just that it may exceed a certain amount some of the time.

In follow-up articles we will not only discuss alternative calculations for VaR, but also outline the concept of Expected Shortfall (also known as Conditional Value at Risk), which provides an answer to how much is likely to be lost.

- Event-Driven Backtesting with Python - Part VI
- Event-Driven Backtesting with Python - Part V
- Event-Driven Backtesting with Python - Part IV
- Event-Driven Backtesting with Python - Part III
- Event-Driven Backtesting with Python - Part II
- Event-Driven Backtesting with Python - Part I
- 申请增加kucoin的API
- Successful Backtesting of Algorithmic Trading Strategies - Part II
- hbdm和ok合约同时用websocket，huobi发出去pong没反应
- Successful Backtesting of Algorithmic Trading Strategies - Part I
- Should You Build Your Own Backtester?
- Money Management via the Kelly Criterion
- Sharpe Ratio for Algorithmic Trading Performance Measurement
- Continuous Futures Contracts for Backtesting Purposes
- Research Backtesting Environments in Python with pandas
- How to make your own trading bot
- Top 5 Essential Beginner Books for Algorithmic Trading
- Can Algorithmic Traders Still Succeed at the Retail Level?
- 网格
- Steps to Becoming a Quant Trader