Coding Double Inside Bar Scanner Using Python and Zerodha

The Double Inside Bar is a variation of the Inside Bar pattern, but it has two consecutive Inside Bars. In other words, the high and low of each of the two candlesticks are fully contained within the range of the previous candlestick.

This pattern is considered more significant and potentially predictive than a single Inside Bar.

Increased Consolidation: 

  • When two Inside Bars appear consecutively, it indicates an extended period of consolidation or indecision in the market. 
  • Traders and analysts pay attention to this because it suggests that the market is compressing energy, preparing for a potential breakout or a significant price movement.

Prerequisite

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

The Double Inside Bar Scanner Using Zerodha KiteConnect API​

To detect and print the last date each stock had a “Double Inside Bar” pattern within the specified date range, you can modify the code from the last chapter as follows. The logic for a “Double Inside Bar” is that there should be two consecutive inside bars:

				
					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-08-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 two consecutive inside bars
                if consecutive_inside_bars == 2:
                    print(f"Double 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 looks like – 

				
					Double Inside Bar detected for NSE:INFY on 2023-08-22 00:00:00+05:30
Double Inside Bar detected for NSE:ESCORTS on 2023-08-29 00:00:00+05:30
Double Inside Bar detected for NSE:APOLLOTYRE on 2023-08-08 00:00:00+05:30
Double Inside Bar detected for NSE:LAURUSLABS on 2023-08-29 00:00:00+05:30
Double Inside Bar detected for NSE:TVSMOTOR on 2023-08-17 00:00:00+05:30
...
...
...
...
Double Inside Bar detected for NSE:HEROMOTOCO on 2023-08-10 00:00:00+05:30
Double Inside Bar detected for NSE:HEROMOTOCO on 2023-08-17 00:00:00+05:30
Double Inside Bar detected for NSE:NMDC on 2023-08-04 00:00:00+05:30
Double Inside Bar detected for NSE:PEL on 2023-08-04 00:00:00+05:30
Double Inside Bar detected for NSE:ZYDUSLIFE on 2023-08-18 00:00:00+05:30
				
			

Here’s an explanation of the modifications made to the code in the Inside Bar Scanner to scan Double Inside Bars:

  1. A new variable called consecutive_inside_bars has been introduced to keep track of consecutive inside bars. This variable helps identify when there are two consecutive inside bars, which is the criterion for detecting a ‘Double Inside Bar’ pattern.
  2. Within the loop that iterates through historical data, logic has been added 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. This ensures that only consecutive inside bars are counted.
  4. After detecting two consecutive inside bars (when consecutive_inside_bars reaches 2), a message is printed indicating that a ‘Double Inside Bar’ pattern has been detected for the current stock on the date of the second inside bar candle. Subsequently, the consecutive_inside_bars counter is reset to zero to continue searching for other patterns.

These changes to the code enable it to specifically search for the ‘Double Inside Bar’ pattern, defined by the presence of two consecutive inside bars, and report the date on which this pattern occurs for each stock in the symbol_list.

Post a comment

Leave a Comment

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

×Close