PINE脚本:是否有一种方法可以考虑计算平均值的一天中的特定小时?

发布于 2025-02-01 01:21:05 字数 338 浏览 3 评论 0原文

例如:我正在使用以下代码来计算过去7天的平均每日体积:

isess = session.regular
t = ticker.new(syminfo.prefix, syminfo.ticker, session=isess)
AvgVol7 = ta.sma(volume, 7)
SecAvgVol7 = request.security(t, 'D', AvgVol7[1], gaps=barmerge.gaps_off , lookahead=barmerge.lookahead_on)

但是我想做的是计算过去7天的相同平均每日体积,但仅考虑从凌晨6点到凌晨6点下午6点。有办法做到吗?

For example: I'm using the following code to calculate the average daily volume of the past 7 days:

isess = session.regular
t = ticker.new(syminfo.prefix, syminfo.ticker, session=isess)
AvgVol7 = ta.sma(volume, 7)
SecAvgVol7 = request.security(t, 'D', AvgVol7[1], gaps=barmerge.gaps_off , lookahead=barmerge.lookahead_on)

But what I would like to do is to calculate the same average daily volume of the past 7 days, but only consider hours from 6am to 6pm. Is there a way to do that?

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

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

发布评论

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

评论(1

帅气尐潴 2025-02-08 01:21:05
//@version=5
indicator("Custom Range Daily Volume", overlay = false)

start_hr = input.int(6, title = "Start hour")
end_hr = input.int(18, title = "End hour")

num_days = input.int(7, title = "Number of days")

var float[] daily_volumes = array.new_float()

var float todays_volume = 0.0

new_day = ta.change(time("D")) != 0

eod_time_close = request.security(syminfo.tickerid, "D", time_close, barmerge.gaps_off, barmerge.lookahead_on)

in_time_range = hour >= start_hr and hour < end_hr
is_eod = time_close == eod_time_close and barstate.isconfirmed

if new_day
    todays_volume := 0.0
else if in_time_range
    todays_volume += volume

if is_eod
    array.unshift(daily_volumes, todays_volume)
    if array.size(daily_volumes) > num_days
        array.pop(daily_volumes)

float daily_avg_vol = na
float repainting_avg_vol = na

if array.size(daily_volumes) == num_days
    daily_avg_vol := array.avg(daily_volumes)
    repainting_avg_vol := in_time_range ? (array.sum(daily_volumes) - array.get(daily_volumes, num_days - 1) + todays_volume) / num_days : na
plot(daily_avg_vol)
plot(fixnan(repainting_avg_vol), color = in_time_range ? color.yellow : color.gray, style = plot.style_circles)

//@version=5
indicator("Custom Range Daily Volume", overlay = false)

start_hr = input.int(6, title = "Start hour")
end_hr = input.int(18, title = "End hour")

num_days = input.int(7, title = "Number of days")

var float[] daily_volumes = array.new_float()

var float todays_volume = 0.0

new_day = ta.change(time("D")) != 0

eod_time_close = request.security(syminfo.tickerid, "D", time_close, barmerge.gaps_off, barmerge.lookahead_on)

in_time_range = hour >= start_hr and hour < end_hr
is_eod = time_close == eod_time_close and barstate.isconfirmed

if new_day
    todays_volume := 0.0
else if in_time_range
    todays_volume += volume

if is_eod
    array.unshift(daily_volumes, todays_volume)
    if array.size(daily_volumes) > num_days
        array.pop(daily_volumes)

float daily_avg_vol = na
float repainting_avg_vol = na

if array.size(daily_volumes) == num_days
    daily_avg_vol := array.avg(daily_volumes)
    repainting_avg_vol := in_time_range ? (array.sum(daily_volumes) - array.get(daily_volumes, num_days - 1) + todays_volume) / num_days : na
plot(daily_avg_vol)
plot(fixnan(repainting_avg_vol), color = in_time_range ? color.yellow : color.gray, style = plot.style_circles)

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