/
52weekshigh.py
73 lines (63 loc) · 2.56 KB
/
52weekshigh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from commonSettings import *
from commonFunctions import getParsedHTML, getText, dumpData, csvWriter, load
from symbols import insertSymbolinDatabase
StockSymbols = load('data/symbolsMCupdated.p')
Indexes = load('data/index.p')
def getCurrentAnd52WeekHighLow(url):
try:
parsedHTML = getParsedHTML(url)
curr = parsedHTML.findAll(attrs={"class":"FL PR5 gD_30"})
curr.extend(parsedHTML.findAll(attrs={"class":"FL PR5 rD_30"}))
curr.extend(parsedHTML.findAll(attrs={"class":"FL PR5 bD_30"}))
curr = float(getText(curr[-1]))
high = parsedHTML.find(attrs={"id":"n_52high"})
low = parsedHTML.find(attrs={"id":"n_52low"})
if high == None:
high = parsedHTML.find(attrs={"id":"b_52high"})
low = parsedHTML.find(attrs={"id":"b_52low"})
if high == None:
return None
high = getText(high)
low = getText(low)
return(curr, float(high), float(low))
except:
#print "error in getting low high for ", url
pass
#find all the stocks whose current prices are in 52 weeks high zone (not more than 10% from 52 week high)
def _52weeksHighZone():
l = len(StockSymbols)
writer = csvWriter('data/52weeksHigh.csv',['symbol','low','high','curr'])
for i in range(l):
if len(StockSymbols[i])> 3 and StockSymbols[i][-1].startswith("http://www.moneycontrol.com/india/stockpricequote/"):
url = StockSymbols[i][-1]
currHighLow = getCurrentAnd52WeekHighLow(url)
if currHighLow is None:
continue;
curr, high,low = currHighLow
diff = (high - curr)/(high)*100
if diff < 20:
print low, high, curr, StockSymbols[i][:3]
writer.writerow({'symbol':str(StockSymbols[i][:3]), 'low':low,'high':high,'curr':curr})
def is52WeekHigh(symbol):
try:
index = Indexes[symbol]
url = StockSymbols[index][-1]
currHighLow = getCurrentAnd52WeekHighLow(url)
if currHighLow is None:
print "could not get low high, might not be trading currently"
curr, high,low = currHighLow
diff = (high - curr)/(high)*100
if diff < 20:
print (True, (low,high, curr))
return
print (False, (low, high, curr))
except:
print "Symbol not found, trying to find and insert in database"
if insertSymbolinDatabase(symbol):
print is52WeekHigh(symbol)
return
print False
def main():
is52WeekHigh('8KMILES')
if __name__ == "__main__":
main()