Pine Script策略无订单生成问题求助
问题:Pine Script策略无订单生成排查
我是Pine Script新手,基于他人指标重构了一个交易策略,但在测试区间内该策略始终未生成任何订单。指标可正常在TradingView的BTC/USDT(币安)交易对上显示,却无订单产出。
相关情况说明:
- 图表显示指标正常加载
- 策略测试面板无订单生成记录
- BTC/USDT图表上指标绘制正常
- 订单历史为空
策略代码如下:
strategy("Strategy", overlay = true, max_lines_count = 300, max_labels_count = 300, max_bars_back=300) //------------------------------------------------------------------------------ //Settings //-----------------------------------------------------------------------------{ h = input.float(8.,'Bandwidth', minval = 0) mult = input.float(3., minval = 0) src = input(close, 'Source') repaint = input(true, 'Repainting Smoothing', tooltip = 'Repainting is an effect where the indicators historical output is subject to change over time. Disabling repainting will cause the indicator to output the endpoints of the calculations') //Style upCss = input.color(color.teal, 'Colors', inline = 'inline1', group = 'Style') dnCss = input.color(color.red, '', inline = 'inline1', group = 'Style') //-----------------------------------------------------------------------------} //Functions //-----------------------------------------------------------------------------{ //Gaussian window gauss(x, h) => math.exp(-(math.pow(x, 2)/(h * h * 2))) //-----------------------------------------------------------------------------} //Append lines //-----------------------------------------------------------------------------{ n = bar_index var ln = array.new_line(0) if barstate.isfirst and repaint for i = 0 to 299 array.push(ln,line.new(na,na,na,na)) //-----------------------------------------------------------------------------} //End point method //-----------------------------------------------------------------------------{ var coefs = array.new_float(0) var den = 0. if barstate.isfirst and not repaint for i = 0 to 299 w = gauss(i, h) coefs.push(w) den := coefs.sum() out = 0. if not repaint for i = 0 to 299 out += src[i] * coefs.get(i) out /= den mae = ta.sma(math.abs(src - out), 299) * mult upper = out + mae lower = out - mae //-----------------------------------------------------------------------------} //Compute and display NWE //-----------------------------------------------------------------------------{ float y2 = na float y1 = na nwe = array.new<float>(0) if barstate.islast and repaint sae = 0. //Compute and set NWE point for i = 0 to math.min(299,n - 1) sum = 0. sumw = 0. //Compute weighted mean for j = 0 to math.min(299,n - 1) w = gauss(i - j, h) sum += src[j] * w sumw += w y2 := sum / sumw sae += math.abs(src[i] - y2) nwe.push(y2) sae := sae / math.min(299,n - 1) * mult for i = 0 to math.min(299,n - 1) if i%2 line.new(n-i+1, y1 + sae, n-i, nwe.get(i) + sae, color = upCss) line.new(n-i+1, y1 - sae, n-i, nwe.get(i) - sae, color = dnCss) if src[i] > nwe.get(i) + sae and src[i+1] < nwe.get(i) + sae label.new(n-i, src[i], '▼', color = color(na), style = label.style_label_down, textcolor = dnCss, textalign = text.align_center) if src[i] < nwe.get(i) - sae and src[i+1] > nwe.get(i) - sae label.new(n-i, src[i], '▲', color = color(na), style = label.style_label_up, textcolor = upCss, textalign = text.align_center) y1 := nwe.get(i) //-----------------------------------------------------------------------------} //Dashboard //-----------------------------------------------------------------------------{ var tb = table.new(position.top_right, 1, 1 , bgcolor = #1e222d , border_color = #373a46 , border_width = 1 , frame_color = #373a46 , frame_width = 1) if repaint tb.cell(0, 0, 'Repainting Mode Enabled', text_color = color.white, text_size = size.small) //-----------------------------------------------------------------------------} //Plot //-----------------------------------------------------------------------------} plot(repaint ? na : out + mae, 'Upper', upCss) plot(repaint ? na : out - mae, 'Lower', dnCss) //Crossing Arrows plotshape(ta.crossunder(close, out - mae) ? low : na, "Crossunder", shape.labelup, location.absolute, color(na), 0 , text = '▲', textcolor = upCss, size = size.tiny) plotshape(ta.crossover(close, out + mae) ? high : na, "Crossover", shape.labeldown, location.absolute, color(na), 0 , text = '▼', textcolor = dnCss, size = size.tiny) //-----------------------------------------------------------------------------} buy = ta.crossunder(close, out - mae) sell = ta.crossover(close, out + mae) if (buy) strategy.entry("Long", strategy.long) if (sell) strategy.close("Long") if (sell) strategy.entry("Short", strategy.short) if (buy) strategy.close("Short")
排查与解决
核心问题在于**repaint输入参数默认是true**,而策略的交易信号完全依赖out、upper、lower这些变量,但在repaint=true的模式下,这些变量的有效计算逻辑被跳过了:
- 在
End point method代码块中,只有当not repaint时才会计算out的有效值,否则out始终为初始赋值的0,导致upper和lower也变成无效值。 - 交易信号
buy = ta.crossunder(close, out - mae)和sell = ta.crossover(close, out + mae)在repaint=true时,由于out无效,永远不会触发交叉条件,自然无法生成订单。 - 另外,
repaint=true模式下的指标绘制仅在最后一根K线执行,属于仅用于查看形态的重绘式显示,无法产生历史有效的交易信号。
修复步骤:
- 临时将
repaint输入参数设置为false,测试策略是否能正常生成订单。 - 如果需要保留重绘模式的显示效果,必须修改交易信号逻辑,让其在两种模式下都能获取有效的上下轨数值:在
repaint=true时,将barstate.islast块中计算的sae和nwe数组值同步到全局的upper/lower变量中(注意重绘模式存在未来函数问题,实盘需谨慎)。 - 建议优先使用非重绘模式进行策略回测和实盘,重绘模式仅用于临时查看指标形态。
内容的提问来源于stack exchange,提问作者Maksym Maksimov
相关产品推荐
相关产品推荐

