-
Notifications
You must be signed in to change notification settings - Fork 0
/
alerts.py
65 lines (49 loc) · 1.3 KB
/
alerts.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
#!/usr/bin/env python
"""
collection of functions which trigger market alerts
"""
import matplotlib.dates as dates
import mdcache
import sto
import ystockquote
import datetime
from decimal import *
D = Decimal
def sto_limits (symbols=None, debug=False):
"""
alerts based on a high (80+) or low (20-) stochastic value (10-day period)
shows if price at last close was either strong or weak relative to all price movement over the period
"""
if symbols is None:
symbols = mdcache.entries()
res = {
'strong': [],
'weak': [],
'neutral': []
}
todaynum = dates.date2num(datetime.date.today())
for symbol in symbols:
data = mdcache.mdcache(symbol).get_data()[-10:]
if data[-1][0] < todaynum:
if debug:
print("%s: adding current spot" % symbol)
try:
spot = D(ystockquote.get_price(symbol))
except (IOError, InvalidOperation):
print("Error getting %s spot, skipping" % symbol)
continue
data += [[todaynum, spot, spot, spot, spot, 0]]
value = sto.sto(data)[-1][0]
if value >= 80:
res['strong'] += [(symbol, value)]
elif value <= 20:
res['weak'] += [(symbol, value)]
else:
res['neutral'] += [(symbol, value)]
if debug:
for k in res.keys():
print("%s:" % k)
for (s,v) in res[k]:
print("\t%7s: %0.2f" % (s, v))
print("\n")
return(res)