Reverse-direction maximum value?


New member
I am trying get the running maximum value of a series in a chart, with the caveat that instead of getting the maximum values of all previous values, it gets the maximum value of all future bars. I have a few ideas on how to do this, but either they don't work, or are too slow, so I am asking for additional ideas on how this can be done. Anyone have ideas on how I can accomplish this in a more elegant way?

Method 1)
MaxSeries = CompoundValue(1, max(MaxSeries[-1], high, 0);
This method would do what I need it to, but it does not work. It returns nothing, implying that forward values cannot be fed into the function.

Method 2)
def FWD_max = fold k = -search_length to 1 with B = 0 do max(B, if !IsNaN(high[k]) then high[k] else 0);
This method works but it requires search_length iterations for every bar, making it incredibly slow - too slow to be used in a scan for reasonable search_length values (i.e. search_length=2000) (there might be other limitations with the way scans use studies as well, I'm not sure). The image below shows the desired result.

here is a version that finds the last bar, then calculates the quantity of bars, from the current bar to the last one.
that way you don't have to use a large number in the loop.

there are comments in the code, describing what the code lines do, and explaining why i changed a formula.
there are labels and bubbles code lines, to display test data. they are commented out. remove the # in front of them to enable them.

# highest_future_value_01

# find the highest high, after the current bar (in future)

def price = close;
def bn = BarNumber();

# find the last barnumber , with valid price data
def barCount = HighestAll(If(IsNaN(price), 0, bn));
#addlabel(1, "bars " + barcount, color.cyan);

# calc the qty of bars from current bar to the last bar with price data
#   this doesn't work for lines. there are bars after the last one with price data.
#     when the first one after the last price bar is encountered in the fold, the fold fails.
#   it does work for plotting the test bubbles, because the bubbles are placed referencing low,
#     and there is no valid low after the last bar with price data.
#def remainingbars = (barcount - bn);

# adjust formula, so it doesn't calc a diff past the last bar
def remainingbars = if bn < barcount then (barcount - bn) else remainingbars[1];
#addchartbubble(1,low, remainingbars, color.cyan,no);

# this looks at future bars and finds the highest high price
def FWD_max = fold k = 0 to remainingbars
   with b
   do max(b, getvalue(high, -k));
#addchartbubble(1,low, FWD_max, color.cyan,no);

plot z = FWD_max;
# adding horizontal forces lines to be horizontal and gets rid of the diagonal connecting lines
Thanks for the help. I was able to implement the bar counting logic nicely and I think it's sped the script up a bit. Unfortunately, I just cannot figure out a way to make the logic run quick enough for scans. I've come to the conclusion that it's probably not possible with the way ThinkScript is designed.

The indicator is an unfilled gap indicator and I may post it a bit later.

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
313 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.