I have been debugging some strange results I was getting when I stumbled on a problem where request.security is not returning consistent results. Indeed, it is massively inconsistent. The following short code demonstrates:
//@version=6
indicator("Weekly EMA50 Consistency Test", overlay = true)
// --- Request Weekly EMA(50) ---
weekly_close = request.security(syminfo.tickerid, "W", close, gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off)
weekly_ema50 = ta.ema(weekly_close, 50)
// --- Create Table ---
var table t = table.new(position.top_right, 1, 1, frame_color = color.new(color.gray, 20), frame_width = 1)
// --- Update on last bar only ---
if barstate.islast
val_txt = str.tostring(weekly_ema50, format.mintick)
table.cell(t, 0, 0, val_txt, text_color = color.white, bgcolor = color.new(color.black, 0), text_size = size.large)
This prints a single cell that contains the EMA(50) from the weekly timeframe. I would expect this number to be consistent regardless of the timeframe of the chart I am viewing it from. It isn't. I get variations of over 25% between the 5-minute chart and the 3-month chart.
Can anyone help explain what is going on and what I should do to restore consistency?
Thanks.
1 Answer 1
In your snippet, you're only requesting the close from the weekly timeframe. As for the EMA is calculated from the 50 bars of the current chart you're viewing.
You can directly get the EMA from the weekly context like so:
// --- Request Weekly EMA(50) ---
weekly_close = request.security(syminfo.tickerid, "W", ta.ema(close, 50), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off)