2 consecutive 10-bar frames (rectangles)

samhanoh

New member
VIP
I help to create a ThinkScript study that displays 2 consecutive 10-bar frames (rectangles) on the chart:

🔷 Logic:​

  1. The first frame should begin from the bar that contains the most recent highest high on the chart and span 10 bars backward from that point.
  2. The second frame should immediately follow the first one — i.e., it starts right after the end of the first frame and spans the next 10 bars.
  3. Each frame should show:
    • A gray or red background cloud between the highest high and lowest low of that frame
    • Optional top/bottom border lines
  4. Important: Once a new frame is formed (e.g., frame 2 begins), the previous one (frame 1) should be disabled or hidden — only one active frame should show at a time. see photo

The first frame should begin from the bar that contains the most recent highest high of last 15 bar
 

Attachments

  • ScreenHunter 864.jpg
    ScreenHunter 864.jpg
    22.8 KB · Views: 85
Last edited by a moderator:
Solution
  • Thank you very much you did good of the way I want to see it on chart but need correct the location, see again clear expiation and I mark on photo attach rectangle 1 and 2 to avoid confusion, my point is to scan for uptrend stock with pullback correction and it make it easy visualization on the chart."

    Rectangle 1 (Most Recent Segment):
    Starts from the most recent highest high in the last 15 bars (excluding current), and goes backward 10 bars.
    • Rectangle 2:
      Begins follow end of Rectangle 1 start after candle No 10 of Rectangle 1 countering from right to left, spans 10 bars backward.
    • It will plot only if Both:
      • Rectangle 1 contains the recent high from last 15 bars...
I help to create a ThinkScript study that displays 2 consecutive 10-bar frames (rectangles) on the chart:

🔷 Logic:​

  1. The first frame should begin from the bar that contains the most recent highest high on the chart and span 10 bars backward from that point.
  2. The second frame should immediately follow the first one — i.e., it starts right after the end of the first frame and spans the next 10 bars.
  3. Each frame should show:
    • A gray or red background cloud between the highest high and lowest low of that frame
    • Optional top/bottom border lines
  4. Important: Once a new frame is formed (e.g., frame 2 begins), the previous one (frame 1) should be disabled or hidden — only one active frame should show at a time. see photo

The first frame should begin from the bar that contains the most recent highest high of last 15 bar
you have conflicting rules, change them.

these are different. which rule do you want?
the most recent highest high on the chart
the most recent highest high of last 15 bar

get rid of the word frame. it has no meaning. say shaded rectangle
not sure what you are talking about. do you want to see 2 rectangles, but not more?
these say opposite things,
The second frame should immediately follow the first one
Once a new frame is formed (e.g., frame 2 begins), the previous one (frame 1) should be disabled or hidden
 
I help to create a ThinkScript study that displays 2 consecutive 10-bar frames (rectangles) on the chart:

🔷 Logic:​

  1. The first frame should begin from the bar that contains the most recent highest high on the chart and span 10 bars backward from that point.
  2. The second frame should immediately follow the first one — i.e., it starts right after the end of the first frame and spans the next 10 bars.
  3. Each frame should show:
    • A gray or red background cloud between the highest high and lowest low of that frame
    • Optional top/bottom border lines
  4. Important: Once a new frame is formed (e.g., frame 2 begins), the previous one (frame 1) should be disabled or hidden — only one active frame should show at a time. see photo

The first frame should begin from the bar that contains the most recent highest high of last 15 bar

this might be close to what you want
find last peak. looks at 7 bars before and after to find a peak
rectangles are 10 bars wide.
rect1 ends on a peak
rect2 starts on bar after rect1 last bar ( bar after peak)
find highest and lowest during rect1 bars
find highest and lowest of rect2 bars
rect2 lines and cloud may extend past last bar


Code:
#clouds_10bar_frames_peak
#https://usethinkscript.com/threads/2-consecutive-10-bar-frames-rectangles.21032/
#2 consecutive 10-bar frames (rectangles)
#-------------------------

def na = Double.NaN;
def bn = BarNumber();

#def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def lastbn = HighestAll(if IsNaN(close) then 0 else bn);
def lastbar = bn == lastbn;
#def lastbar = (!isnan(close) and isnan(close[-1]));
def big = 99999;

