Trouble with lowestall and lookback period

lagan

New member
VIP
So I got this code from the TOS & Thinkscript Collection and it works great. It displays the highest price over a look back period.
Code:
#hint:<b>Plots a line for the highest in the last ? bars</b>\nHas option for a % lower plot
 
declare upper;
input numBars = 50;#hint numBars: The number of bars monitored
input price = high;#hint price:Select the price to monitor
input hidePctLowerPlot = yes;#hint hidePctLowerPlot: Yes hides the hidePctLowerPlot
input PctLower = 5;#hint PctLower: define the % lower plot criteria
 
def barNum = barNumber();
def lastBar = HighestAll( if IsNaN(price) then 0 else barNum );
def startBar = if lastBar <= numBars then 1 else lastBar - numBars;
def totBars = if lastBar <= numBars then lastBar else numBars;
def data = if barNum < startBar then 0 else price;
def highestData = HighestAll( data );
 
plot HighestHigh = highestData;
plot PercentDown = highestData * (1 - (PctLower/100));
HighestHigh.SetPaintingStrategy( PaintingStrategy.LINE );
HighestHigh.SetLineWeight(3);
HighestHigh.AssignValueColor( color.CYAN );
#HighestHigh.SetDefaultColor(Color.CYAN);
HighestHigh.HideBubble();
PercentDown.SetStyle( Curve.SHORT_DASH );
PercentDown.SetLineWeight(1);
PercentDown.SetDefaultColor(Color.RED);
PercentDown.HideBubble();
PercentDown.SetHiding( hidePctLowerPlot );
 
AddChartBubble( barNum == startBar, HighestHigh, "Highest High in the Past " + totBars + " bars: "  + HighestHigh, color.CYAN );
AddChartBubble( barNum == startBar and !hidePctLowerPlot, PercentDown,
PctLower + " % Below Highest High in the Past " + totBars + " bars: "  + PercentDown,
color.RED );


I wanted to modify it do the same displaying the lowest price over a period. Simple right? Not so much. I have some thinkscript experience but this is my first foray into calculating and displaying information based on price bars in the chart.


This line always seems to evalutate to 0.
def lastBar = LowestAll( if IsNaN(price) then 0 else barNum );
I would think that LowestAll performs similarly to HighestAll in the working code. But it doesn't. Any ideas on what I'm missing?

Here is my simplified code to find the lowest price. I split out the lowest functionality for debugging. I know there are several ways to do this. But this is my first foray into bar by bar calculations and I'm looking for something simple that I can build on as I learn.
Code:
declare upper;
input numBars = 50;#hint numBars: The number of bars monitored
input price = low;#hint price:Select the price to monitor
input hidePctUpperPlot = yes;#hint hidePctUpperPlot: Yes hides the hidePctUpperPlot
input PctLower = 5;#hint PctLower: define the % lower plot criteria
 
def barNum = barNumber();
def lastBar = LowestAll( if IsNaN(price) then 0 else barNum );
def startBar = if lastBar <= numBars then 1 else lastBar - numBars;
def totBars = if lastBar <= numBars then lastBar else numBars;
def data = if barNum < startBar then 0 else price;
def lowestData = LowestAll( data );
 
plot LowestLow = lowestData;
plot PercentUp = lowestData * (1 - (PctLower/100));
LowestLow.SetPaintingStrategy( PaintingStrategy.LINE );
LowestLow.SetLineWeight(3);
LowestLow.AssignValueColor( color.Yellow );
#HighestHigh.SetDefaultColor(Color.Yellow);
LowestLow.HideBubble();
PercentUp.SetStyle( Curve.SHORT_DASH );
PercentUp.SetLineWeight(1);
PercentUp.SetDefaultColor(Color.RED);
PercentUp.HideBubble();
PercentUp.SetHiding( hidePctUpperPlot );
 
AddChartBubble( barNum == startBar, LowestLow, "Lowest Low in the Past " + totBars + " bars: "  + LowestLow, color.Yellow );
AddChartBubble( barNum == startBar and !hidePctUpperPlot, PercentUp,
PctLower + " % Above Lowest Low in the Past " + totBars + " bars: "  + PercentUp,
color.RED );
 
Solution
@lagan
The definition of lastbar should not change to LowestAll. Bar Numbers are always increasing, so using LowestAll would reference the first bar of the chart. Should be:
Ruby:
def lastBar = HighestAll( if IsNaN(price) then 0 else barNum );

The definition of data cannot ever be 0 because you want the lowest value of data.
Should be:
Ruby:
def data = if barNum < startBar then double.nan else price;

Finally you have to add the percentup line instead of subtracting because you are plotting above the lowest low.
Should be:
Ruby:
plot PercentUp = lowestData * (1 + (PctLower/100));

Works fine after fixing those issues.
PZzYwy3.png
@lagan
The definition of lastbar should not change to LowestAll. Bar Numbers are always increasing, so using LowestAll would reference the first bar of the chart. Should be:
Ruby:
def lastBar = HighestAll( if IsNaN(price) then 0 else barNum );

The definition of data cannot ever be 0 because you want the lowest value of data.
Should be:
Ruby:
def data = if barNum < startBar then double.nan else price;

Finally you have to add the percentup line instead of subtracting because you are plotting above the lowest low.
Should be:
Ruby:
plot PercentUp = lowestData * (1 + (PctLower/100));

Works fine after fixing those issues.
PZzYwy3.png
 
Solution

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

@lagan
The definition of lastbar should not change to LowestAll. Bar Numbers are always increasing, so using LowestAll would reference the first bar of the chart. Should be:
Ruby:
def lastBar = HighestAll( if IsNaN(price) then 0 else barNum );

The definition of data cannot ever be 0 because you want the lowest value of data.
Should be:
Ruby:
def data = if barNum < startBar then double.nan else price;

Finally you have to add the percentup line instead of subtracting because you are plotting above the lowest low.
Should be:
Ruby:
plot PercentUp = lowestData * (1 + (PctLower/100));

Works fine after fixing those issues.
PZzYwy3.png
Thank you so much for looking at this. I get it now. Highestall function for lastbar is , for lack of a better term, "incrementing" through each bar. I'm going to change the script that checks for the highest price to double.nan instead of zero as well. Should've caught the + instead of -. My eyes had just glazed over trying to fix the other problem. Next I'll just combine into 1 script. Thanks again.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
482 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