Get List of All FNO Stocks:
VIDEO
For Instructions Follow the Video then go For the Code Input:
All FNO Stocks from NSE Website:
"AARTIIND",
"ABB",
"ABBOTINDIA",
"ABCAPITAL",
"ABFRL",
"ACC",
"ADANIENT",
"ADANIPORTS",
"ALKEM",
"AMBUJACEM",
"APOLLOHOSP",
"APOLLOTYRE",
"ASHOKLEY",
"ASIANPAINT",
"ASTRAL",
"ATUL",
"AUBANK",
"AUROPHARMA",
"AXISBANK",
"BAJAJ-AUTO",
"BAJAJFINSV",
"BAJFINANCE",
"BALKRISIND",
"BALRAMCHIN",
"BANDHANBNK",
"BANKBARODA",
"BATAINDIA",
"BEL",
"BERGEPAINT",
"BHARATFORG",
"BHARTIARTL",
"BHEL",
"BIOCON",
"BOSCHLTD",
"BPCL",
"BRITANNIA",
"BSOFT",
"CANBK",
"CANFINHOME",
"CHAMBLFERT",
"CHOLAFIN",
"CIPLA",
"COALINDIA",
"COFORGE",
"COLPAL",
"CONCOR",
"COROMANDEL",
"CROMPTON",
"CUB",
"CUMMINSIND",
"DABUR",
"DALBHARAT",
"DEEPAKNTR",
"DIVISLAB",
"DIXON",
"DLF",
"DRREDDY",
"EICHERMOT",
"ESCORTS",
"EXIDEIND",
"FEDERALBNK",
"GAIL",
"GLENMARK",
"GMRINFRA",
"GNFC",
"GODREJCP",
"GODREJPROP",
"GRANULES",
"GRASIM",
"GUJGASLTD",
"HAL",
"HAVELLS",
"HCLTECH",
"HDFCAMC",
"HDFCBANK",
"HDFCLIFE",
"HEROMOTOCO",
"HINDALCO",
"HINDCOPPER",
"HINDPETRO",
"HINDUNILVR",
"ICICIBANK",
"ICICIGI",
"ICICIPRULI",
"IDEA",
"IDFC",
"IDFCFIRSTB",
"IEX",
"IGL",
"INDHOTEL",
"INDIACEM",
"INDIAMART",
"INDIGO",
"INDUSINDBK",
"INDUSTOWER",
"INFY",
"IOC",
"IPCALAB",
"IRCTC",
"ITC",
"JINDALSTEL",
"JKCEMENT",
"JSWSTEEL",
"JUBLFOOD",
"KOTAKBANK",
"LALPATHLAB",
"LAURUSLABS",
"LICHSGFIN",
"LT",
"LTF",
"LTIM",
"LTTS",
"LUPIN",
"M&M",
"M&MFIN",
"MANAPPURAM",
"MARICO",
"MARUTI",
"MCX",
"METROPOLIS",
"MFSL",
"MGL",
"MOTHERSON",
"MPHASIS",
"MRF",
"MUTHOOTFIN",
"NATIONALUM",
"NAUKRI",
"NAVINFLUOR",
"NESTLEIND",
"NMDC",
"NTPC",
"OBEROIRLTY",
"OFSS",
"ONGC",
"PAGEIND",
"PEL",
"PERSISTENT",
"PETRONET",
"PFC",
"PIDILITIND",
"PIIND",
"PNB",
"POLYCAB",
"POWERGRID",
"PVRINOX",
"RAMCOCEM",
"RBLBANK",
"RECLTD",
"RELIANCE",
"SAIL",
"SBICARD",
"SBILIFE",
"SBIN",
"SHREECEM",
"SHRIRAMFIN",
"SIEMENS",
"SRF",
"SUNPHARMA",
"SUNTV",
"SYNGENE",
"TATACHEM",
"TATACOMM",
"TATACONSUM",
"TATAMOTORS",
"TATAPOWER",
"TATASTEEL",
"TCS",
"TECHM",
"TITAN",
"TORNTPHARM",
"TRENT",
"TVSMOTOR",
"UBL",
"ULTRACEMCO",
"UNITDSPR",
"UPL",
"VEDL",
"VOLTAS",
"WIPRO",
"ZYDUSLIFE"
import pandas as pd
instruments=pd.read_csv('https://assets.upstox.com/market-quote/instruments/exchange/complete.csv.gz')
instruments=instruments[instruments['instrument_type']=='FUTSTK']
tradingsymbol=instruments['tradingsymbol'].sort_values().str.extract(r'([A-Z-&]+)').drop_duplicates()
print(len(tradingsymbol[1:]),tradingsymbol[1:].to_string(index=False))
Get Option TradingSymbol Relevant to Last Price without Iteration:
VIDEO
For Instructions Follow the Video then go For the Code Input:
import pandas as pd
symbol='BANKNIFTY247105'
option_type='CE'
instruments=pd.read_csv('https://assets.upstox.com/market-quote/instruments/exchange/complete.csv.gz')
tradingsymbol=instruments[(instruments['tradingsymbol'].str.contains(symbol))
&(instruments['tradingsymbol'].str.contains(option_type))
&(instruments['last_price'].round()==9)]
tradingsymbol=tradingsymbol[['tradingsymbol','last_price']]
print(tradingsymbol['tradingsymbol'].iloc[0])
Fetch ATM|OTM|ITM Data:
VIDEO
For Instructions Follow the Video then go For the Code Input:
import pandas as pd
import requests
import sys
access_token=open('C:/Users/VIKASH/Desktop/upstox/upa/access_token.txt').read().strip()
headers={
'Authorization':f'Bearer: {access_token}'
}
index='NSE_INDEX|Nifty Bank'
#instruments=pd.read_csv('https://assets.upstox.com/market-quote/instruments/exchange/complete.csv.gz')
instruments=pd.read_csv('instruments.csv')
#last_price=instruments[instruments['name']==index]
#print(last_price['last_price'])
#sys.exit()
spotsymbol='BANKNIFTY'
option_type='CE'
strike_level=100
market_quote=requests.get(f'https://api.upstox.com/v2/market-quote/ltp?symbol={index}',headers=headers)
strike_price=100*round(market_quote.json()['data']['NSE_INDEX:Nifty Bank']['last_price']/100)
tradingsymbol=spotsymbol+'24JUN'+str(strike_price+strike_level)+option_type
instrument_key=instruments[instruments['tradingsymbol']==tradingsymbol]
instrument_key=instrument_key['instrument_key'].iloc[0]
OTM=requests.get(f'https://api.upstox.com/v2/market-quote/ltp?symbol={instrument_key}',headers=headers)
print(OTM.json()['data'][f'NSE_FO:{tradingsymbol}']['instrument_token'])
Retrieve Market Quote such as Full Market Quote|OHLC|LTP in Python:
VIDEO
For Instructions Follow the Video then go For the Code Input:
import pandas as pd
import requests
access_token=open('C:/Users/VIKASH/Desktop/upstox/upa/access_token.txt').read().strip()
headers={
'Authorization':f'Bearer: {access_token}'
}
instruments=pd.read_csv('https://assets.upstox.com/market-quote/instruments/exchange/complete.csv.gz')
spotsymbol='BANKNIFTY'
strike='50000'
option_type='CE'
def get_instrument_key(spotsymbol,strike,option_type):
instruments['tradingsymbol']=instruments['tradingsymbol'].fillna('')
instrument_key=instruments[(instruments['tradingsymbol'].str.contains(spotsymbol))
&(instruments['tradingsymbol'].str.contains(strike))
&(instruments['tradingsymbol'].str.contains(option_type))].sort_values('expiry')
return instrument_key['instrument_key'].iloc[0]
key=get_instrument_key(spotsymbol,strike,option_type)
market_quote=requests.get(f'https://api.upstox.com/v2/market-quote/ltp?symbol={key}&interval=1d',headers=headers)
print(market_quote.json()['data']['NSE_FO:BANKNIFTY2461950000CE']['last_price'])
Fetch Premium Based TradingSymbol:
VIDEO
For Instructions Follow the Video then go For the Code Input:
#BANKNIFTY+YYMMDD+STRIKEPRICE+CE
#Index(['instrument_key', 'exchange_token', 'tradingsymbol', 'name',
#'last_price', 'expiry', 'strike', 'tick_size', 'lot_size',
#'instrument_type', 'option_type', 'exchange'],
#dtype='object')
spotsymbol='BANKNIFTY'
expiry='19'
CE=False
PE=True
premium={}
price=10.0
import pandas as pd
#instruments=pd.read_csv('https://assets.upstox.com/market-quote/instruments/exchange/complete.csv.gz')
instruments=pd.read_csv('instruments.csv')
instruments['tradingsymbol']=instruments['tradingsymbol'].fillna('')
tradingsymbol=instruments[(instruments['tradingsymbol'].str.contains(spotsymbol))
&(instruments['expiry'].str.contains(expiry))]
if PE:
CET=tradingsymbol.sort_values('tradingsymbol',ascending=False)
CES=CET['tradingsymbol'].to_list()
CEP=CET['last_price'].round().to_list()
for x,y in zip(CEP,CES):
premium.update({x:y})
print(premium[8.0][-2:])
Fetch Instrument Key of Trading Symbol Using Upstox API in Python:
VIDEO
For Instructions Follow the Video then go For Code Input:
Sample Output HTML Table Given Below this need to Processed Further for Sorting and Fetching the Desired Instrument Key:
instrument_key
exchange_token
tradingsymbol
name
last_price
expiry
strike
tick_size
lot_size
instrument_type
option_type
exchange
4055
NSE_EQ|INF174K01F59
5851.0
BANKNIFTY1
KOTAKMAMC-KOTAKBKETF
511.69
NaN
NaN
0.01
1.0
EQUITY
NaN
NSE_EQ
To Process the Above Instruments File and get the Instrument Key We Can utilize a Single Column that is Tradingsymbol BANKNIFTY+YYMMDD+50000+CE which Consists of Spot Symbol Name with Expiry,Strike Price and Option Type to get Current Expiry We will Sort it out with Expiry Column:
#BANKNIFTY+YYMMDD+50000+CE
#Index(['instrument_key', 'exchange_token', 'tradingsymbol', 'name',
#'last_price', 'expiry', 'strike', 'tick_size', 'lot_size',
#'instrument_type', 'option_type', 'exchange'],
#dtype='object')
import pandas as pd
#instruments=pd.read_csv('https://assets.upstox.com/market-quote/instruments/exchange/complete.csv.gz')
spotsymbol='BANKNIFTY'
strike='50000'
option_type='CE'
def get_instrument_key(spotsymbol,strike,option_type):
instruments=pd.read_csv('instruments.csv')
instruments['tradingsymbol']=instruments['tradingsymbol'].fillna('')
instrument_key=instruments[(instruments['tradingsymbol'].str.contains(spotsymbol))
&(instruments['tradingsymbol'].str.contains(strike))
&(instruments['tradingsymbol'].str.contains(option_type))].sort_values('expiry')
return instrument_key['instrument_key'].iloc[0]
key=get_instrument_key(spotsymbol,strike,option_type)
print(key)
Websocket Market Data Streamer Upstox Free API in Python:
VIDEO
For Instructions Follow the Video then go For Code Input:
#Generate the Instruments Symbol
import pandas as pd
instruments=pd.read_csv('https://assets.upstox.com/market-quote/instruments/exchange/complete.csv.gz')
instruments.to_csv('instruments.csv',index=False)
#WebSocket Market Data Streamer
import upstox_client
instrument_keys=['NSE_FO|37054','NSE_INDEX|Nifty 50','NSE_INDEX|Bank Nifty']
access_token=open('access_token.txt').read().strip()
def on_message(message):
print(message)
def main():
config=upstox_client.Configuration()
config.access_token=access_token
streamer=upstox_client.MarketDataStreamer(
upstox_client.ApiClient(config),instrument_keys,'ltpc')
streamer.on('message',on_message)
streamer.connect()
if __name__=='__main__':
main()
Automate Trades in Upstox Utilizing Free of Cost API in Python:
VIDEO
For Instructions Follow the Video then go For the Code Input:
import requests
api_key=open('api_key.txt','r').read().strip()
api_secret=open('api_secret.txt','r').read().strip()
uri='https://account.upstox.com/developer/apps'
code='ZnNMAG'
url='https://api-v2.upstox.com/login/authorization/token'
headers={
'accept': 'application/json',
'Api-Version': '2.0',
'Content-Type': 'application/x-www-form-urlencoded'
}
data={
'code':code,
'client_id':api_key,
'client_secret':api_secret,
'redirect_uri':uri,
'grant_type':'authorization_code'
}
response=requests.post(url,headers=headers,data=data)
access_token=response.json()['access_token']
print(access_token,file=open('access_token.txt','w'))
print(access_token)
access_token=open('access_token.txt','r').read().strip()
quote='https://api-v2.upstox.com/market-quote/quotes?symbol=NSE_EQ|INE848E01016'
headers={
'accept': 'application/json',
'Api-Version': '2.0',
'Authorization': f'Bearer {access_token}'
}
response=requests.get(quote,headers=headers)
print(response.json())