-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstockalerter.py
More file actions
43 lines (32 loc) · 1.22 KB
/
stockalerter.py
File metadata and controls
43 lines (32 loc) · 1.22 KB
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
import yfinance as yf
import time
active_alerts = {}
def getActiveAlerts():
alerts = []
for ticker, details in active_alerts.items():
direction = details[0]
price = details[1]
alert = str(ticker) + " " + str(direction) + " " + str(price)
alerts.append(alert)
return alerts
def getCurrentPrice(ticker):
stock = yf.Ticker(ticker)
currentPrice = stock.info['regularMarketPrice']
return round(currentPrice, 2)
def alert(formattedAlert):
ticker = formattedAlert[0]
direction = formattedAlert[1]
trigger_price = formattedAlert[2]
active_alerts[ticker] = (direction, trigger_price)
while (True):
time.sleep(1)
current_price = getCurrentPrice(ticker)
if (direction == 'over' and current_price > trigger_price):
print( "********** %s is %s %d **********" % (ticker, direction, trigger_price))
del active_alerts[ticker]
break
elif (direction == 'under' and current_price < trigger_price):
print( "********** %s is %s %d **********" % (ticker, direction, trigger_price))
del active_alerts[ticker]
break
return True