I just learned thinkscript this weekend and this is my first script. I am trying to plot a horizontal line across the chart for the high of the last 65 bars going back from the most recent bar only. On a daily chart of 10 years, I determined by brute force that the lastbarnumber would be 2517. I have hardcoded this value just for simplicity. For example, on a daily chart of SCCO for 10 years, the high going back 65 bars from the bar of 8/6/21(I am not including today) would have been 83.29. I am therefore aiming to plot a horizontal line at 83.29 going across the chart. the sethiding feature fails. Can anyone tell me how I might accomplish what I am trying to do. I do not want to see any lines except the one at 83.29!
def zoldhigh = 0;
def zcurhigh = fold j = 2516 to 2517 do
if high > zoldhigh then highest(high,65) else zoldhigh;
plot zmaxhigh =if barnumber()==2517 then zcurhigh else 0;
zmaxhigh.sethiding(zmaxhigh==0);
[Edited with corrected Code 20210809]![]()
Ruby:input lookback = 90; def zoldhigh = 0; def lastbar = HighestAll(if !IsNaN(close) then BarNumber() else Double.NaN); def zcurhigh = fold j = 0 to lastbar do if getvalue(high,j) > zoldhigh then Highest(getvalue(high, j),lookback) else zoldhigh; plot zmaxhigh = highestall(zcurhigh); zmaxhigh.SetHiding(zmaxhigh == 0); input debug = yes; AddLabel(debug, lastbar + " " + zcurhigh, color.white);
Join useThinkScript to post your question to a community of 21,000+ developers and traders.
def LastBar = HighestAll(if !IsNaN(close) then BarNumber() else Double.NaN);
def BarRem = LastBar - BarNumber();
def High65 = if !BarRem then Highest(high,65) else Double.NaN;
plot Line = HighestAll(High65);
def LastBar = HighestAll(if !IsNaN(close) then BarNumber() else Double.NaN);
def BarRem = LastBar - BarNumber();
def High65 = if !BarRem then Highest(high,65) else Double.NaN;
plot Line = if BarRem <= 65 then HighestAll(High65) else Double.NaN;
Since your code I modified and I posted seemed to work, I stopped there. See if this works for you in your testing. It seems to work in what few I tested.I just learned thinkscript this weekend and this is my first script. I am trying to plot a horizontal line across the chart for the high of the last 65 bars going back from the most recent bar only. On a daily chart of 10 years, I determined by brute force that the lastbarnumber would be 2517. I have hardcoded this value just for simplicity. For example, on a daily chart of SCCO for 10 years, the high going back 65 bars from the bar of 8/6/21(I am not including today) would have been 83.29. I am therefore aiming to plot a horizontal line at 83.29 going across the chart. the sethiding feature fails. Can anyone tell me how I might accomplish what I am trying to do. I do not want to see any lines except the one at 83.29!
def zoldhigh = 0;
def zcurhigh = fold j = 2516 to 2517 do
if high > zoldhigh then highest(high,65) else zoldhigh;
plot zmaxhigh =if barnumber()==2517 then zcurhigh else 0;
zmaxhigh.sethiding(zmaxhigh==0);
Ruby:input lookback = 90; def zoldhigh = 0; def lastbar = HighestAll(if !IsNaN(close) then BarNumber() else Double.NaN); def zcurhigh = fold j = 0 to lastbar do if getvalue(high,j) > zoldhigh then Highest(getvalue(high, j),lookback) else zoldhigh; plot zmaxhigh = highestall(zcurhigh); zmaxhigh.SetHiding(zmaxhigh == 0); input debug = yes; AddLabel(debug, lastbar + " " + zcurhigh, color.white);
That looks like it will work fine and I will test it. I'm learning quite a bit from the code you guys are posting!!! Thank you.Since your code I modified and I posted seemed to work, I stopped there. See if this works for you in your testing. It seems to work in what few I tested.
Thank you for the feedback. Please let me know if you find it does not work in any testing. Although I am fairly experienced in Thinkscript, I am not as experienced as others may be in using 'fold'.That looks like it will work fine and I will test it. I'm learning quite a bit from the code you guys are posting!!! Thank you.11
I just learned thinkscript this weekend and this is my first script. I am trying to plot a horizontal line across the chart for the high of the last 65 bars going back from the most recent bar only. On a daily chart of 10 years, I determined by brute force that the lastbarnumber would be 2517. I have hardcoded this value just for simplicity. For example, on a daily chart of SCCO for 10 years, the high going back 65 bars from the bar of 8/6/21(I am not including today) would have been 83.29. I am therefore aiming to plot a horizontal line at 83.29 going across the chart. the sethiding feature fails. Can anyone tell me how I might accomplish what I am trying to do. I do not want to see any lines except the one at 83.29!
def zoldhigh = 0;
def zcurhigh = fold j = 2516 to 2517 do
if high > zoldhigh then highest(high,65) else zoldhigh;
plot zmaxhigh =if barnumber()==2517 then zcurhigh else 0;
zmaxhigh.sethiding(zmaxhigh==0);
# test_highestbn_05b
# https://usethinkscript.com/threads/plot-only-the-high-from-the-last-65-most-recent-days.7483/
# find the highest high, in the most rect x bars
# draw a horz line at that level
# in the fold loop, this uses i and other variables to calculate a changing offset, a different offset for each bar in the series, such that for each bar, the loop looks back to the first bar in the series, and not beyond it.
# when the current bar is the first in the lookback period, it doesn't look at previous bars. on the 2nd bar in the series, it only looks back 1 bar (for the high). when the loop gets to the last bar, it looks back 'look_back_bars ' bars.
def na = Double.NaN;
def bn = BarNumber();
def lastbar = HighestAll(if !IsNaN(close) then bn else na);
input look_back_bars = 65;
# is the current bar within the lookback quantity of bars? true/false
def rng = (lastbar - bn <= look_back_bars);
def hi = fold i = 0 to look_back_bars
with p
do if !rng then 0 else if GetValue(high, (bn - (lastbar - i))) > p then GetValue(high, (bn - (lastbar - i))) else p;
plot hix = HighestAll(hi);
hix.SetDefaultColor(Color.yellow);
hix.SetStyle(Curve.MEDIUM_DASH);
# test code -------------------------
# identify the first bar in the lookback group
def firstbar = (!IsNaN(close[-look_back_bars]) and IsNaN(close[-(look_back_bars + 1)]));
plot f = firstbar;
f.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
f.SetDefaultColor(Color.yellow);
f.setlineweight(4);
AddVerticalLine(firstbar, "xx" , Color.CYAN );
input show_barnumber_bubbles = no;
addchartbubble(show_barnumber_bubbles, low * 0.995, bn + "\n" + "x", color.cyan, no);
#addchartbubble(1, high * 1.01, hi, color.cyan, yes);
#
K, awesome @halcyonguy, please let me know if I am correct in your reasoning for that line of code![]()
Can you please provide the codes for the lower line?@Ecantor , congrats on trying a fold function after a day of studying. it took me a couple of weeks of experimenting with fold before it clicked.
i started my version a few hours ago. i'm just getting back now and finished it.
it draws an arrow and a vertical line on the first bar in the look_back_bars series
Ruby:# test_highestbn_05b # https://usethinkscript.com/threads/plot-only-the-high-from-the-last-65-most-recent-days.7483/ # find the highest high, in the most rect x bars # draw a horz line at that level # in the fold loop, this uses i and other variables to calculate a changing offset, a different offset for each bar in the series, such that for each bar, the loop looks back to the first bar in the series, and not beyond it. # when the current bar is the first in the lookback period, it doesn't look at previous bars. on the 2nd bar in the series, it only looks back 1 bar (for the high). when the loop gets to the last bar, it looks back 'look_back_bars ' bars. def na = Double.NaN; def bn = BarNumber(); def lastbar = HighestAll(if !IsNaN(close) then bn else na); input look_back_bars = 65; # is the current bar within the lookback quantity of bars? true/false def rng = (lastbar - bn <= look_back_bars); def hi = fold i = 0 to look_back_bars with p do if !rng then 0 else if GetValue(high, (bn - (lastbar - i))) > p then GetValue(high, (bn - (lastbar - i))) else p; plot hix = HighestAll(hi); hix.SetDefaultColor(Color.yellow); hix.SetStyle(Curve.MEDIUM_DASH); # test code ------------------------- # identify the first bar in the lookback group def firstbar = (!IsNaN(close[-look_back_bars]) and IsNaN(close[-(look_back_bars + 1)])); plot f = firstbar; f.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down); f.SetDefaultColor(Color.yellow); f.setlineweight(4); AddVerticalLine(firstbar, "xx" , Color.CYAN ); input show_barnumber_bubbles = no; addchartbubble(show_barnumber_bubbles, low * 0.995, bn + "\n" + "x", color.cyan, no); #addchartbubble(1, high * 1.01, hi, color.cyan, yes); #
![]()
can you please provide the code for plotting he low from the last 65 most recent days?@Ecantor , congrats on trying a fold function after a day of studying. it took me a couple of weeks of experimenting with fold before it clicked.
i started my version a few hours ago. i'm just getting back now and finished it.
it draws an arrow and a vertical line on the first bar in the look_back_bars series
Ruby:# test_highestbn_05b # https://usethinkscript.com/threads/plot-only-the-high-from-the-last-65-most-recent-days.7483/ # find the highest high, in the most rect x bars # draw a horz line at that level # in the fold loop, this uses i and other variables to calculate a changing offset, a different offset for each bar in the series, such that for each bar, the loop looks back to the first bar in the series, and not beyond it. # when the current bar is the first in the lookback period, it doesn't look at previous bars. on the 2nd bar in the series, it only looks back 1 bar (for the high). when the loop gets to the last bar, it looks back 'look_back_bars ' bars. def na = Double.NaN; def bn = BarNumber(); def lastbar = HighestAll(if !IsNaN(close) then bn else na); input look_back_bars = 65; # is the current bar within the lookback quantity of bars? true/false def rng = (lastbar - bn <= look_back_bars); def hi = fold i = 0 to look_back_bars with p do if !rng then 0 else if GetValue(high, (bn - (lastbar - i))) > p then GetValue(high, (bn - (lastbar - i))) else p; plot hix = HighestAll(hi); hix.SetDefaultColor(Color.yellow); hix.SetStyle(Curve.MEDIUM_DASH); # test code ------------------------- # identify the first bar in the lookback group def firstbar = (!IsNaN(close[-look_back_bars]) and IsNaN(close[-(look_back_bars + 1)])); plot f = firstbar; f.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down); f.SetDefaultColor(Color.yellow); f.setlineweight(4); AddVerticalLine(firstbar, "xx" , Color.CYAN ); input show_barnumber_bubbles = no; addchartbubble(show_barnumber_bubbles, low * 0.995, bn + "\n" + "x", color.cyan, no); #addchartbubble(1, high * 1.01, hi, color.cyan, yes); #
![]()
Thread starter | Similar threads | Forum | Replies | Date |
---|---|---|---|---|
Z | How to plot the average price based on the high & low of the last 5 bars? | Questions | 3 | |
E | How can I plot the high and low of the last 8 bars? | Questions | 3 | |
S | Plot post-market high from previous day | Questions | 1 | |
S | Plot AccumDistBuyPr previous high low | Questions | 14 | |
S | Plot High Low 1st Day to form range | Questions | 9 |
Start a new thread and receive assistance from our community.
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.
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.