# Finding an intermediate term high

#### Cribbage

##### Member
I made the script below using the TOS candle pattern tool to paint swing highs and lows. That is a low with two higher candles on either side of it (inverse is true for highs), forming a short-term low (STL). I'm trying to add intermediate term swing high/lows. That would be a STL with a STH on either side of it. I tried to build this by referencing the last 3 candles (down at line 50 and beyond), but it does not appear to be working, frankly, I'm not sure what its painting (all the cyan wedges). I've circled where I would like it to paint the cyan wedge based on the logic described.

Any help is appreciated!

Code:
``````#Cribbage ICT Swing Highs and Lows: 3 candle pattern 5/26/22#

#input displace = 1;

#SleepyZ Fix 5/28/22 1 of 2
input wedgemover = -1;#Hint wedgemover: increase/decrease arrowmover to move arrowmover right/left
def n  = wedgemover;
def n1 = n + 1;

#SleepyZ Fix 5/28/22 1 of 2 end

#plot AvgExp = ExpAverage(price[-displace], length);

def IsUp = close > open;
def IsDown = close < open;
def IsDoji = IsDoji();
def avgRange = 0.05 * Average(high - low, 20);

#Swing Low

def SL =
(IsUp[2] or IsDown[2] or IsDoji[2]) and
(IsDown[1] or IsUp[1] or IsDoji[1]) and
(IsUp[0] or IsDown[0] or IsDoji[0]) and
low[1] < low[0] and
low[1] is equal to Lowest(low[1], 2);

#Swing High

plot SH =
(IsUp[2] or IsDown[2] or IsDoji[2]) and
(IsDown[1] or IsUp[1] or IsDoji[1]) and
(IsUp[0] or IsDown[0] or IsDoji[0]) and
high[1] > high[0] and
high[1] is equal to Highest(high[1], 2);

#SleepyZ Fix 5/28/22 2 of 2, adjusted by Cribbage to fit code

plot SHH = SH[n];

SHH.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
SHH.SetDefaultColor(Color.UPTICK);

plot SLL = SL[n];

SLL.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
SLL.SetDefaultColor(Color.DOWNTICK);

#SleepyZ Fix 5/28/22 2 of 2 end

def study = SHH;

plot ITH = Study[2] < study[1] and study[1] > study;

ITH.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
ITH.SetDefaultColor(Color.cyan);
ITH.setlineWeight(3);``````

Solution
I made the script below using the TOS candle pattern tool to paint swing highs and lows. That is a low with two higher candles on either side of it (inverse is true for highs), forming a short-term low (STL). I'm trying to add intermediate term swing high/lows. That would be a STL with a STH on either side of it. I tried to build this by referencing the last 3 candles (down at line 50 and beyond), but it does not appear to be working, frankly, I'm not sure what its painting (all the cyan wedges). I've circled where I would like it to paint the cyan wedge based on the logic described.

Any help is appreciated!

Code:
``````#Cribbage ICT Swing Highs and Lows: 3 candle pattern 5/26/22#

#input displace = 1;

#SleepyZ Fix 5/28/22 1...``````
I made the script below using the TOS candle pattern tool to paint swing highs and lows. That is a low with two higher candles on either side of it (inverse is true for highs), forming a short-term low (STL). I'm trying to add intermediate term swing high/lows. That would be a STL with a STH on either side of it. I tried to build this by referencing the last 3 candles (down at line 50 and beyond), but it does not appear to be working, frankly, I'm not sure what its painting (all the cyan wedges). I've circled where I would like it to paint the cyan wedge based on the logic described.

Any help is appreciated!

Code:
``````#Cribbage ICT Swing Highs and Lows: 3 candle pattern 5/26/22#

#input displace = 1;

#SleepyZ Fix 5/28/22 1 of 2
input wedgemover = -1;#Hint wedgemover: increase/decrease arrowmover to move arrowmover right/left
def n  = wedgemover;
def n1 = n + 1;

#SleepyZ Fix 5/28/22 1 of 2 end

#plot AvgExp = ExpAverage(price[-displace], length);

