The Narrow Range 4 (NR4) pattern is a technical analysis pattern used by traders to identify potential breakouts or significant price moves in a financial instrument, such as a stock. This pattern focuses on the trading range or volatility of a stock over a specific period, typically four trading days.
Here’s how the NR4 pattern is defined:
# Iterate through historical data to detect NR4 candles for this batch
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
if min(trading_ranges) == trading_ranges[-1]:
nr4_candles.append(current_candle)
Iteration through Historical Data: We iterate through the historical price data for RELIANCE, starting from the fourth data point. This is because we need to assess the trading range of the previous three trading days for each candle.
# Iterate through historical data to detect NR4 candles for this batch
for i in range(3, len(historical_data)):
current_candle = historical_data[i]
Calculating Trading Ranges: For each candle, we calculate the trading ranges of the previous three trading days. These trading ranges are the differences between the high and low prices of those days.
# 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]
Identifying NR4 Candle: We check if the current candle has the narrowest trading range among the previous three trading days. If the minimum trading range in the trading_ranges
list corresponds to the current candle, it signifies that the current day’s price range is narrower than the previous three days.
# Check if the current candle has the narrowest range among the previous three
if min(trading_ranges) == trading_ranges[-1]:
nr4_candles.append(current_candle)
If the current candle meets the NR4 criteria, we add it to the nr4_candles
list.
So, Here goes the complete code that will scan the Narrow Range 4 (NR4) candles accurately in Reliance –
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 NR4 candles
nr4_candles = []
# 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 NR4 candles for this batch
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
if min(trading_ranges) == trading_ranges[-1]:
nr4_candles.append(current_candle)
# Move the start date for the next batch
start_date = batch_end_date
# Print or post detected NR4 candles for the entire date range
for candle in nr4_candles:
print(f"NR4 Candle detected at {candle['date']} for RELIANCE")
The output is fantastic –
NR4 Candle detected at 2023-01-10 00:00:00+05:30 for RELIANCE
NR4 Candle detected at 2023-01-13 00:00:00+05:30 for RELIANCE
NR4 Candle detected at 2023-01-18 00:00:00+05:30 for RELIANCE
...
...
...
...
NR4 Candle detected at 2023-08-21 00:00:00+05:30 for RELIANCE
NR4 Candle detected at 2023-08-28 00:00:00+05:30 for RELIANCE
NR4 Candle detected at 2023-08-29 00:00:00+05:30 for RELIANCE
Here is a snapshot of our Buddha Scanner where it shows that the stock “Cipla” has NR7 as well as Inside Bar Pattern.
As all NR7 Patterns are by default NR4 too, Let’s checkout for CIPLA in our scanner.
Till now, in the example, We are using the date range as 1st Jan, 2023 to 1st Sept 2023.
Lets change the ending date to today’s as CIPLA popped up in our scanner on 15th.
Here is the modified code –
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-18', '%Y-%m-%d')
# Define the interval (in minutes)
interval = 'day'
# Initialize variables to track NR4 candles
nr4_candles = []
# Define the stock symbol for CIPLA
symbol = 'NSE:CIPLA'
# Fetch instrument data for CIPLA
instruments = kite.ltp([symbol])
# Check if the instrument token for CIPLA is in the instruments dictionary
if symbol in instruments:
instrument_token = instruments[symbol]['instrument_token']
# 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 NR4 candles for this batch
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
if min(trading_ranges) == trading_ranges[-1]:
nr4_candles.append(current_candle)
# Move the start date for the next batch
start_date = batch_end_date
# Print or post detected NR4 candles for CIPLA in the entire date range
for candle in nr4_candles:
print(f"NR4 Candle detected at {candle['date']} for CIPLA")
else:
print("Instrument token not found for CIPLA")
The Output shows CIPLA in the same date !!
NR4 Candle detected at 2023-01-11 00:00:00+05:30 for CIPLA
NR4 Candle detected at 2023-01-16 00:00:00+05:30 for CIPLA
NR4 Candle detected at 2023-01-19 00:00:00+05:30 for CIPLA
NR4 Candle detected at 2023-01-20 00:00:00+05:30 for CIPLA
...
...
...
...
NR4 Candle detected at 2023-08-28 00:00:00+05:30 for CIPLA
NR4 Candle detected at 2023-09-05 00:00:00+05:30 for CIPLA
NR4 Candle detected at 2023-09-08 00:00:00+05:30 for CIPLA
NR4 Candle detected at 2023-09-15 00:00:00+05:30 for CIPLA