find future bar that meets criteria

Cre8able

New member
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.

thanks

Ruby:
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);


image example
 
Solution
i figured out how to fold forward in case anyone needs it in the future

Ruby:
def TP = bullTP[1];
def bullCloseTPBar =
fold iTP = 0 to 100
while GetValue(high, -iTP) < TP
do GetValue(barNumber(), -iTP) + 1;
i figured out how to fold forward in case anyone needs it in the future

Ruby:
def TP = bullTP[1];
def bullCloseTPBar =
fold iTP = 0 to 100
while GetValue(high, -iTP) < TP
do GetValue(barNumber(), -iTP) + 1;
 
Solution

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
405 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top