Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for most active and top volume gainers #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions nse.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ def __init__(self):
self.preopen_niftybank_url =\
"https://www.nseindia.com/live_market/dynaContent/live_analysis/pre_open/niftybank.json"
self.fno_lot_size_url = "https://www.nseindia.com/content/fo/fo_mktlots.csv"
self.top_volume = \
'https://www.nseindia.com/live_market/dynaContent/live_analysis/volume_spurts/volume_spurts.json'
self.most_active = \
'https://www.nseindia.com/live_market/dynaContent/live_analysis/most_active/allTopValue1.json'

def get_fno_lot_sizes(self, cached=True, as_json=False):
"""
Expand Down Expand Up @@ -169,7 +173,7 @@ def get_quote(self, code, as_json=False):
buffer = match.group(1).strip()
# commenting following two lines because now we are not using ast and instead
# relying on json's ability to do parsing. Should be much faster and more
# reliable.
# reliable.
#buffer = js_adaptor(buffer)
#response = self.clean_server_response(ast.literal_eval(buffer)['data'][0])
response = self.clean_server_response(json.loads(buffer)['data'][0])
Expand Down Expand Up @@ -287,7 +291,7 @@ def get_year_high(self, as_json=False):

def get_year_low(self, as_json=False):
return self._get_json_response_from_url(self.year_low_url, as_json)

def get_preopen_nifty(self, as_json=False):
return self._get_json_response_from_url(self.preopen_nifty_url, as_json)

Expand Down Expand Up @@ -458,13 +462,42 @@ def __str__(self):
"""
return 'Driver Class for National Stock Exchange (NSE)'

def get_top_volume(self, as_json=False):
"""
:return: a list of dictionaries containing top volume gainers of the day
"""
url = self.top_volume
req = Request(url, None, self.headers)
# this can raise HTTPError and URLError
res = self.opener.open(req)
# for py3 compat covert byte file like object to
# string file like object
res = byte_adaptor(res)
res_dict = json.load(res)
# clean the output and make appropriate type conversions
res_list = [self.clean_server_response(item) for item in res_dict['data']]
return self.render_response(res_list, as_json)

def get_most_active(self, as_json=False):
"""
:return: a list of dictionaries containing most active of the day
"""
url = self.most_active
req = Request(url, None, self.headers)
# this can raise HTTPError and URLError
res = self.opener.open(req)
# for py3 compat covert byte file like object to
# string file like object
res = byte_adaptor(res)
res_dict = json.load(res)
# clean the output and make appropriate type conversions
res_list = [self.clean_server_response(item) for item in res_dict['data']]
return self.render_response(res_list, as_json)

if __name__ == "__main__":
n = Nse()
data = n.download_bhavcopy("14th Dec")

# TODO: get_most_active()
# TODO: get_top_volume()
# TODO: get_peer_companies()
# TODO: is_market_open()
# TODO: concept of portfolio for fetching price in a batch and field which should be captured
Expand Down