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

Reports: Added logic to download bhav copy and market activity reports. #50

Merged
merged 18 commits into from
May 4, 2024
Merged
37 changes: 30 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Thank you for using Nsedt. Please feel free to send pull requests, comments, and
`pip install -r requirements.txt`
3. Install locally
`pip install . --upgrade`

---

## Equity
Expand Down Expand Up @@ -84,13 +85,14 @@ data["Date"] = pd.to_datetime(data["Date"],format='%d-%b-%Y')

### Details

| Name | Module name | Description | Argument | Response |
| ------------------------ | ------------------------ | ------------------------ | ------------------------------------------------------------------------- | -------- |
| vix | get_vix | price | start_date, end_date,columns_drop_list | panda df |
| option chain | get_option_chain | get option price | symbol,strikePrice,expiryDate | panda df |
| option chain expiry date | get_option_chain_expdate | option chain expiry date | symbol | json |
| future price | get_future_price | get future price | symbol, start_date, end_date, expiryDate,response_type, columns_drop_list | panda df |
| future expiry date | get_future_expdate | future expiry date | symbol | json |
| Name | Module name | Description | Argument | Response |
| ------------------------ | -------------------------- | ---------------------------------------------------- | ------------------------------------------------------------------------- | --------------- |
| vix | get_vix | price | start_date, end_date,columns_drop_list | panda df |
| option chain | get_option_chain | get option price | symbol,strikePrice,expiryDate | panda df |
| option chain expiry date | get_option_chain_expdate | option chain expiry date | symbol | json |
| future price | get_future_price | get future price | symbol, start_date, end_date, expiryDate,response_type, columns_drop_list | panda df |
| future expiry date | get_future_expdate | future expiry date | symbol | json |
| historical option chain | get_historical_option_data | get historical option value for a given strike price | symbol, start_date,end_date,option_type,strike_price,year,expiry_date | json, pandas df |

### step to run

Expand All @@ -99,10 +101,31 @@ from nsedt import derivatives as de
start_date = "24-04-2024"
end_date = "25-04-2024"
# date format "%d-%m-%Y"

print(de.get_vix(start_date, end_date))
print(de.get_option_chain_expdate(symbol="TCS"))
print(de.get_option_chain(symbol="TCS", strike_price=3300, expiry_date="30-05-2024"))
print(de.get_future_price(symbol="TCS", start_date=start_date, end_date=end_date))
print(de.get_future_expdate(symbol="TCS"))
print(de.get_historical_option_data(symbol="TATAMOTORS", start_date=start_date, end_date=end_date, option_type="CE", strike_price="1020", year="2024", expiry_date="30-May-2024"))
```

# Reports

### Details

| Name | Module name | Description | Argument | Response |
| --------------- | -------------------------- | -------------------------------------- | ----------------------- | -------- |
| market activity | get_market_activity_report | get raw text of market activity report | date | string |
| bhav copy | get_bhav_copy_zip | download bhav copy zip for a given day | date, file_path_to_save | bool |

### step to run

```py
from nsedt import reports as rep
# date format "%d-%m-%Y"

print(rep.get_market_activity_report(date="300424")) # format %d%m%y
print(rep.get_bhav_copy_zip(date="30APR2024", file_path="path_where_you_want_to_save")) # format %d%b%Y

