Analyzing HTF on Intraday Bars
This lesson demonstrates how to get the bar index of the intraday bar that created the previous day's higher-timeframe high. Once we have that bar index, we can analyze the bar however we like.
Check out my other free lessons!Source Code
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // www.PineScriptMastery.com // @version=5 indicator("PineScriptMastery.com", overlay=true) // 1. Get the HTF high value // 2. Calculate how many bars we need to look back (intraday bar count of today + total bar count of yesterday) // 3. Loop from the start of yesterday's session to today's current bar // 4. Check if the looped bar's high == HTF_High, if so, save the loop count and exit loop // 5. Subtract loop count from the current bar_index to get the bar_index when our HTF condition was met // Get HTF period string HTF_Period = input.timeframe(title="Higher Timeframe", defval="1D") // Get HTF's high without repainting marketHTF = barstate.isrealtime ? 1 : 0 marketCTF = barstate.isrealtime ? 0 : 1 float Prev_High = request.security(syminfo.tickerid, HTF_Period, high[marketHTF])[marketCTF] // Count how many bars printed on the previous HTF session newSession = ta.change(time(HTF_Period)) var barsThisSession = 0 var barsLastSession = 0 if not newSession barsThisSession := barsThisSession + 1 else barsLastSession := barsThisSession + 1 barsThisSession := 0 // Calculate bars per HTF bar int barsTotalHTF = barsThisSession + barsLastSession // Loop through all intraday bars from yesterday to today and check if intraday condition is met, then save bar count // POST-VIDEO EDIT: We don't need to loop to 0, we can just loop to barsThisSession and skip today's bars. barCountSinceHigh = 0 for i = barsTotalHTF to 0 if high[i] == Prev_High barCountSinceHigh := i break // Subtract bar count from bar that created high int intradayBarIndex = bar_index - barCountSinceHigh // Draw HTF high & bar index plot(Prev_High, "Previous HTF Bar's High", color.red) plotchar(bar_index, "Current Bar Index", "", color=color.new(color.blue,100)) plotchar(high - low, "Current Bar Size", "", color=color.new(color.green,100)) // Draw debug label if barstate.islast barSize = high[barCountSinceHigh] - low[barCountSinceHigh] label htfLabel = label.new(bar_index, high + syminfo.mintick * 500, "Market Type=" + syminfo.type + "\n\nIntrabar Count=" + str.tostring(barsThisSession) + "\nBars Per HTF=" + str.tostring(barsLastSession) + "\nBars To Check=" + str.tostring(barsTotalHTF) + "\nBars Since High=" + str.tostring(barCountSinceHigh) + "\n\nBar Index of High=" + str.tostring(intradayBarIndex) + "\n\nBar Size=" + str.tostring(barSize), color=color.black, textcolor=color.white)