When I executed the following script on CodeBook using RDP Libraries, I could plot a 3D chart of volatility surface, but I can NOT do so today having the following error message.
Can you please let me know the root cause of the error and how to resolve it?
INPUT:
import refinitiv.dataplatform as rdp
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d, Axes3D
import matplotlib.cm as cm
%matplotlib inline
session = rdp.open_desktop_session('DEFAULT_CODE_BOOK_APP_KEY')
endpoint = rdp.Endpoint(session, "https://api.refinitiv.com/data/quantitative-analytics-curves-and-surfaces/v1/surfaces")
request_body={
"universe": [
{ "surfaceTag": "RENAULT",
"underlyingType": "Eti",
"underlyingDefinition": {
"instrumentCode": "RENA.PA"
},
"surfaceParameters": {
"inputVolatilityType": "settle",
"volatilityModel": "SSVI",
"xAxis": "Date",
"yAxis": "Moneyness"
},
"surfaceLayout": { "format": "Matrix" }
},
{ "surfaceTag": "PEUGEOT",
"underlyingType": "Eti",
"underlyingDefinition": {
"instrumentCode": "PEUP.PA"
},
"surfaceParameters": {
"inputVolatilityType": "settle",
"volatilityModel": "SSVI",
"xAxis": "Date",
"yAxis": "Moneyness"
},
"surfaceLayout": { "format": "Matrix" }
},
{ "surfaceTag": "BMW",
"underlyingType": "Eti",
"underlyingDefinition": {
"instrumentCode": "BMWG.DE"
},
"surfaceParameters": {
"inputVolatilityType": "settle",
"volatilityModel": "SSVI",
"xAxis": "Date",
"yAxis": "Moneyness"
},
"surfaceLayout": { "format": "Matrix" }
},
{ "surfaceTag": "VW",
"underlyingType": "Eti",
"underlyingDefinition": {
"instrumentCode": "VOWG.DE"
},
"surfaceParameters": {
"inputVolatilityType": "settle",
"volatilityModel": "SSVI",
"xAxis": "Date",
"yAxis": "Moneyness"
},
"surfaceLayout": { "format": "Matrix" }
}]
}
response = endpoint.send_request(
method = rdp.Endpoint.RequestMethod.POST,
body_parameters = request_body
)
def convert_yyyymmdd_to_float(time_axis):
float_time_axis = []
for time in time_axis:
time = time.replace('-', '')
float_time_axis.append(float(time))
return float_time_axis
def plot_surface(surfaces, surfaceTag):
#various imports removed for brevity
surfaces = pd.DataFrame(data=surfaces)
surfaces.set_index('surfaceTag', inplace=True)
surface = surfaces[surfaces.index == surfaceTag]['surface'][0]
strike_axis = surface[0][1:]
surface = surface[1:]
time_axis = []
surface_grid = []
for line in surface:
time_axis.append(line[0])
surface_grid_line = line[1:]
surface_grid.append(surface_grid_line)
time_axis = convert_yyyymmdd_to_float(time_axis)
x = np.array(strike_axis, dtype=float)
y = np.array(time_axis, dtype=float)
ero = np.array(surface_grid, dtype=float)
X,Y = np.meshgrid(x,y)
Z = ero
fig = plt.figure(figsize=[15,10])
ax = plt.axes(projection='3d')
ax.set_xlabel('moneyness')
ax.set_ylabel('time to expiry')
ax.set_zlabel('volatilities')
surf = ax.plot_surface(X,Y,Z, cmap=cm.coolwarm, linewidth=0, antialiased=False)
plt.show()
surfaces = response.data.raw['data']
plot_surface(surfaces, 'VW')
OUTPUT:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/opt/conda/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2894 try:
-> 2895 return self._engine.get_loc(casted_key)
2896 except KeyError as err:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'surface'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
<ipython-input-35-aa5486f52eab> in <module>
1 surfaces = response.data.raw['data']
----> 2 plot_surface(surfaces, 'VW')
3 # plot_surface(surfaces, 'BMW')
<ipython-input-16-d9df2e95c642> in plot_surface(surfaces, surfaceTag)
10 surfaces = pd.DataFrame(data=surfaces)
11 surfaces.set_index('surfaceTag', inplace=True)
---> 12 surface = surfaces[surfaces.index == surfaceTag]['surface'][0]
13
14 strike_axis = surface[0][1:]
/opt/conda/lib/python3.7/site-packages/pandas/core/frame.py in __getitem__(self, key)
2904 if self.columns.nlevels > 1:
2905 return self._getitem_multilevel(key)
-> 2906 indexer = self.columns.get_loc(key)
2907 if is_integer(indexer):
2908 indexer = [indexer]
/opt/conda/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2895 return self._engine.get_loc(casted_key)
2896 except KeyError as err:
-> 2897 raise KeyError(key) from err
2898
2899 if tolerance is not None:
KeyError: 'surface'
INPUT:
surfaces
OUTPUT:
[{'surfaceTag': 'RENAULT',
'error': {'id': '3b6ecb24-f876-4b9c-a965-9e05400fb629/e7b8fa38-c5b3-40b3-b1bb-8f7fad66fcde',
'status': 'Error',
'message': 'The service failed to build the volatility surface',
'code': 'VolSurf.10300'}},
{'surfaceTag': 'PEUGEOT',
'error': {'id': '3b6ecb24-f876-4b9c-a965-9e05400fb629/4b652a29-ff3b-4277-9e44-d933273c8888',
'status': 'Error',
'message': 'Unknown underlying : PEUP.PA@RIC',
'code': 'VolSurf.10008'}},
{'surfaceTag': 'BMW',
'error': {'id': '3b6ecb24-f876-4b9c-a965-9e05400fb629/854b01f4-a046-4340-a16e-8b5830496185',
'status': 'Error',
'message': 'The service failed to build the volatility surface',
'code': 'VolSurf.10300'}},
{'surfaceTag': 'VW',
'error': {'id': '3b6ecb24-f876-4b9c-a965-9e05400fb629/2bee51a2-780f-40db-871b-15cdb883fc41',
'status': 'Error',
'message': 'The service failed to build the volatility surface',
'code': 'VolSurf.10300'}}]