Skip to content

A weekly value on a daily chart ​

How do you show a weekly average on a daily chart so the past bars don't show values nobody had yet? Read it with request.security. In Pine you choose between two forms. The default matches what history saw but moves on a live chart until the week closes. The week before, read with lookahead on, never moves, but shows each week's value one daily bar later. The AmenoScript form further down has neither cost.

pine
//@version=6
indicator("Weekly trend", overlay = true)
int len = input.int(10, "Weekly SMA length", minval = 1)

// The week closed by each bar's close; live, the week still forming
float weekly = request.security(syminfo.tickerid, "1W",
  ta.sma(close, len))
// The week before, read with lookahead: never moves, even live
float steady = request.security(syminfo.tickerid, "1W",
  ta.sma(close, len)[1], lookahead = barmerge.lookahead_on)

plot(weekly, "Weekly SMA", color.orange, 2)
plot(steady, "Week before", color.gray)
color tint = close > weekly ? color.teal : color.red
bgcolor(color.new(tint, 90))

How it works ​

  • request.security(syminfo.tickerid, "1W", expr) runs expr on the chart symbol's weekly bars and hands each daily bar the value it can see.
  • The ta.sma runs inside the request, so it averages weekly closes. Written outside, it would average daily closes.
  • The default, lookahead off, gives each daily bar in history the week that had closed by its close, so a week's own average first shows on its last daily bar. On a live chart it follows the week still forming, so the last value moves until the week closes.
  • [1] with lookahead_on reads the week before the one in progress. It never moves, live or in history. It trails the default only on each week's last daily bar, where the default already shows that week.

The same in AmenoScript ​

An AmenoScript run is leak-safe: a request reads the last closed week on every bar, live too. So it shows the default form's history, and its last value never moves. lookahead_on is refused. max_age and request.age are the lines that make this script AmenoScript, and they also check how fresh the week is:

ameno
indicator("Weekly trend, leak-safe", overlay = true)
int len = input.int(10, "Weekly SMA length", minval = 1)
// na if the last closed week is more than 14 days old
float weekly = request.security(syminfo.tickerid, "1W",
  ta.sma(close, len), max_age = 14 * 86400)
plot(weekly, "Weekly SMA", color.orange, 2)
plot(request.age(syminfo.tickerid, "1W") / 86400, "Days since it closed",
  display = display.data_window)

Traps ​

lookahead_on without [1] reads the future

request.security(..., close, lookahead = barmerge.lookahead_on) shows each week's final close from the week's first day. A backtest on it trades on prices nobody had. Use lookahead on only with [1].

  • A forming week moves on a live chart. With lookahead off, a Pine script's last weekly value changes until the week closes. The [1] form and the AmenoScript form never do.

See also ​

AmenoScript is compatible with Pine Script v6. Pine Script is a trademark of TradingView, Inc., which has no part in AmenoScript.