Swing Highs / Lows & Candle Patterns For ThinkOrSwim

Cheeter

New member
This looks like a very interesting indicator
that would work well with support resistance. Could someone please convert it to Thinkscript.
Thank you Cheeter

Last edited by a moderator:

samer800

Well-known member
This looks like a very interesting indicator
that would work well with support resistance. Could someone please convert it to Thinkscript.
Thank you Cheeter

//@version=4
study("Swing Highs/Lows & Candle Patterns",overlay=true)
length = input(21)
//------------------------------------------------------------------------------
o = open[length],h = high[length]
l = low[length],c = close[length]
//------------------------------------------------------------------------------
ph = pivothigh(close,length,length)
pl = pivotlow(open,length,length)
valH = valuewhen(ph,c,0)
valL = valuewhen(pl,c,0)
valpH = valuewhen(ph,c,1)
valpL = valuewhen(pl,c,1)
//------------------------------------------------------------------------------
d = abs(c - o)
hammer = pl and min(o,c) - l > d and h - max(c,o) < d
ihammer = pl and h - max(c,o) > d and min(c,o) - l < d
bulleng = c > o and c[1] < o[1] and c > o[1] and o < c[1]
hanging = ph and min(c,o) - l > d and h - max(o,c) < d
shooting = ph and h - max(o,c) > d and min(c,o) - l < d
beareng = c > o and c[1] < o[1] and c > o[1] and o < c[1]
//------------------------------------------------------------------------------
//Descriptions
//------------------------------------------------------------------------------
hammer_ = "The hammer candlestick pattern is formed of a short body with a long lower wick, and is found at the bottom of a downward trend."
+ "\n" + "\n A hammer shows that although there were selling pressures during the day, ultimately a strong buying pressure drove the price back up."
ihammer_ = "The inverted hammer is a similar pattern than the hammer pattern. The only difference being that the upper wick is long, while the lower wick is short."
+ "\n" + "\n It indicates a buying pressure, followed by a selling pressure that was not strong enough to drive the market price down. The inverse hammer suggests that buyers will soon have control of the market."
bulleng_ = "The bullish engulfing pattern is formed of two candlesticks. The first candle is a short red body that is completely engulfed by a larger green candle"
+ "\n" + "\n Though the second day opens lower than the first, the bullish market pushes the price up, culminating in an obvious win for buyers"
hanging_ = "The hanging man is the bearish equivalent of a hammer; it has the same shape but forms at the end of an uptrend."
+ "\n" + "It indicates that there was a significant sell-off during the day, but that buyers were able to push the price up again. The large sell-off is often seen as an indication that the bulls are losing control of the market."
shotting_ = "The shooting star is the same shape as the inverted hammer, but is formed in an uptrend: it has a small lower body, and a long upper wick."
+ "\n" + "Usually, the market will gap slightly higher on opening and rally to an intra-day high before closing at a price just above the open – like a star falling to the ground."
beareng_ = "A bearish engulfing pattern occurs at the end of an uptrend. The first candle has a small green body that is engulfed by a subsequent long red candle."
+ "\n" + "It signifies a peak or slowdown of price movement, and is a sign of an impending market downturn. The lower the second candle goes, the more significant the trend is likely to be."
//------------------------------------------------------------------------------
n = bar_index
label lbl = na
H = valH > valpH ? "HH" : valH < valpH ? "LH" : na
L = valL < valpL ? "LL" : valL > valpL ? "HL" : na
txt = hammer ? "Hammer" : ihammer ? "Inverse Hammer" :
bulleng ? "Bullish Engulfing" : hanging ? "Hanging Man" :
shooting ? "Shooting Star" : beareng ? "Bearish Engulfing" : "None"
des = hammer ? hammer_ : ihammer ? ihammer_ :
bulleng ? bulleng_ : hanging ? hanging_ :
shooting ? shotting_ : beareng ? beareng_ : ""
if ph
lbl := label.new(n[length],max(c,o),H + "\n" + txt,color=#ff1100,
style=label.style_label_down,textcolor=color.white,tooltip=des)
label.delete(lbl[1])
else if pl
lbl := label.new(n[length],min(c,o),L + "\n" + txt,color=#2157f3,
style=label.style_label_up,textcolor=color.white,tooltip=des)
label.delete(lbl[1])
//------------------------------------------------------------------------------
check it out