#https://usethinkscript.com/threads/zigzag-high-low-with-supply-demand-zones-for-thinkorswim.172/#post-7048
#  Robert Payne
# define peaks / valleys
def highx = high;
def lowx = low;
input peak_length = 7;

def offset = Min(peak_length - 1, lastbn - bn);
def peak = highx > Highest(highx[1], peak_length - 1) and highx == GetValue(Highest(highx, peak_length), -offset);
def valley = lowx < Lowest(lowx[1], peak_length - 1) and lowx == GetValue(Lowest(lowx, peak_length), -offset);

input show_peak_arrows = yes;
plot zhi = if show_peak_arrows and peak then high * 1.001 else na;
plot zlo = if show_peak_arrows and valley then low * 0.999 else na;
zlo.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zlo.SetDefaultColor(Color.RED);
zlo.SetLineWeight(1);
zlo.HideBubble();
zhi.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
zhi.SetDefaultColor(Color.GREEN);
zhi.SetLineWeight(1);
zhi.HideBubble();

def peakbn = if peak then bn else 0;
def valleybn = if valley then bn else 0;

def peaklastbn = highestall(peakbn);
#def valleylastbn = highestall(valleybn);


input rectangle_bars = 10;
#def rect1_first = (rectangle_bars + bn - 1) == peaklast;
#def rect1_firstbn = if bn == 1 then 0
# else if (rectangle_bars + bn - 1) == peaklastbn then bn
# else rect1_firstbn[1];

# rect before peak
def rect1_firstbn = peaklastbn - rectangle_bars + 1;
def rect1_lastbn = peaklastbn;
# rect after peak
def rect2_firstbn = rect1_lastbn + 1;
def rect2_lastbn = rect2_firstbn + rectangle_bars - 1;

def rect1 = (bn >= rect1_firstbn and bn <= rect1_lastbn);
def rect2 = (bn >= rect2_firstbn and bn <= rect2_lastbn);


addchartbubble(0, low*0.97,
 bn + "  BN\n" +
 rect1_firstbn + "-" + rect1_lastbn + "\n" +
 rect2_firstbn + "-" + rect2_lastbn + "\n"
, color.yellow, no);


#addverticalline(rect1_firstbn == bn, "-");

#--------------------
# rect1
def rect1_hi;
def rect1_lo;
if rect1_firstbn == bn then {
 rect1_hi =  GetValue(Highest(high,  rectangle_bars), -(rectangle_bars-1));
 rect1_lo = GetValue(Lowest(low,  rectangle_bars), -(rectangle_bars-1));
} else if rect1 then {
 rect1_hi = rect1_hi[1];
 rect1_lo = rect1_lo[1];
} else {
 rect1_hi = 0;
 rect1_lo = 0;
}

plot z1 = if rect1_hi > 0 then rect1_hi else na;
plot z2 = if rect1_lo > 0 then rect1_lo else na;
z1.SetDefaultColor(Color.cyan);
z1.setlineweight(1);
z1.hidebubble();
z2.SetDefaultColor(Color.cyan);
z2.setlineweight(1);
z2.hidebubble();

def r1top = if rect1_hi > 0 then rect1_hi else na;
def r1bot = if rect1_lo > 0 then rect1_lo else na;

addcloud(r1top,r1bot,color.gray);


#--------------------
# rect2
def rect2_hi;
def rect2_lo;
if rect2_firstbn == bn then {
# doesnt work if peak to last bar is < rectangle_bars
# rect2_hi =  GetValue(Highest(high,  rectangle_bars), -(rectangle_bars-1));
# rect2_lo = GetValue(Lowest(low,  rectangle_bars), -(rectangle_bars-1));

rect2_hi = fold a = 0 to (rectangle_bars-1)
 with b
 while !isnan(getvalue(close,-a))
 do max(b, getvalue(high, -a));

rect2_lo = fold c = 0 to (rectangle_bars-1)
 with d = big
 while !isnan(getvalue(close,-c))
 do min(d, getvalue(low,-c));

} else if rect2 then {
 rect2_hi = rect2_hi[1];
 rect2_lo = rect2_lo[1];
} else {
 rect2_hi = 0;
 rect2_lo = 0;
}

plot z3 = if rect2_hi > 0 then rect2_hi else na;
plot z4 = if rect2_lo > 0 then rect2_lo else na;
z3.SetDefaultColor(Color.magenta);
z3.setlineweight(1);
z3.hidebubble();
z4.SetDefaultColor(Color.magenta);
z4.setlineweight(1);
z4.hidebubble();


