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

enhanced-OI #195

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
92 changes: 89 additions & 3 deletions nsepy/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""

from nsepy.urls import *
from nsepy.derivatives import get_expiry_date
import six
from nsepy.commons import *
from nsepy.constants import *
Expand Down Expand Up @@ -312,10 +313,14 @@ def get_index_pe_history_quanta(symbol, start, end):


def get_price_list(dt, series='EQ'):

# Downloads cash market bhavcopy

MMM = dt.strftime("%b").upper()
yyyy = dt.strftime("%Y")

"""
Date format for the url:
1. YYYY
2. MMM
3. ddMMMyyyy
Expand All @@ -324,14 +329,95 @@ def get_price_list(dt, series='EQ'):
txt = unzip_str(res.content)
fp = six.StringIO(txt)
df = pd.read_csv(fp)
del df['Unnamed: 13']
df = df.drop([df.columns[-1]], axis=1)
return df[df['SERIES'] == series]

"""
Get FII/DII Data
"""

def get_participant_wise_OI(dt):

"""
1. dt (datetime.date)
"""
res = daily_fno_participant_wise_oi_url(dt.strftime("%d%m%Y"))

if res.status_code == 404:
print("Failed to fetch data. Check date. ")
return None
else:
df = pd.read_csv(io.StringIO(res.content.decode('utf-8')))
df.columns = df.iloc[0,:]
df = df.drop(index=[0,5],axis=0)
df = df.reset_index(drop=True,inplace=False)
return df

"""
Get Trade and Delivery Volume for each stock
Get Derivatives Segment Bhavcopy
"""

def get_derivatives_price_list(dt, series):

"""
Downloads derivatives market bhavcopy
Args:
dt (datetime.date)
series (str) = FUTIDX, FUTSTK, OPTIDX or OPTSTK
"""

MMM = dt.strftime("%b").upper()
yyyy = dt.strftime("%Y")
"""
Date format for the url:
1. YYYY
2. MMM
3. ddMMMyyyy
"""
try:
res = derivative_price_list_url(yyyy, MMM, dt.strftime("%d%b%Y").upper())
txt = unzip_str(res.content)
fp = six.StringIO(txt)
df = pd.read_csv(fp)
df = df.drop([df.columns[-1]], axis=1)
return df[df['INSTRUMENT'] == series]
except:
pass

"""
Get historical OI (three expiry months combined)
"""

def get_historical_OI(tickerid, start, end):

"""
Gets historical Futures Open Interest

1. tickerid(str): instrument name
2. start (datetime.date): start date
3. end (datetime.date): end date
"""
historical_oi = pd.DataFrame(columns = ['Date', 'Symbol', 'Futures OI','Change in OI'])
while start <= end:
try:
prices = get_derivatives_price_list(dt=start, series='FUTSTK')
prices = prices[['SYMBOL','EXPIRY_DT','OPEN_INT','CHG_IN_OI','TIMESTAMP']]
ticker_data = prices[prices['SYMBOL'] == tickerid]
historical_oi = historical_oi.append({'Date':ticker_data.values[0][4],
'Symbol':ticker_data.values[0][0],
'Futures OI':ticker_data['OPEN_INT'].sum(),
'Change in OI':ticker_data['CHG_IN_OI'].sum()}, ignore_index=True)
#print(f"Downloaded data for {start}")
except:
pass
start += timedelta(days=1)

return historical_oi


"""
Get Trade and Delivery Volume for each stock
"""

def get_delivery_position(dt, segment='EQ'):
MMM = dt.strftime("%b").upper()
Expand Down Expand Up @@ -426,4 +512,4 @@ def get_rbi_ref_history_quanta(start, end):
schema=RBI_REF_RATE_SCHEMA,
headers=RBI_REF_RATE_HEADERS, index="Date")
df = tp.get_df()
return df
return df
7 changes: 7 additions & 0 deletions nsepy/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ def get_symbol_count(symbol):
price_list_url = URLFetchSession(
url='https://www1.nseindia.com/content/historical/EQUITIES/%s/%s/cm%sbhav.csv.zip')

"""
1. ddmmyyyy
"""

daily_fno_participant_wise_oi_url = URLFetchSession(
url='https://www1.nseindia.com/content/nsccl/fao_participant_oi_%s.csv')

"""
1. ddmmyyyy
"""
Expand Down