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 AI in SOC Workflows: Beyond Playbooks, Into Autonomous Defense (2026 Guide)
- 从零构建SOC:Wazuh + IRIS-web 真实项目实战报告
- Building a SOC from Scratch: A Real-World Wazuh + IRIS-web Field Report
- 中国品牌出海东南亚:支付、物流与ERP全链路集成技术方案
- 再生资源工厂管理系统:中国回收企业如何在不知不觉中蒙受损失
- 如何将电商平台与ERP系统打通:实战指南(2026年版)
- AI 编程助手到底在用哪些工具?(Claude Code、Codex CLI、Aider 深度解析)
- 使用 Wazuh + 开源工具构建轻量级 SOC:实战指南(2026年版)
- 能源管理软件的ROI:企业电费真的能降低15–40%吗?
- The ROI of Smart Energy: How Software Is Cutting Costs for Forward-Thinking Businesses
- How to Build a Lightweight SOC Using Wazuh + Open Source
- How to Connect Your Ecommerce Store to Your ERP: A Practical Guide (2026)
- What Tools Do AI Coding Assistants Actually Use? (Claude Code, Codex CLI, Aider)
- How to Improve Fuel Economy: The Physics of High Load, Low RPM Driving
- 泰国榴莲仓储管理系统 — 批次追溯、冷链监控、GMP合规、ERP对接一体化
- Durian & Fruit Depot Management Software — WMS, ERP Integration & Export Automation
- 现代榴莲集散中心:告别手写账本,用系统掌控你的生意
- The Modern Durian Depot: Stop Counting Stock on Paper. Start Running a Real Business.
- AI System Reverse Engineering:用 AI 理解企业遗留软件系统(架构、代码与数据)
- AI System Reverse Engineering: How AI Can Understand Legacy Software Systems (Architecture, Code, and Data)