def IsUp = close > open;
def IsDown = close < open;
def IsDoji = IsDoji();
def avgRange = 0.05 * Average(high - low, 20);

#Swing Low

def SL =
(IsUp[2] or IsDown[2] or IsDoji[2]) and
(IsDown[1] or IsUp[1] or IsDoji[1]) and
(IsUp[0] or IsDown[0] or IsDoji[0]) and
low[1] < low[0] and
low[1] is equal to Lowest(low[1], 2);

#Swing High

plot SH =
(IsUp[2] or IsDown[2] or IsDoji[2]) and
(IsDown[1] or IsUp[1] or IsDoji[1]) and
(IsUp[0] or IsDown[0] or IsDoji[0]) and
high[1] > high[0] and
high[1] is equal to Highest(high[1], 2);

#SleepyZ Fix 5/28/22 2 of 2, adjusted by Cribbage to fit code

plot SHH = SH[n];

SHH.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
SHH.SetDefaultColor(Color.UPTICK);

plot SLL = SL[n];

SLL.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
SLL.SetDefaultColor(Color.DOWNTICK);

#SleepyZ Fix 5/28/22 2 of 2 end

def study = SHH;

plot ITH = Study[2] < study[1] and study[1] > study;

ITH.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
ITH.SetDefaultColor(Color.cyan);
ITH.setlineWeight(3);``````
hal_peak

you need to reevaluate your condition rules and list them out. your words don't match the code.

first thing, your code has a lot of extra nonsense in it, and it doesn't do what you describe.

just because a tool exists, like pattern generator or code wizard, doesn't mean they are capable of creating valid code. you need to study up on logic and variable offsets, so you can interpret the code you are creating.

your words don't mention looking for trending up or down candles, but the formulas include isup and isdown ?

i assume SL is a short term low? , to find a lower low?
the first 3 lines of this formula are useless. they test for every possible scenario, so they don't tell you anything.

def SL =
(IsUp[2] or IsDown[2] or IsDoji[2]) and
(IsDown[1] or IsUp[1] or IsDoji[1]) and
(IsUp[0] or IsDown[0] or IsDoji[0]) and

this checks , previous low < current low, in other words, is the current low higher than the previous low.

low[1] < low[0] and

this checks if the previous low is equal to the previous low or the low before that one.

low[1] is equal to Lowest(low[1], 2);

this formula doesn't tell you if the current low is a short term low.

there are no negative offsets used, so no future variables are checked (the bar after the current bar).
so you aren't checking 'either' side of the current candle.

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

i started from scratch and made this,

this is based on 3 bar patterns,
. peak , current bar has a higher high than bar before and after it
. valley , current bar has a lower low than bar before and after it

green and red dots on every valley and peak

cyan and yellow wedges (or dots) on intermediate peaks and valleys
(the previous and next signals are opposite. not nessesarily the next bars)

Code:
``````# hihi_lolo_alt_0

# Finding an intermediate term high

#plot AvgExp = ExpAverage(price[-displace], length);
#def IsUp = close > open;
#def IsDown = close < open;
#def IsDoji = IsDoji();
#def avgRange = 0.05 * Average(high - low, 20);

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

def na = double.nan;
def bn = barnumber();

# Swing Low
def lo = low[1] > low[0] and low[0] < low[-1];
# Swing High
def hi = high[1] < high[0] and high[0] > high[-1];

# recent hi or lo
def dir = if bn == 1 then 0
else if lo then -1
else if hi then 1
else dir[1];

def y = 0.001;
input show_dot_all_peaks_valleys = yes;
# plot each swing hi and lo
plot zlo = if show_dot_all_peaks_valleys and lo then (low*(1-(y*1))) else na;
zlo.SetPaintingStrategy(PaintingStrategy.points);
zlo.setlineweight(2);
zlo.SetDefaultColor(Color.red);
plot zhi = if show_dot_all_peaks_valleys and hi then (high*(1+(y*1))) else na;
zhi.SetPaintingStrategy(PaintingStrategy.points);
zhi.setlineweight(2);
zhi.SetDefaultColor(Color.green);

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

