nsedt.derivatives.futures
get data for Futures
1""" 2get data for Futures 3""" 4import urllib 5from datetime import datetime, timedelta 6 7from nsedt import utils 8from nsedt.resources import constants as cns 9from nsedt.utils import data_format 10 11 12def get_future_price( 13 symbol: str, 14 start_date: str, 15 end_date: str, 16 expiry_date: str = None, 17 response_type: str = "panda_df", 18 columns_drop_list: list = None, 19): 20 """ 21 get future price of stock / indices 22 Args: 23 symbol (str): _description_ 24 start_date (str): _description_ 25 end_date (str): _description_ 26 expiry_date (str, optional): _description_. Defaults to None. 27 response_type (str, optional): _description_. Defaults to "panda_df". 28 columns_drop_list (list, optional): _description_. Defaults to None. 29 30 Returns: 31 _type_: _description_ 32 """ 33 cookies = utils.get_cookies() 34 base_url = cns.BASE_URL 35 event_api = cns.FNO_HISTORY 36 symbol = utils.get_symbol(symbol=symbol, get_key="derivatives") 37 params = { 38 "symbol": symbol, 39 "from": start_date, 40 "to": end_date, 41 } 42 if symbol in cns.INDICES: 43 params["instrumentType"] = "FUTIDX" 44 else: 45 params["instrumentType"] = "FUTSTK" 46 47 url = base_url + event_api + urllib.parse.urlencode(params) 48 data = utils.fetch_url(url, cookies, response_type="json") 49 50 if expiry_date: 51 filtered_data = [ 52 record 53 for record in data["data"] 54 if record["FH_EXPIRY_DT"] 55 == datetime.strptime(expiry_date, "%d-%m-%Y").strftime("%d-%b-%Y") 56 ] 57 else: 58 filtered_data = data["data"] 59 60 return data_format.derivatives_futures( 61 filtered_data, 62 response_type=response_type, 63 columns_drop_list=columns_drop_list, 64 ) 65 66 67def get_future_expdate(symbol: str) -> list: 68 """get expiry dates of futures 69 70 Args: 71 symbol (str): symbol name 72 73 Returns: 74 list: expiry dates 75 """ 76 cookies = utils.get_cookies() 77 base_url = cns.BASE_URL 78 event_api = cns.FNO_HISTORY 79 params = { 80 "symbol": symbol, 81 "from": (datetime.now() - timedelta(days=3)).strftime("%d-%m-%Y"), 82 "to": datetime.now().strftime("%d-%m-%Y"), 83 } 84 if symbol in cns.INDICES: 85 params["instrumentType"] = "FUTIDX" 86 else: 87 params["instrumentType"] = "FUTSTK" 88 89 url = base_url + event_api + urllib.parse.urlencode(params) 90 data = utils.fetch_url(url, cookies, response_type="json") 91 ret = [] 92 for rec in data["data"]: 93 ret.append( 94 datetime.strptime(rec["FH_EXPIRY_DT"], "%d-%b-%Y").strftime("%d-%m-%Y") 95 ) 96 return list(set(ret))
def
get_future_price( symbol: str, start_date: str, end_date: str, expiry_date: str = None, response_type: str = 'panda_df', columns_drop_list: list = None):
13def get_future_price( 14 symbol: str, 15 start_date: str, 16 end_date: str, 17 expiry_date: str = None, 18 response_type: str = "panda_df", 19 columns_drop_list: list = None, 20): 21 """ 22 get future price of stock / indices 23 Args: 24 symbol (str): _description_ 25 start_date (str): _description_ 26 end_date (str): _description_ 27 expiry_date (str, optional): _description_. Defaults to None. 28 response_type (str, optional): _description_. Defaults to "panda_df". 29 columns_drop_list (list, optional): _description_. Defaults to None. 30 31 Returns: 32 _type_: _description_ 33 """ 34 cookies = utils.get_cookies() 35 base_url = cns.BASE_URL 36 event_api = cns.FNO_HISTORY 37 symbol = utils.get_symbol(symbol=symbol, get_key="derivatives") 38 params = { 39 "symbol": symbol, 40 "from": start_date, 41 "to": end_date, 42 } 43 if symbol in cns.INDICES: 44 params["instrumentType"] = "FUTIDX" 45 else: 46 params["instrumentType"] = "FUTSTK" 47 48 url = base_url + event_api + urllib.parse.urlencode(params) 49 data = utils.fetch_url(url, cookies, response_type="json") 50 51 if expiry_date: 52 filtered_data = [ 53 record 54 for record in data["data"] 55 if record["FH_EXPIRY_DT"] 56 == datetime.strptime(expiry_date, "%d-%m-%Y").strftime("%d-%b-%Y") 57 ] 58 else: 59 filtered_data = data["data"] 60 61 return data_format.derivatives_futures( 62 filtered_data, 63 response_type=response_type, 64 columns_drop_list=columns_drop_list, 65 )
get future price of stock / indices Args: symbol (str): _description_ start_date (str): _description_ end_date (str): _description_ expiry_date (str, optional): _description_. Defaults to None. response_type (str, optional): _description_. Defaults to "panda_df". columns_drop_list (list, optional): _description_. Defaults to None.
Returns: _type_: _description_
def
get_future_expdate(symbol: str) -> list:
68def get_future_expdate(symbol: str) -> list: 69 """get expiry dates of futures 70 71 Args: 72 symbol (str): symbol name 73 74 Returns: 75 list: expiry dates 76 """ 77 cookies = utils.get_cookies() 78 base_url = cns.BASE_URL 79 event_api = cns.FNO_HISTORY 80 params = { 81 "symbol": symbol, 82 "from": (datetime.now() - timedelta(days=3)).strftime("%d-%m-%Y"), 83 "to": datetime.now().strftime("%d-%m-%Y"), 84 } 85 if symbol in cns.INDICES: 86 params["instrumentType"] = "FUTIDX" 87 else: 88 params["instrumentType"] = "FUTSTK" 89 90 url = base_url + event_api + urllib.parse.urlencode(params) 91 data = utils.fetch_url(url, cookies, response_type="json") 92 ret = [] 93 for rec in data["data"]: 94 ret.append( 95 datetime.strptime(rec["FH_EXPIRY_DT"], "%d-%b-%Y").strftime("%d-%m-%Y") 96 ) 97 return list(set(ret))
get expiry dates of futures
Args: symbol (str): symbol name
Returns: list: expiry dates