CSS:
``````#//This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
#//@version=4
#study("Swing Highs/Lows & Candle Patterns",overlay=true)
# Converted by [email protected] - 09/2022

declare upper;

input Length   = 21;
input PatternName = yes;
input HighLowBubble = yes;

def na = Double.NaN;      # non-numeric values
#valuewhen (cond, source, occurrence) =>
script valuewhen {
input cond = 0;
input source = 0;
input occurrence = 0;
def n = occurrence + 1;
# start at 0 so it looks at current bar
def offset2 = fold j = 0 to 200
with p
while p < n + 1
do p + ( if p == n then j - n else if GetValue(cond, j) then 1 else 0 );
# def bnz = bn - offset2 + 1;
plot price = GetValue(source, offset2 - 1);
}
script FindPivots {
input dat = high; # default data or study being evaluated
input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
input PF  = 1;    # default pivot forward period
input PB  = 5;    # default pivot backward period
##############
def _nan;    # used for non-number returns
def _BN;     # the current barnumber
def _VStop;  # confirms that the lookforward period continues the pivot trend
def _V;      # the Value at the actual pivot point
def _VBar;   # the bar number at the pivot point
def _PV;     # the previous pivot Value
def _PVBar;  # the previous pivot bar number
def _VDiff;  # the difference in values between last two pivot points
def _VDist;  # the diffence in barnumbers between last two pivot points
def _VSlope; # the Slope calculated using value and distance changes
def _VPivot; # used for the pivot point connector line only
##############
_BN  = BarNumber();
_nan = Double.NaN;
_VStop =
fold a = 1 to PF + 1
with b = 1 while b
do if HL > 0 then
dat > GetValue(dat, -a) else
dat < GetValue(dat, -a) ;
if (HL > 0) {
_V = if _BN > PB and dat == Highest(dat, PB) and _VStop
then dat else _nan;
} else {
_V = if _BN > PB and dat == Lowest(dat, PB) and _VStop
then dat else _nan;
}
;
_VBar   = if !IsNaN(_V) then _BN else _VBar[1];
_PV     = if !IsNaN(_V) then GetValue(dat, _BN - _VBar[1]) else _PV[1];
_PVBar  = if   _VBar != _VBar[1]
then _PVBar[1] else _VBar;
_VDiff  = AbsValue(_V) - AbsValue(_PV);
_VDist  = _BN - _PVBar;
_VSlope = if _V > _PV  then 1 else
if _V < _PV  then -1 else 0;
if (HL > 0) {
_VPivot = _BN >= HighestAll(_PVBar);
} else {
_VPivot = _BN >= LowestAll(_PVBar);
}
;
plot result = if !IsNaN(_V) and _VStop then _V else _nan; #return the final _dat value at the most recent pivot point (same as V)
}

def ph_1 =  findpivots(close, 1, Length, Length);
def pl_1 =  findpivots(open, -1, Length, Length);

def ph = !isnan(ph_1);
def pl = !isnan(pl_1);

def valH = valuewhen(ph,close[length],0);
def valL = valuewhen(pl,close[length],0);
def valpH = valuewhen(ph,close[length],1);
def valpL = valuewhen(pl,close[length],1);
#//--------------------------------------------------------------------
def d = AbsValue(close - open);
def hammer   = pl and min(open,close) - low> d
and high - max(close,open) < d;
def ihammer  = pl and high - max(close,open) > d and
min(close,open) - low < d;
def bulleng  = close>open and close[1]<open[1]
and close>open[1] and open<close[1];
def hanging  = ph and min(close,open) - low>d
and high - max(open,close)<d;
def shooting = ph and high - max(open,close)>d
and min(close,open) - low < d;
def beareng  = close<open and close[1]>open[1]
and close<open[1] and open>close[1];
#//----------------------------------------------------------------------------

def Higher = if valH > valpH then 22 else if valH < valpH then 12 else na;
def Lower  = if valL < valpL then 11 else if valL > valpL then 21 else na;

