Making Inside Bar Scanner Using Python and Zerodha

In the world of trading and investing, candlestick patterns play a significant role in helping traders make informed decisions. One such pattern is the “Inside Bar,” which can signal potential changes in price direction. 

In this article, we will explore how to use Python and the Zerodha Kite Connect API to detect Inside Bars for a specific stock, such as RELIANCE.

Iterating Every FNO Stocks in the Insider Bar Scanner

Let’s loop through multiple stock symbols and check if each of them had an Inside Bar pattern within the date range while also fetching the instrument data for each stock symbol, you can modify the code 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 = []

            # 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)

            # Check if there was an Inside Bar pattern yesterday
            if len(inside_bars) > 0:
                print(f"Inside Bar detected for {symbol} on {inside_bars[-1]['date']}")

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

				
			

In this modified code:

  • The loop iterates through each stock symbol in symbol_list.
  • For each symbol, it fetches instrument data using kite.ltp([symbol]).
  • Then, it checks if the stock symbol is in the instruments dictionary.
  • If the symbol is found in instruments, it proceeds to fetch historical data and check for Inside Bar patterns as before.
  • If an Inside Bar pattern is detected for the stock, it prints a message indicating that the pattern was detected for that specific stock on the last available date in the historical data.

This code will loop through all the stock symbols in your list and analyze each one for Inside Bar patterns while fetching instrument data for each symbol.

The output is beautiful –
				
					Inside Bar detected for NSE:NIFTY BANK on 2023-08-07 00:00:00+05:30
Inside Bar detected for NSE:IBULHSGFIN on 2023-09-01 00:00:00+05:30
Inside Bar detected for NSE:VOLTAS on 2023-08-28 00:00:00+05:30
Inside Bar detected for NSE:SBIN on 2023-08-28 00:00:00+05:30
Inside Bar detected for NSE:INFY on 2023-08-29 00:00:00+05:30
Inside Bar detected for NSE:GAIL on 2023-08-29 00:00:00+05:30
Inside Bar detected for NSE:ESCORTS on 2023-08-31 00:00:00+05:30
Inside Bar detected for NSE:TATASTEEL on 2023-08-18 00:00:00+05:30
Inside Bar detected for NSE:APOLLOTYRE on 2023-08-30 00:00:00+05:30
Inside Bar detected for NSE:DRREDDY on 2023-08-21 00:00:00+05:30
Inside Bar detected for NSE:LALPATHLAB on 2023-08-08 00:00:00+05:30
...
...
...
...
Inside Bar detected for NSE:WIPRO on 2023-08-23 00:00:00+05:30
Inside Bar detected for NSE:ZEEL on 2023-09-01 00:00:00+05:30
Inside Bar detected for NSE:ZYDUSLIFE on 2023-08-21 00:00:00+05:30
				
			
Post a comment

Leave a Comment

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

×Close