开市第一个小时的成交量 table

Volume table for first hour of market open

我目前有这个脚本可以为我提供今天和过去 20 天的平均交易量数据。但是,我正在寻找市场开盘前 x minutes/hour 的成交量和平均成交量(过去 20 天)。

示例:5 分钟vol/vol 平均基于第一个 5开市分钟。前 5 分钟后,此单元格停止更新,因为它已超过前 5 分钟。


//@version=5
indicator(title="Data Table RVR", shorttitle="Data Table RVR", overlay=true)



///////////////////////////////////////////////////////////////////////////////// INPUTS

var string GP1 = "Volume"

i_length = input.int(defval=20, title=" Volume Days", minval=1, maxval=200, group=GP1)


var string GP2 = "Table Style"
i_tbl_bg_color = input.color(defval=color.rgb(149, 152, 161, 0), title="  Table Background Color", group=GP2) //  rgb(149,152,161) 
i_tbl_border_color = input.color(defval=color.rgb(30, 39, 46, 0), title="  Table Border Color", group=GP2) // Black Pearl= rgb(30, 39, 46)
i_tbl_text_color = input.color(defval=color.rgb(241, 242, 246, 0), title="  Table Text Color", group=GP2) // Anti-Flash White = rgb(241, 242, 246)
i_tbl_frame_width = input.int(defval=2, title="  Table Frame Width", minval=1, maxval=20, group=GP2)
i_tbl_border_width = input.int(defval=1, title="  Table Border Width", minval=1, maxval=20, group=GP2)

string  i_tableYpos = input.string(defval="bottom", title=" Table Position", inline="11", options=["top", "middle", "bottom"], group=GP2)
string  i_tableXpos = input.string(defval="right", title="", inline="11", options=["left", "center", "right"], group=GP2)

///////////////////////////////////////////////////////////////////////////////// HELPER FUNCTIONS

openCurTime(tf) => request.security(syminfo.tickerid, tf, time)


///////////////////////////////////////////////////////////////////////////////// VOLUME DATA

// Current days volume
volToday = request.security(syminfo.tickerid, "D", volume)
volTodaySinceOpen = math.round(request.security(syminfo.tickerid, "5", volume   ))
volTodayMil = math.round(volToday / 1000000, 2)

// Average volume over last X days
volXDayAvg = request.security(syminfo.tickerid, "D", ta.sma(volume, i_length))
volXDayAvgMil = math.round(volXDayAvg / 1000000, 2)

// Percent current days volume of average volume
percentVolXDay = math.round(volToday / volXDayAvg * 100, 0)



///////////////////////////////////////////////////////////////////////////////// TABLE

vol1 = math.round(request.security(syminfo.tickerid, "5", volume))
//vol1Avg = math.round(request.security(syminfo.tickerid, "5", ta.sma(volume, i_length)) / 1000000, 2)
vol1Avg = request.security(syminfo.tickerid, "5", ta.sma(volume, i_length))

vol2 = math.round(request.security(syminfo.tickerid, "15", volume))
vol2Avg = request.security(syminfo.tickerid, "15", ta.sma(volume, i_length))

vol3 = math.round(request.security(syminfo.tickerid, "30", volume))
vol3Avg = request.security(syminfo.tickerid, "30", ta.sma(volume, i_length))

vol4 = math.round(request.security(syminfo.tickerid, "60", volume))
vol4Avg = request.security(syminfo.tickerid, "60", ta.sma(volume, i_length))

vol5 = math.round(request.security(syminfo.tickerid, "90", volume))
vol5Avg = request.security(syminfo.tickerid, "90", ta.sma(volume, i_length))

/////////////////////////////////////////////////////////////////////////////////



