nsedt.indices

get data for indices

  1""" 
  2get data for indices
  3"""
  4import concurrent
  5import datetime
  6import logging
  7import urllib
  8from concurrent.futures import ALL_COMPLETED
  9
 10import pandas as pd
 11
 12from nsedt import utils
 13from nsedt.resources import constants as cns
 14from nsedt.utils import data_format
 15
 16log = logging.getLogger("root")
 17
 18
 19def get_price(
 20    start_date: datetime,
 21    end_date: datetime,
 22    symbol: str,
 23    response_type: str = "panda_df",
 24    columns_drop_list: list = None,
 25    columns_rename_map: map = None,
 26):
 27    """
 28        Get price of index
 29
 30    Args:
 31
 32        start_date (datetime): start date
 33        end_date (datetime): end date
 34        symbol (str): symbol name or index name
 35        response_type (str, optional):  Define return type: panda_df or json.
 36                                        Defaults to "panda_df".
 37        columns_drop_list (list,optional): define columns drop list, Defaults to None
 38        columns_rename_map (map, optional): define columns rename map, Defaults to None
 39
 40    Raises:
 41
 42        exc: general Exception
 43
 44    Returns:
 45
 46            Pandas DataFrame: df containing company info
 47        or
 48            Json: json containing company info
 49
 50    """
 51    params = {}
 52    cookies = utils.get_cookies()
 53    base_url = cns.BASE_URL
 54    event_api = cns.INDEX_PRICE_HISTORY
 55    symbol = utils.get_symbol(symbol=symbol, get_key="indices")
 56
 57    url_list = []
 58
 59    # set the window size to one year
 60    window_size = datetime.timedelta(days=cns.WINDOW_SIZE)
 61    start_date, end_date = utils.check_nd_convert(start_date, end_date)
 62
 63    current_window_start = start_date
 64    while current_window_start < end_date:
 65        current_window_end = current_window_start + window_size
 66
 67        # check if the current window extends beyond the end_date
 68        current_window_end = min(current_window_end, end_date)
 69
 70        params = {
 71            "indexType": symbol,
 72            "from": current_window_start.strftime("%d-%m-%Y"),
 73            "to": current_window_end.strftime("%d-%m-%Y"),
 74        }
 75        url = base_url + event_api + urllib.parse.urlencode(params)
 76        url_list.append(url)
 77
 78        # move the window start to the next day after the current window end
 79        current_window_start = current_window_end + datetime.timedelta(days=1)
 80
 81    result = pd.DataFrame()
 82    with concurrent.futures.ThreadPoolExecutor(max_workers=cns.MAX_WORKERS) as executor:
 83        future_to_url = {
 84            executor.submit(utils.fetch_url, url, cookies, response_type="panda"): url
 85            for url in url_list
 86        }
 87        concurrent.futures.wait(future_to_url, return_when=ALL_COMPLETED)
 88        for future in concurrent.futures.as_completed(future_to_url):
 89            url = future_to_url[future]
 90            try:
 91                data = future.result()
 92                if (
 93                    data.get("data").get("indexCloseOnlineRecords") == []
 94                    or data.get("data").get("indexTurnoverRecords") == []
 95                ):
 96                    continue
 97                dataframe = data_format.indices(
 98                    data, columns_drop_list, columns_rename_map
 99                )
100                result = pd.concat([result, dataframe])
101            except Exception as exc:
102                log.error("%s got exception: %s. Please try again later.", url, exc)
103                raise exc
104
105    if response_type == "panda_df":
106        return result
107    return result.to_json(orient="records")
log = <RootLogger root (INFO)>
def get_price( start_date: <module 'datetime' from '/opt/homebrew/Cellar/python@3.10/3.10.9/Frameworks/Python.framework/Versions/3.10/lib/python3.10/datetime.py'>, end_date: <module 'datetime' from '/opt/homebrew/Cellar/python@3.10/3.10.9/Frameworks/Python.framework/Versions/3.10/lib/python3.10/datetime.py'>, symbol: str, response_type: str = 'panda_df', columns_drop_list: list = None, columns_rename_map: map = None):
 20def get_price(
 21    start_date: datetime,
 22    end_date: datetime,
 23    symbol: str,
 24    response_type: str = "panda_df",
 25    columns_drop_list: list = None,
 26    columns_rename_map: map = None,
 27):
 28    """
 29        Get price of index
 30
 31    Args:
 32
 33        start_date (datetime): start date
 34        end_date (datetime): end date
 35        symbol (str): symbol name or index name
 36        response_type (str, optional):  Define return type: panda_df or json.
 37                                        Defaults to "panda_df".
 38        columns_drop_list (list,optional): define columns drop list, Defaults to None
 39        columns_rename_map (map, optional): define columns rename map, Defaults to None
 40
 41    Raises:
 42
 43        exc: general Exception
 44
 45    Returns:
 46
 47            Pandas DataFrame: df containing company info
 48        or
 49            Json: json containing company info
 50
 51    """
 52    params = {}
 53    cookies = utils.get_cookies()
 54    base_url = cns.BASE_URL
 55    event_api = cns.INDEX_PRICE_HISTORY
 56    symbol = utils.get_symbol(symbol=symbol, get_key="indices")
 57
 58    url_list = []
 59
 60    # set the window size to one year
 61    window_size = datetime.timedelta(days=cns.WINDOW_SIZE)
 62    start_date, end_date = utils.check_nd_convert(start_date, end_date)
 63
 64    current_window_start = start_date
 65    while current_window_start < end_date:
 66        current_window_end = current_window_start + window_size
 67
 68        # check if the current window extends beyond the end_date
 69        current_window_end = min(current_window_end, end_date)
 70
 71        params = {
 72            "indexType": symbol,
 73            "from": current_window_start.strftime("%d-%m-%Y"),
 74            "to": current_window_end.strftime("%d-%m-%Y"),
 75        }
 76        url = base_url + event_api + urllib.parse.urlencode(params)
 77        url_list.append(url)
 78
 79        # move the window start to the next day after the current window end
 80        current_window_start = current_window_end + datetime.timedelta(days=1)
 81
 82    result = pd.DataFrame()
 83    with concurrent.futures.ThreadPoolExecutor(max_workers=cns.MAX_WORKERS) as executor:
 84        future_to_url = {
 85            executor.submit(utils.fetch_url, url, cookies, response_type="panda"): url
 86            for url in url_list
 87        }
 88        concurrent.futures.wait(future_to_url, return_when=ALL_COMPLETED)
 89        for future in concurrent.futures.as_completed(future_to_url):
 90            url = future_to_url[future]
 91            try:
 92                data = future.result()
 93                if (
 94                    data.get("data").get("indexCloseOnlineRecords") == []
 95                    or data.get("data").get("indexTurnoverRecords") == []
 96                ):
 97                    continue
 98                dataframe = data_format.indices(
 99                    data, columns_drop_list, columns_rename_map
100                )
101                result = pd.concat([result, dataframe])
102            except Exception as exc:
103                log.error("%s got exception: %s. Please try again later.", url, exc)
104                raise exc
105
106    if response_type == "panda_df":
107        return result
108    return result.to_json(orient="records")

Get price of index

Args:

start_date (datetime): start date
end_date (datetime): end date
symbol (str): symbol name or index name
response_type (str, optional):  Define return type: panda_df or json.
                                Defaults to "panda_df".
columns_drop_list (list,optional): define columns drop list, Defaults to None
columns_rename_map (map, optional): define columns rename map, Defaults to None

Raises:

exc: general Exception

Returns:

    Pandas DataFrame: df containing company info
or
    Json: json containing company info