All the fundamental data for one type of ticker but from several exchanges (Python)

Hi, i’m a beginner in Python, plus i’m learning about your API at the same time. Got stuck with a task.

I want to get all the fundamental data for one type of ticker (for example for an ETF) from several exchanges, for example from BSE and NSE. How can I do this with Python?

Hey, it sounds not hard, lets see :blush:

Here is a sample script that allows you to get a list of tickers for several exchanges, filter them (if necessary), and then query fundamental data sequentially. I would reccomend to put it into ChatGPT with explanation what it does and ask it to break it down for you with comments :hugs:

import pandas as pd
import requests
from time import sleep  # Import sleep

# Replace 'YOUR_API_KEY' with your actual API key

def eod_get_list_of_ticker(EXCHANGE_CODE, YOUR_API_KEY):
    end_point = f'{EXCHANGE_CODE}?api_token={YOUR_API_KEY}&fmt=json'
    response = requests.get(end_point)
    data = response.json()  # No need to convert, as data is already a list
    return data

list_of_tickers = []
list_of_tickers.extend(eod_get_list_of_ticker('BSE', YOUR_API_KEY))
list_of_tickers.extend(eod_get_list_of_ticker('NSE', YOUR_API_KEY))

def filter_by_type(data, type_value):
    filtered_list = [{"Code": item["Code"], "Name": item["Name"], "Exchange": item["Exchange"]} for item in data if item["Type"] == type_value]
    return filtered_list

# Assuming list_of_tickers is correctly formatted after previous adjustments
list_of_tickers = filter_by_type(list_of_tickers, "ETF")
# This will print the filtered list of tickers
print(list_of_tickers)  # Corrected variable name

def create_full_ticker_codes(list_of_tickers):
    full_ticker_codes = []
    for ticker in list_of_tickers:
        # This concatenates 'Code' and 'Exchange' with a dot in between
        full_ticker_code = f"{ticker['Code']}.{ticker['Exchange']}"
    return full_ticker_codes

full_ticker_codes = create_full_ticker_codes(list_of_tickers)

def eod_fundamentals(SYMBOL, YOUR_API_KEY):
    end_point = f'{SYMBOL}?api_token={YOUR_API_KEY}&fmt=json'
    data = requests.get(end_point).json()
    return data

def get_data_from_fundamentals(filtered_data, YOUR_API_KEY, timeout=2):
    result_data = []  # Initialize result_data within the function
    for elem in filtered_data:
        result = eod_fundamentals(elem, YOUR_API_KEY)
        sleep(timeout)  # Ensure there's a pause between requests
    return result_data  # Return the collected data

result_with_fundamental_data = get_data_from_fundamentals(full_ticker_codes, YOUR_API_KEY)
print(result_with_fundamental_data)  # To see the fetched fundamental data