//@version=5 indicator(title='My Breakout', shorttitle='My Breakout', overlay=true) var prevBarIndexLo = -1 var prevBarIndexUp = -1 var barIndexLo = -1 var barIndexUp = -1 var prevFractalUp = false var prevFractalLo = false var fractalUp = false var fractalLo = false //var lineLoId = -1 isOffsetOfFractalUpStrict(offset) => high[offset] >= high[offset+2] and high[offset] >= high[offset+1] and high[offset] >= high[offset-2] and high[offset] >= high[offset-1] isOffsetOfFractalLoStrict(offset) => low[offset+2] >= low[offset] and low[offset+1] >= low[offset] and low[offset] <= low[offset-1] and low[offset] <= low[offset-2] isOffsetOfFractalUp(offset) => high[offset] >= high[offset+1] and high[offset] >= high[offset-1] isOffsetOfFractalLo(offset) => low[offset+1] >= low[offset] and low[offset] <= low[offset-1] offsetOfFractalUpFromLastOffset(offset) => result = 0 for i = 2 to 100 if isOffsetOfFractalUp(offset+i) result := offset+i break result offsetOfFractalLoFromLastOffset(offset) => result = 0 //for i = lastBarIndexFractal - 2 to 2 by -1 for i = 2 to 100 if isOffsetOfFractalLo(offset+i) result := offset+i break result readCoordinatesAtOffset(offset) => h = high[offset] l = low[offset] [offset, h, l] calulateLineUpFromOffsets(offset1, offset2) => [x1, h1, l1] = readCoordinatesAtOffset(offset1) [x2, h2, l2] = readCoordinatesAtOffset(offset2) slope = (h2-h1)/(x1-x2) [x1,h1,x2,h2,slope] calulateLineLoFromOffsets(offset1, offset2) => [x1, h1, l1] = readCoordinatesAtOffset(offset1) [x2, h2, l2] = readCoordinatesAtOffset(offset2) slope = (l2-l1)/(x1-x2) [x1,l1,x2,l2,slope] buildLineUp() => offsetOfFractalRight = offsetOfFractalUpFromLastOffset(0) offsetOfFractalLeft = offsetOfFractalUpFromLastOffset(offsetOfFractalRight) //plotchar(fractalUp, 'xLow', '▼', location.abovebar, size=size.tiny, color=color.new(color.red, 0), offset=-2) [x1,y1,x2,y2,slope] = calulateLineUpFromOffsets(offsetOfFractalLeft,offsetOfFractalRight) line.new(last_bar_index-x1, high[x1], last_bar_index-x2, high[x2], width = 1, extend = extend.right) [x1,y1,x2,y2,slope] buildLineLo() => offsetOfFractalRight = offsetOfFractalLoFromLastOffset(0) offsetOfFractalLeft = offsetOfFractalLoFromLastOffset(offsetOfFractalRight) [x1,y1,x2,y2,slope] = calulateLineLoFromOffsets(offsetOfFractalLeft,offsetOfFractalRight) line.new(last_bar_index-x1, low[x1], last_bar_index-x2, low[x2], width = 1, extend = extend.right) [x1,y1,x2,y2,slope] a_allLines = line.all if array.size(a_allLines) > 0 for i = 0 to array.size(a_allLines) - 1 line.delete(array.get(a_allLines, i)) [x1Lo,y1Lo,x2Lo,y2Lo,slopeLo] = buildLineLo() [x1Up,y1Up,x2Up,y2Up,slopeUp] = buildLineUp() //plotchar(last_bar_index-x1Lo, 'lLo', '^', location.belowbar, size=size.tiny, color=color.new(color.red, 0)) //plotchar(last_bar_index-x2Lo, 'rLo', '^', location.belowbar, size=size.tiny, color=color.new(color.red, 0)) //plotchar(last_bar_index-x1Up, 'lUp', '▼', location.abovebar, size=size.tiny, color=color.new(color.red, 0)) //plotchar(last_bar_index-x2Up, 'rUp', '▼', location.abovebar, size=size.tiny, color=color.new(color.red, 0)) //plotchar(fractalUp, 'xLow', '▼', location.abovebar, size=size.tiny, color=color.new(color.red, 0), offset=-2) //alertcondition(breakout, title="Breakout detected", message="Price has broken out of the last N days high or low")