Skip to content

Commit

Permalink
major update
Browse files Browse the repository at this point in the history
  • Loading branch information
debpal committed Sep 30, 2024
1 parent 21204e1 commit 4e840ef
Show file tree
Hide file tree
Showing 30 changed files with 1,079 additions and 321 deletions.
1 change: 0 additions & 1 deletion .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,4 @@ jobs:
- name: Lint with flake8
run: |
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
2 changes: 1 addition & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install pytest pytest-cov # Install pytest and pytest-cov for coverage
python -m pip install -r requirements-gh-action.txt # Install dependencies
- name: Run tests with pytest
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/typing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install mypy
python -m pip install -r requirements-mypy.txt # Install dependencies
- name: Type check with mypy
run: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ coverage.xml
.hypothesis/
.pytest_cache/
cover/
tox.ini

# Translations
*.mo
Expand Down
2 changes: 1 addition & 1 deletion .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ build:

python:
install:
- requirements: docs/requirements.txt
- requirements: docs/requirements-docs.txt

sphinx:
configuration: docs/conf.py
Expand Down
8 changes: 5 additions & 3 deletions BharatFinTrack/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from .nse_track import NSETrack
from .nse_product import NSEProduct
from .nse_tri import NSETRI


__all__ = [
'NSETrack'
'NSEProduct',
'NSETRI'
]


__version__ = '0.0.3'
__version__ = '0.1.0'
140 changes: 140 additions & 0 deletions BharatFinTrack/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import os
import json
import typing
import datetime
import pandas
import requests


class Core:

'''
Core functionality of :mod:`BharatFinTrack` module.
'''

def _excel_file_extension(
self,
file_path: str
) -> str:

'''
Returns the extension of an Excel file.
Parameters
----------
file_path : str
Path of the Excel file.
Returns
-------
str
Extension of the Excel file.
'''

output = os.path.splitext(file_path)[-1]

return output

def string_to_date(
self,
date_string: str
) -> datetime.date:

'''
Converts a date string is in format 'DD-MMM-YYYY' to a `datetime.date` object.
Parameters
----------
date_string : str
Date string in the format 'DD-MMM-YYYY'.
Returns
-------
datetime.date
A `datetime.date` object corresponding to the input date string.
'''

output = datetime.datetime.strptime(date_string, '%d-%b-%Y').date()

return output

@property
def default_http_headers(
self,
) -> dict[str, str]:

'''
Returns the default http headers to be used for the web requests.
'''

output = {
'Content-Type': 'application/json; charset=UTF-8',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',
'Origin': 'https://www.niftyindices.com',
'Accept-Encoding': 'gzip, deflate, br, zstd',
'Connection': 'keep-alive',
'X-Requested-With': 'XMLHttpRequest'
}

return output

@property
def url_nse_index_tri_data(
self,
) -> str:

'''
Returns the url to access TRI (Total Return Index) data of NSE equity indices.
'''

output = 'https://www.niftyindices.com/Backpage.aspx/getTotalReturnIndexString'

return output

def _download_nse_tri(
self,
index_api: str,
start_date: str,
end_date: str,
index: str,
http_headers: typing.Optional[dict[str, str]] = None
) -> pandas.DataFrame:

'''
Helper method for the :meth:`NSETRI.download_historical_daily_data`.
'''

# payloads
parameters = {
'name': index_api,
'startDate': start_date,
'endDate': end_date,
'indexName': index
}
payload = json.dumps(
{
'cinfo': json.dumps(parameters)
}
)

# web request headers
headers = self.default_http_headers if http_headers is None else http_headers

# sent web requets
response = requests.post(
url=self.url_nse_index_tri_data,
headers=headers,
data=payload
)
response_data = response.json()
records = json.loads(response_data['d'])
df = pandas.DataFrame.from_records(records)
df = df.iloc[:, -2:][::-1].reset_index(drop=True)
df['Date'] = df['Date'].apply(
lambda x: datetime.datetime.strptime(x, '%d %b %Y').date()
)
df = df.rename(columns={'TotalReturnsIndex': 'Close'})
df['Close'] = df['Close'].astype(float)

return df
Binary file added BharatFinTrack/data/equity_indices.xlsx
Binary file not shown.
Loading

0 comments on commit 4e840ef

Please sign in to comment.