Coding Narrow Range 4 (NR4) Scanner Using Python and Zerodha
Let’s loop through multiple stock symbols and check if each of them had an Narrow Range 4 (NR4) pattern within the date range while also fetching the instrument data for each stock symbol, you can modify the code as follows:
from datetime import datetime, timedelta
# Define the date range
start_date = datetime.strptime('2023-08-01', '%Y-%m-%d')
end_date = datetime.strptime('2023-09-01', '%Y-%m-%d')
# Define the interval (in minutes)
interval = 'day'
# Iterate through the list of stock symbols
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, start_date, end_date, interval='day')
# Initialize variables to track NR4 candles
nr4_candles = []
# Iterate through historical data to detect NR4 candles
for i in range(3, len(historical_data)):
current_candle = historical_data[i]
# Calculate the trading range for the previous three candles
previous_candles = historical_data[i - 3:i]
trading_ranges = [candle['high'] - candle['low'] for candle in previous_candles]
# Check if the current candle has the narrowest range among the previous three (NR4)
if min(trading_ranges) == trading_ranges[-1]:
nr4_candles.append(current_candle)
# Print or post detected NR4 candles for the entire date range
for candle in nr4_candles:
print(f"NR4 Candle detected for {symbol} on {candle['date']}")
else:
print(f"Instrument token not found for {symbol}")
else:
print(f"{symbol} not found in instruments")
The output looks like –
NR4 Candle detected for NSE:NIFTY BANK on 2023-08-07 00:00:00+05:30
NR4 Candle detected for NSE:NIFTY BANK on 2023-08-08 00:00:00+05:30
NR4 Candle detected for NSE:NIFTY BANK on 2023-08-16 00:00:00+05:30
NR4 Candle detected for NSE:NIFTY BANK on 2023-08-17 00:00:00+05:30
...
...
...
...
NR4 Candle detected for NSE:NIFTY FIN SERVICE on 2023-08-21 00:00:00+05:30
NR4 Candle detected for NSE:NIFTY FIN SERVICE on 2023-08-28 00:00:00+05:30
NR4 Candle detected for NSE:NIFTY FIN SERVICE on 2023-08-29 00:00:00+05:30
NR4 Candle detected for NSE:NIFTY FIN SERVICE on 2023-08-30 00:00:00+05:30
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 Narrow Range 4 (NR4) Bar patterns as before. - If an NR4 Bar pattern is detected for the stock, it prints a message indicating that the pattern was detected for that specific stock within the specified date range.
This code will loop through all the stock symbols in your list and analyze each one for NR4 Bar patterns while fetching instrument data for each symbol.
Post a comment