OHCL Banknifty Data


If you want to analyze the stock market data for all the stocks which make up BANKNIFTY Index then the below code will help you. It gets the list of stocks from the import module.


NSEpy is a library to extract historical and real-time data from NSE’s website. This Library aims to keep the API very simple.


For more info refer to - https://thecapitalist19.wixsite.com/website/post/nsepy-documentation


Input:

import concurrent.futures
import nsepy
import pandas as pd
symbol = ["HDFCBANK", "SBIN", "PNB", "ICICIBANK", "AXISBANK", "KOTAKBANK", "INDUSINDBK", "AUBANK", "RBLBANK", "IDFCFIRSTB", "BANDHANBNK","IDFCFIRSTB"]
def data(symbol):
    data_nse = nsepy.get_quote(symbol.replace("&", "%26"))["data"][0]
    return {symbol.upper(): {"OPEN": float(data_nse['open'].replace(",", "")),
                              "HIGH": float(data_nse['dayHigh'].replace(",", "")),
                             "LOW": float(data_nse['dayLow'].replace(",", "")),
                             "CHANGE": float(data_nse['change'].replace(", ", "")),
                             "PCHANGE": float(data_nse['pChange'].replace(", ", "")),
                             "LTP": float(data_nse['lastPrice'].replace(",", ""))}
            }       
def get_multiple_stocks_data(symbol_list):
    multiple_stocks = {}
    with concurrent.futures.ThreadPoolExecutor() as executor:
        results = executor.map(data, symbol_list)
        for i in results:
            for k, v in i.items():
                 multiple_stocks[k] = v

    return multiple_stocks

stocks_data = get_multiple_stocks_data(symbol)
df = pd.DataFrame(stocks_data).transpose()
print(df)


Output: