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

如何在Pine Script中持久化特定时段后的实时买卖成交量数据?

Pine Script指标开发问题:2分钟K线前20秒成交量跟踪持久化问题

我正在开发一款Pine Script指标,目标是利用10秒次周期数据,跟踪2分钟新K线生成后前20秒内的买卖成交量。目前遇到的问题是:虽然能计算该20秒窗口内的成交量数据,但数据会被TradingView的更新周期覆盖或消失。

我的核心需求:

  • 在每根2分钟K线的前20秒内,实时更新并累加成交量;
  • 20秒后冻结买卖成交量数值,使其在该K线剩余的1分40秒内保持可见,展示成交量的表格停止更新,但数据保留至下一根K线启动;
  • 后续计划扩展为在20秒窗口内显示逐笔实时成交量更新。

我尝试过用var和persistent变量存储数据,但没达到预期的持久化效果。当前脚本会显示下一根2分钟K线启动的倒计时,并在20秒节点展示过去2根10秒K线的合并买卖成交量,但数据几乎立即消失。以下是当前脚本代码:

//@version=5
indicator("Buy vs Sell Volumes (First 20s of 2-minute bar)", overlay=true)

// Inputs
main_timeframe = "2"  // 2-minute bars
lower_timeframe = "10S"  // 10-second bars

// Function to calculate buy and sell volumes
calc_volumes(vol, close, high, low) =>
    buy_vol = vol * (close - low) / (high - low)
    sell_vol = vol - buy_vol
    [buy_vol, sell_vol]

// Fetch lower timeframe data
[volume_10s, close_10s, high_10s, low_10s] = request.security_lower_tf(syminfo.tickerid, lower_timeframe, [volume, close, high, low])

// Permanent storage for volumes after 20 seconds
var float persistent_buy_vol = na
var float persistent_sell_vol = na
var string display_text = "Waiting for update..."

var int last_update_time = 0

// Update logic
update_volumes() =>
    float buy_sum = 0.0
    float sell_sum = 0.0
    if array.size(volume_10s) >= 2
        for i = 0 to 1
            vol = array.get(volume_10s, array.size(volume_10s) - 1 - i)
            close_val = array.get(close_10s, array.size(close_10s) - 1 - i)
            high_val = array.get(high_10s, array.size(high_10s) - 1 - i)
            low_val = array.get(low_10s, array.size(low_10s) - 1 - i)
            
            if high_val != low_val
                [buy_vol, sell_vol] = calc_volumes(vol, close_val, high_val, low_val)
                buy_sum += buy_vol
                sell_sum += sell_vol
    [buy_sum, sell_sum]

// Check if it's time to update (exactly 20 seconds into a new 2-minute bar)
current_time = time(main_timeframe)
seconds_into_bar = (timenow - current_time) / 1000
is_update_time = seconds_into_bar >= 20 and seconds_into_bar < 21 and current_time != last_update_time

if is_update_time
    [persistent_buy_vol, persistent_sell_vol] = update_volumes()
    display_text := "Last 20s - Buy: " + str.tostring(persistent_buy_vol, "#.##") + 
                     " | Sell: " + str.tostring(persistent_sell_vol, "#.##")
    last_update_time := current_time

// Ensure the display remains persistent for the whole 2-minute bar
if not na(persistent_buy_vol) and not na(persistent_sell_vol)
    display_text := "Buy: " + str.tostring(persistent_buy_vol, "#.##") + 
                     " | Sell: " + str.tostring(persistent_sell_vol, "#.##")

// Create the table once (ensure it's not recreated on every bar)
var table vol_table = table.new(position.bottom_right, 1, 2, frame_color=color.black, bgcolor=color.new(color.black, 70))

if barstate.islast
    table.cell(vol_table, 0, 0, "Volume Analysis", bgcolor=color.new(color.blue, 70), text_color=color.white)
    table.cell(vol_table, 0, 1, display_text, text_color=color.white)

// Display countdown
next_update_time = 120 - (seconds_into_bar % 120)
countdown_text = "Next update in " + str.tostring(math.round(next_update_time)) + "s"
var label countdown_label = label.new(bar_index, high, countdown_text, color=color.new(color.black, 80), textcolor=color.white, style=label.style_label_down)

// Set label to update on new and last bar
if barstate.islast
    label.set_xy(countdown_label, bar_index, high)
    label.set_text(countdown_label, countdown_text)

内容的提问来源于stack exchange,提问作者bitesized-danni

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 08:32:35