def r2top = if rect2_hi > 0 then rect2_hi else na;
def r2bot = if rect2_lo > 0 then rect2_lo else na;

addcloud(r2top,r2bot,color.gray);
#
 

Attachments

  • 00c-img1.JPG
    00c-img1.JPG
    44.4 KB · Views: 85
  • Thank you very much you did good of the way I want to see it on chart but need correct the location, see again clear expiation and I mark on photo attach rectangle 1 and 2 to avoid confusion, my point is to scan for uptrend stock with pullback correction and it make it easy visualization on the chart."

    Rectangle 1 (Most Recent Segment):
    Starts from the most recent highest high in the last 15 bars (excluding current), and goes backward 10 bars.
    • Rectangle 2:
      Begins follow end of Rectangle 1 start after candle No 10 of Rectangle 1 countering from right to left, spans 10 bars backward.
    • It will plot only if Both:
      • Rectangle 1 contains the recent high from last 15 bars.
      • Rectangle 1 is fully higher than Rectangle 2. Else no need to plot on chart
    • After Rectangle 1 I need see pullback
 

Attachments

  • ScreenHunter 871.jpg
    ScreenHunter 871.jpg
    31.6 KB · Views: 63
  • Thank you very much you did good of the way I want to see it on chart but need correct the location, see again clear expiation and I mark on photo attach rectangle 1 and 2 to avoid confusion, my point is to scan for uptrend stock with pullback correction and it make it easy visualization on the chart."

    Rectangle 1 (Most Recent Segment):
    Starts from the most recent highest high in the last 15 bars (excluding current), and goes backward 10 bars.
    • Rectangle 2:
      Begins follow end of Rectangle 1 start after candle No 10 of Rectangle 1 countering from right to left, spans 10 bars backward.
    • It will plot only if Both:
      • Rectangle 1 contains the recent high from last 15 bars.
      • Rectangle 1 is fully higher than Rectangle 2. Else no need to plot on chart
    • After Rectangle 1 I need see pullback

i went by your words and didn't pay attention to the picture.
your words dont match the picture.
if something is after something on a chart, it is in the future, to the right.

it seems like you want rectangle 2 to appear to the left of rectangle 1, before it.

what does fully higher mean?
 
Last edited:
see photo with explanation showing your scrip and the chnge i need ,i am not looking to the future, if it make it easy for you i can stay with your scrip and just add anther rarctangle backword
 

Attachments

  • ScreenHunter 877.jpg
    ScreenHunter 877.jpg
    41.7 KB · Views: 72
  • Thank you very much you did good of the way I want to see it on chart but need correct the location, see again clear expiation and I mark on photo attach rectangle 1 and 2 to avoid confusion, my point is to scan for uptrend stock with pullback correction and it make it easy visualization on the chart."

    Rectangle 1 (Most Recent Segment):
    Starts from the most recent highest high in the last 15 bars (excluding current), and goes backward 10 bars.
    • Rectangle 2:
      Begins follow end of Rectangle 1 start after candle No 10 of Rectangle 1 countering from right to left, spans 10 bars backward.
    • It will plot only if Both:
      • Rectangle 1 contains the recent high from last 15 bars.
      • Rectangle 1 is fully higher than Rectangle 2. Else no need to plot on chart
    • After Rectangle 1 I need see pullback

sorry about that mix up in other post
here is an updated study

define , 15 bars before last bar
.. find the highest high in those 15 bars
.. a dot is drawn on the high of highest bar
.. a line is drawn above the highbar, spanning the 15 bar range

rectangle1 is 10 bars wide and stops on the bar with the highest high
.. find the highest and lowest in this range

rectangle2, find a 10 bar range before rect1
.. find the highest and lowest in this range

if rect2 high is lower than rect1 high, then draw rect2, before rect1


Code:
#clouds_10bar_frames_peak_00e
#https://usethinkscript.com/threads/2-consecutive-10-bar-frames-rectangles.21032/
#2 consecutive 10-bar frames (rectangles)

def na = Double.NaN;
def bn = BarNumber();

#def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def lastbn = HighestAll(if IsNaN(close) then 0 else bn);
def lastbar = bn == lastbn;
#def lastbar = (!isnan(close) and isnan(close[-1]));
def big = 99999;

