Skip to content

Araon/Sensex-Simulation-Engine

Repository files navigation

BSE SENSEX Simulation Engine with News Sentiment Analysis

This project simulates the BSE SENSEX market movements by incorporating real-world market data and news sentiment analysis. It fetches historical BSE SENSEX data, analyzes financial news sentiment, and simulates trading strategies based on market conditions and sentiment scores.

Features

  • Real-time Data Fetching: Pulls historical BSE SENSEX data using Yahoo Finance
  • News Sentiment Analysis: Analyzes financial news articles for market sentiment
  • Trading Simulation: Simulates trading strategies based on market data and sentiment
  • Performance Metrics: Calculates key performance indicators like returns, volatility, and drawdown
  • Visualization: Generates interactive plots of simulation results

Prerequisites

  • Python 3.7+
  • NewsAPI key (free tier available at NewsAPI)

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd sensex-simulation-engine
  2. Create and activate a virtual environment (recommended):

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
  3. Install the required packages:

    pip install -r requirements.txt
  4. Create a .env file in the project root and add your NewsAPI key:

    NEWS_API_KEY=your_news_api_key_here
    

Usage

Basic Usage

Run the simulation with default parameters:

python main.py

Command Line Arguments

  • --initial: Initial investment amount in INR (default: 1,000,000)
  • --days: Number of days to simulate (default: 30)
  • --sentiment-impact: Impact factor of sentiment on prices (default: 0.05)
  • --no-news: Run simulation without news sentiment analysis

Examples

Run a 90-day simulation with 2 million INR initial investment:

python main.py --days 90 --initial 2000000

Run without news sentiment analysis:

python main.py --no-news

Project Structure

  • main.py: Main script to run the simulation
  • data_fetcher.py: Fetches BSE SENSEX historical data
  • news_fetcher.py: Fetches and processes financial news
  • sentiment_analyzer.py: Analyzes sentiment of news articles
  • simulation_engine.py: Core simulation logic
  • config.py: Configuration settings and constants
  • data/: Directory for storing data files
    • bse_sensex_historical.csv: Cached historical data
    • financial_news.json: Cached news articles
    • simulation_results.csv: Simulation results
    • simulation_results.html: Interactive visualization

Output

The simulation will generate:

  1. A CSV file with detailed simulation results
  2. An interactive HTML plot showing:
    • Portfolio value over time
    • SENSEX price movements
    • Sentiment scores
    • Buy/sell signals

Performance Metrics

The simulation calculates several performance metrics:

  • Total Return
  • Annualized Return
  • Volatility
  • Sharpe Ratio
  • Maximum Drawdown
  • Number of Trades

Notes

  • The simulation uses a simple trading strategy based on sentiment scores. You can modify the _apply_trading_strategy method in simulation_engine.py to implement your own strategies.
  • News sentiment analysis uses both TextBlob and VADER for more robust sentiment detection.
  • The free tier of NewsAPI has rate limits. For extensive backtesting, consider upgrading to a paid plan.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Yahoo Finance for market data
  • NewsAPI for news data
  • NLTK and TextBlob for sentiment analysis
  • Plotly for visualization

About

Sensex Simulation with Sentiment Nudges

Resources

Stars

Watchers

Forks

Contributors

Languages