You need to enable JavaScript to run this app. Pine Script独立开平仓策略组合实现 各仓位分配50%账户权益
优惠活动
大模型
产品
解决方案
定价
更多

Pine Script独立开平仓策略组合实现 各仓位分配50%账户权益

Pine Script双独立突破子策略实现方案

你原有代码无法同时运行两个子策略的核心原因有两个:

  • 全局默认开仓占比为100%权益,第一个子策略开仓后账户无剩余资金可用
  • pyramiding参数设为1,限制策略最多只能同时持有1个未平仓头寸

以下是修改后的完整可运行代码:

//@version=4
strategy(title="Breakout", overlay=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 0, pyramiding = 2, precision=4)
short = input(10)
long = input(20)
start_year = input(2000)
start_month = input(1)
end_year = input(9999)
end_month = input(1)
start     = timestamp(start_year, start_month, 1)  
finish    = timestamp(end_year, end_month, 1)       
window()  => time >= start and time <= finish ? true : false

H = max(close,open)
L = min(close,open)
buy1 = crossover(H,highest(H[1],short))
sell1 = crossunder(L,lowest(L[1],short))
buy2 = crossover(H,highest(H[1],long))
sell2 = crossunder(L,lowest(L[1],long))

// 每个子策略单独指定50%权益的开仓占比
strategy.entry(id="long1", long=true, qty=50, when=buy1 and window(), comment="buy1") 
strategy.close("long1", when = sell1 and window(), comment="sell1")
strategy.entry(id="long2", long=true, qty=50, when=buy2 and window(), comment="buy2") 
strategy.close("long2", when = sell2 and window(), comment="sell2")

核心修改点说明:

  • 将pyramiding参数调整为2,允许策略同时持有最多2个未平仓头寸,匹配两个独立子策略的持仓需求
  • 将全局default_qty_value改为0,避免默认全仓开单,改为在每个strategy.entry调用时通过qty=50参数单独指定,每个子策略开仓占用当前账户50%的权益
  • 保留两个订单的独立ID与对应平仓逻辑,两个子策略的开平仓操作完全独立,互不影响

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.11 09:09:03