Sentinel Trading Bot is a high-performance, automated paper trading bot engineered in Rust for the Indian National Stock Exchange (NSE). It leverages a hybrid strategy combining quantitative technical analysis with real-time news sentiment analysis to make informed, autonomous trading decisions.
This project is designed as a simulation tool to backtest and observe algorithmic trading strategies in a risk-free environment. It operates during NSE market hours, manages a virtual portfolio, and exposes a comprehensive RESTful API for real-time monitoring and control.
- Hybrid Trading Strategy: Fuses quantitative indicators (RSI, MACD, Bollinger Bands, Stochastic Oscillator) with news headline sentiment analysis to generate high-confidence trading signals.
- Automated Execution: Operates autonomously during NSE market hours (09:15 - 15:30 IST), executing buy and sell orders based on its core strategy.
- Comprehensive Portfolio Management: Manages a virtual portfolio, tracking cash, open positions, and overall profit and loss (P&L) in real-time.
- Advanced Risk Management: Implements crucial risk mitigation rules, including configurable per-trade position sizing, stop-loss triggers, and take-profit targets.
- Real-time Monitoring API: Exposes a clean, RESTful API built with Axum to monitor portfolio performance, view trade history, and query market status.
- Scalable & Efficient: Built with Rust and Tokio for high concurrency and low-resource consumption, capable of analyzing hundreds of stocks efficiently.
- Seamless Deployment: Optimized for one-command deployment using the Shuttle platform.
- Language: Rust
- Web Framework: Axum for the REST API
- Deployment: Shuttle
- Asynchronous Runtime: Tokio
- HTTP Client: Reqwest for data fetching
- Data Serialization: Serde for JSON and CSV handling
- Date & Time: Chrono for market hours management
The bot's decision-making engine is based on a multi-factor model that calculates a predicted_change_pct for each analyzed stock. This prediction is derived from:
- News Sentiment: It scrapes headlines from Google News, performs lexicon-based sentiment analysis on them, and calculates an average sentiment score. Positive news contributes positively to the prediction, and vice-versa.
-
Technical Indicators:
-
Relative Strength Index (RSI): Identifies overbought (
$RSI > 70$ ) or oversold ($RSI < 30$ ) conditions. The strategy tends to favor buying oversold stocks and selling overbought ones. - Moving Average Convergence Divergence (MACD): The MACD histogram is used to gauge momentum. A positive histogram suggests upward momentum, while a negative one indicates downward momentum.
-
Relative Strength Index (RSI): Identifies overbought (
-
Signal Generation: A stock is flagged as a 'BUY' if its
predicted_change_pctis above a certain positive threshold and as a 'SELL' if it's below a negative threshold. A trade is only executed if the calculated confidence score (a blend of sentiment strength and prediction magnitude) exceeds the configured minimum.
The bot's behavior can be fine-tuned via constants defined in the main.rs file.
// ======================= Configuration =======================
const INITIAL_CAPITAL: f64 = 100_000.0; // ₹1,00,000 starting capital
const MAX_POSITION_SIZE: f64 = 0.15; // Max 15% of portfolio per stock
const STOP_LOSS_PCT: f64 = 5.0; // 5% stop loss
const TAKE_PROFIT_PCT: f64 = 10.0; // 10% take profit
const MIN_CONFIDENCE: f64 = 0.65; // Minimum confidence to trade
const MAX_OPEN_POSITIONS: usize = 10; // Max concurrent positions
const SCAN_INTERVAL_SECS: u64 = 300; // Scan every 5 minutes during market hoursThe service exposes the following endpoints for monitoring and interaction.
| Method | Path | Description |
|---|---|---|
GET |
/health |
Health check to confirm the bot is running. |
GET |
/market-status |
Returns current NSE market status (open/closed). |
GET |
/portfolio |
Retrieves the current state of the portfolio. |
GET |
/trades |
Fetches the complete history of all executed trades. |
GET |
/report |
Generates a detailed performance report. |
GET |
/insight/{symbol} |
Performs on-demand analysis for a specific stock. |
POST |
/buy/{symbol} |
Manually forces a buy order for a specified stock. |
POST |
/sell/{symbol} |
Manually forces a sell order for an existing position. |
Example Usage:
# Check the bot's health
curl https://nseapi-xgyq.shuttle.app/health
# Get a full performance report
curl https://nseapi-xgyq.shuttle.app/report
# Force a buy order for Reliance Industries
curl -X POST https://nseapi-xgyq.shuttle.app/buy/RELIANCE- Rust toolchain.
- Shuttle CLI.
-
Clone the repository:
git clone https://github.com/adityavijay21/sentinel-tradingbot-rust.git cd nseapi -
Run the project locally using Shuttle: This command will provision necessary infrastructure (like a database if needed) and run the bot.
shuttle run
Deploying the bot is a one-step process with Shuttle.
-
Login to Shuttle:
shuttle login
-
Deploy the project: From the project root directory, run:
shuttle deploy
Shuttle will build, provision, and deploy the application, providing you with a public URL.
This project is licensed under the MIT License.