Open nlsh123 opened 5 years ago
script:
import requests, bs4
import glob, datetime, os, sys
def mkdir(dir):
if not os.path.exists(dir):
os.makedirs(dir)
def save_data(symbol, dirx):
tickr = symbol[0]
stk_type = symbol[1]
expiry = symbol[2]
res = requests.get("https://www.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbol=%s&instrument=%s&date=%s" % (tickr, stk_type, expiry))
soup = bs4.BeautifulSoup(res.text, "html")
curr_px = soup.find("span").text.strip().split(" ")[-1]
table = soup.find("table", {"id":"octable"})
trs = table.find_all("tr")
lines = []
for tr in trs:
tds = tr.find_all("th") + tr.find_all("td")
if len(tds) == 23:
if lines:
fields = [x.text.strip().replace(",", "").replace("-", "") for x in tds[1:-1]] + [curr_px]
else:
fields = [x.text.strip().replace(",", "").replace("-", "") for x in tds[1:-1]] + ['CurrPx']
lines.append(",".join(fields))
with open(os.path.join(dirx, "%s_%s.csv" % (tickr, expiry)), "w") as f:
f.writelines([l+'\n' for l in lines])
if len(sys.argv) < 3:
print "usage python2 get_data.py [symbol_file] [workdir]"
sys.exit(1)
symbol_file = sys.argv[1]
workdir = sys.argv[2]
mkdir(workdir)
with open(symbol_file) as f:
symbols = f.read().splitlines()
symbols = [x.split() for x in symbols]
dir_name = datetime.datetime.now().strftime("%Y%m%d_%H%M")
dirx = os.path.join(workdir, dir_name)
mkdir(dirx)
out_string = ""
for symbol in symbols:
out_string += str(symbol)
print symbol,
try:
save_data(symbol, dirx)
out_string += '\n'
print
except:
out_string += ' failed\n'
print "failed"
with open(os.path.join(dirx, "log"), "w") as f:
f.write(out_string)
symbol file:
BANKNIFTY OPTIDX 7FEB2019
BANKNIFTY OPTIDX 14FEB2019
BANKNIFTY OPTIDX 21FEB2019
BANKNIFTY OPTIDX 28FEB2019
NIFTY OPTIDX 28FEB2019
ACC OPTSTK 28FEB2019
ADANIPORTS OPTSTK 28FEB2019
ADANIPOWER OPTSTK 28FEB2019
AJANTPHARM OPTSTK 28FEB2019
... and so on
usage
python2 get_data.py [symbol_file] [workdir]
how do I run this code with symbol files and work dire .. what to pass in these to fields ..like some file path with SYMBOLS and out directory path ..i tried with this C:\Program Files>python C:\Users\90069748\Desktop\Data_algo_Python\NseOption.py [C:\Users\90069748\Desktop\Data_algo_Python\symbol_file] [C:\Users\90069748\Desktop\Data_algo_Python\testdata.text]
The []
are meant to be indicative of arguments. Not to be actually added. Remove the []
python C:\Users\90069748\Desktop\Data_algo_Python\NseOption.py C:\Users\90069748\Desktop\Data_algo_Python\symbol_file.txt C:\Users\90069748\Desktop\Data_algo_Python\testdata.txt File "C:\Users\90069748\Desktop\Data_algo_Python\NseOption.py", line 34 print "usage python2 get_data.py [symbol_file] [workdir]" ^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print("usage python2 get_data.py [symbol_file] [workdir]")?
I have few symbol in symbol_file.txt format BANKNIFTY OPTIDX 25APR2019 BANKNIFTY OPTIDX 25APR2019 BANKNIFTY OPTIDX 25APR2019 BANKNIFTY OPTIDX 25APR2019 and out file is blank
my mistake .. workdir now , i have passed a dir/folder path ..not sure why it asked for parentheses in all print statements...it's working now . will validate the data and confirm . thanks .
can you make some changes to it so that wherever there is significant amount of OI(open interest) is present those strike price details are stored in output file . example .. OI greater than 2000 .
Try to understand what the code is doing and make them yourself. Also the parantheses for the prints are used in python 3, I've specifically mentioned python2...
thanks a lot bhai :)
it was great help . Mr Swapnil has developed one such good module with API's . Yes you have mentioned python2 ..i didn't know about this difference in py2 & py3 . i'll have to customize few things in your code output as per my requirement .
Hello, the IVs come out blank on NIFTY and BANKNIFTY. Not sure why.
Found the issue. The code was getting the latest week option chain, and since today is expiry, the IVs were all '-'.
The resource URl is missing '=' after the date param, that is why it was getting the current week's option chain and was ignoring the expiry date from the input file.
res = requests.get("https://www.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbol=%s&instrument=%s&date%s" % (tickr, stk_type, expiry))
should be
res = requests.get("https://www.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbol=%s&instrument=%s&date=%s" % (tickr, stk_type, expiry))
thanks, @xsalien fixed.
script:
import requests, bs4 import glob, datetime, os, sys def mkdir(dir): if not os.path.exists(dir): os.makedirs(dir) def save_data(symbol, dirx): tickr = symbol[0] stk_type = symbol[1] expiry = symbol[2] res = requests.get("https://www.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbol=%s&instrument=%s&date=%s" % (tickr, stk_type, expiry)) soup = bs4.BeautifulSoup(res.text, "html") curr_px = soup.find("span").text.strip().split(" ")[-1] table = soup.find("table", {"id":"octable"}) trs = table.find_all("tr") lines = [] for tr in trs: tds = tr.find_all("th") + tr.find_all("td") if len(tds) == 23: if lines: fields = [x.text.strip().replace(",", "").replace("-", "") for x in tds[1:-1]] + [curr_px] else: fields = [x.text.strip().replace(",", "").replace("-", "") for x in tds[1:-1]] + ['CurrPx'] lines.append(",".join(fields)) with open(os.path.join(dirx, "%s_%s.csv" % (tickr, expiry)), "w") as f: f.writelines([l+'\n' for l in lines]) if len(sys.argv) < 3: print "usage python2 get_data.py [symbol_file] [workdir]" sys.exit(1) symbol_file = sys.argv[1] workdir = sys.argv[2] mkdir(workdir) with open(symbol_file) as f: symbols = f.read().splitlines() symbols = [x.split() for x in symbols] dir_name = datetime.datetime.now().strftime("%Y%m%d_%H%M") dirx = os.path.join(workdir, dir_name) mkdir(dirx) out_string = "" for symbol in symbols: out_string += str(symbol) print symbol, try: save_data(symbol, dirx) out_string += '\n' print except: out_string += ' failed\n' print "failed" with open(os.path.join(dirx, "log"), "w") as f: f.write(out_string)
symbol file:
BANKNIFTY OPTIDX 7FEB2019 BANKNIFTY OPTIDX 14FEB2019 BANKNIFTY OPTIDX 21FEB2019 BANKNIFTY OPTIDX 28FEB2019 NIFTY OPTIDX 28FEB2019 ACC OPTSTK 28FEB2019 ADANIPORTS OPTSTK 28FEB2019 ADANIPOWER OPTSTK 28FEB2019 AJANTPHARM OPTSTK 28FEB2019
... and so on
usage
python2 get_data.py [symbol_file] [workdir]
@srajangarg srajangarg the above code does not work anymore as the URL has got changed now . Can you please help getting the correct code for the NSEOption chain .
Hi SrajanGarg, jksevda Nse has made added header and cookie changes to their options chain site. So any code trying to load scrape this url have to add header n then only it will load. There is a YouTube video for excel to refer to.https://youtu.be/v5qL75phcw4
Can NSEpy provide Intraday live option chain of nifty50 index? This refreshes every 2 minutes from nse india like from link: https://www1.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?segmentLink=17&instrument=OPTIDX&symbol=NIFTY&date=14MAR2019