Scanning Bearish 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 –

Bearish Inside Bar:

A “Bearish Inside Bar,” on the other hand, is a candlestick pattern that suggests a potential bearish (downward) reversal in the price of the asset. In this code, the script is specifically designed to detect Bullish Inside Bars. To detect Bearish Inside Bars, you would need to modify the conditions accordingly. Typically, for a Bearish Inside Bar, you would look for conditions where the opening price is higher than the previous closing price, and the closing price is lower than the previous opening price. Here’s a modified example to detect Bearish Inside Bars:

How about we make this more complicated with scanning Bullish Inside Bars instead of Inside Bars

				
					if (
    current_candle['high'] < previous_candle['high']
    and current_candle['low'] > previous_candle['low']
    and current_candle['open'] > previous_candle['close']  # Condition for a bearish inside bar
    and current_candle['close'] < previous_candle['open']  # Condition for a bearish inside bar
):
    inside_bars.append(current_candle)

				
			

In this code, the conditions for a Bearish Inside Bar are as follows:

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

Now, Let’s swap the conditions in our previous code’s condition area which was 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']  # Condition for a bearish inside bar
            and current_candle['close'] < previous_candle['open']  # Condition for a bearish 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 ‘Bearish Inside Bar’ and appends the current candle’s data to the inside_bars list. This pattern suggests that the price may reverse downwards.

When we run this there is output-  

				
					Inside Bar detected at 2023-02-21 00:00:00+05:30 for RELIANCE
Inside Bar detected at 2023-03-01 00:00:00+05:30 for RELIANCE
Inside Bar detected at 2023-03-17 00:00:00+05:30 for RELIANCE
Inside Bar detected at 2023-04-11 00:00:00+05:30 for RELIANCE
Inside Bar detected at 2023-04-27 00:00:00+05:30 for RELIANCE
Inside Bar detected at 2023-07-05 00:00:00+05:30 for RELIANCE
Inside Bar detected at 2023-08-30 00:00:00+05:30 for RELIANCE
				
			

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