Getting time and sales data. No data in the 200 response

Jwan622
Jwan622 Newcomer
edited April 8 in TRTH

What am I doing wrong?

I'm trying to follow this: https://developers.lseg.com/en/article-catalog/article/using-tick-history-in-r-language-part-3

def preview_response(response, num_lines=100):
    """
    Prints the first few lines of a streamed Refinitiv API response.

    :param response: The streamed API response.
    :param num_lines: Number of lines to preview.
    """
    print("Previewing first few lines of response:...")

    for i, line in enumerate(response.iter_lines(decode_unicode=True)):
        if line:
            print(line)  # Print each line of data
        if i >= num_lines - 1:  # Stop after num_lines
            break


def get_time_and_sales(ticker, query_start_date, query_end_date):
    """
    Retrieves time and sales data for a given ticker from Refinitiv.
    
    This method fetches historical time and sales data which includes trade information
    such as price, volume, and timestamp for each trade.
    
    Args:
        ticker (str): The RIC identifier for the instrument (e.g., "AAPL.O")
        query_start_date (str): Start date in ISO format (e.g., "2024-03-01T09:30:00Z")
        query_end_date (str): End date in ISO format (e.g., "2024-03-01T16:00:00Z")
        
    Returns:
        requests.Response: A streaming response containing the time and sales data in CSV format
        
    Example:
        response = get_time_and_sales("AAPL.O", "2024-03-01T09:30:00Z", "2024-03-01T16:00:00Z")
        # Process the response as needed
    """
    logger.info(f"Fetching time and sales data for {ticker} from {query_start_date} to {query_end_date}...")
    
    json_blob = {
        "ExtractionRequest": {
            "@odata.type": "#DataScope.Select.Api.Extractions.ExtractionRequests.TickHistoryTimeAndSalesExtractionRequest",
            "ContentFieldNames": [
                "Trade - Price",
                "Trade - Volume",
                "Trade - Exchange Time"
            ],
            "IdentifierList": {
                "@odata.type": "#DataScope.Select.Api.Extractions.ExtractionRequests.InstrumentIdentifierList",
                "InstrumentIdentifiers": [
                    {
                        "Identifier": f"{ticker}",
                        "IdentifierType": "Ric"
                    }
                ],
            },
            "Condition": {
                "MessageTimeStampIn": "GmtUtc",
                "ReportDateRangeType": "Range",
                "QueryStartDate": query_start_date,
                "QueryEndDate": query_end_date
            },
        }
    }
    
    post_url = URL_BASE + "/Extractions/ExtractRaw"
    
    # Make the request directly instead of using post_request_with_auth to include Content-Type header
    request_response = requests.post(
        post_url,
        headers={
            "Authorization": f"Token {REFINITIV_AUTH_TOKEN}",
            "Content-Type": "application/json",
            "Prefer": "respond-async"
        },
        json=json_blob
    )
    
    # Check for errors and log the response for debugging
    if request_response.status_code != 200:
        logger.error(f"Error response: {request_response.status_code} - {request_response.text}")
        request_response.raise_for_status()
    
    job_id = request_response.json()["JobId"]
    logger.info(f"Time and sales data extraction job started with ID: {job_id}")
    
    data_response = requests.get(
        URL_BASE + f"/Extractions/RawExtractionResults('{job_id}')/$value", 
        headers={
            "Authorization": f"Token {REFINITIV_AUTH_TOKEN}",
            "Prefer": "respond-async"
        }
    )
    
    data_response.raise_for_status()
    logger.info(f"Time and sales data retrieved successfully for {ticker}")
    
    return data_response


# Example usage:
if __name__ == "__main__":
    t_s_data = get_time_and_sales(symbol, start_date, end_date)
    preview_response(t_s_data)

Answers

  • Jirapongse
    Jirapongse ✭✭✭✭✭

    @Jwan622

    The 200 response will contact the JobId and Notes. For example:

    HTTP 200 OK

    {

        "@odata.context": "https://selectapi.datascope.refinitiv.com/RestApi/v1/$metadata#RawExtractionResults/$entity",

        "JobId": "0x05806ed296dc2064",

        "Notes": [

            "Extraction Services Version 15.0.42358 (01a7f7ea050d), Built May 20 2021 18:20:45\nUser ID: 9008895\nExtraction ID: 2000000276543637\nSchedule: 0x079cfda70adcdd0c (ID = 0x0000000000000000)\nInput List (2 items):  (ID = 0x079cfda70adcdd0c) Created: 06/30/2021 09:15:46 Last Modified: 06/30/2021 09:15:46\nReport Template (3 fields): _OnD_0x079cfda70adcdd0c (ID = 0x079cfda70afcdd0c) Created: 06/30/2021 09:14:12 Last Modified: 06/30/2021 09:14:12\nSchedule dispatched via message queue (0x079cfda70adcdd0c), Data source identifier (6E0CE43BA1044BF58F63D9D50AB9DCFF)\nSchedule Time: 06/30/2021 09:14:12\nProcessing started at 06/30/2021 09:14:12\nProcessing completed successfully at 06/30/2021 09:15:47\nExtraction finished at 06/30/2021 08:15:47 UTC, with servers: tm04n03, TRTH (87.507 secs)\nInstrument <RIC,AAPL.O> expanded to 1 RIC: AAPL.O.\nInstrument <RIC,IBM.N> expanded to 1 RIC: IBM.N.\nTotal instruments after instrument expansion = 2\n\nQuota Message: INFO: Tick History Cash Quota Count Before Extraction: 3190; Instruments Approved for Extraction: 1; Tick History Cash Quota Count After Extraction: 3190, 638% of Limit; Tick History Cash Quota Limit: 500\nManifest: #RIC,Domain,Start,End,Status,Count\nManifest: AAPL.O,Market Price,2017-09-28T23:02:04.589932217Z,2017-09-29T10:59:52.746759914Z,Active,157\nManifest: IBM.N,Market Price,,,Inactive,0\n"

        ]

    }

    The JobId can be used to download a file and the Notes contains the extraction's information.

    You need to print the Notes to verify what the problem is.

    request_response.json()["Notes"]