ZijianWang1125/Stock-Market-Sentiment-Analysis

This project extracts investor sentiment from online data to aid investment decisions. In China's inefficient market, we analyze stock commentary, build sentiment indicators, and assess their market impact.

HTML

4

26 commits

updated May 5, 2025

See the code

README

Stock Market Sentiment Analysis

Investor sentiment plays a crucial role in influencing stock market movements, but capturing and quantifying real-time sentiment remains challenging. In this project, we developed a sentiment analysis pipeline by crawling 71,888 posts from Eastmoney’s Shanghai Composite Index forum. We fine-tuned a Chinese ELECTRA model to classify post sentiment into three categories and constructed a rolling sentiment index to track market emotions dynamically.

Project Structure

Key files based on different Usage have sturcture like:

.
├── data/ # Data files for usage: "Train model to predict whole test data" only
│   └── electra_sentiment_chinese/
│       ├── train_data/           # Training datasets from various sources
│       │   ├── train_data_1.xlsx # Dataset from Alibaba Tianchi
│       │   ├── train_data_2.csv  # Dataset from Alibaba Tianchi
│       │   ├── train_data_3.txt  # Positive sentiment dataset from GitHub
│       │   ├── train_data_4.txt  # Negative sentiment dataset from GitHub
│       │   └── train_data_clean.txt # Processed training data
│       ├── test_data/            # Test datasets for model evaluation
│       │   ├── test_data.csv     # Raw scraped test data
│       │   └── test_data_clean.csv # Processed test data
│       └── pred_data/            # Output directory for prediction results
├── model/ # Fine-tuned Chinese ELECTRA model files for usage: "Use trained model to predict several input comments" only
│   └── electra_sentiment_chinese/
│       ├── config.json           # Model configuration
│       ├── model.safetensors     # Model weights
│       ├── special_tokens_map.json # Special tokens configuration
│       ├── tokenizer_config.json # Tokenizer configuration
│       └── vocab.txt             # Model vocabulary
├── script/
│   ├── data_test_loader.py       # Formatter to the test data: `./data/electra_sentiment_chinese/test_data`
│   ├── data_test_crawler.py       # Crawler to the test data: `./data/electra_sentiment_chinese/test_data`
│   ├── data_train_loader.py       # Formatter to the train data: `./data/electra_sentiment_chinese/train_data`
│   ├── main.py                   # Main script for training and evaluating the model
│   ├── model_finetuner.py        # Script for fine-tuning the ELECTRA model
│   └── model_predictor.py        # Script for predicting sentiment of input │
└── README.md                     # Project documentation
└── requirements.txt              # Dependencies for the project

Data Source

After finishing the Unzip step in Train model to predict whole test data part, you will be able to find several datasets in ./data/train_data or ./data/test_data. Here are their sources:

Enviroment

See requirements.txt for a list of necessary Python packages.

Setup and Installation

1. Clone the repository

git clone https://github.com/yourusername/Stock-Market-Sentiment-Analysis1.git
cd Stock-Market-Sentiment-Analysis1

2. Set up the environment and activate

Choose either venv or conda:

  • venv:

    python -m venv venv
    # On Windows
    .\venv\Scripts\activate
    # On macOS/Linux
    source venv/bin/activate
    
  • conda:

    conda create --name risk_env python=3.9 # Or desired Python version
    conda activate risk_env
    

3. Install dependencies

pip install -r ./requirements.txt

Usage

You can either repeat the training pipeline to predict the sentiment using the comments from ./data/test_data or use a trained model to predict the sentiment of several input Chinese comments.

Train model to predict whole test data

To train model to predict whole test data by yourself:

  1. Download: Download the data.rar in Google Drive.

  2. Unzip: Unzip the data.rar in root directory ./.

  3. Execute: Execute main.py and you will be given a model trained by yourself in ./model/electra_sentiment_chinese and a predicted dataset in ./data/electra_sentiment_chinese/pred_data based on a testing dataset in ./data/electra_sentiment_chinese/test_data/test_data_clean.csv.

Use trained model to predict several input comments

To use trained model to predict several input comments:

  1. Download: Download the model.rar in Google Drive.

  2. Unzip: Unzip the model.rar in root directory ./.

  3. Execute: Execute model_predictor and you will be required to input Chinese comments about stock topics in your desired quantity. After inputting, you will be given a sentiment analysis for these comments.

Results

The sentiment analysis model classifies comment into three categories:

  • Positive: Indicating optimistic market sentiment, with sentiment value of 2
  • Neutral: Indicating balanced or uncertain market views, with sentiment value of 1
  • Negative: Indicating pessimistic market sentiment with, sentiment value of 0

Here are different results example for different usages:

  • Example sentiment analysis of market comments in Train model to predict whole test data part:

    User AgeComment #Influecen PowerRead #CommentDataSentiment ValueConfidenceWeighted Sentiment Value
    8.5901.930国内科创板2019-03-2820.561.50
    0.0400.5200科创板融资问题2019-03-2820.651.58
    0.2501.0574缩量都能把股指拉红神勇的大A2019-03-2720.731.59
  • Example sentiment analysis of market comments in Use trained model to predict several input comments part:

    CommentSentimentConfidenceWeighted Sentiment
    经济数据好于预期,市场上涨Positive0.891.88
    投资者对新政策持观望态度Neutral0.751.31
    通胀数据令人担忧,可能引发抛售Negative0.820.03

License

MIT License

Copyright (c) 2025 Stock Market Sentiment Analysis Project

