# Coding Double Narrow Range 7 (Double NR7) Scanner Using Python and Zerodha

A Double NR7 Bar is a specific candlestick pattern in technical analysis that consists of two consecutive NR7 bars. An NR7 bar is a candlestick with the narrowest range among the previous seven days, meaning it has the smallest difference between its high and low prices in the last seven days.

## Double Narrow Range 7 (NR7) Bar

In the context of a Double NR7 Bar, it means that there are two days in a row where the daily price range is exceptionally narrow compared to the previous seven days. This pattern often indicates a period of very low volatility and a potential upcoming price breakout or significant move.

Double NR7 Bar is also NR8 by Definition:

• By definition, a Double NR7 Bar consists of two consecutive NR7 bars.
• Since an NR7 bar implies a narrow range compared to the previous seven days, two consecutive NR7 bars indicate an even narrower range for two consecutive days, making it a Narrow Range 8 (NR8) pattern.

## Prerequisite

To go ahead with this tutorial, One must complete the pre-requsite tutorial which shows how to scan the stocks having Double NR4 Bar Pattern.

### The Double Narrow Range 7 (NR7) Bar Scanner Using Zerodha KiteConnect API

This code is similar to the one for detecting a Double NR4 Bar, with the main difference being that it now searches for two consecutive NR7 candles, which indicate a Double NR7 Bar pattern. When the pattern is detected for a specific stock, it prints a message indicating the date of occurrence for that stock.
```				```
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 NR7 candles
nr7_candles = []
consecutive_nr7_candles = 0  # Initialize a counter for consecutive NR7 candles

# Iterate through historical data to detect NR7 candles
for i in range(6, len(historical_data)):
current_candle = historical_data[i]

# Calculate the trading range for the previous six candles
previous_candles = historical_data[i - 6:i]
trading_ranges = [candle['high'] - candle['low'] for candle in previous_candles]

# Check if the current candle has the narrowest range among the previous six (NR7)
nr7_candles.append(current_candle)
consecutive_nr7_candles += 1
else:
consecutive_nr7_candles = 0  # Reset the counter if no NR7 candle

# Check if there were two consecutive NR7 candles
if consecutive_nr7_candles == 2:
print(f"Double NR7 Candle detected for {symbol} on {current_candle['date']}")
consecutive_nr7_candles = 0  # Reset the counter after detection

else:
else:

```
```

The output looks like –

```				```
Double NR7 Candle detected for NSE:NIFTY BANK on 2023-08-21 00:00:00+05:30
Double NR7 Candle detected for NSE:NIFTY BANK on 2023-08-23 00:00:00+05:30
Double NR7 Candle detected for NSE:VOLTAS on 2023-08-18 00:00:00+05:30
Double NR7 Candle detected for NSE:TATASTEEL on 2023-08-23 00:00:00+05:30
...
...
...
...
Double NR7 Candle detected for NSE:SYNGENE on 2023-08-31 00:00:00+05:30
Double NR7 Candle detected for NSE:NAVINFLUOR on 2023-08-28 00:00:00+05:30
Double NR7 Candle detected for NSE:GRASIM on 2023-08-21 00:00:00+05:30
Double NR7 Candle detected for NSE:NIFTY FIN SERVICE on 2023-08-21 00:00:00+05:30
```
```

Here’s an explanation of the key changes made from the code for detecting Double Narrow Range 4 (NR4) Bars to the code for detecting Double Narrow Range 7 (NR7) Bars:

1. NR7 Criteria: The primary change is in the narrow range criteria. In the NR4 scanner, the code looks for the narrowest range among the previous three candles to identify an NR4 candle. In contrast, the NR7 scanner looks for the narrowest range among the previous six candles to identify an NR7 candle. This change is reflected in the range calculation and loop range within the code.
2. Variable Names: Variable names have been updated to reflect the NR7 pattern. For example, `nr4_candles` becomes `nr7_candles`, and `consecutive_nr4_candles` becomes `consecutive_nr7_candles`. This ensures that the code appropriately tracks NR7 candles and consecutive NR7 candles.
3. Loop Range: In the NR4 scanner, the loop iterates from 3 to the length of historical data. This is because it checks for NR4 candles starting from the fourth candle. In the NR7 scanner, the loop iterates from 6 to the length of historical data because it checks for NR7 candles starting from the seventh candle.
4. Detection Logic: The logic for detecting the narrowest range candle remains consistent but is applied to a different number of previous candles (three for NR4 and six for NR7). The consecutive count logic is updated accordingly.

These changes allow the code to scan for Double NR7 Bars, which require identifying two consecutive NR7 candles within the historical data for a given stock symbol.

×Close