Production-grade, open-source financial news sentiment analysis, continuous Bull/Bear polarity indexing, and stock entity attribution engine.
- π― Continuous Bull/Bear Polarity Score: Outputs both discrete classifications (
BULLISH,BEARISH,NEUTRAL) and a continuous polarity index from-1.00(Max Bearish) to+1.00(Max Bullish), with calibrated probabilities and impact grading (HIGH,MODERATE,LOW). - π’ Multi-Entity Ticker Attribution: Analyzes complex financial articles mentioning multiple stocks (e.g. "TCS profit beat estimates while Infosys faced margin pressure") and attributes separate sentiment ratings to each stock individually (
$TCS,$INFY,$AAPL,$RELIANCE). - β‘ Dual Engine Architecture:
- Lightweight Baseline (Production): TF-IDF + Calibrated Balanced Logistic Regression. Trains in < 2 seconds, runs in < 1ms per sample on CPU, and achieves 85.14% holdout accuracy. Zero heavy dependencies required.
- Deep Transformer (FinBERT): Built-in adapter for
ProsusAI/finbertand local fine-tuned HuggingFace / PyTorch checkpoints with GPU acceleration.
- π Market Catalyst Detection: Automatically detects 16+ market-moving catalysts (earnings surprises, margin changes, dividend announcements, share buybacks, regulatory penalties, debt restructuring, contract wins).
- π Robust Dataset Ingestion:
- Full support for
all-data.csv(CSV format). - Native support for Aalto University's
@-delimitedSentences_*Agree.txtconsensus files (100%, 75%, 66%, 50% annotator agreement tiers). - Multi-encoding fallback (
latin-1,utf-8,iso-8859-1). - Automatic stratified train/validation/test split generation.
- Full support for
- π₯οΈ Command Line Interface (CLI): Out-of-the-box CLI to analyze headlines, parse full articles, and train custom models directly from your terminal.
fin_sentiment/
βββ types.py # Domain types: SentimentScore, EntitySentiment, ArticleAnalysis
βββ ingestion/ # Ingestion & preprocessing
β βββ phrasebank_loader.py # Loader for all-data.csv and Sentences_*Agree.txt
β βββ dataset_integrator.py # Multi-source normalizer, deduplicator & class balancer
β βββ synthetic_stock_generator.py # Modern equity catalyst scenario generator
β βββ text_preprocessor.py # Financial decimal/suffix-aware tokenizer & segmenter
β βββ article_parser.py # Multi-format news article & feed parser
βββ entity/ # Entity & catalyst intelligence
β βββ ticker_extractor.py # Cashtags ($TCS), exchange tickers, company names
β βββ financial_catalysts.py# Earnings, margins, layoffs, dividend catalysts
βββ models/ # Classification backends
β βββ base.py # Abstract BaseSentimentModel protocol
β βββ baseline_model.py # Fast TF-IDF + LogisticRegression model (85%+ acc)
β βββ finbert_model.py # HuggingFace FinBERT PyTorch adapter
βββ training/ # Model training pipelines
β βββ baseline_trainer.py # Automated stratified baseline training & evaluation
β βββ transformer_trainer.py# Template for fine-tuning FinBERT on custom datasets
β βββ metrics.py # Financial accuracy, F1, and Bull/Bear directional metrics
βββ analyzer.py # High-level facade: FinancialNewsSentimentAnalyzer
βββ cli.py # Command-line interface
# Clone the repository
git clone https://github.com/finsentiment/fin-sentiment.git
cd fin-sentiment
# Install core dependencies (CPU baseline)
pip install -e .
# (Optional) Install Deep Learning Transformer support
pip install -e ".[torch]"from fin_sentiment import FinancialNewsSentimentAnalyzer
analyzer = FinancialNewsSentimentAnalyzer()
score = analyzer.analyze_text("Tata Motors net profit surged 137% beating analyst estimates as JLR margins expanded.")
print(score.label) # SentimentLabel.BULLISH
print(score.bull_bear_score) # +0.60 (-1.0 to +1.0)
print(score.confidence) # 0.561 (56.1%)
print(score.probabilities) # {'bullish': 0.561, 'neutral': 0.317, 'bearish': 0.122}
print(score.impact_grade) # ImpactGrade.MODERATEfrom fin_sentiment import FinancialNewsSentimentAnalyzer
analyzer = FinancialNewsSentimentAnalyzer()
article = """
Tech Earnings Divergence: TCS Beats Expectations While Infosys Faces Margin Squeeze
Tata Consultancy Services ($TCS) reported a 14.8% jump in quarterly net profit, clinching a multi-million dollar banking deal. Margins expanded by 80 bps.
On the other hand, Infosys ($INFY) missed revenue expectations due to discretionary spending cuts. Operating margin came down to 20.5% and management announced plans to trim hiring.
"""
result = analyzer.analyze_article(article)
# Overall verdict
print(f"Overall: {result.overall_sentiment.label.value} (Score: {result.overall_sentiment.bull_bear_score:+.2f})")
# Per-stock attribution
for ticker, entity in result.entities.items():
print(f"{ticker}: {entity.sentiment.label.value} (Score: {entity.sentiment.bull_bear_score:+.2f})")
# Output:
# TCS: BULLISH (+0.31)
# INFY: BEARISH (-0.11)
# Catalysts identified
for cat in result.catalysts:
print(f"[{cat.catalyst.value}] '{cat.matched_phrase}' -> {cat.sentiment_bias.value}")The project includes built-in loaders for the Financial PhraseBank v.1.0 dataset (Malo et al., 2014) from Aalto University School of Business.
| File | Agreement Tier | Sentences | Best Use Case |
|---|---|---|---|
FinancialPhraseBank/Sentences_AllAgree.txt |
100% | 2,264 | Gold-standard test benchmark; high-precision trading signals with zero human disagreement. |
FinancialPhraseBank/Sentences_75Agree.txt |
β₯ 75% | 3,453 | Recommended training balance; high signal-to-noise ratio with strong sample diversity. |
FinancialPhraseBank/Sentences_66Agree.txt |
β₯ 66% | 4,217 | Intermediate consensus dataset. |
FinancialPhraseBank/Sentences_50Agree.txt |
β₯ 50% | 4,846 | Full majority-vote dataset. |
all-data.csv |
β₯ 50% | 4,846 | CSV-formatted equivalent of 50Agree (sentiment,sentence). |
from fin_sentiment import PhraseBankLoader
loader = PhraseBankLoader()
# Load 100% agreement gold standard
gold_items = loader.load_agreement_txt(min_agreement=1.00)
# Load CSV dataset
csv_items = loader.load_csv("all-data.csv")
# Create stratified train / val / test splits
train, val, test = loader.create_splits(gold_items, test_size=0.15, val_size=0.15)Run the automated training script:
python examples/train_baseline.py| Metric | Score |
|---|---|
| Overall Accuracy | 85.14% |
| Balanced Accuracy | 79.40% |
| Macro F1-Score | 0.8074 |
| Weighted F1-Score | 0.8502 |
| Directional Accuracy (Bull vs Bear) | 75.51% |
| Training Time | ~1.8 seconds on CPU |
| Inference Latency | < 1ms per sentence |
For maximum contextual understanding, use the transformer training template:
python examples/train_transformer.py# Analyze a sentence
fin-sentiment analyze "Nvidia revenue surged 122% on data center AI chip demand"
# Output as JSON for integration into pipelines
fin-sentiment analyze "Federal Reserve signals interest rate pause" --json
# Analyze an entire article file
fin-sentiment analyze-article data/sample_article.txt --ticker AAPL
# Train a baseline model on 75% agreement tier
fin-sentiment train --agreement 0.75 --save-path checkpoints/custom_baseline.joblibRun the comprehensive unit test suite:
pytest -vAll 25 unit tests verify:
- Ingestion of
.csvand@-separated.txtfiles across encodings. - Financial decimal and abbreviation preservation.
- Cashtag, exchange ticker, and corporate entity extraction.
- Catalyst detection and directional scoring boost.
- Baseline model fitting, inference, and serialization.
- Financial metrics computation.
- End-to-end article sentiment analysis.
We actively welcome open-source contributions from quantitative researchers, NLP engineers, and algorithmic traders!
- Read our Contribution Guidelines for developer setup, testing protocols, and PR workflows.
- Explore our Architectural Audit & Roadmap (GAPS_AND_ROADMAP.md) for in-depth documentation of known linguistic edge cases, multi-entity attribution gaps, and open engineering tickets.
- Check our active GitHub Issues to pick up a task!
- Library Code: Released under the Apache 2.0 License.
- Financial PhraseBank Dataset: The raw dataset files in
FinancialPhraseBank/are authored by Malo et al. (Aalto University School of Business) and licensed under CC BY-NC-SA 3.0. If using for academic or research purposes, please cite:Malo, P., Sinha, A., Korhonen, P., Wallenius, J., & Takala, P. (2014). Good debt or bad debt: Detecting semantic orientations in economic texts. Journal of the Association for Information Science and Technology, 65(4), 782-796.