How to Build a Simple "Price Alert" Bot via API

Building a small price-alert bot is a great first project for API trading in India. The idea is simple: watch the price of a stock, ETF or crypto, and send a notification when it crosses a target. This article explains the concept, gives practical steps, and keeps everything easy to follow for beginners.

Start with the basics. You will need:
- A broker or data provider that offers an API (examples in India: Zerodha Kite Connect, Upstox, Angel Broking, or exchange data feeds for NSE/BSE). For crypto you can use WazirX or global exchanges that provide INR pairs.
- API keys from the provider and a development environment (a small server, Raspberry Pi, or cloud VM).
- A simple notification channel: Telegram is free and easy, SMS or email are alternatives.

Why use an API? APIs let your bot fetch live price quotes programmatically instead of manually checking. You can poll a REST endpoint every few seconds or use a WebSocket for real-time ticks. For small personal bots, polling every 5–30 seconds is typical, respecting rate limits.

High-level design
A minimal alert bot has three parts:
- Price fetcher: gets the latest price for a symbol.
- Decision logic: compares price to your threshold.
- Notifier: sends a message when the condition is met and optionally records alerts to avoid duplicates.

  • Step 1: Get API access and keys
    Register with your chosen provider. Save API key and secret securely (do not hard-code them). Use environment variables or a secrets manager.
  • Step 2: Choose language and libs
    Python is common and beginner-friendly (requests, websocket-client). Node.js with axios or ws is also popular.
  • Step 3: Implement fetch logic
    Call the quote endpoint for the symbol you want, for example RELIANCE.NS for Reliance Industries on NSE. Example pseudocode:
    fetch_price = api.get_quote("RELIANCE.NS")
    current_price = fetch_price["last_price"]
    Compare current_price with your threshold, e.g., target = 3500 (use ₹ for rupees).
  • Step 4: Notification
    If current_price >= target and you haven't alerted yet, send a message:
    - Telegram: create a bot via BotFather and use sendMessage API.
    - Email: SMTP or a transactional service.
    - SMS: service providers charge small fees per SMS.
  • Step 5: Run safely
    Run the bot continuously using a simple loop or a scheduler. Respect rate limits: don’t poll too often. Store a small state file so you don’t repeat alerts every cycle.

Tip: Use INR values for targets if you trade in India. If you ever see examples in USD, convert them—e.g., ₹3,000 rather than $40—so your alerts match local prices and fees.

A short example flow (plain text, no code block):
- Every 10 seconds, call GET /quote?symbol=RELIANCE.NS
- If last_price > 3500 and alerted flag is false:
- send Telegram message "Reliance crossed ₹3500: now ₹X"
- set alerted flag = true
- If last_price falls below 3500 by a margin you set, reset alerted flag so future alerts can trigger again

Practical tips and safety
- Respect rate limits: many Indian broker APIs limit calls per minute. Exceeding limits can get keys blocked.
- Secure keys: use environment variables and avoid pushing keys to GitHub.
- Test on paper or demo accounts before using real funds.
- Consider minor price spikes and use small debounce or confirmation checks (e.g., require two consecutive ticks above threshold) to reduce false alerts.

Extending the bot
Once basic alerts work, you can add:
- Multiple symbols and a simple config file with targets in ₹.
- WebSocket support for lower latency.
- Persistent logging and a small UI/dashboard showing recent alerts.
- Automated orders (only after careful testing).

This project is friendly for beginners and helps you learn API calls, handling real-time data, and integrating notifications. Start small with one symbol, use a free Telegram bot for alerts, and expand as you gain confidence. Happy building!
 
Back
Top