Limit amount of lookback in thinkscript

godzilla74

New member
I'm trying to create a simple script that looks at the previous candle (regardless of timeframe) and plots a line at 50% of that previous candle in the current candles vertical space. Being new to Thinkscript, I've read through the tutorials, but just accomplishing this one thing is alluding me.

I have the 50% happening, however there are a few issues that I can't seem to figure out:

1. There is a plot to the right of the current/latest candle (in this case the rightmost green one), I don't want/need that plot or the other plotted lines that I've marked out.
2. I'd like the actual plotted line "The only one that isn't marked out in the screenshot, to extend all the way to the left of the chart instead of just the bar width. Is that possible? I don't see a 'line length' type of attribute I can set.

Screen-Shot-2021-12-02-at-7-19-38-PM.png


Here is the code:

Code:
input AggregationPeriod = AggregationPeriod.DAY;
plot midrange = hl2(period = AggregationPeriod)[1];
midrange.SetDefaultColor(Color.RED);
midrange.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
midrange.SetLineWeight(2);

Thanks!
Justin
 
Solution
@godzilla74 something like this, maybe?
Code:
# Line At Price
# Mobius
# Alternative to using the HighestAll() function
# modified to limit plot to the right and to use hl2[1] price value

input barsBack = 1000;

def c = if !IsNaN(close) and IsNaN(close[-1]) then hl2[1] else c[1];
plot line = if isNaN(close[-barsBack]) and !isnan(close) then c[-barsBack] else Double.NaN;
@godzilla74 something like this, maybe?
Code:
# Line At Price
# Mobius
# Alternative to using the HighestAll() function
# modified to limit plot to the right and to use hl2[1] price value

input barsBack = 1000;

def c = if !IsNaN(close) and IsNaN(close[-1]) then hl2[1] else c[1];
plot line = if isNaN(close[-barsBack]) and !isnan(close) then c[-barsBack] else Double.NaN;
 
Solution
@godzilla74 something like this, maybe?
Code:
# Line At Price
# Mobius
# Alternative to using the HighestAll() function
# modified to limit plot to the right and to use hl2[1] price value

input barsBack = 1000;

def c = if !IsNaN(close) and IsNaN(close[-1]) then hl2[1] else c[1];
plot line = if isNaN(close[-barsBack]) and !isnan(close) then c[-barsBack] else Double.NaN;
Wow... made it even easier! lol.

One question though...
Code:
c[-barsBack]
is a negative number, but why is the line being extended to the left instead of right?

Screen-Shot-2021-12-03-at-7-48-12-AM.png
 
Last edited by a moderator:
I'm trying to create a simple script that looks at the previous candle (regardless of timeframe) and plots a line at 50% of that previous candle in the current candles vertical space. Being new to Thinkscript, I've read through the tutorials, but just accomplishing this one thing is alluding me.

I have the 50% happening, however there are a few issues that I can't seem to figure out:

1. There is a plot to the right of the current/latest candle (in this case the rightmost green one), I don't want/need that plot or the other plotted lines that I've marked out.
2. I'd like the actual plotted line "The only one that isn't marked out in the screenshot, to extend all the way to the left of the chart instead of just the bar width. Is that possible? I don't see a 'line length' type of attribute I can set.

Here is the code:

Code:
input AggregationPeriod = AggregationPeriod.DAY;
plot midrange = hl2(period = AggregationPeriod)[1];
midrange.SetDefaultColor(Color.RED);
midrange.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
midrange.SetLineWeight(2);

Thanks!
Justin

Wow... made it even easier! lol.

One question though...
Code:
c[-barsBack]
is a negative number, but why is the line being extended to the left instead of right?

@godzilla74 c[-barsBack] ends the plot after 1000 bars to the left. The line extends to the left as you requested in your first post . . . unless that was a typing error?

To make it plot to the right, change the plot line to this -
plot line = if isNaN(close[-2]) and !isnan(close[10]) then c[-1] else Double.NaN; Just adjust the numerical values in the IsNaN()s' parentheses to move the line length back and forth. And you can delete the barsBack input if it is no longer being used.
 
Last edited:
@godzilla74 c[-barsBack] ends the plot after 1000 bars to the left. The line extends to the left as you requested in your first post . . . unless that was a typing error?

To make it plot to the right, change the plot line to this -
plot line = if isNaN(close[-2]) and !isnan(close[10]) then c[-1] else Double.NaN; Just adjust the numerical values in the IsNaN()s' parentheses to move the line length back and forth. And you can delete the barsBack input if it is no longer being used.
Yes, sorry, that was a typo then. Wanted right in my head, fingers typed left. Thank you for the help and the great explaination!
 

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
193 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