I'm looking for the closest future bar that either hits my SL or TP. the code example below is what I'm trying to accomplish but would like to use an elegant thinkscript "function" to find the bar. I will look for both the SL and TP and plot whichever one hits first. You can see in the image example what I am trying accomplish. I don't know if there is a function that allows me to look forward and find the "closest" bar that meets my criteria. I would do it with do-while in C, but not sure what to do here. I will also need to make sure I don't run off the chart when I get to the right edge, so any advice there is also appreciated.
def closeBar = if(high[-1] > bullTp, barNumber() + 1, if(high[-2] > bullTp, barNumber() + 2, if(high[-3] > bullTP, barNumber() + 3, if(high[-4] > bullTP, barNumber() + 4, 0)))); AddChartBubble(ichiBullEntry[-1], low, closeBar + " " + (barNumber()), Color.PINK, no);