Pine Script策略报错resolution参数与副作用函数不兼容如何解决?
问题修复方案
错误触发的核心原因有两点:
- 你当前代码包含
strategy.entry、strategy.close_all这类策略交易函数,属于策略脚本,但代码开头用了仅适用于指标脚本的study()声明,且声明中携带的resolution参数是指标用于跨周期引用数据的参数,和策略函数的副作用逻辑存在兼容性冲突。 - 原有代码的平仓逻辑写在了开仓条件的判断分支内部,逻辑上永远无法触发,需要调整层级。
需要修改的具体内容如下:
- 把开头的
study()声明替换为适配策略脚本的strategy()声明,直接删除冲突的resolution参数 - 调整平仓逻辑的判断层级,把平仓判断从开仓的if分支中移到外层,保证条件可以正常触发
修改后的完整代码如下:
strategy("RedK Slow_Smooth WMA 策略", shorttitle="RSS_WMA v2 策略", overlay = true, initial_capital = 10000, default_qty_type = strategy.cash, default_qty_value = 10000, commission_type = strategy.commission.percent, commission_value = 0.1) //=============================================== f_LazyLine(_data, _length) => w1 = 0, w2 = 0, w3 = 0 L1 = 0.0, L2 = 0.0, L3 = 0.0 w = _length / 3 if _length > 2 w2 := round(w) w1 := round((_length-w2)/2) w3 := int((_length-w2)/2) L1 := wma(_data, w1) L2 := wma(L1, w2) L3 := wma(L2, w3) else L3 := _data L3 //==================================== price = input(title = "Source", type = input.source, defval = close) alpha = input(title = "Combined Smoothness", type = input.integer, defval = 15, minval = 1) LL = f_LazyLine(price, alpha) c_up = color.new(#33ff00, 0) c_dn = color.new(#ff1111, 0) uptrend = LL > LL[1] plot(LL, "SS_WMA Line", color = uptrend ? c_up : c_dn, linewidth=3) plot(wma(price, alpha), "WMA", color = color.purple, display=0) if (LL > LL[1]) strategy.entry("Buy", strategy.long) if (LL < LL[1]) strategy.close_all() if (LL < LL[1]) strategy.entry("Sell", strategy.short) if (LL > LL[1]) strategy.close_all()
如果你不需要双向交易只做单边,可以自行删除对应的做空逻辑即可。
内容的提问来源于stack exchange,提问作者jacky666
相关产品推荐
相关产品推荐

