mail@pastecode.io avatar
a month ago
4.5 kB

indicator('Abnormal Pin Bar', overlay=true)

show_bullish_pinbars = input(title='Show Bullish Pinbars', defval=true)
show_bearish_pinbars = input(title='Show Bearish Pinbars', defval=true)
body_ratio = float(input.float(title='Minimum body ratio in the pattern (%)', defval=2, minval=0, maxval=100, tooltip='e.g. 2% means the body must cover at least 2% of the candle height.')) / 100
long_shadow_body_ratio = float(input.float(title='Minimum ratio of the long shadow to the body', defval=4.5, minval=0, tooltip='e.g. 4.5 means the long shadow must be at least 4.5 times bigger than the body.'))
long_short_ratio = float(input.float(title='Minimum ratio of the long shadow to the short shadow', defval=2, minval=0, tooltip='e.g. 2 means the long shadow must be at least 2 times bigger than the short shadow.'))
check_ma_volume = input(title='Check if the volume is bigger than the Volume MA to confirm pin bar', defval=true)
volume_ma_len = input(title='Volume MA Length', defval=20)
check_pre_volume = input(title='Check if the volume is bigger than the previous volume to confirm pin bar', defval=true)
bullish_cbg_color = input.color(defval=color.new(#27ae60, 78), title='Bullish Background', inline='Bullish Style', group='Style')
bullish_bg_color = input.color(defval=color.new(#27ae60, 85), title='', inline='Bullish Style', group='Style')
bearish_cbg_color = input.color(defval=color.new(#e74c3c, 78), title='Bearish Background', inline='Bearish Style', group='Style')
bearish_bg_color = input.color(defval=color.new(#e74c3c, 85), title='', inline='Bearish Style', group='Style')
show_length_on_badge = input.bool(title='Show Long Shadow and Body Ratio', defval=true, group='Style')

height = high - low
body_high = math.max(open, close)
body_low = math.min(open, close)
body = body_high - body_low

lower_shadow = body_low - low
upper_shadow = high - body_high

is_body_ok = body / height >= body_ratio
is_lower_shadow_longer = lower_shadow / body >= long_shadow_body_ratio
is_upper_shadow_longer = upper_shadow / body >= long_shadow_body_ratio
is_shadows_portion_ok = if is_lower_shadow_longer
    lower_shadow / upper_shadow >= long_short_ratio
else if is_upper_shadow_longer
    upper_shadow / lower_shadow >= long_short_ratio

is_bullish_pinbar = is_body_ok and is_lower_shadow_longer and is_shadows_portion_ok and show_bullish_pinbars
is_bearish_pinbar = is_body_ok and is_upper_shadow_longer and is_shadows_portion_ok and show_bearish_pinbars

is_volume_bigger_ma = volume > ta.sma(volume, volume_ma_len)
is_volume_bigger = volume > volume[1]

if check_ma_volume
    is_bullish_pinbar := is_volume_bigger_ma and is_bullish_pinbar
    is_bearish_pinbar := is_volume_bigger_ma and is_bearish_pinbar

if check_pre_volume
    is_bullish_pinbar := is_volume_bigger and is_bullish_pinbar
    is_bearish_pinbar := is_volume_bigger and is_bearish_pinbar

pinbar_bg_color = is_volume_bigger_ma or is_volume_bigger ? bullish_cbg_color : bullish_bg_color
bgcolor(is_bullish_pinbar ? pinbar_bg_color : na, editable=false, transp=90)

pinbar_bg_color := is_volume_bigger_ma or is_volume_bigger ? bearish_cbg_color : bearish_bg_color
bgcolor(is_bearish_pinbar ? pinbar_bg_color : na, editable=false, transp=90)

// badge visibility
txt = ''
longer_shadow = math.max(lower_shadow, upper_shadow)
if show_length_on_badge
    txt := str.tostring(math.floor(longer_shadow / body * 10) / 10)
badge_color = is_bullish_pinbar ? #2ecc71 : #e74c3c

if not is_volume_bigger_ma and not is_volume_bigger
    badge_color := #f39c12

be = is_bearish_pinbar and not is_bullish_pinbar and show_bearish_pinbars
if be
    label.new(bar_index, low, yloc=yloc.belowbar, style=label.style_none, text=txt, color=badge_color, size=size.tiny)
plotshape(be ? high : na, title='Bearish Pin Bar', style=shape.triangledown, color=badge_color, location=location.abovebar, size=size.small)
alertcondition(be, 'Bearish Pin Bar', 'Bearish pin bar appeared')

bu = is_bullish_pinbar and not is_bearish_pinbar and show_bullish_pinbars
if bu
    label.new(bar_index, high, yloc=yloc.price, style=label.style_none, text=txt, color=badge_color, size=size.tiny)
plotshape(bu ? high : na, title='Bullish Pin Bar', style=shape.triangleup, color=badge_color, location=location.belowbar, size=size.small)
alertcondition(bu, 'Bullish Pin Bar', 'Bullish pin bar appeared')
Leave a Comment