From e6365f5a140a6966f76e53267889d745c216123c Mon Sep 17 00:00:00 2001 From: Uday R Sawant Date: Sun, 4 Apr 2021 18:15:14 +0530 Subject: [PATCH] Get weekly expiry function based on get_expiry --- nsepy/derivatives/__init__.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/nsepy/derivatives/__init__.py b/nsepy/derivatives/__init__.py index 1ec9627..d3ea1a5 100644 --- a/nsepy/derivatives/__init__.py +++ b/nsepy/derivatives/__init__.py @@ -118,3 +118,13 @@ def get_expiry_date(year, month, index=True, stock=False, vix=False, recursion=0 build_dt_dict() return get_expiry_date(year, month, index, stock, vix, recursion=recursion+1) + +def get_weekly_expiry(date_val): + y = date_val.year + m = date_val.month + w = get_week_of_month(date_val) - 1 # week number in a month -1 for 0 based indices + + expiry = get_expiry_date(year=y, month=m) + expiry_list = sorted(list(expiry)) + + return expiry_list[w]