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

Pine Script策略编写求助:RSI交易策略出现‘Line 36: Mismatched input '+=' expecting ')'’语法错误

嘿,我来帮你搞定这个Pine Script的问题~

首先说你遇到的语法错误:你在if条件里用了vrsi += increase这种赋值操作,这在Pine Script的条件判断里是不允许的。条件语句需要的是能返回真假的布尔表达式(比如a > b),而+=是用来给变量赋值的,编译器自然会报错说找不到预期的)。

接下来看你的策略逻辑,你想实现的是:

  • 买入:RSI指标上涨5个点,且当前RSI≤50,同时满足时间条件和无持仓
  • 卖出:RSI指标下跌5个点,且当前RSI≥70,同时满足时间条件

这里的“上涨/下跌5个点”,应该是指当前RSI相比上一根K线的RSI变化了5个点对吧?那我们需要用比较运算符来判断这个差值,而不是赋值。另外你的卖出逻辑里还有两个小问题:一是没判断是否有持仓,二是止损和止盈的参数搞反了。

下面是修改后的完整代码,我标注了核心修改点:

//@version=5
strategy("RSI Simple Strategy", overlay=true)

timePeriod = time >= timestamp(syminfo.timezone, 2022, 6, 1, 0, 0)
notInTrade = strategy.position_size <= 0
//Stop Loss and Take Profit for Long
Stop_loss= input.float(1, "Stop Loss (%)") / 100
Take_profit= input.float(4, "Take Profit (%)") / 100
longStopPrice = strategy.position_avg_price * (1 - Stop_loss)
longTakeProfit = strategy.position_avg_price * (1 + Take_profit)
//RSI
length = input.int(14, "RSI Length")
price = close
vrsi = ta.rsi(price, length)
increase = 5

// 修改点1:把赋值操作改成判断RSI的上涨差值
if (vrsi - vrsi[1] >= increase and vrsi <= 50 and timePeriod and notInTrade)
    strategy.entry("Long", strategy.long)

decrease = 5
// 修改点2:把赋值操作改成判断RSI的下跌差值,同时增加持仓判断
if (vrsi[1] - vrsi >= decrease and vrsi >= 70 and timePeriod and strategy.position_size > 0)
    // 修改点3:修正止损和止盈的参数顺序(止损是更低价格,止盈是更高价格)
    strategy.exit(id='Exit Long', stop=longStopPrice, limit=longTakeProfit)

我再补充几个细节提示:

  1. 我给input加上了类型(input.float/input.int)和标签,这样在指标设置面板里更清晰,这是可选优化,但推荐这么做。
  2. 如果你说的“上涨5个点”不是指相邻K线的差值,而是RSI从最近的低点累计上涨了5个点,那可以用ta.lowest(vrsi, n)计算最近n根K线的RSI低点,再判断vrsi - ta.lowest(vrsi, n) >= 5,你可以根据实际需求调整。
  3. 卖出时一定要判断strategy.position_size > 0,避免在无持仓时触发无效的卖出操作。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 21:24:07