News Sentiment: Hourly Scores for Stocks, Crypto & Pakistan

Outcome

Four Docker containers on one Linux server keep it current: PostgreSQL, the hourly live job, a backfill that refills any GDELT file the live job missed, and the dashboard. About 12 million headlines are stored, giving a sentiment number per asset per hour since January 2021, with no look-ahead and a GDELT-only view that stays consistent throughout. A companion Pakistan Stock Exchange trading app reads the Pakistani series through a keyed endpoint.

Overview

An hourly news-sentiment score for 18 assets, from January 2021 to the current hour: ten US stocks, five cryptocurrencies and the crypto market as a whole, and Pakistan's stock market and economy. It was built for trading research, with no paid data. Major outlets' terms forbid scraping, so the news comes from GDELT's 15-minute files of worldwide news, free for any use, and from the RSS feeds of 12 crypto and 6 Pakistani business outlets, read under terms that allow personal, non-commercial use. Each article is matched to assets by pattern and its English headline scored from −1 to +1 on CPU, beside GDELT's full-text finance-dictionary score.

The history is 403,338 GDELT files, about 2.8 TB, parsed in memory and never kept. Each file's articles go into PostgreSQL in one transaction with its log entry, so a crash or outage never leaves half a file. Lone passing mentions ("apple" in a recipe) were 52% of stored articles, nearly all noise, so a GDELT article counts toward an asset only if its headline names it or its text does twice. Articles are keyed by URL: when GDELT's copy of a story stored from RSS arrives hours later, the row takes GDELT's full-text counts but keeps the RSS time. Hours are bucketed by GDELT's processing time or the feed's publish time, so no number uses later information.

The headline model, a DistilRoBERTa fine-tuned on financial news, gets 13 of 14 labelled test headlines right, where VADER got 3 of 12. But it scored "Short-term inflation rises to 11.32pc" at +1.00, so 14 models were compared on Pakistani headlines labelled by hand before any model ran; FLAME2 ranked closest to the labels (Spearman's ρ 0.78, and 0.85 on a held-out set, against the default's 0.51 and 0.70) and now scores the Pakistani feeds.

Architecture & Pipeline

  1. 01

    GDELT Ingest

    Python · 15-minute files back to 2021

    • Python
  2. 02

    RSS Ingest

    Python · 19 feeds every hour · robots.txt obeyed

    • Python
  3. 03

    Asset Matching

    Regex filter · 18 assets

  4. 04

    Sentiment Scoring

    PyTorch · DistilRoBERTa and FLAME2 models on CPU

    • PyTorch
  5. 05

    Article Store

    PostgreSQL · one row per URL

    • PostgreSQL
  6. 06

    Hourly Rollup

    PostgreSQL · aggregated on demand, weight capped at 5

    • PostgreSQL
  7. 07

    Dashboard & API

    React · Flask · two-factor sign-in · keyed API

    • Flask
    • React

End-to-end flow, in order. Scroll sideways on a narrow screen.

Key Features

  • Two jobs share the work: a live one every hour at :05 UTC reads the feeds and any GDELT file from the last 48 hours not yet stored, and a backfill at low CPU priority fills everything older back to January 2021, newest first, in runs of 400 files across 4 processes.
  • Sources picked by their terms: feeds are read only where the outlet's terms allow this use (40 crypto outlets checked), robots.txt is parsed with Protego (RFC 9309) because Python's built-in parser wrongly allowed a disallowed feed, and each site's Crawl-delay is honoured.
  • Both models are pinned to exact revisions and built into the Docker image, so nothing downloads at runtime, and a self-check fails a new pin that falls below 13 of 14 test headlines (the default model) or 159 of 204 hand-labelled Pakistani ones (FLAME2).
  • Pakistani business feeds also carry foreign news, so each has a filter pattern an item must pass to count; live, it split one feed's 50 items into 24 kept, all Pakistani, and 26 dropped, all foreign.
  • An article's weight is how often it names the asset, capped at 5, so a story about an asset outweighs a passing mention and one long article can't take over an hour.
  • Safe across restarts and outages: network errors never use up a GDELT file's three attempts, the 5,344 files GDELT never published are logged once and skipped, every download has a hard deadline so a trickling server can't stall a cycle, and a feed pages back up to 10 pages to close the gap an outage left.
  • Admin dashboard in React and Flask behind a password and two-factor sign-in, with lockout after 5 failed attempts in 15 minutes, CSRF tokens and a strict Content-Security-Policy, showing runs, feed health, sentiment charts, a 24-hour heatmap and an article browser.
  • Backtests rank each period's sentiment against the next period's return (Spearman's ρ), crypto against Coinbase prices and Pakistani news against the next KSE-100 session; over five years of sessions, with the pass rule fixed in advance, neither Pakistani series added anything beyond the previous session's return.