Saturday 4 August 2018

Identify Stocks which is near to 52 week high or 52 week low price


Hi, A python script to identify BSE/NSE stocks that is close to its 52 week near price or 52 week high price. Results are captured from moneycontrol website. Only change the monyecontrol url to use this script. import requests from bs4 import BeautifulSoup #import os #import csv #url_to_scrape = "https://www.moneycontrol.com/stocks/marketinfo/marketcap/bse/ceramics-granite.html" #url_to_scrape = "https://www.moneycontrol.com/stocks/marketinfo/marketcap/bse/auto-ancillaries.html" url_to_scrape = "https://www.moneycontrol.com/stocks/marketinfo/marketcap/bse/couriers.html" r = requests.get(url_to_scrape) soup = BeautifulSoup(r.text, "html.parser") l1=[] l2=[] l3=[] for name in soup.find_all("td", class_="brdrgtgry"): value = name.get_text() s = value.replace('\n', '') if "Add" in s: #print (s[:-32]) l1.append((s[:-32])) else: #print(s) l2.append(s) for i in range(0, len(l2), 5): pricelist = l2[i:i + 5] l3.append((pricelist)) #print(l3) d1 = dict(zip(l1, l3)) d2 = {} #print(d) #print(d.items()) for key,value in d1.items(): #print(value[0]) #print(value[3]) v1 = float(value[0].replace(',','')) v2 = float(value[3].replace(',','')) #d2[key] = "%.2f" % ((v1 - v2)/ v1 *100) d2[key] = [(100 - ((v1 - v2) / v1 * 100)), (100 - ((v3 - v1)/v3 *100))] #for key, value in sorted(d2.items(), key=lambda item: (item[1], item[0])): #print ("%s: %s" % (key, value)) #print ("%s:" %key, "%.2f" %value + "%") #print(d2) for key, value in sorted(d2.items(), key=lambda item: (item[1][1], item[0])): print ("%s:" %key, "%.2f" %value[0] + "% close to 52wk low", "%.2f" %value[1] + "% close to 52wk high" )