input find_peak_in_last_x_bars = 15;
addlabel(1, "find peak in the " + find_peak_in_last_x_bars + " bars before the last bar", color.yellow);


# find hihi and its bn in last 15 bars, before last bar
def hihi;
def hihibn;
if bn == 1 then {
 hihi = 0;
 hihibn = 0;
} else if bn == (lastbn - find_peak_in_last_x_bars) then {
 hihi = fold a = 0 to find_peak_in_last_x_bars
 with b
 do max(b, getvalue(high,-a));

 hihibn = bn + (fold c = 0 to find_peak_in_last_x_bars
 with d
 while hihi != getvalue(high, -c)
 do d + 1);

} else if lastbar or isnan(close) then {
 hihi = 0;
 hihibn = 0;
} else {
 hihi = hihi[1];
 hihibn = hihibn[1];
}

def hihibn2 = highestall(hihibn);

# plot a line over the 15 bars before the last bar
plot z5 = if hihi > 0 then hihi*1.0005 else na;
z5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z5.setdefaultcolor(color.gray);

# plot a dot on the highest high
plot z6 = if bn >= (lastbn - find_peak_in_last_x_bars) and hihi == high then high else na;
z6.SetPaintingStrategy(PaintingStrategy.POINTS);
z6.SetDefaultColor(Color.white);
z6.setlineweight(3);
z6.hidebubble();

addchartbubble(0,low*.995,
hihi + "\n" +
bn + "\n" +
hihibn
, color.yellow, no);


#-----------------------

input rectangle1_bars = 10;
input rectangle2_bars = 10;

#  rect1 before hihi
def rect1_firstbn = hihibn2 - rectangle1_bars + 1;
def rect1_lastbn = hihibn2;
#  rect2 before rect1
def rect2_firstbn = rect1_firstbn - (rectangle2_bars + 0);
def rect2_lastbn = rect1_firstbn - 1;

def rect1 = (bn >= rect1_firstbn and bn <= rect1_lastbn);
def rect2 = (bn >= rect2_firstbn and bn <= rect2_lastbn);

#--------------------
# rect1

def rect1_hi;
def rect1_lo;
if rect1_firstbn == bn then {
 rect1_hi =  GetValue(Highest(high,  rectangle1_bars), -(rectangle1_bars-1));
 rect1_lo = GetValue(Lowest(low,  rectangle1_bars), -(rectangle1_bars-1));
} else if rect1 then {
 rect1_hi = rect1_hi[1];
 rect1_lo = rect1_lo[1];
} else {
 rect1_hi = 0;
 rect1_lo = 0;
}

plot z1 = if 1 and rect1_hi > 0 then rect1_hi else na;
plot z2 = if 1 and rect1_lo > 0 then rect1_lo else na;
z1.SetDefaultColor(Color.cyan);
z1.setlineweight(1);
z1.hidebubble();
z2.SetDefaultColor(Color.cyan);
z2.setlineweight(1);
z2.hidebubble();

def r1top = if rect1_hi > 0 then rect1_hi else na;
def r1bot = if rect1_lo > 0 then rect1_lo else na;

addcloud(r1top,r1bot,color.gray);


#--------------------
# rect2

def rect2_hi;
def rect2_lo;
def r2sshorter;
if rect2_firstbn == bn then {
 rect2_hi =  GetValue(Highest(high,  rectangle2_bars), -(rectangle2_bars-1));
 rect2_lo = GetValue(Lowest(low,  rectangle2_bars), -(rectangle2_bars-1));
 r2sshorter = rect2_hi < rect1_hi[-rectangle2_bars];
} else if rect2 then {
 rect2_hi = rect2_hi[1];
 rect2_lo = rect2_lo[1];
 r2sshorter = r2sshorter[1];
} else {
 rect2_hi = 0;
 rect2_lo = 0;
 r2sshorter = 0;
}


plot z3 = if r2sshorter and rect2_hi > 0 then rect2_hi else na;
plot z4 = if r2sshorter and rect2_lo > 0 then rect2_lo else na;
z3.SetDefaultColor(Color.magenta);
z3.setlineweight(1);
z3.hidebubble();
z4.SetDefaultColor(Color.magenta);
z4.setlineweight(1);
z4.hidebubble();