```
6 changes: 1 addition & 5 deletions nsedt/derivatives/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
from nsedt import utils
from nsedt.resources import constants as cns
from nsedt.utils import data_format
from nsedt.derivatives.options import (
get_option_chain,
get_option_chain_expdate,
get_historical_option_data
)
from nsedt.derivatives.options import get_option_chain, get_option_chain_expdate, get_historical_option_data
from nsedt.derivatives.futures import get_future_price, get_future_expdate

log = logging.getLogger("root")
Expand Down
61 changes: 61 additions & 0 deletions nsedt/reports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""
function to download reports
"""

import logging

from nsedt.utils import get_cookies, fetch_csv, validate_date_format
from nsedt.resources import constants as cns

log = logging.getLogger("root")


def get_market_activity_report(date: str):
"""
get_market_activity_report

Args:\n
date (str): date for which to download market activity report\n
response_type (str, Optional): define the response type panda_df | json . Default json\n
Returns:
string: string content of the file as right now its not possible
to format the content to json or pandas df
Expects:
date to be in format of "ddmmYY" eg: 30/04/2024 => 300424
all other cases will be invalidated
"""
if not validate_date_format(date,format='%d%m%y'):
raise ValueError("Please provide date format in 'ddmmYY' format")
pratik141 marked this conversation as resolved.
Show resolved Hide resolved

cookies = get_cookies()
url = f"{cns.REPORT_URL}{cns.MARKET_ACTIVITY_REPORT}{date}.csv"
return fetch_csv(url, cookies, response_type="raw")


def get_bhav_copy_zip(date: str, file_path: str):
"""
get_market_activity_report

Args:\n
date (str): date for which to download market activity report\n
path (str): path to save the bhav copy zip
Returns:
bool: if the file is save to the local path or not
Expects:
date to be in format of "ddmmYY" eg: 30/04/2024 => 30APR2024
all other cases will be invalidated
"""
if not validate_date_format(date, format='%d%b%Y'):
raise ValueError("Please provide date format in 'ddMMMYYYY' format")
pratik141 marked this conversation as resolved.
Show resolved Hide resolved

cookies = get_cookies()
url = f"{cns.REPORT_URL}{cns.BHAV_COPY_REPORT}{date}bhav.csv.zip"
print(url)
content = fetch_csv(url, cookies, response_type="raw")
file_path = file_path.removesuffix("/")
try:
with open(f"{file_path}/{date}bhav.csv.zip", 'wb') as f:
f.write(content)
except Exception as e:
raise ValueError("Unable to download the bhavcopy zip") from e

5 changes: 5 additions & 0 deletions nsedt/resources/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
}"""

BASE_URL = "https://www.nseindia.com/"
REPORT_URL = "https://nsearchives.nseindia.com/"

### EQUITY
EQUITY_PRICE_HISTORY = "api/historical/securityArchives?"
Expand All @@ -38,3 +39,7 @@
INDICES = ["NIFTY", "FINNIFTY", "BANKNIFTY"]
VIX_HISTORY = "api/historical/vixhistory?"
FNO_HISTORY = "api/historical/foCPV?"

# Reports
MARKET_ACTIVITY_REPORT = "archives/equities/mkt/MA"
BHAV_COPY_REPORT = "content/historical/EQUITIES/2024/APR/cm"
58 changes: 56 additions & 2 deletions nsedt/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,36 @@
utils for nsedt
"""

import io
import json
import datetime
from warnings import warn
import pandas as pd
import requests

import pandas as pd

from warnings import warn

from nsedt.resources import constants as cns


from datetime import datetime


def validate_date_format(input_string: str, format='%d%m%y'):
"""
Args:\n
- input_string : str date format for a format to check
- format : type of string to format
Returns:\n
- bool: whether a given date format matches the given format
"""
try:
datetime.strptime(input_string, format)
return True
except ValueError:
return False



def get_headers():
"""
Expand All @@ -31,6 +53,7 @@ def get_headers():
}



def get_cookies():
"""
Args:
Expand Down Expand Up @@ -132,3 +155,34 @@ def check_nd_convert(start_date: str, end_date: str) -> datetime:
raise ValueError("Input is of an unknown type")

return start_date, end_date



def fetch_csv(url, cookies, response_type="panda_df"):
"""
Args:

url (str): URL to fetch
cookies (str): NSE cookies
key (str, Optional):

Returns:

Pandas DataFrame: df generated from csv
OR
Json: json output of the csv
OR
String: raw content for files where it cannot be processed into Json or
Pandas df

"""

response = requests.get(
url=url, timeout=30, headers=get_headers(), cookies=cookies )
if response.status_code == 200:
if response_type == "raw":
return response.content
csv_content = response.content.decode('utf-8')
df = pd.read_csv(io.StringIO(csv_content), error_bad_lines=False)
return df.to_json(orient='records') if response_type == "json" else df
raise ValueError("Please try again in a minute.")
2 changes: 1 addition & 1 deletion nsedt/utils/data_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def derivaties_options(
"FH_LAST_TRADED_PRICE",
"TIMESTAMP",
]

if response_type == "json":
data_json_ret = []
for record in data_json:
Expand Down
Loading