nsedt.derivatives

get data for Equity

 1""" 
 2get data for Equity
 3"""
 4import concurrent
 5import logging
 6import urllib
 7from concurrent.futures import ALL_COMPLETED
 8from datetime import datetime, timedelta
 9
10import pandas as pd
11from nsedt import utils
12from nsedt.resources import constants as cns
13from nsedt.utils import data_format
14from nsedt.derivatives.options import (
15    get_option_chain, get_option_chain_expdate, get_historical_option_data)
16from nsedt.derivatives.futures import get_future_price, get_future_expdate
17
18log = logging.getLogger("root")
19
20
21def get_vix(
22    start_date: str,
23    end_date: str,
24    response_type: str = "panda_df",
25    columns_drop_list: list = None,
26):
27    """Get Vix data
28
29    Args:
30        start_date (str): start date in "%d-%m-%Y" format
31        end_date (str): end_date in "%d-%m-%Y" format
32        response_type (str, optional): response_type. Defaults to "panda_df".
33        columns_drop_list (list, optional): provide custom columns drop list. Defaults to None.
34
35    Raises:
36        exc: general Exception
37
38    Returns:
39            Pandas DataFrame: df containing option data
40        or
41            Json: json containing option data
42
43    """
44    cookies = utils.get_cookies()
45    params = {
46        "from": start_date,
47        "to": end_date,
48    }
49    base_url = cns.BASE_URL
50    event_api = cns.VIX_HISTORY
51    url_list = []
52    start_date = datetime.strptime(start_date, "%d-%m-%Y")
53    end_date = datetime.strptime(end_date, "%d-%m-%Y")
54
55    # set the window size to one year
56    window_size = timedelta(days=cns.WINDOW_SIZE)
57
58    current_window_start = start_date
59    while current_window_start < end_date:
60        current_window_end = current_window_start + window_size
61
62        # check if the current window extends beyond the end_date
63        current_window_end = min(current_window_end, end_date)
64
65        params = {
66            "from": current_window_start.strftime("%d-%m-%Y"),
67            "to": current_window_end.strftime("%d-%m-%Y"),
68        }
69        url = base_url + event_api + urllib.parse.urlencode(params)
70        url_list.append(url)
71
72        # move the window start to the next day after the current window end
73        current_window_start = current_window_end + timedelta(days=1)
74
75    result = pd.DataFrame()
76    with concurrent.futures.ThreadPoolExecutor(max_workers=cns.MAX_WORKERS) as executor:
77        future_to_url = {
78            executor.submit(utils.fetch_url, url, cookies, response_type="panda"): url
79            for url in url_list
80        }
81        concurrent.futures.wait(future_to_url, return_when=ALL_COMPLETED)
82        for future in concurrent.futures.as_completed(future_to_url):
83            url = future_to_url[future]
84            try:
85                dataframe = data_format.get_vix(
86                    future.result(),
87                    response_type=response_type,
88                    columns_drop_list=columns_drop_list,
89                )
90                result = pd.concat([result, dataframe])
91            except Exception as exc:
92                log.error("%s got exception: %s. Please try again later.", url, exc)
93                raise exc
94
95    if response_type == "panda_df":
96        return result
97    return result.to_json(orient="records")
log = <RootLogger root (INFO)>
def get_vix( start_date: str, end_date: str, response_type: str = 'panda_df', columns_drop_list: list = None):
22def get_vix(
23    start_date: str,
24    end_date: str,
25    response_type: str = "panda_df",
26    columns_drop_list: list = None,
27):
28    """Get Vix data
29
30    Args:
31        start_date (str): start date in "%d-%m-%Y" format
32        end_date (str): end_date in "%d-%m-%Y" format
33        response_type (str, optional): response_type. Defaults to "panda_df".
34        columns_drop_list (list, optional): provide custom columns drop list. Defaults to None.
35
36    Raises:
37        exc: general Exception
38
39    Returns:
40            Pandas DataFrame: df containing option data
41        or
42            Json: json containing option data
43
44    """
45    cookies = utils.get_cookies()
46    params = {
47        "from": start_date,
48        "to": end_date,
49    }
50    base_url = cns.BASE_URL
51    event_api = cns.VIX_HISTORY
52    url_list = []
53    start_date = datetime.strptime(start_date, "%d-%m-%Y")
54    end_date = datetime.strptime(end_date, "%d-%m-%Y")
55
56    # set the window size to one year
57    window_size = timedelta(days=cns.WINDOW_SIZE)
58
59    current_window_start = start_date
60    while current_window_start < end_date:
61        current_window_end = current_window_start + window_size
62
63        # check if the current window extends beyond the end_date
64        current_window_end = min(current_window_end, end_date)
65
66        params = {
67            "from": current_window_start.strftime("%d-%m-%Y"),
68            "to": current_window_end.strftime("%d-%m-%Y"),
69        }
70        url = base_url + event_api + urllib.parse.urlencode(params)
71        url_list.append(url)
72
73        # move the window start to the next day after the current window end
74        current_window_start = current_window_end + timedelta(days=1)
75
76    result = pd.DataFrame()
77    with concurrent.futures.ThreadPoolExecutor(max_workers=cns.MAX_WORKERS) as executor:
78        future_to_url = {
79            executor.submit(utils.fetch_url, url, cookies, response_type="panda"): url
80            for url in url_list
81        }
82        concurrent.futures.wait(future_to_url, return_when=ALL_COMPLETED)
83        for future in concurrent.futures.as_completed(future_to_url):
84            url = future_to_url[future]
85            try:
86                dataframe = data_format.get_vix(
87                    future.result(),
88                    response_type=response_type,
89                    columns_drop_list=columns_drop_list,
90                )
91                result = pd.concat([result, dataframe])
92            except Exception as exc:
93                log.error("%s got exception: %s. Please try again later.", url, exc)
94                raise exc
95
96    if response_type == "panda_df":
97        return result
98    return result.to_json(orient="records")

Get Vix data

Args: start_date (str): start date in "%d-%m-%Y" format end_date (str): end_date in "%d-%m-%Y" format response_type (str, optional): response_type. Defaults to "panda_df". columns_drop_list (list, optional): provide custom columns drop list. Defaults to None.

Raises: exc: general Exception

Returns: Pandas DataFrame: df containing option data or Json: json containing option data