OilPriceAPI guides

Point-in-time oil price history for backtests

By OilPriceAPI · Behaviour checked against the live API and the docs on November 23, 2026

A backtest is only honest if each decision uses what was knowable at the time. Price history gets in the way: a series you download today can contain rows that did not exist on the day you pretend to trade, and source revisions can change values after the fact. The API's as_of parameter gives you the series as it stood at an instant you choose. This post shows how it behaves, with a script you can run, and where it stops being useful.

What as_of does

Add as_of=<ISO 8601> to past_day, past_week, past_month or past_year and the response contains rows as they stood at that instant, with later source revisions rolled back. Two headers echo what was applied: X-Vintage-As-Of is the instant in UTC (a bare date becomes the end of that day) and X-Vintage-Revision-Coverage-Since is the date from which revisions are recorded. On November 23, 2026 that header read 2026-07-28. For an earlier vintage the docs say you get rows as first collected, so treat anything before that date as "first print", not as a reconstructed revision history.

Rules I confirmed against the live API today: the parameter needs interval=raw (a daily request returns 400 with "as_of requires interval=raw"), a future instant returns 400, and an unparseable value returns 400 with the accepted formats. On past_year, supplying as_of without an interval already returns raw rows.

The window is anchored to today

This is the part that trips people up. The window name is relative to the moment you call, not to as_of. I asked past_week for as_of=2026-08-15T12:00:00Z on November 23. The window was November 16 to 23, no row in it existed on August 15, so the prices array was empty and the API reported empty_window with the last row it had seen before the vintage. Nothing was wrong; the question just had no overlap.

For a vintage that is weeks or months old, use past_year: its window is long enough to reach back, and it defaults to raw rows when as_of is set. The same call with as_of=2026-08-15T12:00:00Z returned rows from August 14 and 15. Raw pages hold up to 100 rows, newest first, and page=2 returns the next 100. I fetched both pages for a November 10 vintage on past_month.

Run it: the last price known at each decision time

Set OILPRICEAPI_KEY, save the script as known_at.py and run python3 known_at.py. It asks for the newest WTI_USD row that existed at three instants. Because pages are newest first, the first row of the first page is the answer; you only need more pages if you want the whole series as of that instant.

curl -sD - -H "Authorization: Token $OILPRICEAPI_KEY" \
  "https://api.oilpriceapi.com/v1/prices/past_year?by_code=WTI_USD&interval=raw&as_of=2026-10-01T00:00:00Z"
import json
import os
import sys
from urllib.error import HTTPError
from urllib.parse import urlencode
from urllib.request import Request, urlopen

KEY = os.environ.get("OILPRICEAPI_KEY")
if not KEY:
    sys.exit("Set OILPRICEAPI_KEY first")

def known_at(code, instant):
    """Newest row that existed at `instant` (UTC ISO 8601), or None."""
    query = urlencode({"by_code": code, "interval": "raw", "as_of": instant})
    request = Request(
        "https://api.oilpriceapi.com/v1/prices/past_year?" + query,
        headers={"Authorization": "Token " + KEY},
    )
    try:
        with urlopen(request, timeout=15) as response:
            vintage = response.headers.get("X-Vintage-As-Of")
            since = response.headers.get("X-Vintage-Revision-Coverage-Since")
            body = json.load(response)
    except HTTPError as error:
        if error.code == 402:
            sys.exit("History needs a paid plan or an active trial")
        detail = error.read().decode("utf-8", "replace")[:200]
        sys.exit(f"HTTP {error.code}: {detail}")
    rows = body.get("data", {}).get("prices", [])
    if not rows:
        return None, vintage, since
    # Pages are newest first, so row 0 is the last observation before `instant`.
    return rows[0], vintage, since

for instant in ("2026-08-03T00:00:00Z", "2026-09-01T00:00:00Z", "2026-10-01T00:00:00Z"):
    row, vintage, since = known_at("WTI_USD", instant)
    if row is None:
        print(instant, "no rows existed at that instant")
        continue
    print(instant, "->", row["price"], row["currency"], "observed", row["created_at"],
          "| vintage", vintage, "| revisions tracked since", since)

On November 23, 2026 it printed 80.73 USD for the August 3 vintage (observed August 2, 23:55 UTC), 86.37 USD for September 1 and 90.03 USD for October 1, each with the vintage header echoed back. Your numbers should match those if you run it, because past observations do not change unless a source revises them. A WTI_USD row is NYMEX front-month futures, not a physical spot assessment; the API says so in instrument_disclosures on the same response, and a backtest should record that.

What the vintage hides

I checked one day to see the effect. Asking past_day?at=2026-08-12 for WTI_USD returned 100 rows (the page cap). The same day with as_of=2026-08-15returned the same rows with identical values. With as_of=2026-07-29 it returned none, because none of those rows had been collected yet. That is the look-ahead guard in its simplest form: a vintage cannot see rows from its own future.

I did not find a revised WTI_USD value in that sample, so this post does not show one. Revisions are rare for a code that comes from a live market feed; they matter more for series built from weekly or monthly source publications. If your strategy depends on them, run the same query at two vintages on either side of a known publication date and diff the rows before you trust the result.

Limits to plan around

  • /v1/prices/historical did not accept as_of when I tried it: it returned 400 UNKNOWN_PARAMETERS. Point-in-time lives on the four past_* endpoints.
  • Raw pages are capped at 100 rows, so a month of five-to-ten minute points takes several pages. Cache what you fetch.
  • Aggregates cannot be vintage-corrected row by row, so build daily bars yourself from the raw rows you retrieved.
  • Revision tracking starts 2026-07-28. A backtest whose revision sensitivity matters needs a longer record than the API holds.
  • History needs a paid plan or an active trial. A Free-plan key gets 402. On a paid plan, the Developer plan covers the last 365 days, Starter five years, and Professional and Scale the full archive, per the historical prices reference.

Is it free?

Free plan: 50 requests/day, with no expiry. New accounts start with a 7-day trial: 10,000 requests, full API (except licensed datasets: aviation fuel pilot), no card. After the trial, you automatically continue on the Free plan. The Free plan is hard-capped and never billed.

Point-in-time history is not part of the Free plan; it works during the trial and on paid plans. See the historical oil prices API page for coverage, the historical data guide for the other endpoints, and pricing for plan depth.