Strategy vault / Trend following
Intelligent Trend Following Strategy System Based on Nadaraya Watson Kernel Estimation and Moving Average Crossover
This strategy is a trend following trading system based on Nadaraya-Watson kernel estimation method and moving average crossover. The strategy uses a Gaussian kernel function to smooth price data and combines moving…

Overview
This strategy is a trend following trading system based on Nadaraya-Watson kernel estimation method and moving average crossover. The strategy uses a Gaussian kernel function to smooth price data and combines moving average crossover signals to capture market trends, achieving intelligent trend following trading. The strategy adopts percentage position management, using 10% of account equity by default for each trade.
Strategy Principles
The core of the strategy is the Nadaraya-Watson kernel estimation method, which uses a Gaussian kernel function for non-parametric smoothing of price data. The specific implementation includes the following steps:
- Calculate weights using Gaussian kernel function with bandwidth parameter h set to 8.0
- Perform weighted smoothing on past 500 price data points
- Calculate Simple Moving Average (SMA) of smoothed data with 15-period lookback
- Generate long signal when smoothed curve crosses above moving average
- Generate short signal when smoothed curve crosses below moving average
- Use position state variable to track current holdings and avoid duplicate entries
Strategy Advantages
- Uses non-parametric estimation method, no assumption of data distribution needed
- Gaussian kernel smoothing effectively reduces noise impact and improves signal quality
- Moving average crossover validation reduces false signals
- Position management system controls risk exposure
- Code implementation is concise and efficient, easy to maintain and optimize
- Clear strategy logic suitable for trading across various timeframes
Strategy Risks
- Parameter sensitivity risk: choice of bandwidth h and moving average period significantly affects strategy performance
- Lag risk: both kernel estimation and moving average have inherent lag, may miss sharp market moves
- Choppy market risk: prone to false signals in sideways markets
- Computational overhead: processing large historical data may affect real-time performance
- Overfitting risk: parameter optimization may lead to overfitting historical data
Strategy Optimization Directions
- Introduce adaptive bandwidth: dynamically adjust bandwidth parameter based on market volatility
- Add market environment filtering: incorporate trend strength indicators, only enter positions in strong trend markets
- Optimize stop-loss mechanism: design volatility-based dynamic stop-loss
- Improve position management: adjust position size based on signal strength and market volatility
- Introduce multi-timeframe analysis: combine trend judgment from longer timeframes
Summary
This strategy innovatively combines Nadaraya-Watson kernel estimation with traditional technical analysis to build a robust trend following system. Through Gaussian kernel smoothing and moving average crossover, it effectively captures market trends while controlling risk. The strategy has good scalability and optimization potential, suitable for further development and practical application. Traders are advised to conduct thorough parameter optimization and backtesting before live trading.
Source Pine Script
/*backtest
start: 2024-02-21 00:00:00
end: 2025-02-18 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Binance","currency":"BTC_USDT"}]
*/
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © UniCapInvest
//@version=5
strategy("Nadaraya-Watson Strategy with Moving Average Crossover", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10, max_bars_back=500)
// Girdiler
h = input.float(8.,'Bandwidth', minval = 0)
src = input(close,'Source')
lookback = input.int(15, "Moving Average Lookback", minval=1)
// Gaussian fonksiyonu
gauss(x, h) => math.exp(-(math.pow(x, 2)/(h * h * 2)))
// Nadaraya-Watson smoothed değerini hesaplama
var float smoothed = na
sum_w = 0.0
sum_xw = 0.0
for i = 0 to 499
w = gauss(i, h)
sum_w += w
sum_xw += src[i] * w
smoothed := sum_w != 0 ? sum_xw / sum_w : na
// Hareketli ortalama hesaplama
ma = ta.sma(smoothed, lookback)
// Alım ve satım koşulları (kesişimlere göre)
longCondition = ta.crossover(smoothed, ma)
shortCondition = ta.crossunder(smoothed, ma)
// Pozisyon durumu
var bool inPosition = false
// Strateji giriş ve çıkış koşulları
if (longCondition and not inPosition)
strategy.entry("Long", strategy.long)
inPosition := true
if (shortCondition and inPosition)
strategy.entry("Short", strategy.short)
inPosition := false
// Plotting
plot(smoothed, color=color.blue, title="Nadaraya-Watson Smoothed")
plot(ma, color=color.red, title="Moving Average")Published by ianzeng123 on the FMZ strategy square · last modified 2025-02-20. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Intelligent-Trend-Following-Strategy-System-Based-on-Nadaraya-Watson-Kernel-Estimation-and-Moving-Average-Crossover.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- ADX MA and EMA Long Only Trend Tracking StrategyPine Script · Trend following · 1d
- ATR and Moving Average Crossover Hybrid StrategyPine Script · Trend following · 1h
- Adaptive FVG Detection and MA Trend Trading Strategy with Dynamic ResistancePine Script · Trend following · 1d
- Adaptive Risk Management Strategy Based on Dynamic ATR and Moving Average CrossoverPine Script · Trend following · 2h
- Adaptive Trading Strategy Based on Stochastic RSI Dual Line CrossoverPine Script · Trend following · 4h
- Adaptive Volatility Based Trend Following Short Strategy with Time Delay and Stop Loss ProtectionPine Script · Trend following · 2m