# look after a low for next signal
def lo_after = if lo and dir[1] > 0 then
(fold i = 1 to 40
with p = 1
while (!getvalue(lo, -i) and !getvalue(hi, -i))
#   do p + (-getvalue(lo, -(i+1)) + getvalue(hi, -(i+1))))
do p + 1)
else 0;

def lo2 = if !lo or lo_after == 0 then 0
else if getvalue(hi, -lo_after) then 1
else 0;

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

# look after a high for next signal
def hi_after = if hi and dir[1] < 0 then
(fold j = 1 to 40
with q = 1
while (!getvalue(lo, -j) and !getvalue(hi, -j))
#   do p + (-getvalue(lo, -(i+1)) + getvalue(hi, -(i+1))))
do q + 1)
else 0;

def hi2 = if !hi or hi_after == 0 then 0
else if getvalue(lo, -hi_after) then 1
else 0;

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

# place a dot on peaks and valleys that are surrounded by opposite signals
input show_intermediate_dots = no;
plot zlo2 = if show_intermediate_dots and lo2 then (low*(1-(y*2))) else na;
zlo2.SetPaintingStrategy(PaintingStrategy.points);
#zlo2.setlineweight(2);
zlo2.SetDefaultColor(Color.yellow);
plot zhi2 = if show_intermediate_dots and hi2 then (high*(1+(y*2))) else na;
zhi2.SetPaintingStrategy(PaintingStrategy.points);
#zhi2.setlineweight(2);
zhi2.SetDefaultColor(Color.cyan);

# place a wedge on peaks and valleys that are surrounded by opposite signals
input show_intermediate_wedges = yes;
plot zlo2w = if show_intermediate_wedges and lo2 then (low*(1-(y*2))) else na;
zlo2w.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_down);
zlo2w.setlineweight(2);
zlo2w.SetDefaultColor(Color.yellow);
plot zhi2w = if show_intermediate_wedges and hi2 then (high*(1+(y*2))) else na;
zhi2w.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
zhi2w.setlineweight(2);
zhi2w.SetDefaultColor(Color.cyan);

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

input test_signal = no;
def ref = lowest(low, 500);
plot r = if test_signal then (ref + dir) else na;

dir + "\n" +
lo + "\n" +
hi + "\n" +
lo_after + "\n" +
lo2
, color.yellow, no);
#``````

peak , higher high than bar before and after
valley , lower low than bar before and after
green and red dots on every valley and peak
cyan and yellow wedges on intermediate peaks and valleys
(the previous and next signals are opposite, not nessesarily the next bars)

Last edited:
I made the script below using the TOS candle pattern tool to paint swing highs and lows. That is a low with two higher candles on either side of it (inverse is true for highs), forming a short-term low (STL). I'm trying to add intermediate term swing high/lows. That would be a STL with a STH on either side of it. I tried to build this by referencing the last 3 candles (down at line 50 and beyond), but it does not appear to be working, frankly, I'm not sure what its painting (all the cyan wedges). I've circled where I would like it to paint the cyan wedge based on the logic described.

Any help is appreciated!

Code:
``````#Cribbage ICT Swing Highs and Lows: 3 candle pattern 5/26/22#

#input displace = 1;

#SleepyZ Fix 5/28/22 1 of 2
input wedgemover = -1;#Hint wedgemover: increase/decrease arrowmover to move arrowmover right/left
def n  = wedgemover;
def n1 = n + 1;

#SleepyZ Fix 5/28/22 1 of 2 end

#plot AvgExp = ExpAverage(price[-displace], length);

def IsUp = close > open;
def IsDown = close < open;
def IsDoji = IsDoji();
def avgRange = 0.05 * Average(high - low, 20);

#Swing Low

def SL =
(IsUp[2] or IsDown[2] or IsDoji[2]) and
(IsDown[1] or IsUp[1] or IsDoji[1]) and
(IsUp[0] or IsDown[0] or IsDoji[0]) and
low[1] < low[0] and
low[1] is equal to Lowest(low[1], 2);

#Swing High

