Skip to content

Commit

Permalink
Merge pull request #100 from fuzzyhandle/master
Browse files Browse the repository at this point in the history
Survived
  • Loading branch information
swapniljariwala authored Jan 20, 2020
2 parents 3a603e0 + 314c3da commit 76b1f5a
Show file tree
Hide file tree
Showing 18 changed files with 832 additions and 502 deletions.
89 changes: 54 additions & 35 deletions nsepy/derivatives/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,90 +11,109 @@
vix_exp = {}
stk_exp = {}


def add_dt(instru, dt):
try:
instru[dt.year][dt.month] = dt
except:
instru[dt.year]={}
instru[dt.year][dt.month] = dt
if not dt.year in instru:
instru[dt.year] = {}

if not dt.month in instru[dt.year]:
instru[dt.year][dt.month] = set()

instru[dt.year][dt.month].add(dt)


class ExpiryDateError(Exception):
def __init__(self, message):

# Call the base class constructor with the parameters it needs
super(ExpiryDateError, self).__init__(message)


def build_dt_dict():
lines = urls.derivative_expiry_dates_url().text

for line in lines.split('\n'):
s = re_date.search(line)
s = re_date.search(line)

if s:
dt = datetime.datetime.strptime(s.group(1), "%d-%m-%Y").date()

if line.find('indxExpryDt')>-1 :
# Start Kludge
# The list on NSE portal for expiry date has a wrong entry for 20 Sep 2019
# Handle this oulier use case by ignoring this date and skpping it for processing
if dt == datetime.datetime(2019, 9, 20).date():
continue
# End Kludge
if line.find('indxExpryDt') > -1:
try:

existing_date = try_to_get_expiry_date(dt.year, dt.month, index=True)
existing_date = try_to_get_expiry_date(
dt.year, dt.month, index=True)
if existing_date < dt:
add_dt(idx_exp, dt)
except:
add_dt(idx_exp, dt)
if line.find('stk')>-1 :

if line.find('stk') > -1:
try:
existing_date = try_to_get_expiry_date(dt.year, dt.month, index=True)
existing_date = try_to_get_expiry_date(
dt.year, dt.month, index=False, stock=False, vix=False)
if existing_date < dt:
add_dt(stk_exp, dt)
except:
add_dt(stk_exp, dt)

if line.find('vix')>-1:
add_dt(vix_exp, dt)
if line.find('vix') > -1:
try:
existing_date = try_to_get_expiry_date(
dt.year, dt.month, index=False, stock=False, vix=True)
if existing_date < dt:
add_dt(vix_exp, dt)
except:
add_dt(vix_exp, dt)


def is_valid_expiry(dt):
# not a perfect logic :P
if (dt.month!=2 and dt.day>=23) or (dt.month==2 and dt.day>=21):
if (dt.month != 2 and dt.day >= 23) or (dt.month == 2 and dt.day >= 21):
return True
def try_to_get_expiry_date(year, month,index=True, stock=False, vix=False):


def try_to_get_expiry_date(year, month, index=True, stock=False, vix=False):

try:
if vix and vix_exp:
return vix_exp[year][month]

if stock and stk_exp:
return stk_exp[year][month]

if index and idx_exp:
return idx_exp[year][month]

raise Exception
except:

if index:
name = 'index derivatives'
if stock:
name = 'stock derivatives'
else:
name = 'vix derivatives'
raise ExpiryDateError('No expiry date found in the month of {}-{} for {}'.format(year, month, name))


raise ExpiryDateError(
'No expiry date found in the month of {}-{} for {}'.format(year, month, name))


def get_expiry_date(year, month, index=True, stock=False, vix=False, recursion=0):

try:
return try_to_get_expiry_date(year, month, index, stock, vix)
except:
if recursion>1:

if recursion > 1:
raise

else: pass


else:
pass

#print("building dictionary")

build_dt_dict()
Expand Down
4 changes: 2 additions & 2 deletions nsepy/derivatives/archives.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
from nsepy.archives import date_to_str, __raw_zip_data_to_str


PRICE_LIST_URL = 'http://www.nseindia.com/content/historical/DERIVATIVES/%s/%s/fo%sbhav.csv.zip'
PRICE_LIST_URL = 'http://www1.nseindia.com/content/historical/DERIVATIVES/%s/%s/fo%sbhav.csv.zip'

DERIVATIVE_ARCHIVES = 'http://www.nseindia.com/products/dynaContent/common/productsSymbolMapping.jsp?instrumentType=OPTIDX&symbol=NIFTY&expiryDate=27-07-2006&optionType=CE&strikePrice=&dateRange=week&fromDate=&toDate=&segmentLink=9&symbolCount='
DERIVATIVE_ARCHIVES = 'http://www1.nseindia.com/products/dynaContent/common/productsSymbolMapping.jsp?instrumentType=OPTIDX&symbol=NIFTY&expiryDate=27-07-2006&optionType=CE&strikePrice=&dateRange=week&fromDate=&toDate=&segmentLink=9&symbolCount='


def get_price_list(dt, proxies={}):
Expand Down
Loading

0 comments on commit 76b1f5a

Please sign in to comment.