nsedt.equity
get data for Equity
1""" 2get data for Equity 3""" 4import concurrent 5import datetime 6import logging 7import urllib 8from concurrent.futures import ALL_COMPLETED 9 10import pandas as pd 11 12from nsedt import utils 13from nsedt.resources import constants as cns 14 15from nsedt.utils import data_format 16 17log = logging.getLogger("root") 18 19 20def get_companyinfo( 21 symbol: str, 22 response_type: str = "panda_df", 23): 24 """get_companyinfo 25 26 Args:\n 27 symbol (str): stock name\n 28 response_type (str, Optional): define the response type panda_df | json. Default panda_df\n 29 Returns:\n 30 Pandas DataFrame: df containing company info\n 31 or\n 32 Json: json containing company info\n 33 """ 34 35 params = {} 36 cookies = utils.get_cookies() 37 base_url = cns.BASE_URL 38 event_api = cns.EQUITY_INFO 39 40 params["symbol"] = symbol 41 42 url = base_url + event_api + urllib.parse.urlencode(params) 43 data = utils.fetch_url( 44 url, 45 cookies, 46 key=None, 47 response_type=response_type, 48 ) 49 50 return data 51 52 53def get_marketstatus( 54 response_type: str = "panda_df", 55): 56 """ 57 Args:\n 58 response_type (str, Optional): define the response type panda_df | json. Default panda_df\n 59 Returns:\n 60 Pandas DataFrame: df containing market status\n 61 Json : Json containing market status\n 62 """ 63 64 cookies = utils.get_cookies() 65 base_url = cns.BASE_URL 66 event_api = cns.MARKETSTATUS 67 68 url = base_url + event_api 69 data = utils.fetch_url( 70 url, 71 cookies, 72 key="marketState", 73 response_type=response_type, 74 ) 75 76 return data 77 78 79def get_price( 80 start_date, 81 end_date, 82 symbol=None, 83 input_type="stock", 84 series="EQ", 85): 86 """ 87 Create threads for different requests, parses data, combines them and returns dataframe\n 88 Args:\n 89 start_date (datetime.datetime): start date\n 90 end_date (datetime.datetime): end date\n 91 input_type (str): Either 'stock' or 'index'\n 92 symbol (str, optional): stock symbol. Defaults to None. TODO: implement for index`\n 93 Returns:\n 94 Pandas DataFrame: df containing data for symbol of provided date range\n 95 """ 96 cookies = utils.get_cookies() 97 base_url = cns.BASE_URL 98 price_api = cns.EQUITY_PRICE_HISTORY 99 url_list = [] 100 101 # set the window size to one year 102 window_size = datetime.timedelta(days=cns.WINDOW_SIZE) 103 104 start_date, end_date = utils.check_nd_convert(start_date, end_date) 105 106 current_window_start = start_date 107 while current_window_start < end_date: 108 current_window_end = current_window_start + window_size 109 110 # check if the current window extends beyond the end_date 111 current_window_end = min(current_window_end, end_date) 112 113 if input_type == "stock": 114 params = { 115 "symbol": symbol, 116 "from": current_window_start.strftime("%d-%m-%Y"), 117 "to": current_window_end.strftime("%d-%m-%Y"), 118 "dataType": "priceVolumeDeliverable", 119 "series": series, 120 } 121 url = base_url + price_api + urllib.parse.urlencode(params) 122 url_list.append(url) 123 124 # move the window start to the next day after the current window end 125 current_window_start = current_window_end + datetime.timedelta(days=1) 126 127 result = pd.DataFrame() 128 with concurrent.futures.ThreadPoolExecutor(max_workers=cns.MAX_WORKERS) as executor: 129 future_to_url = { 130 executor.submit(utils.fetch_url, url, cookies, "data"): url 131 for url in url_list 132 } 133 concurrent.futures.wait(future_to_url, return_when=ALL_COMPLETED) 134 for future in concurrent.futures.as_completed(future_to_url): 135 url = future_to_url[future] 136 try: 137 dataframe = future.result() 138 result = pd.concat([result, dataframe]) 139 except Exception as exc: 140 logging.error("%s got exception: %s. Please try again later.", url, exc) 141 raise exc 142 return data_format.price(result) 143 144 145def get_corpinfo( 146 start_date, 147 end_date, 148 symbol=None, 149 response_type="panda_df", 150): 151 """ 152 Create threads for different requests, parses data, combines them and returns dataframe\n 153 Args:\n 154 start_date (datetime.datetime): start date\n 155 end_date (datetime.datetime): end date\n 156 symbol (str, optional): stock symbol. Defaults to None.\n 157 Returns:\n 158 Pandas DataFrame: df containing data for symbol of provided date range\n 159 or\n 160 Json: json containing data for symbol of provided date range\n 161 """ 162 cookies = utils.get_cookies() 163 params = { 164 "symbol": symbol, 165 "from_date": start_date, 166 "to_date": end_date, 167 "index": "equities", 168 } 169 base_url = cns.BASE_URL 170 price_api = cns.EQUITY_CORPINFO 171 url = base_url + price_api + urllib.parse.urlencode(params) 172 173 data = utils.fetch_url( 174 url, 175 cookies, 176 key=None, 177 response_type=response_type, 178 ) 179 180 return data 181 182 183def get_event( 184 start_date=None, 185 end_date=None, 186 index="equities", 187): 188 """ 189 Args:\n 190 start_date (datetime.datetime,optional): start date\n 191 end_date (datetime.datetime,optional): end date\n 192 Returns:\n 193 Pandas DataFrame: df containing event of provided date range\n 194 """ 195 params = {} 196 cookies = utils.get_cookies() 197 base_url = cns.BASE_URL 198 event_api = cns.EQUITY_EVENT 199 200 params["index"] = index 201 if start_date is not None: 202 params["from_date"] = start_date 203 if end_date is not None: 204 params["to_date"] = end_date 205 206 url = base_url + event_api + urllib.parse.urlencode(params) 207 return utils.fetch_url(url, cookies) 208 209 210def get_chartdata( 211 symbol, 212 preopen=False, 213 response_type="panda_df", 214): 215 """ 216 Args:\n 217 symbol (str): stock symbol.\n 218 Returns:\n 219 Pandas DataFrame: df containing chart data of provided date\n 220 """ 221 params = {} 222 cookies = utils.get_cookies() 223 base_url = cns.BASE_URL 224 event_api = cns.EQUITY_CHART 225 try: 226 identifier = get_companyinfo( 227 symbol, 228 response_type="json", 229 )[ 230 "info" 231 ]["identifier"] 232 233 except KeyError: 234 return f"Invalid symbol name: {symbol}" 235 236 params["index"] = identifier 237 if preopen: 238 params["preopen"] = "true" 239 240 url = base_url + event_api + urllib.parse.urlencode(params) 241 242 data = utils.fetch_url( 243 url, 244 cookies, 245 key="grapthData", 246 response_type=response_type, 247 ) 248 if response_type == "panda_df": 249 data_frame = data.rename( 250 columns={ 251 0: "timestamp_milliseconds", 252 1: "price", 253 } 254 ) 255 data_frame["datetime"] = pd.to_datetime( 256 data_frame["timestamp_milliseconds"], unit="ms" 257 ) 258 return data_frame 259 return data 260 261 262def get_symbols_list(): 263 """ 264 Args:\n 265 No arguments needed\n 266 Returns:\n 267 List of stock or equity symbols\n 268 """ 269 cookies = utils.get_cookies() 270 base_url = cns.BASE_URL 271 event_api = cns.EQUITY_LIST 272 273 url = base_url + event_api 274 data = utils.fetch_url(url, cookies) 275 f_dict = data.to_dict() 276 eq_list = [] 277 for i in range(len(f_dict["data"])): 278 eq_list.append(f_dict["data"][i]["metadata"]["symbol"]) 279 280 return eq_list 281 282 283def get_asm_list(asm_type="both") -> list: 284 """ 285 Args:\n 286 asm_type (str): ASM type, possible values: both,longterm,shortterm\n 287 Returns:\n 288 List of stocks under ASM\n 289 """ 290 cookies = utils.get_cookies() 291 base_url = cns.BASE_URL 292 event_api = cns.ASM_LIST 293 294 url = base_url + event_api 295 data = utils.fetch_url(url, cookies) 296 _data = data.to_dict() 297 298 if asm_type == "both": 299 return _data 300 if asm_type == "longterm": 301 return _data.get("longterm").get("data") 302 if asm_type == "shortterm": 303 return _data.get("shortterm").get("data") 304 return ["possible values are both,longterm,shortterm"]
log =
<RootLogger root (INFO)>
def
get_companyinfo(symbol: str, response_type: str = 'panda_df'):
21def get_companyinfo( 22 symbol: str, 23 response_type: str = "panda_df", 24): 25 """get_companyinfo 26 27 Args:\n 28 symbol (str): stock name\n 29 response_type (str, Optional): define the response type panda_df | json. Default panda_df\n 30 Returns:\n 31 Pandas DataFrame: df containing company info\n 32 or\n 33 Json: json containing company info\n 34 """ 35 36 params = {} 37 cookies = utils.get_cookies() 38 base_url = cns.BASE_URL 39 event_api = cns.EQUITY_INFO 40 41 params["symbol"] = symbol 42 43 url = base_url + event_api + urllib.parse.urlencode(params) 44 data = utils.fetch_url( 45 url, 46 cookies, 47 key=None, 48 response_type=response_type, 49 ) 50 51 return data
get_companyinfo
Args:
symbol (str): stock name
response_type (str, Optional): define the response type panda_df | json. Default panda_df
Returns:
Pandas DataFrame: df containing company info
or
Json: json containing company info
def
get_marketstatus(response_type: str = 'panda_df'):
54def get_marketstatus( 55 response_type: str = "panda_df", 56): 57 """ 58 Args:\n 59 response_type (str, Optional): define the response type panda_df | json. Default panda_df\n 60 Returns:\n 61 Pandas DataFrame: df containing market status\n 62 Json : Json containing market status\n 63 """ 64 65 cookies = utils.get_cookies() 66 base_url = cns.BASE_URL 67 event_api = cns.MARKETSTATUS 68 69 url = base_url + event_api 70 data = utils.fetch_url( 71 url, 72 cookies, 73 key="marketState", 74 response_type=response_type, 75 ) 76 77 return data
Args:
response_type (str, Optional): define the response type panda_df | json. Default panda_df
Returns:
Pandas DataFrame: df containing market status
Json : Json containing market status
def
get_price(start_date, end_date, symbol=None, input_type='stock', series='EQ'):
80def get_price( 81 start_date, 82 end_date, 83 symbol=None, 84 input_type="stock", 85 series="EQ", 86): 87 """ 88 Create threads for different requests, parses data, combines them and returns dataframe\n 89 Args:\n 90 start_date (datetime.datetime): start date\n 91 end_date (datetime.datetime): end date\n 92 input_type (str): Either 'stock' or 'index'\n 93 symbol (str, optional): stock symbol. Defaults to None. TODO: implement for index`\n 94 Returns:\n 95 Pandas DataFrame: df containing data for symbol of provided date range\n 96 """ 97 cookies = utils.get_cookies() 98 base_url = cns.BASE_URL 99 price_api = cns.EQUITY_PRICE_HISTORY 100 url_list = [] 101 102 # set the window size to one year 103 window_size = datetime.timedelta(days=cns.WINDOW_SIZE) 104 105 start_date, end_date = utils.check_nd_convert(start_date, end_date) 106 107 current_window_start = start_date 108 while current_window_start < end_date: 109 current_window_end = current_window_start + window_size 110 111 # check if the current window extends beyond the end_date 112 current_window_end = min(current_window_end, end_date) 113 114 if input_type == "stock": 115 params = { 116 "symbol": symbol, 117 "from": current_window_start.strftime("%d-%m-%Y"), 118 "to": current_window_end.strftime("%d-%m-%Y"), 119 "dataType": "priceVolumeDeliverable", 120 "series": series, 121 } 122 url = base_url + price_api + urllib.parse.urlencode(params) 123 url_list.append(url) 124 125 # move the window start to the next day after the current window end 126 current_window_start = current_window_end + datetime.timedelta(days=1) 127 128 result = pd.DataFrame() 129 with concurrent.futures.ThreadPoolExecutor(max_workers=cns.MAX_WORKERS) as executor: 130 future_to_url = { 131 executor.submit(utils.fetch_url, url, cookies, "data"): url 132 for url in url_list 133 } 134 concurrent.futures.wait(future_to_url, return_when=ALL_COMPLETED) 135 for future in concurrent.futures.as_completed(future_to_url): 136 url = future_to_url[future] 137 try: 138 dataframe = future.result() 139 result = pd.concat([result, dataframe]) 140 except Exception as exc: 141 logging.error("%s got exception: %s. Please try again later.", url, exc) 142 raise exc 143 return data_format.price(result)
Create threads for different requests, parses data, combines them and returns dataframe
Args:
start_date (datetime.datetime): start date
end_date (datetime.datetime): end date
input_type (str): Either 'stock' or 'index'
symbol (str, optional): stock symbol. Defaults to None. TODO: implement for index`
Returns:
Pandas DataFrame: df containing data for symbol of provided date range
def
get_corpinfo(start_date, end_date, symbol=None, response_type='panda_df'):
146def get_corpinfo( 147 start_date, 148 end_date, 149 symbol=None, 150 response_type="panda_df", 151): 152 """ 153 Create threads for different requests, parses data, combines them and returns dataframe\n 154 Args:\n 155 start_date (datetime.datetime): start date\n 156 end_date (datetime.datetime): end date\n 157 symbol (str, optional): stock symbol. Defaults to None.\n 158 Returns:\n 159 Pandas DataFrame: df containing data for symbol of provided date range\n 160 or\n 161 Json: json containing data for symbol of provided date range\n 162 """ 163 cookies = utils.get_cookies() 164 params = { 165 "symbol": symbol, 166 "from_date": start_date, 167 "to_date": end_date, 168 "index": "equities", 169 } 170 base_url = cns.BASE_URL 171 price_api = cns.EQUITY_CORPINFO 172 url = base_url + price_api + urllib.parse.urlencode(params) 173 174 data = utils.fetch_url( 175 url, 176 cookies, 177 key=None, 178 response_type=response_type, 179 ) 180 181 return data
Create threads for different requests, parses data, combines them and returns dataframe
Args:
start_date (datetime.datetime): start date
end_date (datetime.datetime): end date
symbol (str, optional): stock symbol. Defaults to None.
Returns:
Pandas DataFrame: df containing data for symbol of provided date range
or
Json: json containing data for symbol of provided date range
def
get_event(start_date=None, end_date=None, index='equities'):
184def get_event( 185 start_date=None, 186 end_date=None, 187 index="equities", 188): 189 """ 190 Args:\n 191 start_date (datetime.datetime,optional): start date\n 192 end_date (datetime.datetime,optional): end date\n 193 Returns:\n 194 Pandas DataFrame: df containing event of provided date range\n 195 """ 196 params = {} 197 cookies = utils.get_cookies() 198 base_url = cns.BASE_URL 199 event_api = cns.EQUITY_EVENT 200 201 params["index"] = index 202 if start_date is not None: 203 params["from_date"] = start_date 204 if end_date is not None: 205 params["to_date"] = end_date 206 207 url = base_url + event_api + urllib.parse.urlencode(params) 208 return utils.fetch_url(url, cookies)
Args:
start_date (datetime.datetime,optional): start date
end_date (datetime.datetime,optional): end date
Returns:
Pandas DataFrame: df containing event of provided date range
def
get_chartdata(symbol, preopen=False, response_type='panda_df'):
211def get_chartdata( 212 symbol, 213 preopen=False, 214 response_type="panda_df", 215): 216 """ 217 Args:\n 218 symbol (str): stock symbol.\n 219 Returns:\n 220 Pandas DataFrame: df containing chart data of provided date\n 221 """ 222 params = {} 223 cookies = utils.get_cookies() 224 base_url = cns.BASE_URL 225 event_api = cns.EQUITY_CHART 226 try: 227 identifier = get_companyinfo( 228 symbol, 229 response_type="json", 230 )[ 231 "info" 232 ]["identifier"] 233 234 except KeyError: 235 return f"Invalid symbol name: {symbol}" 236 237 params["index"] = identifier 238 if preopen: 239 params["preopen"] = "true" 240 241 url = base_url + event_api + urllib.parse.urlencode(params) 242 243 data = utils.fetch_url( 244 url, 245 cookies, 246 key="grapthData", 247 response_type=response_type, 248 ) 249 if response_type == "panda_df": 250 data_frame = data.rename( 251 columns={ 252 0: "timestamp_milliseconds", 253 1: "price", 254 } 255 ) 256 data_frame["datetime"] = pd.to_datetime( 257 data_frame["timestamp_milliseconds"], unit="ms" 258 ) 259 return data_frame 260 return data
Args:
symbol (str): stock symbol.
Returns:
Pandas DataFrame: df containing chart data of provided date
def
get_symbols_list():
263def get_symbols_list(): 264 """ 265 Args:\n 266 No arguments needed\n 267 Returns:\n 268 List of stock or equity symbols\n 269 """ 270 cookies = utils.get_cookies() 271 base_url = cns.BASE_URL 272 event_api = cns.EQUITY_LIST 273 274 url = base_url + event_api 275 data = utils.fetch_url(url, cookies) 276 f_dict = data.to_dict() 277 eq_list = [] 278 for i in range(len(f_dict["data"])): 279 eq_list.append(f_dict["data"][i]["metadata"]["symbol"]) 280 281 return eq_list
Args:
No arguments needed
Returns:
List of stock or equity symbols
def
get_asm_list(asm_type='both') -> list:
284def get_asm_list(asm_type="both") -> list: 285 """ 286 Args:\n 287 asm_type (str): ASM type, possible values: both,longterm,shortterm\n 288 Returns:\n 289 List of stocks under ASM\n 290 """ 291 cookies = utils.get_cookies() 292 base_url = cns.BASE_URL 293 event_api = cns.ASM_LIST 294 295 url = base_url + event_api 296 data = utils.fetch_url(url, cookies) 297 _data = data.to_dict() 298 299 if asm_type == "both": 300 return _data 301 if asm_type == "longterm": 302 return _data.get("longterm").get("data") 303 if asm_type == "shortterm": 304 return _data.get("shortterm").get("data") 305 return ["possible values are both,longterm,shortterm"]
Args:
asm_type (str): ASM type, possible values: both,longterm,shortterm
Returns:
List of stocks under ASM