How To Identify Candle Patterns Automatically in TradingView using Pine Script?

How To Identify Candle Patterns Automatically in TradingView using Pine Script?

In this episode I’ll show you how to detect basic candlestick patterns using Pine Script.

We’ll focus solely on Engulfing Candles for now, but the process involved in identifying them is similar for all other candle patterns such as pinbars, shooting stars and hammers, dojis, higher-high higher-close and lower-low lower-close candles.

If you’re inexperienced with Pine Script and you haven’t gone through the previous episodes of Pine Script series then I highly recommend that you do that first. Otherwise… let’s get started.

Candlestick basics:

There are four built-in Pine Script variables we have to work with in order to detect candle patterns: the open price, the close price, the high and the low.

Using these four variables we can determine if a candle meets the criteria to be called a certain pattern — such as an “engulfing candle”.

The example here is called an engulfing candle. This is because the close of the green candle closes higher than the open of the red candle.

This is a sign of bullish strength — but if this pattern occurs in the opposite direction as a bearish engulfing candle, then it’s a sign of potential bearish strength.

This simple pattern when used in conjunction with market and indicator conditions and filters can make for a high-accuracy entry reason for almost any strategy.

So we know which variables we need to work with — open, close, high, low.

In the case of a bullish engulfing candle, the completion candle must close at a higher price than the previous candle’s open price, just like in the picture above.

In Pine Script we could detect this candle condition with the following line of code:

close > open[1]

This condition will check if the current candle’s closing price is greater than or equal to the previous candle’s opening price.

But for engulfing candle, the previous candle must have also closed in Red, which can be checked using another simple line of code

close[1] < open[1]

So putting it together, we will get a bullish engulfing pattern recognition script.

bullishEC = close > open[1] and close[1] < open[1]

It’s that simple!

There are multiple variations of engulfing candles — such as a higher-high higher-close engulfing candle and if you want to check for higher high engulf, you can change the open[1] in the above script to high[1]

bullishEC = close > high[1] and close[1] < open[1]

This will consider pattern is engulfing if engulfing candle closes above the high of the previous candle.

Let’s go back to the old script again, which uses higher-close engulfing.

So, the variable “bullishEC” will turn true if the current candle’s closing price is higher than the previous candle’s opening price and the previous candle was bearish.

Likewise, we can create another variable “bearishEC” will turn true if the current candle’s closing price is lower than the previous candle’s opening price and the previous candle was bullish.

bearishEC = close < open[1] and close[1] > open[1]

With these 2 variables we can now detect basic engulfing candles. Now you can simply change your “plot” code.

plotshape(bullishEC, title=”Long”, location=location.belowbar, color=color.green, transp=0, style=shape.triangleup, text=”Long”)

plotshape(bearishEC, title=”Short”, location=location.abovebar, color=color.red, transp=0, style=shape.triangledown, text=”Short”)

That’s it. You now have an indicator that will detect counter-trend setups based on engulfing pattern. You can now add some more conditions to it, like RSI or MACD to generate a proper trading signal.


Check Out Script Library to get script used for this episode:
https://marketsecrets.in/script-library/

To know more, checkout:

https://youtu.be/wOiutj4R494

 

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!