Coding Triple Inside Bar Scanner Using Python and Zerodha

Amit Kumar Ghosh
3 min readSep 25, 2023

--

A Triple Inside Bar is a variation of the Inside Bar pattern where there are three consecutive inside bars. In other words, the high and low of each of the three candlesticks are entirely contained within the range of the previous candlestick. This pattern is even rarer and signifies an extended period of consolidation or indecision, often leading to a significant price breakout or trend reversal.

Prerequisite

To go ahead with this tutorial, One must complete the pre-requsite tutorial which shows how to scan the stocks having Double Inside Bar Pattern.

Check it out here

The Triple Inside Bar Scanner Using Zerodha KiteConnect API

To scan for a “Triple Inside Bar” pattern (where there are three consecutive inside bars), you can modify the code from the last chapter as follows:

for symbol in symbol_list:
# Fetch instrument data for the current stock symbol
instruments = kite.ltp([symbol])

# Check if the stock symbol is in the instruments dictionary
if symbol in instruments:
instrument_token = instruments[symbol]['instrument_token']

if instrument_token:
# Fetch historical data for the stock
historical_data = kite.historical_data(instrument_token, '2023-01-01', '2023-09-01', interval='day')

# Initialize variables to track inside bars
inside_bars = []
consecutive_inside_bars = 0 # Initialize a counter for consecutive inside bars

# Iterate through historical data to detect inside bars
for i in range(1, len(historical_data)):
current_candle = historical_data[i]
previous_candle = historical_data[i - 1]

if (current_candle['high'] < previous_candle['high']) and (current_candle['low'] > previous_candle['low']):
inside_bars.append(current_candle)
consecutive_inside_bars += 1
else:
consecutive_inside_bars = 0 # Reset the counter if no inside bar

# Check if there were three consecutive inside bars
if consecutive_inside_bars == 3:
print(f"Triple Inside Bar detected for {symbol} on {current_candle['date']}")
consecutive_inside_bars = 0 # Reset the counter after detection

else:
print(f"Instrument token not found for {symbol}")
else:
print(f"{symbol} not found in instruments")

The output will be –

Triple Inside Bar detected for NSE:LALPATHLAB on 2023-02-08 00:00:00+05:30
Triple Inside Bar detected for NSE:SUNPHARMA on 2023-08-10 00:00:00+05:30
Triple Inside Bar detected for NSE:TCS on 2023-03-03 00:00:00+05:30
Triple Inside Bar detected for NSE:NTPC on 2023-06-27 00:00:00+05:30
Triple Inside Bar detected for NSE:FEDERALBNK on 2023-06-20 00:00:00+05:30

Code Modifications:

  1. Introduced a new variable, consecutive_inside_bars, to track consecutive Inside Bars.
  2. Inside the loop that iterates through historical data, added logic to determine if the current candle and the previous candle form an Inside Bar. If they do, the consecutive_inside_bars counter is incremented, and the current candle is added to the inside_bars list.
  3. If the current candles do not form an Inside Bar, the consecutive_inside_bars counter is reset to zero to ensure we’re only counting consecutive Inside Bars.
  4. After detecting three consecutive Inside Bars (when consecutive_inside_bars reaches 3), a message is printed indicating that a ‘Triple Inside Bar’ pattern has been detected for the current stock on the date of the third Inside Bar candle. Subsequently, the consecutive_inside_bars counter is reset to zero for further pattern detection.

These code modifications enable the script to specifically search for the rare ‘Triple Inside Bar’ pattern, consisting of three consecutive Inside Bars, and report the date of occurrence for each stock in the symbol_list. This pattern is considered more important because of its rarity and potential significance in predicting price movements.

A Note from Unofficed

Thank you for being a valued member of our community! Before you depart:

👏 Applaud the story and give a follow to the author 👉

📰 Explore additional content on the Unofficed

📚 Join our FREE Masterclass

📈 Unlock potent trading tools

--

--

Amit Kumar Ghosh
Amit Kumar Ghosh

Written by Amit Kumar Ghosh

Aloha, I’m Amit Ghosh, a web entrepreneur and avid blogger. Bitten by entrepreneurial bug, I got kicked out from college and ended up being millionaire!

No responses yet