vsjha18 / nsetools

Realtime Data From National Stock Exchange (India)
http://nsetools.readthedocs.io
MIT License
797 stars 420 forks source link

how to get lot size of stocks with original stock name, instead of some other symbol... #123

Open kishoretarun opened 3 years ago

kishoretarun commented 3 years ago

how to get lot size of stocks with original stock name, instead of some other symbol...

for example- It is returing "banknifty" with its lot size.. but the original stock symbol is "NIFTY BANK" is their any way to filter this out?

seenureddy commented 3 years ago

@kishoretarun As of now this code won't support that feature. You can try this code to find "NIFTY BANK".

If you want stock name in nsetool library change these lines in get_fno_lot_sizes method.

(underlying_code, _,  name) = [x.strip() for x in line.split(',')[0:3]]
res_dict[underlying_code] = int(name)

Save this in stock.py file and run python stock.py.

import six
import re
from http.cookiejar import CookieJar
from urllib.request import build_opener, HTTPCookieProcessor, Request
from pprint import pprint # just for neatness of display

headers = {
    'Accept': '*/*',
    'Accept-Language': 'en-US,en;q=0.5',
    'Host': 'www1.nseindia.com',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0',
    'X-Requested-With': 'XMLHttpRequest'
}

fno_lot_size_url = "https://www1.nseindia.com/content/fo/fo_mktlots.csv"
req = Request(fno_lot_size_url, None, headers, None, True)

cj = CookieJar()
opener = build_opener(HTTPCookieProcessor(cj))
res = opener.open(req)

strings = res.read().decode('latin-1')
fbuffer = six.StringIO(strings)

res_dict = {}
for line in fbuffer.read().split('\n'):
    if line != '' and re.search(',', line) and (line.casefold().find('symbol') == -1):
        (underlying_code, _,  name) = [x.strip() for x in line.split(',')[0:3]]
        res_dict[underlying_code] = int(name)
pprint(res_dict)