Files
xPineScript/xSamples/indicators/candle_patterns.pine
T
2024-01-25 04:07:49 +03:30

244 lines
21 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © BlockchainSpecialists
//@version=4
study("Candlestick Pattern - All Patterns", overlay=true)
C_DownTrend = true
C_UpTrend = true
var trendRule1 = "Price 50 SMA"
var trendRule2 = "50 SMA and 200 SMA"
var trendRule = input(trendRule1, "Trend Detection Rule", options=[trendRule1, trendRule2, "None"])
if trendRule == trendRule1
priceAvg = sma(close, 50)
C_DownTrend := close < priceAvg
C_UpTrend := close > priceAvg
if trendRule == trendRule2
sma200 = sma(close, 200)
sma50 = sma(close, 50)
C_DownTrend := close < sma200 and sma50 < sma200
C_UpTrend := close > sma200 and sma50 > sma200
C_Len = 14 // ema depth for bodyAvg
C_ShadowPercent = 5.0 // size of shadows
C_ShadowEqualsPercent = 100.0
C_DojiBodyPercent = 5.0
C_Factor = 2.0 // shows the number of times the shadow dominates the candlestick body
C_BodyHi = max(close, open)
C_BodyLo = min(close, open)
C_Body = C_BodyHi - C_BodyLo
C_BodyAvg = ema(C_Body, C_Len)
C_SmallBody = C_Body < C_BodyAvg
C_LongBody = C_Body > C_BodyAvg
C_UpShadow = high - C_BodyHi
C_DnShadow = C_BodyLo - low
C_HasUpShadow = C_UpShadow > C_ShadowPercent / 100 * C_Body
C_HasDnShadow = C_DnShadow > C_ShadowPercent / 100 * C_Body
C_WhiteBody = open < close
C_BlackBody = open > close
C_Range = high-low
C_IsInsideBar = C_BodyHi[1] > C_BodyHi and C_BodyLo[1] < C_BodyLo
C_BodyMiddle = C_Body / 2 + C_BodyLo
C_ShadowEquals = C_UpShadow == C_DnShadow or (abs(C_UpShadow - C_DnShadow) / C_DnShadow * 100) < C_ShadowEqualsPercent and (abs(C_DnShadow - C_UpShadow) / C_UpShadow * 100) < C_ShadowEqualsPercent
C_IsDojiBody = C_Range > 0 and C_Body <= C_Range * C_DojiBodyPercent / 100
C_Doji = C_IsDojiBody and C_ShadowEquals
C_oAbandonedBaby = C_UpTrend[2] and C_WhiteBody[2] and C_IsDojiBody[1] and high[2] < low[1] and C_BlackBody and low[1] > high
alertcondition(C_oAbandonedBaby, title = "Abandoned Baby")
if C_oAbandonedBaby
var tt = "Abandoned Baby\nA bearish abandoned baby is a specific candlestick pattern that often signals a downward reversal trend in terms of security price. It is formed when a gap appears between the lowest price of a doji-like candle and the candlestick of the day before. The earlier candlestick is green, tall, and has small shadows. The doji candle is also tailed by a gap between its lowest price point and the highest price point of the candle that comes next, which is red, tall and also has small shadows. The doji candle shadows must completely gap either below or above the shadows of the first and third day in order to have the abandoned baby pattern effect."
label.new(bar_index, low, yloc=yloc.abovebar, text="AB", style=label.style_label_down, color = color.red, textcolor=color.white, tooltip = tt)
C_oEngulfing = C_UpTrend and C_BlackBody and C_LongBody and C_WhiteBody[1] and C_SmallBody[1] and close <= open[1] and open >= close[1] and ( close < open[1] or open > close[1] )
alertcondition(C_oEngulfing, title = "Engulfing")
if C_oEngulfing
var tt = "Engulfing\nAt the end of a given uptrend, a reversal pattern will most likely appear. During the first day, this candlestick pattern uses a small body. It is then followed by a day where the candle body fully overtakes the body from the day before it and closes in the trend’s opposite direction. Although similar to the outside reversal chart pattern, it is not essential for this pattern to fully overtake the range (high to low), rather only the open and the close."
label.new(bar_index, low, yloc=yloc.abovebar, text="BE", style=label.style_label_down, color = color.red, textcolor=color.white, tooltip = tt)
C_oEveningStar = false
if C_LongBody[2] and C_SmallBody[1] and C_LongBody
if C_UpTrend and C_WhiteBody[2] and C_BodyLo[1] > C_BodyHi[2] and C_BlackBody and C_BodyLo <= C_BodyMiddle[2] and C_BodyLo > C_BodyLo[2] and C_BodyLo[1] > C_BodyHi
C_oEveningStar := true
alertcondition(C_oEveningStar, title = "Evening Star")
if C_oEveningStar
var tt = "Evening Star\nThis candlestick pattern is bearish and continues an uptrend with a long-bodied, green candle day. It is then followed by a gapped and small-bodied candle day, and concludes with a downward close. The close would be below the first day’s midpoint."
label.new(bar_index, low, yloc=yloc.abovebar, text="ES", style=label.style_label_down, color = color.red, textcolor=color.white, tooltip = tt)
C_GravestoneDoji = C_IsDojiBody and C_DnShadow <= C_Body
alertcondition(C_GravestoneDoji, title = "Gravestone Doji")
if C_GravestoneDoji
var tt = "Gravestone Doji\nWhen a doji is at or is close to the day’s low point, a doji line will develop."
label.new(bar_index, low, yloc=yloc.abovebar, text="GD", style=label.style_label_down, color = color.red, textcolor=color.white, tooltip = tt)
C_oHangingMan = false
if C_SmallBody and C_Body > 0 and C_BodyLo > hl2 and C_DnShadow >= C_Factor * C_Body and not C_HasUpShadow
if C_UpTrend
C_oHangingMan := true
alertcondition(C_oHangingMan, title = "Hanging Man")
if C_oHangingMan
var tt = "Hanging Man\nWhen a specified security notably moves lower after the open, but continues to rally to close above the intraday low, a Hanging Man candlestick will form. The candlestick will resemble a square, attached to a long stick-like figure. It is referred to as a Hanging Man if the candlestick forms during an advance."
label.new(bar_index, low, yloc=yloc.abovebar, text="HM", style=label.style_label_down, color = color.red, textcolor=color.white, tooltip = tt)
C_Harami = C_LongBody[1] and C_WhiteBody[1] and C_UpTrend[1] and C_IsDojiBody and C_IsInsideBar
alertcondition(C_Harami, title = "Harami")
if C_Harami
var tt = "Harami\nThis is a two-day candlestick pattern with a small, red-bodied candle that is entirely encompassed within the range that was once a green-bodied candle."
label.new(bar_index, low, yloc=yloc.abovebar, text="BH", style=label.style_label_down, color = color.red, textcolor=color.white, tooltip = tt)
C_MarubozuShadowPercent = 5.0
C_Marubozu = C_LongBody and C_UpShadow <= C_MarubozuShadowPercent/100*C_Body and C_DnShadow <= C_MarubozuShadowPercent/100*C_Body
C_MarubozuWhite = C_Marubozu and C_WhiteBody
C_MarubozuBlack = C_Marubozu and C_BlackBody
C_Kicking = C_MarubozuWhite[1] and C_MarubozuBlack and low[1] > high
alertcondition(C_Kicking, title = "Kicking")
if C_Kicking
var tt = "Kicking\nA bearish kicking pattern will occur, subsequently signaling a reversal for a new downtrend. The first day candlestick is a bullish marubozu. The second day gaps down extensively and opens below the opening price of the day before. There is a gap between day one and two’s bearish candlesticks."
label.new(bar_index, low, yloc=yloc.abovebar, text="K", style=label.style_label_down, color = color.red, textcolor=color.white, tooltip = tt)
C_LongShadowPercent = 75.0
C_LongUpperShadow = C_UpShadow > C_Range/100*C_LongShadowPercent
alertcondition(C_LongUpperShadow, title = "Long Upper Shadow")
if C_LongUpperShadow
var tt = "Long Upper Shadow\nTo indicate buyer domination of the first part of a session, candlesticks will present with long upper shadows, as well as short lower shadows, consequently raising bidding prices."
label.new(bar_index, low, yloc=yloc.abovebar, text="LUS", style=label.style_label_down, color = color.red, textcolor=color.white, tooltip = tt)
alertcondition(C_MarubozuBlack, title = "Marubozu Black")
if C_MarubozuBlack
var tt = "Marubozu Black\nThis is a candlestick that has no shadow, which extends from the red-bodied candle at the open, the close, or even at both. In Japanese, the name means “close-cropped” or “close-cut.” The candlestick can also be referred to as Bald or Shaven Head."
label.new(bar_index, low, yloc=yloc.abovebar, text="MB", style=label.style_label_down, color = color.red, textcolor=color.white, tooltip = tt)
C_oShootingStar = false
if C_SmallBody and C_Body > 0 and C_BodyHi < hl2 and C_UpShadow >= C_Factor * C_Body and not C_HasDnShadow
if C_UpTrend
C_oShootingStar := true
alertcondition(C_oShootingStar, title = "Shooting Star")
if C_oShootingStar
var tt = "Shooting Star\nThis single day pattern can appear during an uptrend and opens high, while it closes near its open. It trades much higher as well. It is bearish in nature, but looks like an Inverted Hammer."
label.new(bar_index, low, yloc=yloc.abovebar, text="SS", style=label.style_label_down, color = color.red, textcolor=color.white, tooltip = tt)
C_3BCrw_ShadowPercent = 5.0
C_3BCrw_HaveNotDnShadow = C_Range * C_3BCrw_ShadowPercent / 100 > C_DnShadow
C_oThreeBlackCrows = false
if C_LongBody and C_LongBody[1] and C_LongBody[2]
if C_BlackBody and C_BlackBody[1] and C_BlackBody[2]
C_oThreeBlackCrows := close < close[1] and close[1] < close[2] and open > close[1] and open < open[1] and open[1] > close[2] and open[1] < open[2] and C_3BCrw_HaveNotDnShadow and C_3BCrw_HaveNotDnShadow[1] and C_3BCrw_HaveNotDnShadow[2]
alertcondition(C_oThreeBlackCrows, title = "Three Black Crows")
if C_oThreeBlackCrows
var tt = "Three Black Crows\nThis is a bearish reversal pattern that consists of three long, red-bodied candles in immediate succession. For each of these candles, each day opens within the body of the day before and closes either at or near its low."
label.new(bar_index, low, yloc=yloc.abovebar, text="3BC", style=label.style_label_down, color = color.red, textcolor=color.white, tooltip = tt)
C_3Dojis = C_Doji[2] and C_Doji[1] and C_Doji
C_BodyGapUp = C_BodyHi[1] < C_BodyLo
C_BodyGapDn = C_BodyLo[1] > C_BodyHi
C_TriStar = C_3Dojis and C_UpTrend[2] and C_BodyGapUp[1] and C_BodyGapDn
alertcondition(C_TriStar, title = "Tri-Star")
if C_TriStar
var tt = "Tri-Star\nThis particular pattern can form when three doji candlesticks appear in immediate succession at the end of an extended uptrend. The first doji candle marks indecision between bull and bear. The second doji gaps in the direction of the leading trend. The third changes the attitude of the market once the candlestick opens in the direction opposite to the trend. Each doji candle has a shadow, all comparatively shallow, which signify an interim cutback in volatility."
label.new(bar_index, low, yloc=yloc.abovebar, text="3S", style=label.style_label_down, color = color.red, textcolor=color.white, tooltip = tt)
C_oAbandonedBaby1 = C_DownTrend[2] and C_BlackBody[2] and C_IsDojiBody[1] and low[2] > high[1] and C_WhiteBody and high[1] < low
alertcondition(C_oAbandonedBaby1, title = "Abandoned Baby")
if C_oAbandonedBaby1
var tt = "Abandoned Baby\nThis candlestick pattern is quite rare as far as reversal patterns go. The first of the pattern is a large down candle. Next comes a doji candle that gaps below the candle before it. The doji candle is then followed by another candle that opens even higher and swiftly moves to the upside."
label.new(bar_index, low, yloc=yloc.belowbar, text="AB", style=label.style_label_up, color = color.blue, textcolor=color.white, tooltip = tt)
C_DragonflyDoji1 = C_IsDojiBody and C_UpShadow <= C_Body
alertcondition(C_DragonflyDoji1, title = "Dragonfly Doji")
if C_DragonflyDoji1
var tt = "Dragonfly Doji\nSimilar to other Doji days, this particular Doji also regularly appears at pivotal market moments. This is a specific Doji where both the open and close price are at the high of a given day."
label.new(bar_index, low, yloc=yloc.belowbar, text="DD", style=label.style_label_up, color = color.blue, textcolor=color.white, tooltip = tt)
C_oEngulfing1 = C_DownTrend and C_WhiteBody and C_LongBody and C_BlackBody[1] and C_SmallBody[1] and close >= open[1] and open <= close[1] and ( close > open[1] or open < close[1] )
alertcondition(C_oEngulfing1, title = "Engulfing")
if C_oEngulfing1
var tt = "Engulfing\nAt the end of a given downward trend, there will most likely be a reversal pattern. To distinguish the first day, this candlestick pattern uses a small body, followed by a day where the candle body fully overtakes the body from the day before, and closes in the trend’s opposite direction. Although similar to the outside reversal chart pattern, it is not essential for this pattern to completely overtake the range (high to low), rather only the open and the close."
label.new(bar_index, low, yloc=yloc.belowbar, text="BE", style=label.style_label_up, color = color.blue, textcolor=color.white, tooltip = tt)
C_oHammer1 = false
if C_SmallBody and C_Body > 0 and C_BodyLo > hl2 and C_DnShadow >= C_Factor * C_Body and not C_HasUpShadow
if C_DownTrend
C_oHammer1 := true
alertcondition(C_oHammer1, title = "Hammer")
if C_oHammer1
var tt = "Hammer\nHammer candlesticks form when a security moves lower after the open, but continues to rally into close above the intraday low. The candlestick that you are left with will look like a square attached to a long stick-like figure. This candlestick is called a Hammer if it happens to form during a decline."
label.new(bar_index, low, yloc=yloc.belowbar, text="H", style=label.style_label_up, color = color.blue, textcolor=color.white, tooltip = tt)
C_Harami1 = C_LongBody[1] and C_BlackBody[1] and C_DownTrend[1] and C_IsDojiBody and C_IsInsideBar
alertcondition(C_Harami1, title = "Harami")
if C_Harami1
var tt = "Harami\nThis two-day candlestick pattern consists of a small-bodied green candle that is entirely encompassed within the range of what was once a red-bodied candle."
label.new(bar_index, low, yloc=yloc.belowbar, text="BH", style=label.style_label_up, color = color.blue, textcolor=color.white, tooltip = tt)
C_Kicking1 = C_MarubozuBlack[1] and C_MarubozuWhite and high[1] < low
alertcondition(C_Kicking1, title = "Kicking")
if C_Kicking1
var tt = "Kicking\nThe first day candlestick is a bearish marubozu candlestick with next to no upper or lower shadow and where the price opens at the day’s high and closes at the day’s low. The second day is a bullish marubozu pattern, with next to no upper or lower shadow and where the price opens at the day’s low and closes at the day’s high. Additionally, the second day gaps up extensively and opens above the opening price of the day before. This gap or window, as the Japanese call it, lies between day one and day two’s bullish candlesticks."
label.new(bar_index, low, yloc=yloc.belowbar, text="K", style=label.style_label_up, color = color.blue, textcolor=color.white, tooltip = tt)
C_oInvertedHammer = false
if C_SmallBody and C_Body > 0 and C_BodyHi < hl2 and C_UpShadow >= C_Factor * C_Body and not C_HasDnShadow
if C_DownTrend
C_oInvertedHammer := true
alertcondition(C_oInvertedHammer, title = "Inverted Hammer")
if C_oInvertedHammer
var tt = "Inverted Hammer\nIf in a downtrend, then the open is lower. When it eventually trades higher, but closes near its open, it will look like an inverted version of the Hammer Candlestick. This is a one-day bullish reversal pattern."
label.new(bar_index, low, yloc=yloc.belowbar, text="IH", style=label.style_label_up, color = color.blue, textcolor=color.white, tooltip = tt)
C_LongLowerShadow1 = C_DnShadow > C_Range/100*C_LongShadowPercent
alertcondition(C_LongLowerShadow1, title = "Long Lower Shadow")
if C_LongLowerShadow1
var tt = "Long Lower Shadow\nTo indicate seller domination of the first part of a session, candlesticks will present with long lower shadows and short upper shadows, consequently lowering prices."
label.new(bar_index, low, yloc=yloc.belowbar, text="LLS", style=label.style_label_up, color = color.blue, textcolor=color.white, tooltip = tt)
C_MarubozuWhite1 = C_Marubozu and C_WhiteBody
alertcondition(C_MarubozuWhite1, title = "Marubozu White")
if C_MarubozuWhite1
var tt = "Marubozu White\nA Marubozu White Candle is a candlestick that does not have a shadow that extends from its candle body at either the open or the close. Marubozu is Japanese for “close-cropped” or “close-cut.” Other sources may call it a Bald or Shaven Head Candle."
label.new(bar_index, low, yloc=yloc.belowbar, text="MW", style=label.style_label_up, color = color.blue, textcolor=color.white, tooltip = tt)
C_oMorningStar1 = false
if C_LongBody[2] and C_SmallBody[1] and C_LongBody
if C_DownTrend and C_BlackBody[2] and C_BodyHi[1] < C_BodyLo[2] and C_WhiteBody and C_BodyHi >= C_BodyMiddle[2] and C_BodyHi < C_BodyHi[2] and C_BodyHi[1] < C_BodyLo
C_oMorningStar1 := true
alertcondition(C_oMorningStar1, title = "Morning Star")
if C_oMorningStar1
var tt = "Morning Star\nA three-day bullish reversal pattern, which consists of three candlesticks will look something like this: The first being a long-bodied red candle that extends the current downtrend. Next comes a short, middle candle that gaps down on the open. After comes a long-bodied green candle, which gaps up on the open and closes above the midpoint of the body of the first day."
label.new(bar_index, low, yloc=yloc.belowbar, text="MS", style=label.style_label_up, color = color.blue, textcolor=color.white, tooltip = tt)
C_3WSld_ShadowPercent = 5.0
C_3WSld_HaveNotUpShadow = C_Range * C_3WSld_ShadowPercent / 100 > C_UpShadow
C_oThreeWhiteSoldiers = false
if C_LongBody and C_LongBody[1] and C_LongBody[2]
if C_WhiteBody and C_WhiteBody[1] and C_WhiteBody[2]
C_oThreeWhiteSoldiers := close > close[1] and close[1] > close[2] and open < close[1] and open > open[1] and open[1] < close[2] and open[1] > open[2] and C_3WSld_HaveNotUpShadow and C_3WSld_HaveNotUpShadow[1] and C_3WSld_HaveNotUpShadow[2]
alertcondition(C_oThreeWhiteSoldiers, title = "Three White Soldiers")
if C_oThreeWhiteSoldiers
var tt = "Three White Soldiers\nThis bullish reversal pattern is made up of three long-bodied, green candles in immediate succession. Each one opens within the body before it and the close is near to the daily high."
label.new(bar_index, low, yloc=yloc.belowbar, text="3WS", style=label.style_label_up, color = color.blue, textcolor=color.white, tooltip = tt)
C_TriStar1 = C_3Dojis and C_DownTrend[2] and C_BodyGapDn[1] and C_BodyGapUp
alertcondition(C_TriStar1, title = "Tri-Star")
if C_TriStar1
var tt = "Tri-Star\nA bullish TriStar candlestick pattern can form when three doji candlesticks materialize in immediate succession at the tail-end of an extended downtrend. The first doji candle marks indecision between bull and bear. The second doji gaps in the direction of the leading trend. The third changes the attitude of the market once the candlestick opens in the direction opposite to the trend. Each doji candle has a shadow, all comparatively shallow, which signify an interim cutback in volatility."
label.new(bar_index, low, yloc=yloc.belowbar, text="3S", style=label.style_label_up, color = color.blue, textcolor=color.white, tooltip = tt)
C_DragonflyDoji = C_IsDojiBody and C_UpShadow <= C_Body
alertcondition(C_Doji and not C_DragonflyDoji and not C_GravestoneDoji, title = "Doji")
if C_Doji and not C_DragonflyDoji and not C_GravestoneDoji
var tt = "Doji\nWhen the open and close of a security are essentially equal to each other, a doji candle forms. The length of both upper and lower shadows may vary, causing the candlestick you are left with to either resemble a cross, an inverted cross, or a plus sign. Doji candles show the playout of buyer-seller indecision in a tug-of-war of sorts. As price moves either above or below the opening level during the session, the close is either at or near the opening level."
label.new(bar_index, low, yloc=yloc.belowbar, text="D", style=label.style_label_center, color = color.gray, textcolor=color.white, tooltip = tt)
C_SpinningTopPercent = 34.0
C_IsSpinningTop = C_DnShadow >= C_Range / 100 * C_SpinningTopPercent and C_UpShadow >= C_Range / 100 * C_SpinningTopPercent and not C_IsDojiBody
C_SpinningTopBlack = C_IsSpinningTop and C_BlackBody
alertcondition(C_SpinningTopBlack, title = "Spinning Top Black")
if C_SpinningTopBlack
var tt = "Spinning Top Black\nBlack spinning tops are candlestick lines that are small, red-bodied, and possess shadows (upper and lower) that end up exceeding the length of candle bodies. They often signal indecision."
label.new(bar_index, low, yloc=yloc.belowbar, text="STB", style=label.style_label_center, color = color.gray, textcolor=color.white, tooltip = tt)
C_SpinningTopWhite = C_IsSpinningTop and C_WhiteBody
alertcondition(C_SpinningTopWhite, title = "Spinning Top White")
if C_SpinningTopWhite
var tt = "Spinning Top White\nWhite spinning tops are candlestick lines that are small, green-bodied, and possess shadows (upper and lower) that end up exceeding the length of candle bodies. They often signal indecision between buyer and seller."
label.new(bar_index, low, yloc=yloc.belowbar, text="STW", style=label.style_label_center, color = color.gray, textcolor=color.white, tooltip = tt)