def r2top = if r2sshorter and rect2_hi > 0 then rect2_hi else na;
def r2bot = if r2sshorter and rect2_lo > 0 then rect2_lo else na;

addcloud(r2top,r2bot,color.gray);


#--------------------
# test stuff

addchartbubble(0, low*0.995,
 r2sshorter + "\n" +
 rect2_hi + "\n" +
 rect1_hi + "\n" +
 rect1_hi[-rectangle2_bars]
, color.yellow, no);


input test1_bub = no;
addchartbubble(test1_bub, low*0.99,
 bn + "  BN\n" +
 rect1_firstbn + "-" + rect1_lastbn + "\n" +
rect1_hi + "\n" +
rect1_lo + "\n" +
 rect2_firstbn + "-" + rect2_lastbn + "\n" +
rect2_hi + "\n" +
rect2_lo + "\n"
, (if rect1 then color.green else if rect2 then color.magenta else color.gray), no);

#addverticalline(rect2, "-");
#

CSCO 15min
SKX 15min
DIS 15 min
.
 

Attachments

  • 00e-1 CSCO 15min.JPG
    00e-1 CSCO 15min.JPG
    55.6 KB · Views: 79
  • 00e-2 SKX 15min.JPG
    00e-2 SKX 15min.JPG
    51.5 KB · Views: 75
  • 00e-2 DIS 15min.JPG
    00e-2 DIS 15min.JPG
    59.1 KB · Views: 73
Last edited:
Solution
Excellent, you did exactly what I asked. To complete the setup, I need your help creating a scan code that finds stocks that have dropped by 20% or more. The drop should be measured from the high of Rectangle 1 to the low of Rectangle 2 — but only if the closing price is not lower than the low of Rectangle 2.


For example: if the high of Rectangle 1 is 100 and the low of Rectangle 2 is 50, the difference is 50 points. A 20% drop would be 10 points, so we’re looking for stocks that dropped to 90 or lower, but not below 50 (the lowest point).


This is similar to identifying a pullback in an uptrend, based on Fibonacci retracement levels.


Please make sure the scan allows for:


  • Customizable percentage input (e.g., 20%, 38.2%, 50%, etc.)
  • Adjustable bar count for each rectangle (as you already implemented in the previous code)

Thank you very much for your help!
 
Excellent, you did exactly what I asked. To complete the setup, I need your help creating a scan code that finds stocks that have dropped by 20% or more. The drop should be measured from the high of Rectangle 1 to the low of Rectangle 2 — but only if the closing price is not lower than the low of Rectangle 2.


For example: if the high of Rectangle 1 is 100 and the low of Rectangle 2 is 50, the difference is 50 points. A 20% drop would be 10 points, so we’re looking for stocks that dropped to 90 or lower, but not below 50 (the lowest point).


This is similar to identifying a pullback in an uptrend, based on Fibonacci retracement levels.


Please make sure the scan allows for:


  • Customizable percentage input (e.g., 20%, 38.2%, 50%, etc.)
  • Adjustable bar count for each rectangle (as you already implemented in the previous code)

Thank you very much for your help!

when should this % drop be looked for?
after the highest high that starts the rectangles?

so, the drop range is from ,
high of Rectangle 1
to the low of Rectangle 2 .
and that would be 100% drop?
 
when should this % drop be looked for?
after the highest high that starts the rectangles?

so, the drop range is from ,
high of Rectangle 1
to the low of Rectangle 2 .
and that would be 100% drop?
I want to identify stocks that had:


  1. A recent uptrend ending in a Rectangle 1 — defined as the most recent highest high in the last N bars (e.g., 15 bars).
  2. A pullback — defined by Rectangle 2, which follows Rectangle 1 and marks the lowest low in a separate N-bar window (e.g., another 10 bars).
  3. I want to measure the distance between the high of Rectangle 1 and the low of Rectangle 2 — call it the “range.”
  4. I’m interested in cases where the current closing price is:
    • Below the high of Rectangle 1,
    • By at least a customizable percentage (e.g., 20%) of that range,
    • But still above the low of Rectangle 2.

------------------------------------------------------
I want to scan for stocks that:
  • Have already made a strong move up, forming a recent high (Rectangle 1),
  • I want to measure the pullback from the high of Rectangle 1 to the low of Rectangle 2, and calculate the difference.
  • I am looking for stocks where the current closing price is below the high of Rectangle 1 by at least X% of that difference, but still above the low of Rectangle 2.
