# Nadaraya-Watson Envelope

Let's delve into the concept of the Nadaraya-Watson Envelope, a fascinating technical indicator used in trading and market analysis.

## What is the Nadaraya-Watson Envelope?

- The
**Nadaraya-Watson Envelope**is a type of moving average calculated by taking a weighted average of data points over a specified period. - It is named after its creators,
**Vasily Nadaraya**and**Geoffrey Watson**. - The envelope is created by drawing two lines (one above and one below) parallel to the moving average at a user-defined percentage distance.
- These upper and lower lines form a “band” around the moving average, which can be used to identify potential trend changes and generate trading signals.

## How Does It Work?

- The main computation in the Nadaraya-Watson Envelope involves a weighted average of data points.
- Here's how it's calculated:
- Given a sequence of data points p1,p2,ldots,pn, the Nadaraya-Watson Envelope at data point i is computed using an inner loop that iterates over j: [ \text{Weighted Average} = \sum_{j=1}^{n} p_j \cdot w_{ij} ]
- The weight wij is exponentially calculated and depends on the difference between indices i and j and a smoothing parameter h.
- The resulting weighted average forms the upper and lower bands around the moving average.

## Trading Signals

The Nadaraya-Watson Envelope generates buy and sell signals based on the difference between two elements:

- If the difference between consecutive upper envelope values (y2) exceeds a threshold and the previous y2 value is below the previous lower envelope value (y1), it's a
**buy signal**. - Conversely, if the difference falls below the negative of the threshold and the previous y2 value is above the previous y1 value, it's a
**sell signal**.

- If the difference between consecutive upper envelope values (y2) exceeds a threshold and the previous y2 value is below the previous lower envelope value (y1), it's a

## Advantages

- Non-repainting: Unlike some indicators, the Nadaraya-Watson Envelope does not repaint historical data points.
- Dynamic Analysis: It adapts to changing market conditions and provides insights into price patterns.

## Application in Financial Markets

- Traders use the Nadaraya-Watson Envelope to identify underlying price patterns, potential reversals, and trend shifts.
- It's particularly useful for analyzing volatile markets.

## Backtesting Example (Python)

Here's a simplified Python example for backtesting using the Nadaraya-Watson Envelope

```
import numpy as np
import pandas as pd
import yfinance as yf
class Backtest:
def __init__(self, symbol, time_period):
self.symbol = symbol
self.df = yf.download(tickers=symbol, period=time_period)
self.src = self.df["Close"].values
self.h = 7
y2, y1 = self.nadaraya_watson_envelope()
# Generate buy/sell signals based on y2 and y1
def nadaraya_watson_envelope(self):
n = len(self.src)
y2 = np.empty(n)
y1 = np.empty(n)
h = self.h
for i in range(n):
sum = 0
sumw = 0
for j in range(n):
w = np.exp(-(np.power(i - j, 2) / (h * h * 2)))
sum += self.src[j] * w
sumw += w
y2[i] = sum / sumw
if i > 0:
y1[i] = (y2[i] + y2[i - 1]) / 2
return y2, y1
```

Remember that no single indicator guarantees success, but understanding the Nadaraya-Watson Envelope can enhance your technical analysis toolkit.

Reference Nadaraya-Watson Envelope LuxAlgo Nadaraya-Watson Envelope Non-Repainting

Publish Date: 2024-05-13, Update Date: 2024-05-13