Skip to content

Instantly share code, notes, and snippets.

# Importing libraries
from pandas_datareader import data as pdr
# Defining general variables
ticker = 'SPY'
first_date = '2010-01-01'
last_date = '2018-08-31'
conficence_level = 0.05
k = 0.02
def value_at_risk(returns, confidence_level=.05):
"""
It calculates the Value at Risk (VaR) of some time series. It represents
the maximum loss with the given confidence level.
Parameters
----------
returns : pandas.DataFrame
Returns of each time serie. It could be daily, weekly, monthly, ...
def expected_shortfall(returns, confidence_level=.05):
"""
It calculates the Expected Shortfall (ES) of some time series. It represents
the average loss according to the Value at Risk.
Parameters
----------
returns : pandas.DataFrame
Returns of each time serie. It could be daily, weekly, monthly, ...