question

Upvotes
Accepted
3 0 0 2

Timeout errors and too many requests for instrument pricing analytics

Hey. I am calling the instrument pricing analytics serivce, specifically: https://api.refinitiv.com/data/quantitative-analytics/v1/financial-contracts


import refinitiv.dataplatform as rdp
...
endpoint = rdp.Endpoint(session, 'https://api.refinitiv.com/data/quantitative-analytics/v1/financial-contracts')
req = get_request_body(swap_name, currency, legs, valuation_date, forward_starting_date, tenor=tenor)
rsp = endpoint.send_request(method = rdp.Endpoint.RequestMethod.POST, body_parameters = req)


This works okay mostly but I get two different errors when trying to run this across several dates:

- ReadTimeout <- just fails with no more information.
- Too many requests <- fails seemingly because I issue too many requests.

I tried inserting some "sleep" commands but still get the same error. I am certainly issuing many requests, but does anyone know the policy so I can throttle this, if that is the case.


And, how can I avoid the "timeout" error? I don't care if it takes a long time to respond; I can happily just execute my code overnight if needed.

python#technology#productpricingipatime-outswaps
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Hello @mikael.ohman

Thank you for your participation in the forum. Is the reply below satisfactory in resolving your query?


If so please can you click the 'Accept' text next to the appropriate reply? This will guide all community members who have a similar question.

Thanks,


AHS


Please be informed that a reply has been verified as correct in answering the question, and has been marked as such.

Thanks,

AHS

Upvotes
Accepted
17.1k 80 39 63

Hi @mikael.ohman

Via configuration, you should be able to override the default timeout. Is it possible for you to provide some details about your request so we can attempt to replicate and determine if changing the default timeout can help?

Note: I can see you are using an older version of the library. I would suggest you update your code to a newer version. You can see how to use it via examples. The design of the newer library is similar to the one you are using so the effort to change should be minimal.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
3 0 0 2

Hey.

I reviewed your example link and updated to using the newer library:


import refinitiv.data as rd
rd.open_session()

The code is like this:


from typing import List


# Refinitiv throws an error when we issue too many requests. So sleep some...
import time


def swap_valuation(currency, swap_name, leg_function, fixed_rate_pct, floating_spread_bps, valuation_date, forward_starting_date, tenor):
    time.sleep(1)
    legs = leg_function(fixed_rate_pct=fixed_rate_pct, floating_spread_bps=floating_spread_bps)
    req = get_request_body(swap_name, currency, legs, valuation_date, forward_starting_date, tenor=tenor)
    request_definition = rd.delivery.endpoint_request.Definition(
        url = ipa_url,
        method = rd.delivery.endpoint_request.RequestMethod.POST,
        body_parameters = req
    )
    response = request_definition.get_data()
    
    if not 'headers' in response.data.raw:
        display(response.data.raw)
        raise Exception('Refinitiv error...')
    else:
        value = get_value(response)
        return value


As you can see, I try to "sleep" to not get limited and thrown out...

The request body is like this:

from typing import List


def get_request_body(
    instr_tag:str, 
    currency:str, 
    legs,
    valuation_date: pd.Timestamp, 
    start_date: pd.Timestamp, 
    tenor:str=None, 
    flds:List[str]=None):
    '''
    :param instr_tag str: Custom name of the instrument.
    :param currency str: The currency.
    :param legs: Fixed and floating leg definitions.
    :param valuation_date: The date at which to value the swap.
    :param start_date: The start date of the swap, e.g. T+2, T+1M or T-1M or similar.
    :param tenor str: Tenor string, e.g. 5Y.
    :param flds List[str]: Fields to retrieve in the response (optional).
    '''
    
    tenor = tenor or '5Y'
    flds = flds or [
        'InstrumentTag',
        'LegTag',
        'Direction',
        'MarketValueInDealCcy',
        'CleanMarketValueInDealCcy',
        'DirtyPricePercent', 
        'FixedRatePercent', 
        'SpreadBp', 
        'PV01Bp', 
        'PV01AmountInDealCcy', 
        'DiscountCurveNamne', 
        'ForwardCurveName', 
        'ErrorCode', 
        'ErrorMessage']
    
    return {
        'fields': flds,
        
        'universe': [{
            'instrumentType': 'Swap',
            'instrumentDefinition': {
                'instrumentTag': instr_tag,
                'startDate': start_date.strftime('%Y-%m-%d'),
                'tenor': tenor,
                'legs': legs
            }
        }],
        
        'pricingParameters': {
            'valuationDate': valuation_date.strftime('%Y-%m-%d')
        },
        
        'outputs': ['Data', 'Headers'],
    }


And so this is just a valuation of a plain vanilla forward starting swap.

I loop over a couple of swaps and value them each day from say 2014 through 2020. So in all, that becomes many requests.

I have seen that there is a json configuration file that can be used to increase timeouts. However, I can't find any example. I found this: Example.DataLibrary.Python/refinitiv-data.config.json at main · Refinitiv-API-Samples/Example.DataLibrary.Python (github.com)

Can you provide me a link to documentation on the config options here?

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.