Pine Script问题:RSI低于30买入正常但高于70无法卖出
问题分析与修复方案
核心问题
- 平仓订单ID不匹配:
strategy.close()需要指定对应开仓订单的标识ID,你开仓时用的是"Entry",但平仓代码里写的是"Exit",导致系统找不到目标平仓订单。 - 仓位判断逻辑局限:
strategy.position_size == 1只适用于固定1手的仓位场景,而你使用了strategy.percent_of_equity模式,实际仓位大小大概率不是1,应该用strategy.position_size > 0来判断是否持有多单。
修复后的完整代码
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © debonairToucan74454 //@version=5 strategy("70/30", overlay = true, initial_capital = 4000, default_qty_value = 100, default_qty_type = strategy.percent_of_equity) longCondition = ta.crossunder(ta.rsi(close, 14), 30) sellCondition = ta.crossover(ta.rsi(close, 14), 70) timePeriod = time >= timestamp(syminfo.timezone, 2022, 11, 1, 0, 0) if (longCondition and strategy.position_size == 0 and timePeriod) strategy.entry("Entry", strategy.long) // 修复:匹配开仓订单ID,调整持仓判断逻辑 if(sellCondition and strategy.position_size > 0 and timePeriod) strategy.close("Entry")
额外优化建议
如果需要更灵活的平仓控制(比如同时设置止损止盈),可以用strategy.exit()替代strategy.close(),示例如下:
if (longCondition and strategy.position_size == 0 and timePeriod) strategy.entry("Entry", strategy.long) // 示例:设置开仓价下浮2%止损,上浮3%止盈 strategy.exit("Exit", "Entry", stop=close*0.98, limit=close*1.03)
内容的提问来源于stack exchange,提问作者ASANGER Simon
相关产品推荐
相关产品推荐

