I am pretty new to coding pine script and am still learning. I stumbled across this forum and thought maybe someone here can help me out.
So basically what I am trying to build is an indicator that recognizes breaks in market structure. So for example I have a swing low, this low then gets swept and prices closes above the high that swept that low for a long signal and vice versa for short signals. I found a pretty basic code for detecting swing highs and lows, but I cant wrap my head around how I have to write the code to get where I want.
The indicator should plot a Line from the low that got swept to the low that went below it and then also plot a line from the high that swept that low to the bar that then closed above high, resulting in the break in market structure. It doesnt matter if the low that got swept was only lets say 2 bars back or for example 20. It should always take the low that is next highest. Hope that makes sense.
I added a picture in hope to makes things clearer. You can ignore the blue box and all that
https://imgur.com/a/Y7WXvWe
The code that I have right now looks like this:
Code: Select all
//@version=5
indicator(title='Market Structure Break', shorttitle='MSB', overlay=true)
highSwing = high[1] > high[2] and high < high[1]
lowSwing = low[1] < low[2] and low > low[1]
plotshape(highSwing, title='highSwing', text='High', style=shape.triangledown, location=location.abovebar, color=color.new(color.blue, 40), size=size.small, offset=-1)
plotshape(lowSwing, title='lowSwing', text='Low', style=shape.triangleup, location=location.belowbar, color=color.new(color.red, 40), size=size.small, offset=-1)