For example:
Rectangle 1 high = 100
Rectangle 2 low = 50
Difference = 50
20% of that difference = 10
So I’m looking for stocks where the close is at or below 90 (100-10), but above 50.
 
Last edited by a moderator:
I want to identify stocks that had:


  1. A recent uptrend ending in a Rectangle 1 — defined as the most recent highest high in the last N bars (e.g., 15 bars).
  2. A pullback — defined by Rectangle 2, which follows Rectangle 1 and marks the lowest low in a separate N-bar window (e.g., another 10 bars).
  3. I want to measure the distance between the high of Rectangle 1 and the low of Rectangle 2 — call it the “range.”
  4. I’m interested in cases where the current closing priceis:
    • Below the high of Rectangle 1,
    • By at least a customizable percentage (e.g., 20%) of that range,
    • But still above the low of Rectangle 2.

------------------------------------------------------
I want to scan for stocks that:
  • Have already made a strong move up, forming a recent high (Rectangle 1),
  • I want to measure the pullback from the high of Rectangle 1 to the low of Rectangle 2, and calculate the difference.
  • I am looking for stocks where the current closing price is below the high of Rectangle 1 by at least X% of that difference, but still above the low of Rectangle 2.
For example:
Rectangle 1 high = 100
Rectangle 2 low = 50
Difference = 50
20% of that difference = 10
So I’m looking for stocks where the close is at or below 90 (100-10), but above 50.

i try to ask specific questions. all you had to do was say yes or no. you didn't need to repeat post #9

if you want a scan, it will probably have to be completely rewritten.
this chart study has loops that look into the future., so it can draw things based on future events.
to try and use this in a scan may not work? may be too complicated?

a scan uses data on the last candle, so all the data is in the past, so all formulas look at old data.

-----------------

here is another chart study
i added % lines after the highest high
can pick up to 5 % levels. defaults are fib levels
can pick a target % to drop below. default is 20%
from target % down to 100% is shaded purple.
bubbles tell what the %'s are
alert goes off when close is in purple range 20 - 100%

Code:
#clouds_10bar_frames_peak_00f2
#https://usethinkscript.com/threads/2-consecutive-10-bar-frames-rectangles.21032/
#2 consecutive 10-bar frames (rectangles)

def na = Double.NaN;
def bn = BarNumber();

#def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def lastbn = HighestAll(if IsNaN(close) then 0 else bn);
def lastbar = bn == lastbn;
#def lastbar = (!isnan(close) and isnan(close[-1]));
def big = 99999;

input find_peak_in_last_x_bars = 15;
addlabel(1, "find peak in the " + find_peak_in_last_x_bars + " bars before the last bar", color.yellow);


# find hihi and its bn in last 15 bars, before last bar
def hihi;
def hihibn;
if bn == 1 then {
 hihi = 0;
 hihibn = 0;
} else if bn == (lastbn - find_peak_in_last_x_bars) then {
 hihi = fold a = 0 to find_peak_in_last_x_bars
 with b
 do max(b, getvalue(high,-a));

 hihibn = bn + (fold c = 0 to find_peak_in_last_x_bars
 with d
 while hihi != getvalue(high, -c)
 do d + 1);

} else if lastbar or isnan(close) then {
 hihi = 0;
 hihibn = 0;
} else {
 hihi = hihi[1];
 hihibn = hihibn[1];
}

def hihibn2 = highestall(hihibn);

input show_line_over_last_xbars = no;
# plot a line over the 15 bars before the last bar
plot z5 = if show_line_over_last_xbars and hihi > 0 then hihi*1.0005 else na;
#z5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z5.SetPaintingStrategy(PaintingStrategy.dashes);
#z5.SetStyle(Curve.short_DASH);
z5.setdefaultcolor(color.gray);
z5.hidebubble();

# plot a dot on the highest high
plot z6 = if bn >= (lastbn - find_peak_in_last_x_bars) and hihi == high then high else na;
z6.SetPaintingStrategy(PaintingStrategy.POINTS);
z6.SetDefaultColor(Color.white);
z6.setlineweight(3);
z6.hidebubble();

addchartbubble(0,low*.995,
hihi + "\n" +
bn + "\n" +
hihibn
, color.yellow, no);


#-----------------------