plot SH =
(IsUp[2] or IsDown[2] or IsDoji[2]) and
(IsDown[1] or IsUp[1] or IsDoji[1]) and
(IsUp[0] or IsDown[0] or IsDoji[0]) and
high[1] > high[0] and
high[1] is equal to Highest(high[1], 2);

#SleepyZ Fix 5/28/22 2 of 2, adjusted by Cribbage to fit code

plot SHH = SH[n];

SHH.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
SHH.SetDefaultColor(Color.UPTICK);

plot SLL = SL[n];

SLL.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
SLL.SetDefaultColor(Color.DOWNTICK);

#SleepyZ Fix 5/28/22 2 of 2 end

def study = SHH;

plot ITH = Study[2] < study[1] and study[1] > study;

ITH.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
ITH.SetDefaultColor(Color.cyan);
ITH.setlineWeight(3);``````

if you want to be able to pick the quantity of bars, before and after, a peak or valley, that define them ( instead of 1), try this code,

Code:
``````# template_peaks_valleys_off_robert_01
#------------------------
# define swing low points , robert payne
# modifified by halcyonguy to ignore last bar
#------------------------

def bn = BarNumber();
def na = double.nan;

input length = 7;

def lastbn = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(length - 1, lastbn - bn);

input ignore_last_bar = yes;
def ignorelast = if (ignore_last_bar and bn == lastbn) then 0 else 1;

def peak = ignorelast and high > highest(high[1], length - 1) and high == GetValue(highest(high, length), -offset);
def valley = ignorelast and low < Lowest(low[1], length - 1) and low == GetValue(Lowest(low, length), -offset);

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

input show_arrows = yes;
def vert = 0.001;
plot z1 = if show_arrows and peak then high*(1+vert) else na;
z1.SetPaintingStrategy(PaintingStrategy.arrow_down);
z1.SetDefaultColor(Color.cyan);
z1.setlineweight(3);
z1.hidebubble();

plot z2 = if show_arrows and valley then low*(1-vert) else na;
z2.SetPaintingStrategy(PaintingStrategy.arrow_up);
z2.SetDefaultColor(Color.cyan);
z2.setlineweight(3);
z2.hidebubble();
#``````

you need to reevaluate your condition rules and list them out. your words don't match the code.

first thing, your code has a lot of extra nonsense in it, and it doesn't do what you describe.

just because a tool exists, like pattern generator or code wizard, doesn't mean they are capable of creating valid code. you need to study up on logic and variable offsets, so you can interpret the code you are creating.

your words don't mention looking for trending up or down candles, but the formulas include isup and isdown ?

i assume SL is a short term low? , to find a lower low?
the first 3 lines of this formula are useless. they test for every possible scenario, so they don't tell you anything.

def SL =
(IsUp[2] or IsDown[2] or IsDoji[2]) and
(IsDown[1] or IsUp[1] or IsDoji[1]) and
(IsUp[0] or IsDown[0] or IsDoji[0]) and

this checks , previous low < current low, in other words, is the current low higher than the previous low.

low[1] < low[0] and

this checks if the previous low is equal to the previous low or the low before that one.

low[1] is equal to Lowest(low[1], 2);

this formula doesn't tell you if the current low is a short term low.

there are no negative offsets used, so no future variables are checked (the bar after the current bar).
so you aren't checking 'either' side of the current candle.

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

i started from scratch and made this,

this is based on 3 bar patterns,
. peak , current bar has a higher high than bar before and after it
. valley , current bar has a lower low than bar before and after it

green and red dots on every valley and peak

cyan and yellow wedges (or dots) on intermediate peaks and valleys
(the previous and next signals are opposite. not nessesarily the next bars)

Code:
``````# hihi_lolo_alt_0

# Finding an intermediate term high

#plot AvgExp = ExpAverage(price[-displace], length);
#def IsUp = close > open;
#def IsDown = close < open;
#def IsDoji = IsDoji();
#def avgRange = 0.05 * Average(high - low, 20);

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

def na = double.nan;
def bn = barnumber();

# Swing Low
def lo = low[1] > low[0] and low[0] < low[-1];
# Swing High
def hi = high[1] < high[0] and high[0] > high[-1];

