首个市场开放的音量表

发布于 2025-01-20 12:41:35 字数 5641 浏览 1 评论 0原文

我目前有这个脚本,可以为今天和最后20天的平均数据提供数量数据。但是,我正在寻找市场开放的前X分钟/小时,我正在寻找平均数量和量的平均值(最近20天)。

示例 :5分钟vol/vol avg基于首先 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)) + "%")```

I currently have this script that gives me volume data for Today and last 20 days average. But, I am looking for volume and volume average (last 20 days) for only the first x minutes/hour of the market opening.

Example: 5 mins vol/vol avg is based on first 5 mins of the market open. After the first 5 mins, this cell stops updating since it's beyond the first 5 mins.


//@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)) + "%")```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落成白 2025-01-27 12:41:35

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

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

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

  • 首先,您应该确定当前柱是否是一个新会话。这对于重置一些变量很有用。

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

  • 有一个计数器,用于记录每个会话中的柱数(在新会话时重置)。

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

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

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

//@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)) + "%")

So, here is a small example for 5min and 15min.

Below code is written specifically for the 1min timeframe. If you need to use this on a different timeframe, it needs to be adjusted.

  • You should use arrays with a size of your volume days. This way your array would always have the data of the last n days.

  • First of all, you should figure out if the current bar is a new session. This will be useful to reset some variables.

  • Let's say you want to get 5 min volume data. On the 1min timeframe, this would be 5 bars.

  • Have a counter for number of bars in each session (reset when it is a new session).

  • Keep track of the volume if the current session bar number is less than your target (5 min/bars for example)

  • When you hit your target bar number, add the total volume to your array

This is a sample code which you can use. I have added some comments for you. Let me know if something is unclear.

//@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)

Edit:

//@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)) + "%")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文