Scanning Bullish Inside Bar Candles in Reliance Using Python and Zerodha

To go ahead with this tutorial, One must complete the pre-requsite tutorial which shows how to scan candles from Reliance Stock to spot which ones are having Inside Bar Pattern –

Bullish Inside Bar:

A “Bullish Inside Bar” is a candlestick pattern that suggests a potential bullish (upward) reversal in the price of the asset. Here’s how it’s identified in the code:
				
					if (
    current_candle['high'] < previous_candle['high']
    and current_candle['low'] > previous_candle['low']
    and current_candle['open'] < previous_candle['close']  # Additional condition for a bullish inside bar
    and current_candle['close'] > previous_candle['open']  # Additional condition for a bullish inside bar
):
    inside_bars.append(current_candle)

				
			

In the code, a Bullish Inside Bar is detected when all the following conditions are met for a current candle (the candle being analyzed) in comparison to the previous candle:

  1. Lower High: The high of the current candle is lower than the high of the previous candle.
  2. Higher Low: The low of the current candle is higher than the low of the previous candle.
  3. Opening Below the Previous Close: The opening price of the current candle is lower than the closing price of the previous candle. This condition indicates a potential gap down.
  4. Closing Above the Previous Open: The closing price of the current candle is higher than the opening price of the previous candle.

How about we make this more complicated with scanning Bullish Inside Bars instead of Inside Bars? Although it sounds extremely scary but in terms of code logic, it is pretty easy to achive! 

All we had to do is to write two more lines in the code which we have discussed in the last chapter – 

				
					from datetime import datetime, timedelta

# Define the date range
start_date = datetime.strptime('2023-01-01', '%Y-%m-%d')
end_date = datetime.strptime('2023-09-01', '%Y-%m-%d')

# Define the interval (in minutes)
interval = 'day'

# Initialize variables to track inside bars
inside_bars = []

# Iterate through smaller date ranges (each within 60 days)
while start_date < end_date:
    # Calculate the end date for the current batch (within 60 days)
    batch_end_date = start_date + timedelta(days=60)

    # Ensure that the batch end date does not exceed the overall end date
    if batch_end_date > end_date:
        batch_end_date = end_date

    # Fetch historical data for the current batch
    historical_data = kite.historical_data(instrument_token, start_date, batch_end_date, interval)

    # Iterate through historical data to detect inside bars for this batch
    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']
            and current_candle['open'] < previous_candle['close']  # Additional condition for a bullish inside bar
            and current_candle['close'] > previous_candle['open']  # Additional condition for a bullish inside bar
        ):
            inside_bars.append(current_candle)

    # Move the start date for the next batch
    start_date = batch_end_date

# Print or post detected inside bars for the entire date range
for candle in inside_bars:
    print(f"Inside Bar detected at {candle['date']} for RELIANCE")
				
			

When all these conditions are met, the script considers it a ‘Bullish Inside Bar’ and appends the current candle’s data to the inside_bars list. This pattern suggests that the price may reverse upwards.

When we run this there is no output. It means none of the Inside Bars were Bullish Inside Bars.

The ones that are not Bullish or Bearish should be declared as Neutral Inside Bars.

Post a comment

Leave a Comment

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

×Close