# recent hi or lo
def dir = if bn == 1 then 0
else if lo then -1
else if hi then 1
else dir[1];

def y = 0.001;
input show_dot_all_peaks_valleys = yes;
# plot each swing hi and lo
plot zlo = if show_dot_all_peaks_valleys and lo then (low*(1-(y*1))) else na;
zlo.SetPaintingStrategy(PaintingStrategy.points);
zlo.setlineweight(2);
zlo.SetDefaultColor(Color.red);
plot zhi = if show_dot_all_peaks_valleys and hi then (high*(1+(y*1))) else na;
zhi.SetPaintingStrategy(PaintingStrategy.points);
zhi.setlineweight(2);
zhi.SetDefaultColor(Color.green);

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

# look after a low for next signal
def lo_after = if lo and dir[1] > 0 then
(fold i = 1 to 40
with p = 1
while (!getvalue(lo, -i) and !getvalue(hi, -i))
#   do p + (-getvalue(lo, -(i+1)) + getvalue(hi, -(i+1))))
do p + 1)
else 0;

def lo2 = if !lo or lo_after == 0 then 0
else if getvalue(hi, -lo_after) then 1
else 0;

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

# look after a high for next signal
def hi_after = if hi and dir[1] < 0 then
(fold j = 1 to 40
with q = 1
while (!getvalue(lo, -j) and !getvalue(hi, -j))
#   do p + (-getvalue(lo, -(i+1)) + getvalue(hi, -(i+1))))
do q + 1)
else 0;

def hi2 = if !hi or hi_after == 0 then 0
else if getvalue(lo, -hi_after) then 1
else 0;

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

# place a dot on peaks and valleys that are surrounded by opposite signals
input show_intermediate_dots = no;
plot zlo2 = if show_intermediate_dots and lo2 then (low*(1-(y*2))) else na;
zlo2.SetPaintingStrategy(PaintingStrategy.points);
#zlo2.setlineweight(2);
zlo2.SetDefaultColor(Color.yellow);
plot zhi2 = if show_intermediate_dots and hi2 then (high*(1+(y*2))) else na;
zhi2.SetPaintingStrategy(PaintingStrategy.points);
#zhi2.setlineweight(2);
zhi2.SetDefaultColor(Color.cyan);

# place a wedge on peaks and valleys that are surrounded by opposite signals
input show_intermediate_wedges = yes;
plot zlo2w = if show_intermediate_wedges and lo2 then (low*(1-(y*2))) else na;
zlo2w.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_down);
zlo2w.setlineweight(2);
zlo2w.SetDefaultColor(Color.yellow);
plot zhi2w = if show_intermediate_wedges and hi2 then (high*(1+(y*2))) else na;
zhi2w.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
zhi2w.setlineweight(2);
zhi2w.SetDefaultColor(Color.cyan);

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

input test_signal = no;
def ref = lowest(low, 500);
plot r = if test_signal then (ref + dir) else na;

dir + "\n" +
lo + "\n" +
hi + "\n" +
lo_after + "\n" +
lo2
, color.yellow, no);
#``````

peak , higher high than bar before and after
valley , lower low than bar before and after
green and red dots on every valley and peak
cyan and yellow wedges on intermediate peaks and valleys
(the previous and next signals are opposite, not nessesarily the next bars)
Thanks for the reply - The pattern tool created the logic. You have to choose a candle color before you can start to choose where the candles need to be in relation to eachother (hence the isup/isdown code). I thought that by defining them it would essentially encapsulate it altogether for easy reference. Thanks for explaining all that. My day job is pretty far off from coding so a lot of this is by the seat of my ****s.

Of the 2 codes you posted, the first one is closer to what I'm looking for. As far as the concept I'm trying to illustrate, every swing low is a short term low until its in the context of the swing lows on either side of it. So in this case, I want the code to recognize every swing low. Then I want it to be able to decipher when there are 3 STLs in a row, call them STLs A B and C, if B is the lowest of the 3, that makes it an ITH that I'd like marked. So for a ITL I want STL-A > STL-B < STL-C. If STL-A < STL-B < STL-C then we're trending.

87k+ Posts
261 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?