ZijianWang1125/Stock-Market-Sentiment-Analysis

This project extracts investor sentiment from online data to aid investment decisions. In China's inefficient market, we analyze stock commentary, build sentiment indicators, and assess their market impact.

HTML

4

26 commits

updated May 5, 2025

See the code

README

Stock Market Sentiment Analysis

Investor sentiment plays a crucial role in influencing stock market movements, but capturing and quantifying real-time sentiment remains challenging. In this project, we developed a sentiment analysis pipeline by crawling 71,888 posts from Eastmoney’s Shanghai Composite Index forum. We fine-tuned a Chinese ELECTRA model to classify post sentiment into three categories and constructed a rolling sentiment index to track market emotions dynamically.

Project Structure

Key files based on different Usage have sturcture like:

.
├── data/ # Data files for usage: "Train model to predict whole test data" only
│   └── electra_sentiment_chinese/
│       ├── train_data/           # Training datasets from various sources
│       │   ├── train_data_1.xlsx # Dataset from Alibaba Tianchi
│       │   ├── train_data_2.csv  # Dataset from Alibaba Tianchi
│       │   ├── train_data_3.txt  # Positive sentiment dataset from GitHub
│       │   ├── train_data_4.txt  # Negative sentiment dataset from GitHub
│       │   └── train_data_clean.txt # Processed training data
│       ├── test_data/            # Test datasets for model evaluation
│       │   ├── test_data.csv     # Raw scraped test data
│       │   └── test_data_clean.csv # Processed test data
│       └── pred_data/            # Output directory for prediction results
├── model/ # Fine-tuned Chinese ELECTRA model files for usage: "Use trained model to predict several input comments" only
│   └── electra_sentiment_chinese/
│       ├── config.json           # Model configuration
│       ├── model.safetensors     # Model weights
│       ├── special_tokens_map.json # Special tokens configuration
│       ├── tokenizer_config.json # Tokenizer configuration
│       └── vocab.txt             # Model vocabulary
├── script/
│   ├── data_test_loader.py       # Formatter to the test data: `./data/electra_sentiment_chinese/test_data`
│   ├── data_test_crawler.py       # Crawler to the test data: `./data/electra_sentiment_chinese/test_data`
│   ├── data_train_loader.py       # Formatter to the train data: `./data/electra_sentiment_chinese/train_data`
│   ├── main.py                   # Main script for training and evaluating the model
│   ├── model_finetuner.py        # Script for fine-tuning the ELECTRA model
│   └── model_predictor.py        # Script for predicting sentiment of input │
└── README.md                     # Project documentation
└── requirements.txt              # Dependencies for the project

Data Source

After finishing the Unzip step in Train model to predict whole test data part, you will be able to find several datasets in ./data/train_data or ./data/test_data. Here are their sources:

Enviroment

See requirements.txt for a list of necessary Python packages.

Setup and Installation

1. Clone the repository

git clone https://github.com/yourusername/Stock-Market-Sentiment-Analysis1.git
cd Stock-Market-Sentiment-Analysis1

2. Set up the environment and activate

Choose either venv or conda:

  • venv:

    python -m venv venv
    # On Windows
    .\venv\Scripts\activate
    # On macOS/Linux
    source venv/bin/activate
    
  • conda:

    conda create --name risk_env python=3.9 # Or desired Python version
    conda activate risk_env
    

3. Install dependencies

pip install -r ./requirements.txt

Usage

You can either repeat the training pipeline to predict the sentiment using the comments from ./data/test_data or use a trained model to predict the sentiment of several input Chinese comments.

Train model to predict whole test data

To train model to predict whole test data by yourself:

  1. Download: Download the data.rar in Google Drive.

  2. Unzip: Unzip the data.rar in root directory ./.

  3. Execute: Execute main.py and you will be given a model trained by yourself in ./model/electra_sentiment_chinese and a predicted dataset in ./data/electra_sentiment_chinese/pred_data based on a testing dataset in ./data/electra_sentiment_chinese/test_data/test_data_clean.csv.

Use trained model to predict several input comments

To use trained model to predict several input comments:

  1. Download: Download the model.rar in Google Drive.

  2. Unzip: Unzip the model.rar in root directory ./.

  3. Execute: Execute model_predictor and you will be required to input Chinese comments about stock topics in your desired quantity. After inputting, you will be given a sentiment analysis for these comments.

Results

The sentiment analysis model classifies comment into three categories:

  • Positive: Indicating optimistic market sentiment, with sentiment value of 2
  • Neutral: Indicating balanced or uncertain market views, with sentiment value of 1
  • Negative: Indicating pessimistic market sentiment with, sentiment value of 0

Here are different results example for different usages:

  • Example sentiment analysis of market comments in Train model to predict whole test data part:

    User AgeComment #Influecen PowerRead #CommentDataSentiment ValueConfidenceWeighted Sentiment Value
    8.5901.930国内科创板2019-03-2820.561.50
    0.0400.5200科创板融资问题2019-03-2820.651.58
    0.2501.0574缩量都能把股指拉红神勇的大A2019-03-2720.731.59
  • Example sentiment analysis of market comments in Use trained model to predict several input comments part:

    CommentSentimentConfidenceWeighted Sentiment
    经济数据好于预期,市场上涨Positive0.891.88
    投资者对新政策持观望态度Neutral0.751.31
    通胀数据令人担忧,可能引发抛售Negative0.820.03

License

MIT License

Copyright (c) 2025 Stock Market Sentiment Analysis Project