Skip to content

Commit

Permalink
unified-datetime-format (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
pratik141 authored Oct 29, 2023
1 parent 704f324 commit ef34cd1
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Thank you for using Nsedt. Please feel free to send pull requests, comments, and
| symbols_list | get_symbols_list | symbols_list | -- | json |
| asm_list | get_asm_list | symbols_list | asm_type | json |

Now get_price work with start_date, end_date without datetime format it support `%d-%m-%Y`

### step to run
```py
Expand Down
2 changes: 2 additions & 0 deletions nsedt/equity.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ def get_price(
# set the window size to one year
window_size = datetime.timedelta(days=cns.WINDOW_SIZE)

start_date, end_date = utils.check_nd_convert(start_date, end_date)

current_window_start = start_date
while current_window_start < end_date:
current_window_end = current_window_start + window_size
Expand Down
1 change: 1 addition & 0 deletions nsedt/indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def get_price(

# set the window size to one year
window_size = datetime.timedelta(days=cns.WINDOW_SIZE)
start_date, end_date = utils.check_nd_convert(start_date, end_date)

current_window_start = start_date
while current_window_start < end_date:
Expand Down
1 change: 1 addition & 0 deletions nsedt/resources/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
EQUITY_CHART = "api/chart-databyindex?"
EQUITY_INFO = "api/quote-equity?"
EQUITY_LIST = "api/market-data-pre-open?key=ALL"
ASM_LIST = "api/reportASM"

### Index
INDEX_PRICE_HISTORY = "api/historical/indicesHistory?"
Expand Down
35 changes: 35 additions & 0 deletions nsedt/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
"""

import json
import deprecated

import pandas as pd
import requests
from nsedt.resources import constants as cns
import datetime
from warnings import warn


def get_headers():
Expand Down Expand Up @@ -100,3 +103,35 @@ def get_symbol(symbol: str, get_key: str) -> str:
val = item[get_key]

return val if val else symbol


def check_nd_convert(start_date: str, end_date: str) -> datetime:
"""
The function `check_nd_convert` takes two date strings in the format "%d-%m-%Y" and converts them to
datetime objects if they are not already in that format.
:param start_date: The `start_date` parameter is the starting date of a period, specified as a
string in the format "%d-%m-%Y"
:type start_date: str
:param end_date: The `end_date` parameter is a string representing the end date in the format
"%d-%m-%Y"
:type end_date: str
:return: the start_date and end_date as datetime objects.
"""

if isinstance(start_date, datetime.date) and isinstance(end_date, datetime.date):
warn(
"""Passing start_date, end_date in date is deprecated
now pass in str '%d-%m-%Y' format""",
DeprecationWarning,
stacklevel=2,
)

elif isinstance(start_date, str) and isinstance(end_date, str):
start_date = datetime.datetime.strptime(start_date, "%d-%m-%Y")
end_date = datetime.datetime.strptime(end_date, "%d-%m-%Y")

else:
raise ("Input is of an unknown type")

return start_date, end_date
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
requests
numpy
pandas
pandas
Deprecated

0 comments on commit ef34cd1

Please sign in to comment.