Skip to content

Instantly share code, notes, and snippets.

@MaleicAcid
Forked from TellonUK/H1
Created September 24, 2024 15:58
Show Gist options
  • Select an option

  • Save MaleicAcid/f745ca891079149a845af003232a5e02 to your computer and use it in GitHub Desktop.

Select an option

Save MaleicAcid/f745ca891079149a845af003232a5e02 to your computer and use it in GitHub Desktop.
//@version=5
indicator("Uptrend, PB, and H1 Identification with ADR Condition", overlay=true)
// Initialize variables to track state
var bool inUptrend = false
var bool inPB = false
var bool inPB2 = false
var bool inH1 = false
var float firstUpHigh = na
var int pbBarIndex = na
var int pbBarIndex2 = na
// Initialize flags for plotting
var bool plotUp = false
var bool plotPB = false
var bool plotH1 = false
var bool plotH2 = false
// Calculate the Average Daily Range (ADR) over a specified period (e.g., 10 days)
adrPeriod = 10
adr = ta.sma(high - low, adrPeriod)
// Define the conditions for an "Up" bar (part of an uptrend)
// All bars in the uptrend sequence must be bull bars (close > open)
// Additionally, there must be a microgap, where the low of one bar is higher than the high of the bar two bars before it
uptrend = (low > low[1] and low[1] > low[2] and low[2] > low[3] and low[3] > low[4]) and
(close > open and close[1] > open[1] and close[2] > open[2] and close[3] > open[3]) and
((low > high[2]) or (low[1] > high[3]) or (low[2] > high[4]) or (low[3] > high[5]))
// Only proceed if ADR is greater than 4
if (adr > 4)
// Mark the bar as "Up" if it meets the conditions
if (uptrend and not inUptrend)
inUptrend := true
firstUpHigh := high[4] // Track the high of the bar 4 bars before the uptrend started
plotUp := true
// Check if the current bar's high is lower, indicating a Pullback (PB)
if (inUptrend and not inPB)
if (high < high[1])
inPB := true
pbBarIndex := bar_index
plotPB := true
// Check if in a PB and the current bar's high is higher than the previous bar, indicating an H1
if (inPB)
if (high > high[1])
plotH1 := true
inPB := false // Exit PB state if an H1 is found
inUptrend := false
// Reset conditions if the current bar's low goes below the first uptrend high
if (low < firstUpHigh)
inUptrend := false
inPB := false
firstUpHigh := na
pbBarIndex := na
// Plotting shapes outside the local scope
plotshape(series=plotUp, title="Up", location=location.belowbar, color=color.blue, style=shape.triangleup, text="Up")
plotshape(series=plotPB, title="PB", location=location.abovebar, color=color.red, style=shape.triangledown, text="PB")
plotshape(series=plotH1, title="H1", location=location.abovebar, color=color.green, style=shape.triangledown, text="H1")
plotshape(series=plotH2, title="H2", location=location.abovebar, color=color.green, style=shape.triangledown, text="H2")
// Reset flags for the next iteration
plotUp := false
plotPB := false
plotH1 := false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment