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

如何在Pine Script V5布林带策略代码中设置多笔买卖委托单?

布林带策略连续信号问题解决方案

问题根源

你当前代码中使用了oca_name="BollingerBands"和oca_type=strategy.oca.cancel的OCA(一键取消全部)订单组配置,这意味着同一组内只能存在一个订单——当新订单生成时,系统会自动取消组内已有的旧订单。所以连续出现的同方向信号会被互相抵消,只能交替生成买入/卖出限价单。

解决方案

针对你的需求,推荐将买入和卖出信号的OCA组分开设置,让同方向的新订单仅替换同组的旧订单,不同方向订单可同时存在。修改后的代码如下:

strategy("Bollinger Bands Strategy V6", overlay=true)
source = close[0]
length = input.int(20, minval=1)
mult = input.float(2.0, minval=0.001, maxval=50)
basis = ta.sma(source, length)
dev = mult * ta.stdev(source, length)
upper = basis + dev
lower = basis - dev

smaHIGH = ta.sma(high[0], 1)
plot(smaHIGH, offset=0, color = color.rgb(206, 59, 232))

smaLOW = ta.sma(low[0], 1)
plot(smaLOW, offset=0, color = color.rgb(232, 145, 175))

plot(upper)
plot(lower)

// 买入信号使用独立OCA组
if smaHIGH >= lower
    strategy.entry("Buy L", strategy.long, limit = lower, oca_name="BollingerBuy", oca_type=strategy.oca.cancel, comment="Buy L")

// 卖出信号使用独立OCA组
if smaLOW <= upper
    strategy.entry("Sell L", strategy.short, limit = upper, oca_name="BollingerSell", oca_type=strategy.oca.cancel, comment="Sell L")

修改说明

  • 将原来统一的BollingerBands OCA组拆分为BollingerBuy和BollingerSell两个独立组
  • 当连续出现买入信号时,新的买入限价单会取消同组内旧的买入订单,不会影响卖出订单
  • 同理,连续卖出信号只会替换旧的卖出订单,两个方向的订单可以同时存在

如果你需要允许同时挂多个同方向限价单,直接删除所有oca_name和oca_type参数即可,但需注意仓位风险控制。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 07:50:36