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 –
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:
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.