本策略是一个基于Nadaraya-Watson核估计方法和移动平均线交叉的趋势跟踪交易系统。该策略通过高斯核函数对价格数据进行平滑处理,结合移动平均线的交叉信号来捕捉市场趋势,实现智能化的趋势跟踪交易。策略采用百分比仓位管理方式,默认每次交易使用10%的账户权益。
策略的核心是Nadaraya-Watson核估计方法,该方法使用高斯核函数对价格数据进行非参数平滑。具体实现包括以下步骤: 1. 使用高斯核函数计算权重,带宽参数h设置为8.0 2. 对过去500个价格数据点进行加权平滑 3. 计算平滑后数据的简单移动平均线(SMA),回溯期为15个周期 4. 当平滑曲线上穿移动平均线时,产生做多信号 5. 当平滑曲线下穿移动平均线时,产生做空信号 6. 使用仓位状态变量跟踪当前持仓情况,避免重复开仓
该策略创新性地将Nadaraya-Watson核估计与传统技术分析相结合,构建了一个稳健的趋势跟踪系统。通过高斯核平滑和移动平均线交叉,有效捕捉市场趋势,同时控制风险。策略具有良好的可扩展性和优化空间,适合进一步开发和实践应用。建议交易者在实盘使用前进行充分的参数优化和回测验证。
/*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")