I amd trying to mark a bar a structural pivot high (SPH) or structural pivot low (SPL) as per the conditions below: A bar is marked as an SPL by finding a combination of 3 bars, an Anchor Bar, Bar 1 and Bar 2.Bar 1 and Bar 2 should have a higher high and a higher close relative to the Anchor Bar in order to qualify as a legitimate SPL. The complication happens for the fact that Anchor Bar, Bar 1 and Bar 2 need not be consecutive.They can be far apart from themselves.To solve this problem, the script generates 3-bar Combinations of all the bars from the last pivot to the current bar.
//@version=5
indicator("Structural Pivots", overlay=true)
// Function to check if a point is a Structural Pivot Low (SPL)
isSPL(anchorHigh, anchorClose, bar1High, bar1Close, bar2High, bar2Close) =>
bar1High > anchorHigh and bar1Close > anchorClose and bar2High > bar1High and bar2Close > bar1Close
// Declare lastPivotIndex as an integer
var int lastPivotIndex = na
lastPivotIndex := bar_index - ta.barssince(high[1] > high and high[1] > high[2])
// Array to store SPL bars
var int[] splBarIndexes = array.new_int(0)
// Iterate through bars from the last pivot to the current bar
for i = lastPivotIndex to bar_index
// Iterate through previous bars for Anchor Bar
for j = lastPivotIndex to i - 2
var anchorHigh = high[j]
var anchorClose = close[j]
// Iterate through bars between Anchor Bar and the current bar for Bar 1
for k = j + 1 to i - 1
var bar1High = high[k]
var bar1Close = close[k]
var bar2High = high[i]
var bar2Close = close[i]
// Check for SPL
if (isSPL(anchorHigh, anchorClose, bar1High, bar1Close, bar2High, bar2Close))
array.push(splBarIndexes, i)
// Plot SPL with blue triangles below the bars
plotshape(series=array.includes(splBarIndexes, bar_index - lastPivotIndex) ? 1 : na, title="SPL", color=color.blue, style=shape.triangleup, location=location.belowbar, size=size.small)
I seem to be lost here. Also dont know how to debug this as pinescript doesnt have print facility