Code is to extract implied volatility data for multiple stocks/indices from Equity Volatility Surfac
Hi, Below code is to extract implied volatility data for SP500 index. However, I want to pull data for multiple stocks and indices for multiple time horizon using single run. How can I amend the code to do that?
import refinitiv.data as rd
from refinitiv.data.content.ipa import surfaces
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
rd.open_session()
request_definition = rd.delivery.endpoint_request.Definition(
url="https://api.refinitiv.com/data/quantitative-analytics-curves-and-surfaces/v1/surfaces",
method=rd.delivery.endpoint_request.RequestMethod.POST,
body_parameters={
"universe": [
{
"surfaceTag": "SPX",
"underlyingType": "Eti",
"underlyingDefinition": {
"instrumentCode": ".SPX@RIC"
},
"surfaceParameters": {
"inputVolatilityType": "Quoted",
"timeStamp": "Default",
"priceSide": "Mid",
"volatilityModel": "SSVI",
"xAxis": "Tenor",
"yAxis": "Moneyness",
"calculationDate": "2024-03-21"
},
"surfaceLayout": {
"format": "Matrix",
}
}],
"outputs": ["Data", "ForwardCurve", "MoneynessStrike"]
}
)
response = request_definition.get_data()
surface_tag = response.data.raw['data'][0]['surfaceTag']
_surface_df = pd.DataFrame(
data=response.data.raw['data'][0]['surface'])
surface_df_title = f"{surface_tag} Equity Volatility Surface"
surface_df = pd.DataFrame(
data=_surface_df.loc[1:,1:].values,
index=_surface_df[0][1:],
columns=_surface_df.iloc[0][1:])
surface_df.columns.name = "Moneyness"
surface_df.index.name = "Tenor"
surface_df.loc[["3M","6M"],["1"]]
Best Answer
-
Hi @Wasim Akram01 ,
Regarding making the code cleaner and more readable, you can probably use the following example where I refactored the code:
stocks = {".SPX":["2024-03-21"], "AAPL.O": ["2024-03-21", "2024-02-22"]}
universe = [
{
"surfaceTag": stock,
"underlyingType": "Eti",
"underlyingDefinition": {
"instrumentCode": f"{stock}@RIC"
},
"surfaceParameters": {
"inputVolatilityType": "Quoted",
"timeStamp": "Default",
"priceSide": "Mid",
"volatilityModel": "SSVI",
"xAxis": "Tenor",
"yAxis": "Moneyness",
"calculationDate": date
},
"surfaceLayout": {
"format": "Matrix",
}
} for stock, dates in stocks.items() for date in dates
]
request_definition = rd.delivery.endpoint_request.Definition(
url="https://api.refinitiv.com/data/quantitative-analytics-curves-and-surfaces/v1/surfaces",
method=rd.delivery.endpoint_request.RequestMethod.POST,
body_parameters={
"universe": universe,
"outputs": ["Data", "ForwardCurve", "MoneynessStrike"]
}
)
response = request_definition.get_data()Best regards,
Haykaz
0
Answers
-
Hi @Wasim Akram01 ,
You can include multiple entries in the "universe" list within the request body. Here's an example of how you can modify the provided code:
request_definition = rd.delivery.endpoint_request.Definition(
url="https://api.refinitiv.com/data/quantitative-analytics-curves-and-surfaces/v1/surfaces",
method=rd.delivery.endpoint_request.RequestMethod.POST,
body_parameters={
"universe": [
{
"surfaceTag": "SPX",
"underlyingType": "Eti",
"underlyingDefinition": {
"instrumentCode": ".SPX@RIC"
},
"surfaceParameters": {
"inputVolatilityType": "Quoted",
"timeStamp": "Default",
"priceSide": "Mid",
"volatilityModel": "SSVI",
"xAxis": "Tenor",
"yAxis": "Moneyness",
"calculationDate": "2024-03-21"
},
"surfaceLayout": {
"format": "Matrix",
}
},
{
"surfaceTag": "AAPL",
"underlyingType": "Eti",
"underlyingDefinition": {
"instrumentCode": "AAPL.O@RIC"
},
"surfaceParameters": {
"inputVolatilityType": "Quoted",
"timeStamp": "Default",
"priceSide": "Mid",
"volatilityModel": "SVI",
"xAxis": "Tenor",
"yAxis": "Moneyness",
"calculationDate": "2024-03-21"
},
"surfaceLayout": {
"format": "Matrix",
}
},
{
"surfaceTag": "AAPL",
"underlyingType": "Eti",
"underlyingDefinition": {
"instrumentCode": "AAPL.O@RIC"
},
"surfaceParameters": {
"inputVolatilityType": "Quoted",
"timeStamp": "Default",
"priceSide": "Mid",
"volatilityModel": "SVI",
"xAxis": "Tenor",
"yAxis": "Moneyness",
"calculationDate": "2024-02-22"
},
"surfaceLayout": {
"format": "Matrix",
}
}
],
"outputs": ["Data", "ForwardCurve", "MoneynessStrike"]
}
)
response = request_definition.get_data()Hope this helps.
Best regards,
Haykaz
0 -
I have around 100 stocks, the approach you advised it is good for 3 or 5 stocks, however not useful for 100 stocks. Can you please check if there is any possibility to enter multiple stocks in "instrumentCode" line (like any list/tuple/dictionary) ?0 -
Hi @Wasim Akram01 ,
Just checked, according to the Swagger type for instrumentCode is string. So I am afraid, you can't have list of instruments there:
"instrumentCode": {
"description": "Code to define the instrument. Only RIC is supported.",
"type": "string"
},0 -
0
Categories
- All Categories
- 6 AHS
- 37 Alpha
- 161 App Studio
- 4 Block Chain
- 4 Bot Platform
- 16 Connected Risk APIs
- 47 Data Fusion
- 30 Data Model Discovery
- 608 Datastream
- 1.3K DSS
- 577 Eikon COM
- 4.9K Eikon Data APIs
- 7 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- Trading API
- 2.7K Elektron
- 1.3K EMA
- 236 ETA
- 519 WebSocket API
- 33 FX Venues
- 10 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 20 Messenger Bot
- 2 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 59 Open Calais
- 264 Open PermID
- 39 Entity Search
- 2 Org ID
- PAM
- PAM - Logging
- 8.4K Private Comments
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 20 RDMS
- 1.4K Refinitiv Data Platform
- 367 Refinitiv Data Platform Libraries
- 3 Refinitiv Due Diligence
- LSEG Due Diligence Portal API
- 3 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.1K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 10 World-Check Customer Risk Screener
- 990 World-Check One
- 44 World-Check One Zero Footprint
- 45 Side by Side Integration API
- Test Space
- 3 Thomson One Smart
- 1.2K TR Internal
- Global Hackathon 2015
- 2 Specialists Who Code
- 10 TR Knowledge Graph
- 150 Transactions
- 142 REDI API
- 1.7K TREP APIs
- 4 CAT
- 21 DACS Station
- 117 Open DACS
- 1.1K RFA
- 103 UPA
- 172 TREP Infrastructure
- 224 TRKD
- 886 TRTH
- 5 Velocity Analytics
- 5 Wealth Management Web Services
- 59 Workspace SDK
- 9 Element Framework
- 5 Grid
- 13 World-Check Data File
- Yield Book Analytics
- 46 中文论坛