addchartbubble(HighLowBubble and Lower ==11, pl_1, "LL", CreateColor(33,87,243), NO);
addchartbubble(HighLowBubble and Lower ==21, pl_1, "HL", Color.BLUE, no);

addchartbubble(PatternName and hammer  and Lower, pl_1, "Hammer" , CreateColor(33,87,243), no);
addchartbubble(PatternName and ihammer and Lower, pl_1, "InvHammer", CreateColor(33,87,243), no);
addchartbubble(PatternName and bulleng and Lower, pl_1, "BullEng", CreateColor(33,87,243), NO);
addchartbubble(PatternName and hanging and Lower, pl_1, "Hanging", CreateColor(33,87,243), NO);
addchartbubble(PatternName and shooting and Lower, pl_1, "ShootStar",CreateColor(33,87,243), NO);
addchartbubble(PatternName and beareng and Lower, pl_1,  "BearEng", CreateColor(33,87,243), NO);

addchartbubble(PatternName and hammer  and Higher, ph_1,"Hammer" , Color.RED, yes);
addchartbubble(PatternName and ihammer and Higher, ph_1,"InvHammer", Color.DARK_RED, yes);
addchartbubble(PatternName and bulleng and Higher, ph_1,"BullEng", Color.RED, yes);
addchartbubble(PatternName and hanging and Higher, ph_1,"Hanging", Color.RED, yes);
addchartbubble(PatternName and shooting and Higher, ph_1,"ShootStar",Color.RED, yes);
addchartbubble(PatternName and beareng and Higher, ph_1, "BearEng", Color.RED, yes);

addchartbubble(HighLowBubble and Higher==22, ph_1, "HH", Color.RED, yes);
addchartbubble(HighLowBubble and Higher==12, ph_1, "LH", Color.DARK_RED, yes);

#### END``````

Cheeter

New member
Thank you samer800. Absolutely brilliant work on your part.

majidg

Member
Thank you samer800. Absolutely brilliant work on your part.
This is not the same as the chart above it.

samer800

Well-known member
This is not the same as the chart above it.
pic above the code from Trading View. TOS code results may vary due various reasons.

Cheeter

New member
It's all good. I am not using the candle signals just the highs and lows for a quick visual of direction.
Thanks. Cheeter

Member
pic above the code from Trading View. TOS code results may vary due various reasons.
How real time are these ?Thanks

majidg

Member
check it out

CSS:
``````#//This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
#//@version=4
#study("Swing Highs/Lows & Candle Patterns",overlay=true)
# Converted by [email protected] - 09/2022

declare upper;

input Length   = 21;
input PatternName = yes;
input HighLowBubble = yes;

def na = Double.NaN;      # non-numeric values
#valuewhen (cond, source, occurrence) =>
script valuewhen {
input cond = 0;
input source = 0;
input occurrence = 0;
def n = occurrence + 1;
# start at 0 so it looks at current bar
def offset2 = fold j = 0 to 200
with p
while p < n + 1
do p + ( if p == n then j - n else if GetValue(cond, j) then 1 else 0 );
# def bnz = bn - offset2 + 1;
plot price = GetValue(source, offset2 - 1);
}
script FindPivots {
input dat = high; # default data or study being evaluated
input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
input PF  = 1;    # default pivot forward period
input PB  = 5;    # default pivot backward period
##############
def _nan;    # used for non-number returns
def _BN;     # the current barnumber
def _VStop;  # confirms that the lookforward period continues the pivot trend
def _V;      # the Value at the actual pivot point
def _VBar;   # the bar number at the pivot point
def _PV;     # the previous pivot Value
def _PVBar;  # the previous pivot bar number
def _VDiff;  # the difference in values between last two pivot points
def _VDist;  # the diffence in barnumbers between last two pivot points
def _VSlope; # the Slope calculated using value and distance changes
def _VPivot; # used for the pivot point connector line only
##############
_BN  = BarNumber();
_nan = Double.NaN;
_VStop =
fold a = 1 to PF + 1
with b = 1 while b
do if HL > 0 then
dat > GetValue(dat, -a) else
dat < GetValue(dat, -a) ;
if (HL > 0) {
_V = if _BN > PB and dat == Highest(dat, PB) and _VStop
then dat else _nan;
} else {
_V = if _BN > PB and dat == Lowest(dat, PB) and _VStop
then dat else _nan;
}
;
_VBar   = if !IsNaN(_V) then _BN else _VBar[1];
_PV     = if !IsNaN(_V) then GetValue(dat, _BN - _VBar[1]) else _PV[1];
_PVBar  = if   _VBar != _VBar[1]
then _PVBar[1] else _VBar;
_VDiff  = AbsValue(_V) - AbsValue(_PV);
_VDist  = _BN - _PVBar;
_VSlope = if _V > _PV  then 1 else
if _V < _PV  then -1 else 0;
if (HL > 0) {
_VPivot = _BN >= HighestAll(_PVBar);
} else {
_VPivot = _BN >= LowestAll(_PVBar);
}
;
plot result = if !IsNaN(_V) and _VStop then _V else _nan; #return the final _dat value at the most recent pivot point (same as V)
}

