create an alerts.txt file with following:
25000, above
24900, below
Then create a python script nifty_tray.py :
————————————————————
import time import threading import requests """from win10toast import ToastNotifier""" import pystray from PIL import Image, ImageDraw import os import queue from winotify import Notification, audio CHECK_INTERVAL = 30 ALERTS_FILE = os.path.join(os.path.dirname(__file__), "alerts.txt") current_ltp = 0 icon = None alerts = [] """toaster = ToastNotifier()""" alert_state = {} notif_queue = queue.Queue() def load_alerts(): """Load alerts from alerts.txt""" global alerts, alert_state alerts = [] if not os.path.exists(ALERTS_FILE): return with open(ALERTS_FILE, "r") as f: for line in f: line = line.strip() if not line or line.startswith("#"): continue try: level_str, direction = line.split(",") level = float(level_str.strip()) direction = direction.strip().lower() alerts.append((level, direction)) if (level, direction) not in alert_state: alert_state[(level, direction)] = False except Exception: continue def fetch_nifty(): """Fetch latest Nifty value""" url = "https://www.nseindia.com/api/marketStatus" headers = { "User-Agent": "Mozilla/5.0", "Accept": "application/json", "Referer": "https://www.nseindia.com/" } try: resp = requests.get(url, headers=headers, timeout=10) data = resp.json() return float(data["marketState"][0]["last"]) except Exception: return 0 def queue_notification(title, message): notif_queue.put((title, message)) def process_notifications(): while not notif_queue.empty(): title, message = notif_queue.get_nowait() toast = Notification(app_id="Nifty Tray", title=title, msg=message, duration="short") toast.set_audio(audio.Default, loop=False) toast.show() def monitor_price(): """Monitor Nifty price and trigger alerts""" global current_ltp, icon while True: load_alerts() ltp = fetch_nifty() if ltp > 0: current_ltp = ltp if icon: icon.title = f"Nifty: {ltp}" for level, direction in alerts: key = (level, direction) if direction == "above": if ltp > level and not alert_state[key]: queue_notification("Nifty Alert", f"Nifty crossed above {level} (Now {ltp})") alert_state[key] = True elif ltp <= level: alert_state[key] = False elif direction == "below": if ltp < level and not alert_state[key]: queue_notification("Nifty Alert", f"Nifty crossed below {level} (Now {ltp})") alert_state[key] = True elif ltp >= level: alert_state[key] = False time.sleep(CHECK_INTERVAL) def notification_loop(): """Background loop to process notifications""" while True: process_notifications() time.sleep(1) def create_image(): """Tray icon image""" img = Image.new("RGB", (64, 64), "green") d = ImageDraw.Draw(img) d.rectangle([16, 16, 48, 48], fill="white") return img def quit_app(icon_obj, item): icon_obj.stop() def run_tray(): global icon load_alerts() icon = pystray.Icon( "nifty", create_image(), "Nifty: Loading...", menu=pystray.Menu(pystray.MenuItem("Quit", quit_app)) ) # background threads threading.Thread(target=monitor_price, daemon=True).start() threading.Thread(target=notification_loop, daemon=True).start() # run tray icon loop icon.run() if __name__ == "__main__": run_tray()
————————————–
install following python modules
pip install requests plyer pystray pillow
pip install pyinstaller
pip install win10toast
pip install winotify
pip install –upgrade packaging
Now Compile the above script to generate an exe file:
C:\Users\admin\AppData\Roaming\Python\Python312\Scripts\pyinstaller.exe --noconsole --onefile nifty_tray.py