Estimating Short-Term Price Direction with Heuristics and News Sentiment (Python)
Predicting market movements is famously hard. Instead of claiming “AI will predict prices,” this Python script takes a more honest and practical approach:
Estimate the probability that price will move up or down in the near term using transparent heuristics.
This article explains the script’s design, the signals it blends, and how you can extend it.
Why a heuristic instead of a prediction model?
This script:
- ❌ does not predict price targets
- ❌ does not promise alpha
- ✅ produces a probabilistic directional bias
- ✅ remains interpretable
- ✅ combines price action + sentiment
That makes it useful for:
- trade-bias confirmation
- risk context and dashboards
- human-in-the-loop decision systems
- education and research
High-level architecture
The script estimates Prob Up vs Prob Down from five signal groups:
| Signal type | What it captures |
|---|---|
| Momentum | Recent returns (1, 5, 20 bars) |
| RSI | Overbought/oversold context |
| Trend | Price vs EMA(20) |
| News sentiment | Headline polarity (keyword or LLM) |
| Macro bias | Gold-aware tilt from macro keywords |
These are blended into a single score, then mapped into probabilities via a logistic function.
Step 1: Market data → momentum features
The script fetches OHLCV data and requires enough history (e.g., 30+ bars) to compute indicators.
It derives simple momentum features:
- ret1 = 1-bar return
- ret5 = 5-bar return
- ret20 = 20-bar return
Momentum is intentionally “plain”—no complex pattern mining—because the goal is explainable bias, not overfit prediction.
Step 2: RSI as context (not a hard rule)
RSI(14) is computed (commonly via EMA-smoothed gains/losses) and converted into a continuous score.
Instead of rigid thresholds (“RSI > 70 = sell”), the script normalizes RSI around 50:
- RSI above 50 contributes bullish bias
- RSI below 50 contributes bearish bias
This makes RSI compatible with other numeric signals.
Step 3: Trend via EMA(20)
A short-term trend check compares current price to EMA(20):
- price above EMA(20) → mild bullish tilt
- price below EMA(20) → mild bearish tilt
This answers a simple question:
Is price trading above or below its recent trend baseline?
Step 4: News sentiment (two modes)
Mode A: Keyword-based sentiment (fast + deterministic)
Headlines are scored using a whitelist/blacklist of words.
Bullish examples
- beats, upgrade, strong, record
Bearish examples
- miss, downgrade, lawsuit, warning
Scores are normalized to roughly [-1, +1].
Mode B: Optional LLM sentiment (Ollama)
If enabled, the script uses a local LLM (Ollama) to classify news text as:
- bullish
- neutral
- bearish
LLM mode is optional and meant to stay:
- local
- auditable
- supportive (not dominant)
Step 5: Macro keyword tilt (gold-aware)
For gold-related symbols (e.g., XAU, GOLD, GC=F), macro context matters.
The script boosts sentiment when headlines mention:
- war / conflict
- central bank buying
- inflation
- debt
- rate cuts
- currency debasement
And penalizes:
- rate hikes
- hawkish policy
- strong dollar
This prevents an equity-centric sentiment model from misreading commodity drivers.
Step 6: Blending into a score
Signals are combined with simple weights (example):
- momentum dominates (ret5, ret20)
- RSI and trend add context
- news sentiment supports (doesn’t lead)
This weighted score is intentionally conservative: no single feature should overwhelm the result.
Step 7: Score → probability
A logistic (sigmoid) function converts the score into probabilities:
- prob_up = sigmoid(score × scale)
- prob_down = 1 − prob_up
Logistic mapping is useful because it:
- stays bounded between 0 and 1
- responds smoothly to changing signals
- naturally represents uncertainty
Many implementations also cap extremes (e.g., 1%–99%) to avoid false certainty.
Example output (how to read it)
A typical run might produce:
- Prob Up: 63%
- Prob Down: 37%
With a breakdown like:
- ret5 positive
- ret20 positive
- RSI moderately above 50
- price above EMA(20)
- news sentiment slightly bullish
Interpretation:
“Given recent momentum, trend, and news, upward movement is more likely than downward—yet uncertainty remains.”
What this script is (and isn’t)
✅ Good for
- directional bias / confirmation
- trade filtering (only act when bias is strong)
- risk dashboards / monitoring
- research and education
❌ Not for
- price targets
- high-frequency trading
- fully automated execution
- “guaranteed” prediction claims
Practical extensions
If you want to evolve the script while keeping it interpretable:
- volatility-adjusted weighting (scale returns by ATR or realized vol)
- regime detection (trend vs mean-reversion)
- time-decay for news (fresh headlines matter more)
- symbol-specific calibration (gold vs stocks vs crypto)
- portfolio aggregation (bias across multiple instruments)
Closing thoughts
This script is a solid example of post-hype engineering:
- transparent
- interpretable
- honest about uncertainty
Instead of asking “Can AI predict the market?”, it asks a better question:
Given what we know right now, which direction is more plausible?
Get in Touch with us
Related Posts
- Agentic Commerce:自主化采购系统的未来(2026 年完整指南)
- Agentic Commerce: The Future of Autonomous Buying Systems (Complete 2026 Guide)
- 如何在现代 SOC 中构建 Automated Decision Logic(基于 Shuffle + SOC Integrator)
- How to Build Automated Decision Logic in a Modern SOC (Using Shuffle + SOC Integrator)
- 为什么我们选择设计 SOC Integrator,而不是直接进行 Tool-to-Tool 集成
- Why We Designed a SOC Integrator Instead of Direct Tool-to-Tool Connections
- 基于 OCPP 1.6 的 EV 充电平台构建 面向仪表盘、API 与真实充电桩的实战演示指南
- Building an OCPP 1.6 Charging Platform A Practical Demo Guide for API, Dashboard, and Real EV Stations
- 软件开发技能的演进(2026)
- Skill Evolution in Software Development (2026)
- Retro Tech Revival:从经典思想到可落地的产品创意
- Retro Tech Revival: From Nostalgia to Real Product Ideas
- SmartFarm Lite — 简单易用的离线农场记录应用
- OffGridOps — 面向真实现场的离线作业管理应用
- OffGridOps — Offline‑First Field Operations for the Real World
- SmartFarm Lite — Simple, Offline-First Farm Records in Your Pocket
- 基于启发式与新闻情绪的短期价格方向评估(Python)
- Rust vs Python:AI 与大型系统时代的编程语言选择
- Rust vs Python: Choosing the Right Tool in the AI & Systems Era
- How Software Technology Can Help Chanthaburi Farmers Regain Control of Fruit Prices