input rectangle1_bars = 10;
input rectangle2_bars = 10;

#  rect1 before hihi
def rect1_firstbn = hihibn2 - rectangle1_bars + 1;
def rect1_lastbn = hihibn2;
#  rect2 before rect1
def rect2_firstbn = rect1_firstbn - (rectangle2_bars + 0);
def rect2_lastbn = rect1_firstbn - 1;

def rect1 = (bn >= rect1_firstbn and bn <= rect1_lastbn);
def rect2 = (bn >= rect2_firstbn and bn <= rect2_lastbn);

#--------------------
# rect1

def rect1_hi;
def rect1_lo;
if rect1_firstbn == bn then {
 rect1_hi =  GetValue(Highest(high,  rectangle1_bars), -(rectangle1_bars-1));
 rect1_lo = GetValue(Lowest(low,  rectangle1_bars), -(rectangle1_bars-1));
} else if rect1 then {
 rect1_hi = rect1_hi[1];
 rect1_lo = rect1_lo[1];
} else {
 rect1_hi = 0;
 rect1_lo = 0;
}

plot z1 = if 1 and rect1_hi > 0 then rect1_hi else na;
plot z2 = if 1 and rect1_lo > 0 then rect1_lo else na;
z1.SetDefaultColor(Color.cyan);
z1.setlineweight(1);
z1.hidebubble();
z2.SetDefaultColor(Color.cyan);
z2.setlineweight(1);
z2.hidebubble();

def r1top = if rect1_hi > 0 then rect1_hi else na;
def r1bot = if rect1_lo > 0 then rect1_lo else na;

addcloud(r1top,r1bot,color.gray);


#--------------------
# rect2

def rect2_hi;
def rect2_lo;
def r2sshorter;
if rect2_firstbn == bn then {
 rect2_hi =  GetValue(Highest(high,  rectangle2_bars), -(rectangle2_bars-1));
 rect2_lo = GetValue(Lowest(low,  rectangle2_bars), -(rectangle2_bars-1));
 r2sshorter = rect2_hi < rect1_hi[-rectangle2_bars];
} else if rect2 then {
 rect2_hi = rect2_hi[1];
 rect2_lo = rect2_lo[1];
 r2sshorter = r2sshorter[1];
} else {
 rect2_hi = 0;
 rect2_lo = 0;
 r2sshorter = 0;
}


plot z3 = if r2sshorter and rect2_hi > 0 then rect2_hi else na;
plot z4 = if r2sshorter and rect2_lo > 0 then rect2_lo else na;
z3.SetDefaultColor(Color.magenta);
z3.setlineweight(1);
z3.hidebubble();
z4.SetDefaultColor(Color.magenta);
z4.setlineweight(1);
z4.hidebubble();

def r2top = if r2sshorter and rect2_hi > 0 then rect2_hi else na;
def r2bot = if r2sshorter and rect2_lo > 0 then rect2_lo else na;

addcloud(r2top,r2bot,color.gray);

#--------------------
# % lines after peak

def rect2_en = if bn == 1 then 0
 else if r2sshorter then 1
 else rect2_en[1];


def hi;
def lo;
if bn == 1 or isnan(close[1]) then {
 hi = na;
 lo = na;
} else if bn == (hihibn2+1) and rect2_en then {
 hi = rect1_hi[1];
 lo = rect2_lo[rectangle1_bars + 1];
} else {
 hi = hi[1];
 lo = lo[1];
}

# hi = 0%
# lo = 100% drop
def rng = hi-lo;

input drop_percent = 20;
# purple line

#23.6%, 38.2%, 50%, 61.8%, and 78.6%. 
input line_percent1 = 23.6;
input line_percent2 = 38.2;
input line_percent3 = 50.0;
input line_percent4 = 61.8;
input line_percent5 = 78.6;
#hint line_percent1: Enter 0 to turn line off
#hint line_percent2: Enter 0 to turn line off
#hint line_percent3: Enter 0 to turn line off
#hint line_percent4: Enter 0 to turn line off
#hint line_percent5: Enter 0 to turn line off


plot drop00 = hi;
plot drop100 = lo;
drop00.setdefaultcolor(getcolor(8));
drop100.setdefaultcolor(getcolor(8));

