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

TradingView策略脚本问题:持仓时无法触发加仓条件排查

TradingView通道突破策略加仓触发异常问题

问题背景

我正在编写一个基于通道突破的TradingView策略脚本,核心交易规则:

  • 开仓条件:收盘价大于50日最高收盘价
  • 平仓条件:收盘价小于50日最低收盘价

当前核心问题:持仓且价格持续上涨时,无法触发加仓条件。

排查细节:仅当strategy_position_size == 0且注释掉加仓后的next_purchase价格更新代码时,long_additional加仓条件才会触发。图中绿色加号为预期的加仓触发位置。

脚本代码

lookback = input.int(title="Length", minval=1, maxval=1000, defval=100)
atr_length = input.int(21, title="ATR Length")
atrMultiplier = input.int(3, title="ATR Multiplier")
risk_per_trade = input.float(5, title="risk percentage")

// Variables
float atr = ta.atr(atr_length)
float atr_value = atr * atrMultiplier

// Calculate stop loss values
var float trailing_stop = na
var float next_purchase = na

longCondition = close > ta.highest(close, lookback)[1] 
sellCondition = close < ta.lowest(close, lookback)[1]

if(longCondition and strategy.position_size == 0 and time_cond)
    // calculate risk per trade
    float risk_amount = strategy.initial_capital * (risk_per_trade / 100)
    // calculate amount of shares to purchase
    float amount_of_shares = risk_amount / atr_value
   
    // buy
    strategy.entry("long", strategy.long, qty=amount_of_shares)
   
    if (na(trailing_stop))
        trailing_stop := close - atr_value

    if (na(next_purchase))
        next_purchase = close + atr_value

// executes condition if close hits next_purchase
if ( strategy.position_size >= 1 and close > next_purchase)
    float risk_amount = strategy.initial_capital * (risk_per_trade / 100)
    float amount_of_shares = risk_amount / atr_value
    strategy.entry("Long_additonal", strategy.long, qty=amount_of_shares)

    // Update next_purchase after buying additional shares
    next_purchase := close + atr_value

已尝试的解决方法

  • 缩小回溯周期,尝试在上涨趋势中触发加仓;
  • 将加仓条件调整为仅当strategy_position_size >=1且close > next_purchase,但long_additional仍未触发,仅在平仓后才会买入,与预期不符。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 16:46:17