-
Notifications
You must be signed in to change notification settings - Fork 1
/
priceAlarm.py
64 lines (48 loc) · 2.38 KB
/
priceAlarm.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
import os
import time
import yfinance as yf
from winotify import Notification, audio
import pystray
import PIL.Image
import threading
tickers = ["AAPL", "TSLA", "AMD", "TQQQ"]
upper_limits = [200, 260, 180, 55]
lower_limits = [190, 200, 100, 30]
#price alerts
def alert():
while True:
last_prices = [yf.Ticker(ticker).history(period="1d")["Close"].iloc[-1] for ticker in tickers]
print(last_prices)
time.sleep(30) #change interval for longer delays
for i in range(len(tickers)):
# If stock price hit upper limit
if last_prices[i] > upper_limits[i]:
toast = Notification(app_id="Stock Alarm Bot",
title ="Price Alert for " + tickers[i],
msg=f"{tickers[i]} has reached a price of {last_prices[i]:.2f}. You might want to sell.",
icon=os.path.join(os.getcwd(),"sell.png"),
duration="long")
toast.add_actions(label="Go to IBKR", launch ="https://www.interactivebrokers.ca/en/home.php") #Change the broker according to your preference
toast.set_audio(audio.LoopingAlarm6, loop=False)
toast.show()
# If stock price hit lower limit
elif last_prices[i] < lower_limits[i]:
toast = Notification(app_id="Stock Alarm Bot",
title ="Price Alert for " + tickers[i],
msg=f"{tickers[i]} has reached a price of {last_prices[i]:.2f}. You might want to buy.",
icon=os.path.join(os.getcwd(),"buy.png"),
duration="long")
toast.add_actions(label="Go to IBKR", launch ="https://www.interactivebrokers.ca/en/home.php")
toast.set_audio(audio.LoopingAlarm8, loop=False)
toast.show()
time.sleep(1) #To enable 2 notification to display at the same time
threading.Thread(target=alert, daemon=True).start()
#sytem tray
image = PIL.Image.open("bot.png")
def on_clicked(icon, item):
if str(item) == "Exit":
icon.stop()
icon = pystray.Icon("Stock Bot", image, menu=pystray.Menu(
pystray.MenuItem("Exit", on_clicked)
))
icon.run()