def ph_1 =  findpivots(close, 1, Length, Length);
def pl_1 =  findpivots(open, -1, Length, Length);

def ph = !isnan(ph_1);
def pl = !isnan(pl_1);

def valH = valuewhen(ph,close[length],0);
def valL = valuewhen(pl,close[length],0);
def valpH = valuewhen(ph,close[length],1);
def valpL = valuewhen(pl,close[length],1);
#//--------------------------------------------------------------------
def d = AbsValue(close - open);
def hammer   = pl and min(open,close) - low> d
and high - max(close,open) < d;
def ihammer  = pl and high - max(close,open) > d and
min(close,open) - low < d;
def bulleng  = close>open and close[1]<open[1]
and close>open[1] and open<close[1];
def hanging  = ph and min(close,open) - low>d
and high - max(open,close)<d;
def shooting = ph and high - max(open,close)>d
and min(close,open) - low < d;
def beareng  = close<open and close[1]>open[1]
and close<open[1] and open>close[1];
#//----------------------------------------------------------------------------

def Higher = if valH > valpH then 22 else if valH < valpH then 12 else na;
def Lower  = if valL < valpL then 11 else if valL > valpL then 21 else na;

addchartbubble(HighLowBubble and Lower ==11, pl_1, "LL", CreateColor(33,87,243), NO);
addchartbubble(HighLowBubble and Lower ==21, pl_1, "HL", Color.BLUE, no);

addchartbubble(PatternName and hammer  and Lower, pl_1, "Hammer" , CreateColor(33,87,243), no);
addchartbubble(PatternName and ihammer and Lower, pl_1, "InvHammer", CreateColor(33,87,243), no);
addchartbubble(PatternName and bulleng and Lower, pl_1, "BullEng", CreateColor(33,87,243), NO);
addchartbubble(PatternName and hanging and Lower, pl_1, "Hanging", CreateColor(33,87,243), NO);
addchartbubble(PatternName and shooting and Lower, pl_1, "ShootStar",CreateColor(33,87,243), NO);
addchartbubble(PatternName and beareng and Lower, pl_1,  "BearEng", CreateColor(33,87,243), NO);

addchartbubble(PatternName and hammer  and Higher, ph_1,"Hammer" , Color.RED, yes);
addchartbubble(PatternName and ihammer and Higher, ph_1,"InvHammer", Color.DARK_RED, yes);
addchartbubble(PatternName and bulleng and Higher, ph_1,"BullEng", Color.RED, yes);
addchartbubble(PatternName and hanging and Higher, ph_1,"Hanging", Color.RED, yes);
addchartbubble(PatternName and shooting and Higher, ph_1,"ShootStar",Color.RED, yes);
addchartbubble(PatternName and beareng and Higher, ph_1, "BearEng", Color.RED, yes);

addchartbubble(HighLowBubble and Higher==22, ph_1, "HH", Color.RED, yes);
addchartbubble(HighLowBubble and Higher==12, ph_1, "LH", Color.DARK_RED, yes);

#### END``````

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?