Wuming 無名

Blog

Three Months of a Small Site (2): More Than One Clock

· Technology · English

Ink-wash illustration for “Three Months of a Small Site (2): More Than One Clock”

Previous: Why Build a Premium Tracker | Next: Red for Up, or Green for Up? | 中文版:世界上不止一个时钟

Before building this site I assumed the hard part would be calculating the premium. It turned out the hard part was time.

A fund that invests overseas trades at home, but its holdings sit in the US, Japan, Korea, Taiwan and Hong Kong. Every market has its own opening hours, its own lunch break, its own holidays. When it is daytime at home, Wall Street is asleep; by the time New York opens, the home market has closed.

The same “now” means something completely different in each market.

Two modes

The answer was to split the pages into two modes: live and history.

Live mode answers “roughly what is it worth right now”: open markets use their latest prices, closed markets use their last close. History mode answers “what was it worth at yesterday’s close”: every market uses its settled closing price, giving a number that will not change again.

The two numbers differ, and they should. Mixing them up was the most common early confusion: someone saw the estimate differ between morning and evening and assumed it was wrong, when in fact their “now” had changed.

Closed means rest

A set of scheduled jobs fetches prices every few minutes. Early on they were very diligent: when the timer went off, they ran, whether the market was open or not.

One day I noticed the server’s processor spiking now and then. The culprit was the job fetching US prices. It was five in the morning in New York; the market had long closed, yet the job kept fetching a price that would not change and redoing every calculation each time.

The fix was as simple as the idea: check the calendar before doing the work. Fetch live prices only while a market is open; after the close, fetch the closing price once when it is needed. Holidays need their own list, because every market has different ones and they change every year.

Stale prices

A subtler problem: a price that is “correct” but old.

Some data sources hand out a pre-market or after-hours quote overnight. It looks fresh, but it is a number from hours ago. Using it for a live estimate quietly skews the result, only a little, which is exactly why it is hard to spot.

The fix was to carry “when is this from” with every price, and to stop treating a price as live once it is too old. Better to show “no recent update” than a number that looks precise but is stale.

Eight hours off

The last one is an old friend: time zones.

For a while the “updated at” time on the page was always eight hours behind. The data was fine and the arithmetic was fine; the display simply treated universal time as Beijing time. This kind of bug changes no calculation, but it does a lot of damage to trust: a reader who sees “updated eight hours ago” naturally starts doubting the whole page.

Takeaways

For anything that spans markets, time is the first-class problem. A few lessons:

  • Be clear about whose “now” you mean;
  • Scheduled jobs should check the calendar before working;
  • Every number carries its time, and admits it when it is old;
  • Times shown to people are converted to the reader’s time zone.

The next post is about softer things: colours, heights, and an error message that should never have appeared.

Comments

  1. Loading comments…

Leave a comment

Plain text; links are fine. Comments with many links wait for review.