PineScript: Drawing Circle with two or three candles for multi-candlestick patterns identification

Viewed 58

I am writing a basic candlestick detection functionality where I want to also detect 2-candles and 3-candles pattern.

I am able to detect two candles patterns like Inside Candle or Engulfing.

But I want to encircle the two candles into a circle such that anyone checking it can be benefited with this, specially the new learners.

How can I do it?

My current detection code is:


// Two Candles Patterns ///////////////////////////////////////////////////////////////////////////////////////
// Engulfing Pattern
// op: Open Prev, cp: Close Prev .. so on
bullish_engulfing(op, cp, hp, lp, o, c, h, l) => red(op, cp) and green(o, c) and (StrictEngulf ? (c > hp and o < lp) : (h > hp and l < lp))
bearish_engulfing(op, cp, hp, lp, o, c, h, l) => green(op, cp) and red(o, c) and (StrictEngulf ? (o > hp and c < lp) : (h > hp and l < lp))
plotshape(bullish_engulfing(open[1], close[1], high[1], low[1], open, close, high, low), title='Bull-Engulf', text='Bull-Engulf', location=location.belowbar, style=shape.triangleup, color=color.green, textcolor=color.green)
plotshape(bearish_engulfing(open[1], close[1], high[1], low[1], open, close, high, low), title='Bear-Engulf', text='Bear-Engulf', location=location.abovebar, style=shape.triangledown, color=color.red, textcolor=color.red)



// Inside Candle (Harami) Pattern
// op: Open Prev, cp: Close Prev .. so on
bullish_inside_candle(op, cp, hp, lp, o, c, h, l) => red(op, cp) and green(o, c) and (StrictInsideCandle ? (op > h and cp < l) : (hp > h and lp < l))
bearish_inside_candle(op, cp, hp, lp, o, c, h, l) => green(op, cp) and red(o, c) and (StrictInsideCandle ? (cp > h and op < l) : (hp > h and lp < l))
plotshape(bullish_inside_candle(open[1], close[1], high[1], low[1], open, close, high, low), title='Bull-IC', text='Bull-IC', location=location.belowbar, style=shape.triangleup, color=color.green, textcolor=color.green)
plotshape(bearish_inside_candle(open[1], close[1], high[1], low[1], open, close, high, low), title='Bear-IC', text='Bear-IC', location=location.abovebar, style=shape.triangledown, color=color.red, textcolor=color.red)


The photo I get is:

enter image description here

I have two queries:

  1. How can I add the circle across the 2 or 3 candles in case of multi-candlestick patterns?
  2. How, if needed, I can erase out any plotschar or plotshape I did in the previous candles? Because when I can encircle 2 or more candles to detect a multi-candle pattern, then keeping the previous candle type does not make a lot of sense (e.g. morning/evening star .. where I want to erase out the Doji on the second candle after I detect the 3 candle formed a definite pattern).
2 Answers

With the great pointer from @Daveatt I can able to code it out.


// Two Candles Patterns ///////////////////////////////////////////////////////////////////////////////////////
// Engulfing Pattern
// op: Open Prev, cp: Close Prev .. so on
bullish_engulfing(op, cp, hp, lp, o, c, h, l) => red(op, cp) and green(o, c) and (StrictEngulf ? (c > hp and o < lp) : (h > hp and l < lp))
bearish_engulfing(op, cp, hp, lp, o, c, h, l) => green(op, cp) and red(o, c) and (StrictEngulf ? (o > hp and c < lp) : (h > hp and l < lp))
bullish_engulf_data = bullish_engulfing(open[1], close[1], high[1], low[1], open, close, high, low)
bearish_engulf_data = bearish_engulfing(open[1], close[1], high[1], low[1], open, close, high, low)
plotshape(bullish_engulf_data, title='Bull-Engulf', text='Bull-Engulf', location=location.belowbar, style=shape.triangleup, color=color.green, textcolor=color.green)
if (bullish_engulf_data and barstate.isconfirmed)
    box.new(bar_index-1, high, bar_index, low, border_color=color.green, border_style=line.style_dotted, border_width=2, bgcolor=color.new(color.white, 100))
plotshape(bearish_engulf_data, title='Bear-Engulf', text='Bear-Engulf', location=location.abovebar, style=shape.triangledown, color=color.red, textcolor=color.red)
if (bearish_engulf_data and barstate.isconfirmed)
    box.new(bar_index-1, high, bar_index, low, border_color=color.red, border_style=line.style_dotted, border_width=2, bgcolor=color.new(color.white, 100))



// Inside Candle (Harami) Pattern
// op: Open Prev, cp: Close Prev .. so on
bullish_inside_candle(op, cp, hp, lp, o, c, h, l) => red(op, cp) and green(o, c) and (StrictInsideCandle ? (op > h and cp < l) : (hp > h and lp < l))
bearish_inside_candle(op, cp, hp, lp, o, c, h, l) => green(op, cp) and red(o, c) and (StrictInsideCandle ? (cp > h and op < l) : (hp > h and lp < l))
bullish_inside_data = bullish_inside_candle(open[1], close[1], high[1], low[1], open, close, high, low)
bearish_inside_data = bearish_inside_candle(open[1], close[1], high[1], low[1], open, close, high, low)
plotshape(bullish_inside_data, title='Bull-IC', text='Bull-IC', location=location.belowbar, style=shape.triangleup, color=color.green, textcolor=color.green)
if (bullish_inside_data and barstate.isconfirmed)
    box.new(bar_index-1, high[1], bar_index, low[1], border_color=color.green, border_style=line.style_dotted, border_width=2, bgcolor=color.new(color.white, 100))
plotshape(bearish_inside_data, title='Bear-IC', text='Bear-IC', location=location.abovebar, style=shape.triangledown, color=color.red, textcolor=color.red)
if (bearish_inside_data and barstate.isconfirmed)
    box.new(bar_index-1, high[1], bar_index, low[1], border_color=color.red, border_style=line.style_dotted, border_width=2, bgcolor=color.new(color.white, 100))

The photo is coming as (OK for me):

enter image description here

Related