News sentiment, a new endpoint measured before it is drawn
By the end of this lesson you will have the sentiment endpoint in the proxy's allowlist and the cost table in one commit, a mock that answers in the API's own shapes with its empty array included, and a panel that ranks a few symbols by news tone, says how many articles each reading rests on and shows no reading under twenty.
Every panel so far drew data the cockpit already fetched. This one needs an endpoint the proxy has never forwarded, so it starts where the proxy's own comment says a new endpoint starts: in the allowlist and the cost table, in the same commit.
Two lines and a measurement
ALLOWED_ENDPOINTS in src/lib/proxy.ts is an allowlist, and the comment above it asks for the endpoint's cost in src/lib/cost.ts in the same commit. That cost was measured on 13 November 2026 against a live account's counter: sentiment for one symbol cost 5 calls, for two symbols 10. It is priced per symbol, like news per ticker. News and sentiment are on the Free plan, where 20 calls a day buys four symbols' sentiment and nothing else.
The answer's shape, and its trap
One request names its symbols in s and gets an object keyed by symbol, each a list of days, newest first:
{"AAPL.US": [{"date": "2026-11-13", "count": 55, "normalized": 0.8246},
{"date": "2026-11-12", "count": 72, "normalized": 0.7357}]}
count is how many articles mentioned the symbol that day and normalized is their tone, from −1, negative, to 1, positive. About a month of days comes back unless from and to narrow it. A symbol the API has nothing for is left out of the object, and when none of the symbols has anything the body is [], an empty array. The parser accepts both shapes, and so must the mock: a mock that only ever answers with an object hides the array until a real key meets it.
The recipe
From the vendor/sentiment.js file of eodhd-news-sentiment:
- Tone is the mean of the daily
normalized, weighted bycount, so a day with ninety articles counts ninety times as much as a day with one. A day whose count is 0 or missing weighs 1. - Trend splits the days into a newer half and an older half and compares their tones: more than 0.05 apart is up or down, anything closer is flat.
Measure before you draw bands
On 13 November 2026 we asked for a month of twelve large symbols: Apple, Amazon, Boeing, Alphabet, Intel, Microsoft, Nike, NVIDIA, Tesla, UnitedHealth, the SPY fund and Vodafone. Their article-weighted tone ran from 0.57 (Intel) to 0.87 (SPY). Put on a dial from 0 to 100 with 50 as neutral, that is 79 to 94, and a band that starts at 65 holds all twelve.
A fixed band would say the same word about every one of them. What does tell them apart is how they compare with each other, and how a symbol's newer half of the month compares with its older half. So the panel shows tone as a number on its own −1 to 1 scale, sorted highest first, with the trend beside it, and draws no bands.
How many articles make a reading
Vodafone came back with seven days and eight articles in the month, one of its days at −0.60, and a tone of 0.77. Eight articles are an anecdote. The panel shows no tone below 20 articles in the window, says so ("8 articles: too few to read"), and sorts those rows last. Twenty is a threshold you chose: write it as a named constant with a test, so the next reader can see it was chosen.
A panel priced per symbol does not follow
This panel costs 5 calls per symbol, so one that followed the selection would spend 5 calls on every click. Its follows is false and it holds the one to five symbols you gave it. The container unions the symbols of every sentiment panel into one request and keeps the answer for an hour: the data has one row per day, and a copy an hour old still says what the day says.
Try it now
Add the endpoint with its price, teach the mock its shapes, then build the panel on top, all from one prompt.
The prompt
Mock mode needs an answer to draw on, and ours is the live one of 13 November 2026 for Apple, Microsoft, NVIDIA and Vodafone. With the starter as your upstream remote, run git fetch upstream and then git checkout upstream/ref/build-a-dashboard/u2 -- fixtures/sentiments.json. Then mock mode on, plan mode on, and paste:
Plan, then build, a sentiment panel. First the endpoint, in one
commit: sentiments in ALLOWED_ENDPOINTS in src/lib/proxy.ts, and in
CALL_COST in src/lib/cost.ts at 5 calls per symbol, priced per symbol
like news. The answer is an object keyed by symbol, each a list of
days, newest first, with date, count and normalized; a symbol with
nothing is left out, and when none has anything the body is [], an
empty array. Teach src/lib/mock.ts to answer from
fixtures/sentiments.json in that shape, the empty array included. In
a new src/lib/sentiment.ts: tone(days), the mean of normalized
weighted by count, a count of 0 weighing 1 and a missing normalized
skipped; articles(days); trend(days), the newer half of the days
against the older, up or down past 0.05, flat otherwise; and one
reading per symbol that shows no tone below MIN_ARTICLES = 20. No
fear or greed bands. Write tests/sentiment.test.ts first. Add a
sentiment panel type: one to five symbols, follows false, dataNeeds
asking for sentiment for them. The container unions those symbols
across panels into one request and keeps the answer for an hour. The
panel's table sorts by tone, highest first, says how many articles
each reading rests on, and puts the too-few rows last. Add it to the
Markets layout on two symbols. Tell me with estimateCalls what one
load of Markets costs now. Mock mode. Run npm test, typecheck, lint
and check:leak, and show me the diff.
A good answer changes proxy.ts, cost.ts, mock.ts, panels.ts, the container and the layout, and adds sentiment.ts, the panel and the test. The agent's price for one load of Markets should be the screen's 5, the quote's 1 and 5 for each sentiment symbol.
Check it yourself
- The new tests failed first and pass now; one of them prices two symbols' sentiment at 10 calls.
- In mock mode the Markets layout shows the sentiment panel with Microsoft above Apple, each with its tone and a four-digit article count.
- Ask the agent to add Vodafone to the panel's symbols: its row says it has 8 articles, too few to read, and sits last.
- Ask the agent to request sentiment for a made-up ticker through your proxy in mock mode: the answer is
[], as the live API's is. - Ask the agent to put a second sentiment panel, on Microsoft and NVIDIA, into the layout, and reload with the Network tab open: one sentiment request asks for all three symbols, Microsoft once. Take the second panel out again.
Compare with ours
// src/lib/sentiment.ts: tone is weighted by articles, and twenty is a choice
export const MIN_ARTICLES = 20;
export function tone(days: SentimentDay[]): number | null {
let sum = 0;
let weight = 0;
for (const d of days) {
if (d.normalized === null || !Number.isFinite(d.normalized)) continue;
const w = Math.max(1, d.count); // a day with no articles weighs 1
sum += d.normalized * w;
weight += w;
}
return weight > 0 ? sum / weight : null;
}
The full state at the end of this unit is on the starter's branch ref/build-a-dashboard/u2.
Checkpoint
npm test && npm run typecheck
git status # the files you expect, and no .env file
git add -A
git commit -m "Sentiment panel: a new endpoint priced per symbol, tone weighted by articles, nothing under twenty"
Then answer this lesson's questions.