To go ahead with this tutorial, One must complete the pre-requsite tutorial which shows how to scan the stocks having Double Inside Bar Pattern.
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 = []
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 three consecutive inside bars
if consecutive_inside_bars == 3:
print(f"Triple 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")
Triple Inside Bar detected for NSE:LALPATHLAB on 2023-02-08 00:00:00+05:30
Triple Inside Bar detected for NSE:SUNPHARMA on 2023-08-10 00:00:00+05:30
Triple Inside Bar detected for NSE:TCS on 2023-03-03 00:00:00+05:30
Triple Inside Bar detected for NSE:NTPC on 2023-06-27 00:00:00+05:30
Triple Inside Bar detected for NSE:FEDERALBNK on 2023-06-20 00:00:00+05:30
Code Modifications:
consecutive_inside_bars
, to track consecutive Inside Bars.consecutive_inside_bars
counter is incremented, and the current candle is added to the inside_bars
list.consecutive_inside_bars
counter is reset to zero to ensure we’re only counting consecutive Inside Bars.consecutive_inside_bars
reaches 3), a message is printed indicating that a ‘Triple Inside Bar’ pattern has been detected for the current stock on the date of the third Inside Bar candle. Subsequently, the consecutive_inside_bars
counter is reset to zero for further pattern detection.These code modifications enable the script to specifically search for the rare ‘Triple Inside Bar’ pattern, consisting of three consecutive Inside Bars, and report the date of occurrence for each stock in the symbol_list
. This pattern is considered more important because of its rarity and potential significance in predicting price movements.