Skip to content

An autonomous paper trading bot for Indian NSE stocks built in Rust, using a hybrid strategy of technical indicators and news sentiment analysis.

Notifications You must be signed in to change notification settings

adityavijay21/sentinel-tradingbot-rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Automated NSE Paper Trading Bot

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.


Core Features

  • 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.

Technology Stack

  • 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

Trading Strategy Overview

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:

  1. 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.
  2. 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.
  3. Signal Generation: A stock is flagged as a 'BUY' if its predicted_change_pct is 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.

Configuration

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 hours

API Endpoints

The 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

Getting Started

Prerequisites

Local Development

  1. Clone the repository:

    git clone https://github.com/adityavijay21/sentinel-tradingbot-rust.git
    cd nseapi
  2. Run the project locally using Shuttle: This command will provision necessary infrastructure (like a database if needed) and run the bot.

    shuttle run

Deployment

Deploying the bot is a one-step process with Shuttle.

  1. Login to Shuttle:

    shuttle login
  2. 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.


Disclaimer

⚠️ This is a paper trading bot for simulation purposes only. The trading logic is experimental and is not intended for use with real capital. The author and contributors are not liable for any financial losses incurred from using or adapting this software. Always trade responsibly.

License

This project is licensed under the MIT License.

About

An autonomous paper trading bot for Indian NSE stocks built in Rust, using a hybrid strategy of technical indicators and news sentiment analysis.

Topics

Resources

Stars

Watchers

Forks

Languages