// Display table only on last bar to reduce computation time
if barstate.islast
     
    // Only Display table on daily chart or a lower timeframe
    if timeframe.isminutes and timeframe.multiplier < 60
        // Create a table with 3 columns and 2 rows
        var table volTable = table.new(i_tableYpos + "_" + i_tableXpos, 6, 7, bgcolor = i_tbl_bg_color, frame_width = i_tbl_frame_width, frame_color = i_tbl_border_color, border_width = i_tbl_border_width, border_color = i_tbl_border_color)
        // Populate cells in table
        
        // Column 1
        table.cell(volTable, 0, 0, text=" ", text_halign=text.align_left)
        table.cell(volTable, 0, 1, text="Volume (Avg)", text_halign=text.align_left)
        table.cell(volTable, 0, 2, text="Volume (Today)", text_halign=text.align_left)
        table.cell(volTable, 0, 3, text="Volume (Today %)", text_halign=text.align_left)
        // Column 2
        table.cell(volTable, 1, 0, text="5m")
        table.cell(volTable, 1, 1, str.tostring(vol1Avg, format.volume))
        table.cell(volTable, 1, 2, str.tostring(vol1, format.volume))
        table.cell(volTable, 1, 3, str.tostring(math.round(vol1 / vol1Avg * 100,0)) + "%")
        // Column 3
        table.cell(volTable, 2, 0, text="15m")
        table.cell(volTable, 2, 1, str.tostring(vol2Avg, format.volume))
        table.cell(volTable, 2, 2, str.tostring(vol2, format.volume))        
        table.cell(volTable, 2, 3, str.tostring(math.round(vol2 / vol2Avg * 100,0)) + "%")
        // Column 4
        table.cell(volTable, 3, 0, text="30m")
        table.cell(volTable, 3, 1, str.tostring(vol3Avg, format.volume))
        table.cell(volTable, 3, 2, str.tostring(vol3, format.volume))        
        table.cell(volTable, 3, 3, str.tostring(math.round(vol3 / vol3Avg * 100,0)) + "%")
        // Column 5
        table.cell(volTable, 4, 0, text="1hr")
        table.cell(volTable, 4, 1, str.tostring(vol4Avg, format.volume))
        table.cell(volTable, 4, 2, str.tostring(vol4, format.volume))        
        table.cell(volTable, 4, 3, str.tostring(math.round(vol4 / vol4Avg * 100,0)) + "%")
        // Column 6
        table.cell(volTable, 5, 0, text="90m")
        table.cell(volTable, 5, 1, str.tostring(vol5Avg, format.volume))
        table.cell(volTable, 5, 2, str.tostring(vol5, format.volume))        
        table.cell(volTable, 5, 3, str.tostring(math.round(vol5 / vol5Avg * 100,0)) + "%")```

所以,这里有一个 5 分钟和 15 分钟的小例子。

下面的代码是专门为 1 分钟的时间范围编写的。如果您需要在不同的时间范围内使用它,则需要对其进行调整。

  • 您应该使用 volume days 大小的数组。这样您的数组将始终包含最近 n 天的数据。

  • 首先,您应该弄清楚当前的柱线是否是一个新的时段。这将有助于重置一些变量。

  • 假设您想要获取 5 分钟的体积数据。在 1 分钟的时间范围内,这将是 5 个柱。

  • 在每个会话中有一个柱数计数器(在新会话时重置)。

  • 如果当前会话柱数小于您的目标(例如 5 min/bars),请跟踪交易量

  • 当您达到目标柱数时,将总成交量添加到数组中

这是您可以使用的示例代码。我已经为你添加了一些评论。如果有什么不清楚的地方,请告诉我。

//@version=5
indicator(title="Data Table RVR", shorttitle="Data Table RVR", overlay=false, format=format.volume)

i_length = input.int(defval=20, title=" Volume Days", minval=1, maxval=200)


f_add_to_array(_arr, _val) =>
    len = array.size(_arr)

    if (len == i_length)    // We hit the lookback period
        array.pop(_arr)     // Remove the last element
        array.unshift(_arr, _val)   // Insert to first position
    else    // Still days to go
        array.unshift(_arr, _val)

// Find if it is a new session
is_new_sesion = ta.change(time("1D"))
bgcolor(is_new_sesion ? color.new(color.blue, 85) : na)

// Arrays for total volume
var vol_arr_5 = array.new_float(i_length, 0)
var vol_arr_15 = array.new_float(i_length, 0)


// Variables for volume
var int bar_cnt = 0
var float vol_5 = 0
var float vol_15 = 0

// If it's a new session, start over
// If we have some bars to go, add the volume to current count
// If we are beyond the target number of bars, keep the last value
vol_5 := is_new_sesion ? volume : (bar_cnt < 5) ? vol_5 + volume : vol_5
vol_15 := is_new_sesion ? volume : (bar_cnt < 15) ? vol_15 + volume : vol_15

bar_cnt := is_new_sesion ? 0 : bar_cnt + 1

if (bar_cnt == 5)
    f_add_to_array(vol_arr_5, vol_5)
else if (bar_cnt == 15)
    f_add_to_array(vol_arr_15, vol_15)


total_volume_5 = array.sum(vol_arr_5)
avg_volume_5 = array.avg(vol_arr_5)

total_volume_15 = array.sum(vol_arr_15)
avg_volume_15 = array.avg(vol_arr_15)

plot(total_volume_5, color=color.green)
plot(avg_volume_5, color=color.lime)
plot(vol_5, color=color.blue)

plot(total_volume_15, color=color.red)
plot(avg_volume_15, color=color.orange)
plot(vol_5, color=color.white)

编辑:

//@version=5
indicator(title="Market Open RVR", shorttitle="Market Open RVR", overlay=true, format=format.volume)
 
i_length = input.int(defval=20, title=" Volume Days", minval=1, maxval=200)
 
 
f_add_to_array(_arr, _val) =>
    len = array.size(_arr)
 
    if (len == i_length)    // We hit the lookback period
        array.pop(_arr)     // Remove the last element
        array.unshift(_arr, _val)   // Insert to first position
    else    // Still days to go
        array.unshift(_arr, _val)
 
// Find if it is a new session
is_new_sesion = ta.change(time("1D"))
bgcolor(is_new_sesion ? color.new(color.blue, 85) : na)
 
// Arrays for total volume
var vol_arr_5 = array.new_float(i_length, 0)
var vol_arr_15 = array.new_float(i_length, 0)
var vol_arr_30 = array.new_float(i_length, 0)
var vol_arr_60 = array.new_float(i_length, 0)
var vol_arr_90 = array.new_float(i_length, 0)
 
// Variables for volume
var int bar_cnt = 0
var float vol_5 = 0
var float vol_15 = 0
var float vol_30 = 0
var float vol_60 = 0
var float vol_90 = 0
 
// If it's a new session, start over
// If we have some bars to go, add the volume to current count
// If we are beyond the target number of bars, keep the last value
vol_5 := is_new_sesion ? volume : (bar_cnt < 5) ? vol_5 + volume : vol_5
vol_15 := is_new_sesion ? volume : (bar_cnt < 15) ? vol_15 + volume : vol_15
vol_30 := is_new_sesion ? volume : (bar_cnt < 30) ? vol_30 + volume : vol_30
vol_60 := is_new_sesion ? volume : (bar_cnt < 60) ? vol_60 + volume : vol_60
vol_90 := is_new_sesion ? volume : (bar_cnt < 60) ? vol_90 + volume : vol_90
 
bar_cnt := is_new_sesion ? 0 : bar_cnt + 1
 
if (bar_cnt == 5)
    f_add_to_array(vol_arr_5, vol_5)
if (bar_cnt == 15)
    f_add_to_array(vol_arr_15, vol_15)
if (bar_cnt == 30)
    f_add_to_array(vol_arr_30, vol_30)
if (bar_cnt == 60)
    f_add_to_array(vol_arr_60, vol_60)
else if (bar_cnt == 90)
    f_add_to_array(vol_arr_90, vol_90)
 
 
total_volume_5 = array.sum(vol_arr_5)
avg_volume_5 = array.avg(vol_arr_5)
 
total_volume_15 = array.sum(vol_arr_15)
avg_volume_15 = array.avg(vol_arr_15)
 
total_volume_30 = array.sum(vol_arr_30)
avg_volume_30 = array.avg(vol_arr_30)
 
total_volume_60 = array.sum(vol_arr_60)
avg_volume_60 = array.avg(vol_arr_60)
 
total_volume_90 = array.sum(vol_arr_90)
avg_volume_90 = array.avg(vol_arr_90)
 
 
var string GP2 = "Table Style"
i_tbl_bg_color = input.color(defval=color.rgb(149, 152, 161, 0), title="  Table Background Color", group=GP2) //  rgb(149,152,161) 
i_tbl_border_color = input.color(defval=color.rgb(30, 39, 46, 0), title="  Table Border Color", group=GP2) // Black Pearl= rgb(30, 39, 46)
i_tbl_text_color = input.color(defval=color.rgb(241, 242, 246, 0), title="  Table Text Color", group=GP2) // Anti-Flash White = rgb(241, 242, 246)
i_tbl_frame_width = input.int(defval=2, title="  Table Frame Width", minval=1, maxval=20, group=GP2)
i_tbl_border_width = input.int(defval=1, title="  Table Border Width", minval=1, maxval=20, group=GP2)
 
string  i_tableYpos = input.string(defval="bottom", title=" Table Position", inline="11", options=["top", "middle", "bottom"], group=GP2)
string  i_tableXpos = input.string(defval="right", title="", inline="11", options=["left", "center", "right"], group=GP2)
 
 
// Display table only on last bar to reduce computation time
if barstate.islast
 
    // Only Display table on daily chart or a lower timeframe
    if timeframe.isminutes and timeframe.multiplier < 60
        // Create a table with 3 columns and 2 rows
        var table volTable = table.new(i_tableYpos + "_" + i_tableXpos, 6, 7, bgcolor = i_tbl_bg_color, frame_width = i_tbl_frame_width, frame_color = i_tbl_border_color, border_width = i_tbl_border_width, border_color = i_tbl_border_color)
        // Populate cells in table
 
        // Column 1
        table.cell(volTable, 0, 0, text=" ", text_halign=text.align_left)
        table.cell(volTable, 0, 1, text="Volume (Avg)", text_halign=text.align_left)
        table.cell(volTable, 0, 2, text="Volume (Today)", text_halign=text.align_left)
        table.cell(volTable, 0, 3, text="Volume (Today %)", text_halign=text.align_left)
        // Column 2
        table.cell(volTable, 1, 0, text="5m")
        table.cell(volTable, 1, 1, str.tostring(avg_volume_5, format.volume))
        table.cell(volTable, 1, 2, str.tostring(vol_5, format.volume))
        table.cell(volTable, 1, 3, str.tostring(math.round(vol_5 / avg_volume_5 * 100,0)) + "%")
        // Column 3
        table.cell(volTable, 2, 0, text="15m")
        table.cell(volTable, 2, 1, str.tostring(avg_volume_15, format.volume))
        table.cell(volTable, 2, 2, str.tostring(vol_15, format.volume))        
        table.cell(volTable, 2, 3, str.tostring(math.round(vol_15 / avg_volume_15 * 100,0)) + "%")
        // Column 4
        table.cell(volTable, 3, 0, text="30m")
        table.cell(volTable, 3, 1, str.tostring(avg_volume_30, format.volume))
        table.cell(volTable, 3, 2, str.tostring(vol_30, format.volume))        
        table.cell(volTable, 3, 3, str.tostring(math.round(vol_30 / avg_volume_30 * 100,0)) + "%")
        // Column 5
        table.cell(volTable, 4, 0, text="1hr")
        table.cell(volTable, 4, 1, str.tostring(avg_volume_60, format.volume))
        table.cell(volTable, 4, 2, str.tostring(vol_60, format.volume))        
        table.cell(volTable, 4, 3, str.tostring(math.round(vol_60 / avg_volume_60 * 100,0)) + "%")
        // Column 6
        table.cell(volTable, 5, 0, text="90m")
        table.cell(volTable, 5, 1, str.tostring(avg_volume_90, format.volume))
        table.cell(volTable, 5, 2, str.tostring(vol_90, format.volume))        
        table.cell(volTable, 5, 3, str.tostring(math.round(vol_90 / avg_volume_90 * 100,0)) + "%")