SmartAPI Bot is an automated trading system designed for Indian equity markets using Angel One's SmartAPI.
It supports intraday, delivery, and F&O trades, integrating portfolio management, watchlists, compliance checks, GTT orders, and real-time WebSocket feeds.
The bot comes with:
- Streamlit-based dashboard for monitoring and control
- Strategy execution framework with pluggable trading strategies
- Rate-limit and exception handling per SmartAPI guidelines
- Database-backed persistence for trades, watchlists, and historical data
- Automate order placement, monitoring, and risk management.
- Unify session/token management for all API usage (trade, WebSocket, historical).
- Enable strategy selection from pre-defined and external strategy pools.
- Monitor portfolio performance, holdings, and available margins.
- Control the system in real-time via a web UI.
- Stay compliant with SmartAPI rate limits and handle API exceptions gracefully.
- Persist all critical trading data for analytics and audits.
- Multi-Product Trading – Intraday, Delivery, and Futures & Options.
- Global Risk Toggles:
auto_sq
: Auto square-off intraday positions before market close.allow_cv
: Allow conversion from intraday to delivery under certain loss/hold conditions.buf_min
: Time buffer (minutes) before market close for square-off.trail_pct
: Trailing stop loss percentage.step_pct
: Step size for trailing logic.
- Stop Loss & Take Profit for all trades.
- Unified Session Management for:
- Trading API
- WebSocket feeds
- Historical data
- Rate-Limit & Exception Handlers to stay within API limits.
- Instrument Management:
- Download and update tradable instruments list.
- Save to database or CSV for quick lookups.
- Streamlit Dashboard:
- Portfolio snapshot (holdings, margins, PnL)
- Intraday control panel
- Performance metrics
- Strategy selector
- News feed integration
smart_trading_bot/
├── .env # Environment variables (DB creds, API keys, secrets)
├── config.ini # Overrides for services, intervals, market hours, etc.
├── requirements.txt # Python dependencies
├── Dockerfile # Build instructions for Dockerized Streamlit app
├── docker-compose.yml # Compose setup (app + optional local MariaDB)
├── docker-compose.override.yml # Override to point app at remote MariaDB
├── supervisord.conf # Supervisor config for background jobs
├── pytest.ini # Pytest settings
├── README.md # Project overview & setup guide
│
├── app.py # Streamlit entrypoint:
│ └─ main() # Renders sidebar + loads selected page via load_page()
│ └─ load_page(module_name) # Dynamically imports and caches pages.
│
├── runner.py # Standalone runner:
│ └─ main_loop() # Loop: health_check → run_strategy_logic → sleep
│
├── pages/ # Streamlit tabs
│ ├── Dashboard.py # Dashboard UI:
│ │ └─ render_dashboard() # Flags, funds, holdings, intraday controls
│ │ └─ (internal widgets) # Manual order form + validation
│ │
│ ├── Trade_Reporter.py # Trades reporting:
│ │ └─ render_trade_reporter() # Filters, table, perf cards, export UI
│ │
│ ├── Metrics.py # Performance & risk charts:
│ │ └─ render_metrics() # Summary cards + equity curve (Plotly)
│ │
│ ├── System_Monitor.py # Health & monitoring:
│ │ └─ render_system_monitor() # Flags, holdings, positions snapshots
│ │
│ ├── Strategy_Selection.py # Strategy loader:
│ │ └─ render_strategy_selection() # Group → multi‑select → import
│ │
│ └── News.py # Symbol news:
│ └─ render_news() # Fetch & display Finnhub/NewsAPI feeds
│
├── jobs/ # Background services
│ ├── health_check.py # Health check loop:
│ │ └─ run_health_check() # DB, API, holdings, market‑hours, flags
│ │ └─ log_health_results() # Insert into health_logs
│ │ └─ health_check_loop() # Infinite loop w/ interval
│ │
│ ├── daily_scheduler.py # Daily batch:
│ │ └─ run_daily_cycle() # Health + run_strategy_logic
│ │ └─ log_schedule_status() # Insert into daily_schedule_log
│ │ └─ daily_scheduler_loop() # Wait until configured time
│ │
│ ├── monitor.py # Real‑time monitor:
│ │ └─ run_system_monitor() # Flags, holdings, positions, metrics
│ │ └─ save_monitor_log() # Insert into monitor_logs
│ │ └─ monitor_loop() # Sleep/loop
│ │
│ ├── position_monitor.py # Intraday position exits:
│ │ └─ time_to_square_off() # Check if near market‑close
│ │ └─ run_position_monitor() # TP/SL/time logic → sell orders
│ │ └─ position_monitor_loop() # Sleep/loop
│ │
│ ├── instruments_updater.py # Instruments master refresh:
│ │ └─ instruments_update_loop() # download → save CSV/DB → sleep
│ │
│ ├── rate_limit_updater.py # Rate‑limit refresh:
│ │ └─ rate_limit_update_loop() # scrape docs → CSV/DB → sleep
│ │
│ └── exceptions_updater.py # API exceptions refresh:
│ └─ exceptions_update_loop() # scrape docs → CSV/DB → sleep
│
├── session/ # Shared SmartAPI sessions & limits
│ ├── smartapi_session.py # get_smartapi_session(purpose) → SmartConnect
│ │ └─ (auth, totp, feedToken)
│ │ └─ accessors: auth_token, feed_token
│ │
│ ├── rate_limit.py # RateLimitManager & refresh_rate_limits()
│ │ └─ fetch_rate_limit_table()
│ │ └─ save/load CSV & DB
│ │ └─ RateLimitManager.check()/call()
│ │
│ └── exceptions.py # refresh_exceptions() & lookup_exception()
│ └─ fetch_exceptions_table()
│ └─ save/load CSV & DB
│ └─ lookup_exception(code)
│
├── orders/ # Order management wrappers
│ ├── init.py # Exposes place/modify/cancel & GTT & history
│ ├── basic.py # place_order(), modify_order(), cancel_order()
│ ├── gtt.py # create_gtt(), list_gtt(), get_gtt(), modify_gtt(), cancel_gtt()
│ └── history.py # get_order_book(), get_trade_book()
│
├── trade/ # High‑level trading ops
│ ├── delivery.py # buy_delivery(), sell_delivery(), cancel_delivery_order()
│ ├── intraday.py # buy_intraday(), sell_intraday(), cancel_intraday_order()
│ ├── fno.py # buy_future(), sell_future(), buy_option(), sell_option(), cancel_fno_order()
│ └── compliance_check.py # is_market_open(), is_compliant_order()
│
├── portfolio/ # Portfolio endpoints
│ ├── funds.py # get_available_funds(), get_margin_limits()
│ ├── holdings.py # get_holdings(), get_all_holdings_summary()
│ ├── positions.py # get_positions()
│ └── orderbook.py # get_order_book(), get_trade_book()
│
├── watchlists/ # Watchlist loader
│ └── service.py # load_watchlist(path) → list of dicts
│
├── instruments/ # Instrument master management
│ ├── fetch.py # download_master_list() → DataFrame
│ └── service.py # save/load CSV & DB, refresh_master_csv_and_db()
│
├── services/ # Business‑logic facades
│ ├── order_service.py # execute_order(), cancel_order()
│ ├── status_service.py # should_trade_now(), get_system_flags()
│ ├── portfolio_service.py # fetch_available_funds(), fetch_holdings(), size_position()
│ ├── watchlist_service.py # load_watchlist()
│ ├── news_service.py # fetch_finnhub_news(), fetch_newsapi_news()
│ └── analytics_service.py # fetch_trades_df()
│
├── strategies/ # Strategy definitions & discovery
│ ├── init.py
│ ├── strategy_discovery.py # _discover_local(), _discover_pyalgo(), all_available_strategies()
│ ├── strategy_manager.py # get_trade_signal(symbol_data, modules)
│ ├── volume_spike.py # volume_spike_signal(data)
│ ├── vwap_crossover.py # vwap_crossover_signal(data)
│ ├── pyalgotrading_adapter.py # sma_crossover_signal(adapter)
│ └── ... external adapters ...
│
├── metrics/ # Analytics & reporting
│ ├── performance_metrics.py # calculate_metrics(df)
│ ├── risk_metrics.py # calculate_risk_metrics(df)
│ └── trade_reporter.py # fetch_trades(), compute_metrics(), export_trades()
│
├── utils/ # Helpers & exports
│ ├── db_utils.py # get_db_connection(), init_db_schema()
│ ├── config_utils.py # settings = ConfigParser + env
│ ├── logger_utils.py # setup_logger(), default logger
│ ├── retry_utils.py # retry() decorator with backoff
│ ├── notification_utils.py # notify_admin() via Telegram/Email
│ └── export_utils.py # export_to_excel(), export_to_pdf()
│
├── database/ # Schema creation & seeding
│ └── trading_setup.py # create tables: status_flags, health_logs, trades, exports, monitor_logs, instruments, rate_limits, exceptions
│
├── data/ # Static & generated data
│ ├── watchlist.csv # Example watchlist file
│ └── exports/ # Output folder for Excel/PDF reports
│
└── tests/ # Pytest unit tests
├── conftest.py # SQLite+pytest fixtures
├── test_db_utils.py
├── test_status_utils.py
├── test_trade_utils.py
├── test_compliance_check.py
├── test_portfolio_service.py
├── test_watchlist_service.py
├── test_news_service.py
├── test_performance_metrics.py
├── test_risk_metrics.py
├── test_analytics_service.py
└── test_trade_reporter.py
See detailed tree in docs/PROJECT_STRUCTURE.md
(or refer to project root structure in code).
app.py
starts the Streamlit server and loads pages dynamically.- User sets global toggles (auto_sq, trailing, etc.).
- User selects strategies from
Strategy_Selection.py
.
runner.py
callstrade/core.py → run_strategy_logic()
.- Strategy is executed via
strategies/strategy_executor.py
. - Signal generation (BUY/SELL/HOLD) from strategy.
- Compliance checks (
trade/compliance_check.py
). - Orders placed via
orders
module.
- Portfolio data fetched from
portfolio/
modules. - WebSocket live price updates handled by
data/websocket.py
. - Historical data loaded via
data/historical.py
.
jobs/
modules run in background via scheduler or Supervisor.- Instruments & rate limits updated periodically.
- Exceptions table updated for better error reporting.
Page | Purpose |
---|---|
Dashboard | Real-time portfolio + intraday controls |
Trade Reporter | View and export trade history |
Metrics | Performance metrics and charts |
System Monitor | System health & last actions |
Strategy Selection | Select and enable strategies |
News | Live market news feed |
-
Clone Repo
git clone https://github.com/yourorg/smart_trading_bot.git cd smart_trading_bot
-
Configure environment:
-
Create a
.env
file in the project root:
DB_HOST=... DB_PORT=3306 DB_NAME=smartapi DB_USER=... DB_PASSWORD=...
SMARTAPI_TRADE_API_KEY=... SMARTAPI_TRADE_SECRET=... SMARTAPI_WS_API_KEY=... SMARTAPI_WS_SECRET=... SMARTAPI_HIST_API_KEY=... SMARTAPI_HIST_SECRET=... SMARTAPI_CLIENT_CODE=... SMARTAPI_PASSWORD=...
FINNHUB_API_KEY=... NEWSAPI_API_KEY=... RATE_LIMIT_SEC=0.5 MAX_RETRIES=3 RETRY_BASE_DELAY=2 MARKET_CLOSE_TIME=15:30
-
-
Install dependencies:
pip install -r requirements.txt
-
Database:
- Ensure your remote MariaDB 10+ instance has the necessary tables.
Run
database/trading_setup.py
if needed.
- Ensure your remote MariaDB 10+ instance has the necessary tables.
Run
-
Run locally:
streamlit run app.py
-
Build & start:
docker-compose up --build
- Uses .env for remote DB and API credentials.
- Exposes Streamlit on http://localhost:8501.
- Supervisor will manage background jobs inside the container.
-
Start:
supervisord -c supervisord.conf
-
Manages:
runner.py
(continuous strategy loop)jobs/daily_scheduler.py
(daily batch)jobs/monitor.py
(system monitoring)
-
Run tests:
pytest
-
See
pytest.ini
for configuration.
- Fork and open PRs for enhancements, bug fixes, or new strategies.
- Please include tests and update documentation.
MIT License. See LICENSE for details.