PineScript策略反向信号无法平仓及TradingView无平仓标记问题求助
PineScript策略平仓失效问题排查与解决建议
我开发了一套PineScript交易策略,规则是开仓(多仓/买入或空仓/卖出)后跟随趋势,当出现反向交易信号时平仓原有仓位,但当前策略无法执行平仓操作,同时TradingView图表上也不显示平仓标记(LX或SX)。目前只能手动平仓,已经造成亏损,我使用WunderTrading作为TradingView与Binance(加密货币交易)的中转工具。
代码示例
bool longCondition = long_entry bool shortCondition = short_entry // Create entries based on the cross conditions for both trades biases. if longCondition and inDateRange strategy.entry("LONG", strategy.long, comment=i_alert_txt_entry_long) strategy.close("SX", comment=i_alert_txt_exit_short) // Close the short position if open if shortCondition and inDateRange strategy.entry("SHORT", strategy.short, comment=i_alert_txt_entry_short) strategy.close("LX",comment=i_alert_txt_exit_long) // Close the long position if open
问题排查与解决建议
- 平仓目标ID错误:
strategy.close()的参数是要平仓的仓位ID,你代码里用的"SX"和"LX"是TradingView自动生成的平仓标记,不是实际开仓的仓位ID。正确的做法是把strategy.close("SX")改成strategy.close("SHORT"),把strategy.close("LX")改成strategy.close("LONG"),对应你开仓时指定的"LONG"和"SHORT"仓位ID。 - 策略基础设置检查:打开TradingView策略设置面板,在「Position Sizing」选项中,确认「Allow opposite direction entries」未开启。如果开启该选项,策略会允许同时持有多空仓位,不会自动平仓反向仓位。
- 信号触发逻辑验证:添加可视化标记确认信号触发时机,在代码中加入以下代码,能直观看到
longCondition和shortCondition是否在有持仓时正确触发:plotshape(longCondition, title="Long Signal", location=location.belowbar, color=color.green, style=shape.labelup) plotshape(shortCondition, title="Short Signal", location=location.abovebar, color=color.red, style=shape.labeldown) - WunderTrading同步配置检查:登录WunderTrading后台,确认你的策略同步设置中,平仓信号的转发权限已开启。部分中转工具需要策略明确输出标准化的alert信息才能识别平仓信号,可以在
strategy.entry和strategy.close中补充清晰的comment内容,避免信号被过滤。
内容的提问来源于stack exchange,提问作者Wahab Khorsheed
相关产品推荐
相关产品推荐

