You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pine Script出现strategy.long语法错误 仅高于入场价平仓如何解决

错误原因排查

  • 语法错误1:代码中price := close行末尾多了一个多余的反引号,破坏了代码结构,属于拼写类低级错误。
  • 语法错误2:strategy.entry方法的参数使用不规范:Pine Script不允许混合使用位置传参和命名传参,你显式指定了id="buy"(命名传参),后续的交易方向参数也必须显式指定参数名direction=strategy.long,不能直接写strategy.long,这就是你收到第17行语法错误的直接原因。
  • 逻辑错误:平仓条件写反,你需要当前价高于入场价才平仓,但你写的判断是price > close,也就是入场价高于当前价,和需求完全相反。

修复后完整代码

strategy("Stoch. RSI", format=format.price, overlay=true)
length = input.int(10, minval=1)
OverBought = input(85)
OverSold = input(5)
smoothK = 3
smoothD = 3

k = ta.sma(ta.stoch(close, high, low, length), smoothK)
d = ta.sma(k, smoothD)
b = ta.crossover(k,OverSold)
s = ta.crossunder(d,OverBought)

var price = 0.0
if (b and k > OverSold) and strategy.position_size == 0
    price := close
    strategy.entry(id="buy", direction=strategy.long , comment="Buy")

if (s and d < OverBought) and (close > price)
    strategy.close(id="buy", comment="Sell")

内容的提问来源于stack exchange,提问作者Crabo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 11:15:03