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.
- 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
- Python 3.7+
- NewsAPI key (free tier available at NewsAPI)
-
Clone the repository:
git clone <repository-url> cd sensex-simulation-engine
-
Create and activate a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install the required packages:
pip install -r requirements.txt
-
Create a
.envfile in the project root and add your NewsAPI key:NEWS_API_KEY=your_news_api_key_here
Run the simulation with default parameters:
python main.py--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
Run a 90-day simulation with 2 million INR initial investment:
python main.py --days 90 --initial 2000000Run without news sentiment analysis:
python main.py --no-newsmain.py: Main script to run the simulationdata_fetcher.py: Fetches BSE SENSEX historical datanews_fetcher.py: Fetches and processes financial newssentiment_analyzer.py: Analyzes sentiment of news articlessimulation_engine.py: Core simulation logicconfig.py: Configuration settings and constantsdata/: Directory for storing data filesbse_sensex_historical.csv: Cached historical datafinancial_news.json: Cached news articlessimulation_results.csv: Simulation resultssimulation_results.html: Interactive visualization
The simulation will generate:
- A CSV file with detailed simulation results
- An interactive HTML plot showing:
- Portfolio value over time
- SENSEX price movements
- Sentiment scores
- Buy/sell signals
The simulation calculates several performance metrics:
- Total Return
- Annualized Return
- Volatility
- Sharpe Ratio
- Maximum Drawdown
- Number of Trades
- The simulation uses a simple trading strategy based on sentiment scores. You can modify the
_apply_trading_strategymethod insimulation_engine.pyto 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.
This project is licensed under the MIT License - see the LICENSE file for details.
- Yahoo Finance for market data
- NewsAPI for news data
- NLTK and TextBlob for sentiment analysis
- Plotly for visualization