Black Friday 30% off Basic and ProBlack Friday −30%Ends in––d––h––m––sSee the deal

News

The economic calendar news, like jobs data, inflation and rate decisions: time, currency, impact and the actual, forecast and previous values.

High-impact news as vertical lines on the chart
One line for each hour with news, green if the data is better than forecast and red if worse, and the background on the candles with a news event.

request.events

News is requested with request.events("news"), as the value of an argument, next to the candles. The first argument is the event type: today there is only "news". The other two choose which news arrives.

Python
def calculate(
    bars=request.bars(),
    news=request.events("news", currencies=["USD", "EUR"], impacts="high"),
):
    for event in news:
        print(event.title, event.actual_text)
ArgumentWhat it choosesDefault
kindThe event type: "news"Always written
currenciesThe currencies, one or a list: "USD", "EUR", "GBP", "JPY", "CAD", "AUD", "CHF", "NZD", "CNY" or "All" for global newsNone: the symbol’s
impactsThe impacts, one or a list: "high", "medium", "low" or "none", for holidays and the likeNone: all
  • The symbol’s currencies are the same as “From chart” in the calendar: the symbol’s currency and, for a forex pair like EURUSD, the first one too. Global news, "All", arrives only if you request it.
  • The script’s news doesn’t follow the calendar filters or “Show on chart”: only currencies and impacts decide.
  • A value that doesn’t exist, like "SEK" or "huge", or an empty list is a compile error, with the allowed values.

With an input

Currencies and impacts can be chosen with an input.choice: each option is a currency or an impact, or None for the default. An input delivers a single value; to choose several together, request them all and filter in the code. As with request.bars, the input’s key is the argument name, here news.

Python
news=request.events("news", impacts=input.choice("Impact", "high", options=[
    (None, "All"),
    ("high", "High"),
    ("medium", "Medium"),
])),

The list

The function receives a list of news in time order: from the first calculated candle as far as the drawings reach, up to 500 candles after the latest one. So it also includes upcoming news, with the forecast and the previous value but without the actual.

Python
count = len(news)                          # how many news events
last = news[-1] if news else None          # the furthest ahead in time
high = [e for e in news if e.impact == "high"]
usd = [e for e in news if e.currency == "USD"]

The list and the news are read-only, like the candles: to work on a list, make a copy, list(news), or a comprehension. print(event) only writes <News>: to read its values print the fields, like print(event.title, event.actual).

What’s in a news event

FieldContent
timeThe time, in UTC milliseconds, like bars.time
currencyThe currency, or "All" for global news
impact"high", "medium", "low" or "none"
titleThe name, like "CPI m/m"
actual, forecast, previousActual, forecast and previous as numbers, nan if missing
actual_text, forecast_text, previous_textThe same as the source writes them, "" if missing
higher_is_betterTrue if a higher value is better for the currency, False if lower, None if not compared
toneThe data compared with the forecast: "better", "worse", "even" or None
idThe news code: it stays the same for its whole life, even if the time changes

All the fields are also in the news reference.

Numbers and text

The fields with _text are the values as the source writes them, to put in labels with the percentage or the K. Those without are the numbers, for calculations:

TextNumber
"0.3%"0.3, the number as written
"165K", "-1.4M", "3.25B", "1.49T"Thousands, millions, billions, trillions: 165000, -1400000…
"-$87.62B", "C$0.71B"Without the currency symbol
"<0.25%"0.25, the stated limit
"4.22|1.4"4.22: in auctions the yield counts, the first number
"", "Pass", "Yes", "2-0-7"nan: missing, not a number, or a vote

Data not yet released has actual nan and actual_text empty; when it’s released both are filled. A speech or a holiday has no numbers.

Better or worse than forecast

tone compares the actual with the forecast, or with the previous if the forecast is missing, in the direction of higher_is_better: for unemployment, jobless claims or inventories a lower value is "better". Equal values give "even". It’s the same color as the actual value in the calendar.

tone is None if the data hasn’t been released yet, if there’s nothing to compare it with or if higher_is_better is None: speeches, holidays, meetings, votes and auctions. higher_is_better is set per series, so it’s the same for every release of the same data.

Python
surprises = [e for e in news if e.tone == "better" and e.impact == "high"]
change = event.actual - event.forecast     # nan if either is missing

On the candles: match

bars.match(news) gives, for each candle, the (start, end) pair of indices of the news that falls inside it: its news is news[start:end], and a candle without news has start == end. It works with any candle request, even of another timeframe, like daily.match(news).

  • A news event belongs to the candle that opens before it, up to the open of the next one. A news event in the gap between two candles, like over the weekend, goes with the candle before, like the dots on the chart.
  • After the latest candle, only up to its end: upcoming news further ahead isn’t in any candle.
  • A filtered list works too, as long as it stays in time order: a list in another order is a ValueError, a list that isn’t news a TypeError.
Candles with news
from totaltrade.v1 import indicator, request, plot, color

@indicator(short="News", overlay=True, plots=[
    plot.background("news", "High-impact news", color=color("#F23645", 15)),
])
def calculate(bars=request.bars(), news=request.events("news", impacts="high")):
    # The background colors the candles with at least one high-impact news event.
    return {"news": [end > start for start, end in bars.match(news)]}
Python
# For each candle, how many news events and the first one.
for i, (start, end) in enumerate(bars.match(news)):
    if end > start:
        print(i, end - start, news[start].title)

Drawing them

With xloc="time" a point’s x is a time: a drawing falls at the exact time of the news, even after the latest candle. Inside a market pause the time is split proportionally between the two candles: a Saturday news event falls in the space between Friday and the reopening.

News lines
from totaltrade.v1 import indicator, request, draw, color

TONES = {"better": color("#26A69A"), "worse": color("#EF5350")}
GRAY = color("#787B86")

@indicator(short="News", overlay=True)
def calculate(bars=request.bars(), news=request.events("news", impacts="high")):
    for event in news:
        line = {"color": TONES.get(event.tone, GRAY), "pattern": "dashed"}
        # The text runs along the line, at the top; the point's price doesn't matter.
        draw.vertical_line((event.time, bars.close[-1]), xloc="time", line=line,
                           text={"value": event.title, "align": "right"},
                           show_time_label=False)

Several news events at the same time make lines on top of each other: the example News on the chart draws only one, with the number of the others.

When they change

  • When data is released the news event stays the same, with the same id: actual and tone are filled in, and the indicator recalculates by itself, as with a new candle.
  • If the source moves the time or corrects the title, currency or impact, the news event keeps its id and changes place in the list.
  • A news event cancelled by the source stays in the list until the chart is next loaded. This is rare.

When they’re missing

CaseWhat happens
The news is arrivingThe calculation waits, with the circle spinning in the legend
The chart doesn’t have newsThe list is empty and the legend warns about it
The news can’t be downloadedAn error in the legend: the indicator doesn’t draw
No news for the chosen currencies and impactsThe list is empty

Event requests count towards the request limit together with candle requests: see Limits.