Example #1
0
 def get_participant_oi_url(self,date,dayfirst=False):
     try:
         date = get_formated_date(date,format=self.nse_date_formats["part_oi"],dayfirst=dayfirst)
         url = self.__PARTICIPANT_OI_PRE_URL + date + self.__PARTICIPANT_OI_POST_URL
         return url
     except Exception as err:
         raise Exception("Error occurred while getting participant OI. ", str(err))
Example #2
0
 def get_option_chain_url(self,symbol,expiry_date=None,dayfirst=False):
     try:
         if expiry_date:
             expiry_date = get_formated_date(expiry_date,self.nse_date_formats["opt_chain"],dayfirst).upper()
             if expiry_date.startswith('0'):
                 expiry_date = expiry_date[1:]
             complete_url =  self.__OPTION_CHAIN_BASE_URL + symbol + "&date=" + expiry_date
             return complete_url
         else:
             return self.__OPTION_CHAIN_BASE_URL + symbol
     except Exception as err:
         raise Exception("Error occurred while getting OC url, Error: ",str(err))
Example #3
0
    def get_part_oi_df(self,
                       start=None,
                       end=None,
                       periods=None,
                       dayfirst=False,
                       workers=None):
        """Return dictionary of participants containing data frames

        :param start: start date , defaults to None
        :type start: string, optional
        :param end: end date, defaults to None
        :type end: string, optional
        :param periods: number of days, defaults to None
        :type periods: interger, optional
        :param dayfirst: True if date format is european style DD/MM/YYYY, defaults to False
        :type dayfirst: bool, optional
        :param workers: Number of threads for requesting nse, defaults to None
        :type workers: interger, optional
        :raises Exception: NSE Connection/Request overload
        :return: participant wise open interest
        :rtype: pandas.DataFrame
        """
        try:
            #format date just in case
            if start:
                start = get_formated_date(start, dayfirst=dayfirst)
            if end:
                end = get_formated_date(end, dayfirst=dayfirst)

            #if both none, we set end to today
            if not start and not end:
                end = get_formated_date()
                if not periods:
                    periods = PART_OI_DAYS
            #get urls for these days
            dates = pd.date_range(start=start,
                                  end=end,
                                  periods=periods,
                                  freq='B')
            url_date = [(self.__nse_urls.get_participant_oi_url(date), date)
                        for date in dates]  #

            oi_clm = self.__nse_urls.PART_OI_CLM
            #lets preinitialize, better readability
            oi_dfs = {
                "Client": pd.DataFrame(columns=oi_clm, index=dates),
                "DII": pd.DataFrame(columns=oi_clm, index=dates),
                "FII": pd.DataFrame(columns=oi_clm, index=dates),
                "Pro": pd.DataFrame(columns=oi_clm, index=dates),
                "TOTAL": pd.DataFrame(columns=oi_clm, index=dates)
            }

            if not workers:
                workers = os.cpu_count() * 2

            with concurrent.futures.ThreadPoolExecutor(
                    max_workers=workers) as executor:
                responses = {
                    executor.submit(self.__request.get, url, self.__headers):
                    date
                    for url, date in url_date
                }
                for res in concurrent.futures.as_completed(responses):
                    date = responses[res]
                    try:
                        csv = res.result()
                    except Exception as exc:
                        #might be holiday
                        pass
                    else:
                        df = pd.read_csv(
                            io.StringIO(csv.content.decode('utf-8')))
                        #drop the first header
                        df_header = df.iloc[0]
                        #is there any implace way?
                        df = df[1:]
                        df.columns = df_header
                        df.set_index('Client Type', inplace=True)
                        #lets us create data frome for all client type
                        oi_dfs['Client'].loc[date] = df.loc['Client']
                        oi_dfs['FII'].loc[date] = df.loc['FII']
                        oi_dfs['DII'].loc[date] = df.loc['DII']
                        oi_dfs['Pro'].loc[date] = df.loc['Pro']
                        oi_dfs['TOTAL'].loc[date] = df.loc['TOTAL']

            if not oi_dfs['Client'].empty:
                #remove nan row
                for client in oi_dfs:
                    oi_dfs[client].dropna(inplace=True)

                #if holiday occurred in business day, lets retrive more data equivalent to holdidays.
                if oi_dfs['Client'].shape[0] < periods:
                    new_periods = periods - oi_dfs['Client'].shape[0]
                    try:
                        #if only start, find till today
                        if start and (not end):
                            s_from = oi_dfs['Client'].index[-1] + timedelta(1)
                            e_till = None
                        #if not start, can go to past
                        elif (end and (not start)):
                            s_from = None
                            e_till = oi_dfs['Client'].index[0] - timedelta(1)
                        #if start and end, no need to change
                        else:
                            return oi_dfs
                    except IndexError as err:
                        raise Exception(
                            "NSE Access error.size down/clean cookies to resolve the issue."
                        )
                    except Exception as exc:
                        raise Exception("participant OI error: ", str(exc))

                    oi_dfs_new = self.get_part_oi_df(start=s_from,
                                                     end=e_till,
                                                     periods=new_periods)
                    self.__join_part_oi_dfs(oi_dfs, oi_dfs_new)

                return oi_dfs

        except Exception as err:
            raise Exception("Error occurred while getting part_oi :", str(err))