如何在Pine脚本中保存设定时间后的实时买卖量数据?

问题描述 投票:0回答:1

我正在开发一个 Pine Script 指标,该指标使用较低时间范围内的 10 秒数据来跟踪新 2 分钟柱的前 20 秒内的买入和卖出量。

我遇到的问题是,虽然我可以计算 20 秒窗口内的交易量,但这些值在显示后会消失或被 TradingView 的更新周期覆盖。

我想要实现的目标:

实时更新每个 2 分钟柱的前 20 秒(以累积交易量)。 20 秒后,我想冻结买入/卖出量值并在柱的剩余 1 分 40 秒内保留它们。 显示交易量的表格应在前 20 秒后停止更新,但值应保持可见,直到下一个柱开始。 我尝试过使用 var 和持久变量,但我认为我遗漏了 Pine Script 处理实时数据和更新的方式。

关于如何在不消失或重置值的情况下实现此目的有什么想法吗?

我使用以下脚本,它为下一个 2 分钟柱线的开始提供一个倒计时器,然后在 20 秒标记处显示最后 2 个 10 秒柱线的合并买入/卖出量。然而,这些值在显示后几乎立即消失。

我尝试使用 var 将这些值存储在持久变量中,以便它们在 2 分钟柱的剩余时间内保持可见,但我无法让这些值按预期持续存在。

我理想中想要实现的目标:

使计算值在 2 分钟柱的整个剩余持续时间内持续存在。 最终,我想扩展它以在 20 秒窗口内显示实时逐笔交易量更新。

这是我当前正在使用的脚本:

提前致谢!



```
`//@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)
```
pine-script pine-script-v5
1个回答
0
投票

如果您希望您的值在同一柱上的每次报价更新期间保留其值,则需要使用

varip
关键字。

varip (var intrabar persist) 是用于赋值的关键字 以及用户定义的变量或字段的一次性初始化 类型。它与 var 关键字类似,但变量和字段 用varip声明的在执行之间保留它们的值 脚本在同一个栏上。

varip float persistent_buy_vol = na
varip float persistent_sell_vol = na
varip string display_text = "Waiting for update..."

varip int last_update_time = 0
© www.soinside.com 2019 - 2024. All rights reserved.