plot drop1 = if drop_percent == line_percent1 or line_percent1 == 0 then na else (drop00 - (line_percent1/100*rng));
plot drop2 = if drop_percent == line_percent2 or line_percent2 == 0 then na else (drop00 - (line_percent2/100*rng));
plot drop3 = if drop_percent == line_percent3 or line_percent3 == 0 then na else (drop00 - (line_percent3/100*rng));
plot drop4 = if drop_percent == line_percent4 or line_percent4 == 0 then na else (drop00 - (line_percent4/100*rng));
plot drop5 = if drop_percent == line_percent5 or line_percent5 == 0 then na else (drop00 - (line_percent5/100*rng));
drop1.setdefaultcolor(color.gray);
drop2.setdefaultcolor(color.gray);
drop3.setdefaultcolor(color.gray);
drop4.setdefaultcolor(color.gray);
drop5.setdefaultcolor(color.gray);


plot drop_per = drop00 - (drop_percent/100*rng);
drop_per.setdefaultcolor(color.magenta);

input show_sell_cloud = yes;
def cldtop = if show_sell_cloud then drop_per else na;
addcloud(cldtop, drop100, color.magenta);

input show_percent_bubbles = yes;

def bub_off = 2;
addchartbubble(show_percent_bubbles and rect2_en[bub_off] and !isnan(close[bub_off]) and isnan(close[bub_off-1]), drop00[bub_off],
"0%"
, color.yellow, yes);

addchartbubble(show_percent_bubbles and rect2_en[bub_off] and !isnan(close[bub_off]) and isnan(close[bub_off-1]), drop1[bub_off],
line_percent1 + "%"
, color.yellow, no);

addchartbubble(show_percent_bubbles and rect2_en[bub_off] and !isnan(close[bub_off]) and isnan(close[bub_off-1]), drop2[bub_off],
line_percent2 + "%"
, color.yellow, no);

addchartbubble(show_percent_bubbles and rect2_en[bub_off] and !isnan(close[bub_off]) and isnan(close[bub_off-1]), drop3[bub_off],
line_percent3 + "%"
, color.yellow, no);

addchartbubble(show_percent_bubbles and rect2_en[bub_off] and !isnan(close[bub_off]) and isnan(close[bub_off-1]), drop4[bub_off],
line_percent4 + "%"
, color.yellow, no);

addchartbubble(show_percent_bubbles and rect2_en[bub_off] and !isnan(close[bub_off]) and isnan(close[bub_off-1]), drop5[bub_off],
line_percent5 + "%"
, color.yellow, no);

addchartbubble(show_percent_bubbles and rect2_en[bub_off] and !isnan(close[bub_off]) and isnan(close[bub_off-1]), drop100[bub_off],
"100%"
, color.yellow, no);

# target %
addchartbubble(show_percent_bubbles and rect2_en[bub_off] and !isnan(close[bub_off]) and isnan(close[bub_off-1]), drop_per[bub_off],
drop_percent + "%"
, color.cyan, yes);


# close is in sell range  x% to 100%
def x = (close <= drop_per and close > drop100);
#alert(a, "crossed up" ,alert.BAR, sound.DING);
alert(x, "sell" ,alert.BAR, sound.bell);


#--------------------
# test stuff

addchartbubble(0, low*0.995,
 r2sshorter + "\n" +
rect2_en + "\n" +
 rect2_hi + "\n" +
 rect1_hi + "\n" +
 rect1_hi[-rectangle2_bars]
, color.yellow, no);


input test1_bub = no;
addchartbubble(test1_bub, low*0.99,
 bn + "  BN\n" +
 hihibn2 + " pk bn\n" +
 rect1_firstbn + "-" + rect1_lastbn + "\n" +
  rect1_hi + " R1\n" +
  rect1_lo + " R1\n" +
 rect2_firstbn + "-" + rect2_lastbn + "\n" +
  rect2_hi + " R2\n" +
  rect2_lo + " R2\n" 
, (if rect1 then color.green else if rect2 then color.magenta else color.gray), no);


addchartbubble(0, low,
drop_per + "\n" +
close + "\n" +
drop100
, color.yellow, no);

#addverticalline(rect2, "-");
#
 

Attachments

  • 00f2 GM 15min.JPG
    00f2 GM 15min.JPG
    62.1 KB · Views: 74
  • 00f2 RTX 15min.JPG
    00f2 RTX 15min.JPG
    66.7 KB · Views: 77

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