Skip to content

Commit

Permalink
Adding indices data (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
pratik141 authored Jul 3, 2023
1 parent 15a606b commit 379c5bb
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 13 deletions.
17 changes: 16 additions & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,19 @@ max-args=10
[FORMAT]

# Regexp for a line that is allowed to be longer than the limit.
ignore-long-lines=^\s*(# )?<?https?://\S+>?$|^\s*(\w*\s*=\s*)?(\"|\').*(\"|\'),?\s*$
ignore-long-lines=^\s*(# )?<?https?://\S+>?$|^\s*(\w*\s*=\s*)?(\"|\').*(\"|\'),?\s*$


[SIMILARITIES]

# Minimum lines number of a similarity.
min-similarity-lines=10

# Ignore comments when computing similarities.
ignore-comments=yes

# Ignore docstrings when computing similarities.
ignore-docstrings=yes

# Ignore imports when computing similarities.
ignore-imports=no
11 changes: 11 additions & 0 deletions nsedt/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""
Main file
"""
import logging
from nsedt.resources import constants as cns

logging.basicConfig(
level=logging.INFO,
format=cns.LOG_FORMAT,
datefmt="%m/%d/%Y %I:%M:%S %p",
)
8 changes: 2 additions & 6 deletions nsedt/equity.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
from nsedt.resources import constants as cns
from nsedt.utils import data_format

logging.basicConfig(
level=logging.INFO,
format=cns.LOG_FORMAT,
datefmt="%m/%d/%Y %I:%M:%S %p",
)
logger = logging.getLogger(__name__)


def get_companyinfo(
Expand Down Expand Up @@ -95,7 +91,7 @@ def get_price(
"""
cookies = utils.get_cookies()
base_url = cns.BASE_URL
price_api = cns.EQUITY_PRICE_HISTROY
price_api = cns.EQUITY_PRICE_HISTORY
url_list = []

# set the window size to one year
Expand Down
82 changes: 82 additions & 0 deletions nsedt/indices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"""
get data for indices
"""
import concurrent
import datetime
import logging
import urllib
from concurrent.futures import ALL_COMPLETED

import pandas as pd

from nsedt import utils
from nsedt.resources import constants as cns
from nsedt.utils import data_format

log = logging.getLogger("root")


def get_price(
start_date,
end_date,
symbol,
response_type="panda_df",
):
"""
Args:
symbol (str): stock symbol.
response_type (str, Optional): define the response type panda_df | json. Default panda_df
Returns:
Pandas DataFrame: df containing company info
or
Json: json containing company info
"""
params = {}
cookies = utils.get_cookies()
base_url = cns.BASE_URL
event_api = cns.INDEX_PRICE_HISTORY

url_list = []

# set the window size to one year
window_size = datetime.timedelta(days=cns.WINDOW_SIZE)

current_window_start = start_date
while current_window_start < end_date:
current_window_end = current_window_start + window_size

# check if the current window extends beyond the end_date
current_window_end = min(current_window_end, end_date)

params = {
"indexType": symbol,
"from": current_window_start.strftime("%d-%m-%Y"),
"to": current_window_end.strftime("%d-%m-%Y"),
}
url = base_url + event_api + urllib.parse.urlencode(params)
url_list.append(url)

# move the window start to the next day after the current window end
current_window_start = current_window_end + datetime.timedelta(days=1)

result = pd.DataFrame()
with concurrent.futures.ThreadPoolExecutor(max_workers=cns.MAX_WORKERS) as executor:
future_to_url = {
executor.submit(utils.fetch_url, url, cookies, response_type="json"): url
for url in url_list
}
concurrent.futures.wait(future_to_url, return_when=ALL_COMPLETED)
for future in concurrent.futures.as_completed(future_to_url):
url = future_to_url[future]
try:
dataframe = data_format.indices(future.result())
result = pd.concat([result, dataframe])
except Exception as exc:
log.error("%s got exception: %s. Please try again later.", url, exc)
raise exc

if response_type == "panda_df":
return result
return result.to_json(orient="records")
10 changes: 9 additions & 1 deletion nsedt/resources/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@
}"""

BASE_URL = "https://www.nseindia.com/"
EQUITY_PRICE_HISTROY = "api/historical/securityArchives?"

### EQUITY
EQUITY_PRICE_HISTORY = "api/historical/securityArchives?"
EQUITY_CORPINFO = "api/corporates-corporateActions?"
MARKETSTATUS = "api/marketStatus"
EQUITY_EVENT = "api/event-calendar?"
EQUITY_CHART = "api/chart-databyindex?"
EQUITY_INFO = "api/quote-equity?"

### Index
INDEX_PRICE_HISTORY = "api/historical/indicesHistory?"

### DERIVATIVES
DERIVATIVES_PRICE = "api/option-chain-equities?"
14 changes: 11 additions & 3 deletions nsedt/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import pandas as pd
import requests

from nsedt.resources import constants as cns


Expand Down Expand Up @@ -44,7 +43,7 @@ def get_cookies():
return response.cookies.get_dict()


def fetch_url(url, cookies, key=None):
def fetch_url(url, cookies, key=None, response_type="panda_df"):
"""
Args:
url (str): URL to fetch
Expand All @@ -55,9 +54,18 @@ def fetch_url(url, cookies, key=None):
"""

response = requests.get(url, timeout=30, headers=get_headers(), cookies=cookies)
response = requests.get(
url=url,
timeout=30,
headers=get_headers(),
cookies=cookies,
)

if response.status_code == 200:
json_response = json.loads(response.content)

if response_type != "panda_df":
return json_response
if key is None:
return pd.DataFrame.from_dict(json_response)

Expand Down
36 changes: 36 additions & 0 deletions nsedt/utils/data_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,39 @@ def price(result):
result = result.sort_values("Date", ascending=True)
result.reset_index(drop=True, inplace=True)
return result


def indices(data_json):
"""
Args:
data_json (json): data in json format
Returns:
Pandas DataFrame: df with indexCloseOnlineRecords and indexTurnoverRecords
"""
data_close_df = (
pd.DataFrame(data_json["data"]["indexCloseOnlineRecords"])
.drop(columns=["_id", "EOD_INDEX_NAME", "EOD_TIMESTAMP"])
.rename(
columns={
"EOD_OPEN_INDEX_VAL": "Open Price",
"EOD_HIGH_INDEX_VAL": "High Price",
"EOD_CLOSE_INDEX_VAL": "Close Price",
"EOD_LOW_INDEX_VAL": "Low Price",
"TIMESTAMP": "Date",
}
)
)

data_turnover_df = (
pd.DataFrame(data_json["data"]["indexTurnoverRecords"])
.drop(columns=["_id", "HIT_INDEX_NAME_UPPER", "HIT_TIMESTAMP"])
.rename(
columns={
"HIT_TRADED_QTY": "Total Traded Quantity",
"HIT_TURN_OVER": "Total Traded Value",
"TIMESTAMP": "Date",
}
)
)

return pd.merge(data_close_df, data_turnover_df, on="Date", how="inner")
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
"""
Install script
"""
from setuptools import setup, find_packages
from setuptools import find_packages, setup

with open("README.md", "r", encoding="utf-8") as file:
long_description = file.read()

setup(
name="nsedt",
version="0.0.5",
version="0.0.6",
author="Pratik Anand",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 379c5bb

Please sign in to comment.