Created
July 29, 2024 15:57
-
-
Save githubdebugger/c6224eca6ae6fdf0cb55293e9555a01f to your computer and use it in GitHub Desktop.
Cross compatible Security ID for retrieving Security ID of dhan for a given strike to place Orders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #This is a Runtime script and not efficient, Effecient way is to preprocess before market hours and save everything for quick retrieval | |
| import requests | |
| import polars as pl | |
| from datetime import datetime | |
| import io | |
| #Give Shoonya Symbol name and get dhan Security ID for Placing orders | |
| def get_security_id_dhan(search_value): | |
| url = 'https://images.dhan.co/api-data/api-scrip-master.csv' | |
| response = requests.get(url) | |
| # Check Request status | |
| if response.status_code != 200: | |
| return f"Failed to download the file. Status code: {response.status_code}" | |
| data = pl.read_csv(io.BytesIO(response.content)) | |
| data = data.select([ | |
| 'SEM_EXM_EXCH_ID', 'SEM_INSTRUMENT_NAME', 'SEM_EXPIRY_DATE', 'SEM_TRADING_SYMBOL', | |
| 'SEM_OPTION_TYPE', 'SEM_STRIKE_PRICE', 'SEM_SMST_SECURITY_ID' | |
| ]) | |
| filtered_data = data.filter((pl.col('SEM_EXM_EXCH_ID') == 'NSE') & (pl.col('SEM_INSTRUMENT_NAME') == 'OPTIDX')) | |
| sorted_data = filtered_data.sort('SEM_EXPIRY_DATE') | |
| sorted_data = sorted_data.with_columns([ | |
| pl.col('SEM_EXPIRY_DATE').str.to_datetime().alias('SEM_EXPIRY_DATE_dt') | |
| ]) | |
| sorted_data = sorted_data.with_columns([ | |
| pl.col('SEM_EXPIRY_DATE_dt').dt.strftime('%d%b%y').str.to_uppercase().alias('SEM_EXPIRY_DATE_changed') | |
| ]) | |
| sorted_data = sorted_data.with_columns([ | |
| pl.col('SEM_TRADING_SYMBOL').str.split('-').list.get(0).alias('Symbol_Prefix') | |
| ]) | |
| sorted_data = sorted_data.with_columns([ | |
| (pl.col('Symbol_Prefix') + pl.col('SEM_EXPIRY_DATE_changed')).alias('SEM_EXPIRY_DATE_changed') | |
| ]) | |
| sorted_data = sorted_data.with_columns([ | |
| pl.when(pl.col('SEM_OPTION_TYPE') == 'CE') | |
| .then(pl.col('SEM_EXPIRY_DATE_changed') + 'C') | |
| .otherwise(pl.col('SEM_EXPIRY_DATE_changed') + 'P') | |
| .alias('SEM_EXPIRY_DATE_changed') | |
| ]) | |
| sorted_data = sorted_data.with_columns([ | |
| (pl.col('SEM_EXPIRY_DATE_changed') + pl.col('SEM_STRIKE_PRICE').cast(pl.Int32).cast(pl.Utf8)).alias('SEM_EXPIRY_DATE_changed') | |
| ]) | |
| sorted_data = sorted_data.drop(['Symbol_Prefix', 'SEM_EXPIRY_DATE_dt']) | |
| result = sorted_data.filter(pl.col('SEM_EXPIRY_DATE_changed') == search_value) | |
| if not result.is_empty(): | |
| return result['SEM_SMST_SECURITY_ID'][0] | |
| else: | |
| return f"No matching SEM_EXPIRY_DATE_changed found for '{search_value}'" | |
| #usage | |
| search_value = 'BANKNIFTY31JUL24P51200' | |
| security_id = get_security_id_dhan(search_value) | |
| print(security_id) | |
| # print(type(security_id)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment