微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Pine Script - 时间范围较短的增量交易量

如何解决Pine Script - 时间范围较短的增量交易量

我有 Tradingview Delta 交易量指标的代码。它在较短的时间范围(5 分钟)上收集交易量数据,以获得较高的时间范围安全图表。除了安全性的最后一个栏之外,这似乎可以正常工作。在这里,带有体积数据的 5 分钟间隔完全不正常并显示出胡说八道。 任何的想法?这是 Pine Script 中的问题吗?

// This source code is subject to the terms of the Mozilla Public License 2.0 at 
https://mozilla.org/MPL/2.0/
//@version=4

study(title="Volume Delta",shorttitle="Volume Delta",precision=2)
size = input(title="Fragment Size (minutes)",type=input.integer,defval=5,maxval=1440,minval=1)
// Comment: make sure you have set a large (default) fragment size before switching to a monthly 
chart
var string res = tostring(size)

periodMultiplier = security(syminfo.tickerid,timeframe.period,timeframe.multiplier)
periodisInTraday = security(syminfo.tickerid,timeframe.isinTraday)
periodisDaily = security(syminfo.tickerid,timeframe.isdaily)
periodisWeekly = security(syminfo.tickerid,timeframe.isweekly)

int conversion = periodisInTraday ? 1 : periodisDaily ? 24 * 60 : periodisWeekly ? 24 * 60 * 7 : 0
int count = barstate.islast ? floor((timeNow - time)/(1000 * 60 * size)) + 1 : conversion * 
periodMultiplier / size

// variable to add or substract volume when computing delta volume
float tickVol = close > open ? volume : close < open ? -volume : 0.0

// function to compute delta volume,max and min delta volume and total delta volume
Vol() =>
    float deltaVol = 0.0
    float totalDeltaVol = 0.0
    float maxDeltaVol = 0.0
    float minDeltaVol = 0.0
    int temp = 0
    temp := count
    for i = 0 to temp - 1
        deltaVol := deltaVol + nz(tickVol[i])
        totalDeltaVol := totalDeltaVol + nz(abs(tickVol[i]))
        if deltaVol > maxDeltaVol
            maxDeltaVol := deltaVol
        if deltaVol < minDeltaVol
            minDeltaVol := deltaVol
    [deltaVol,maxDeltaVol,minDeltaVol,totalDeltaVol]

// calling security with lower timeframe resolution to compute up and down volume more accurately
[deltaVolume,maxDeltaVolume,minDeltaVolume,totalDeltaVolume] = security(syminfo.tickerid,res,Vol(),lookahead=barmerge.lookahead_off)

// plotting delta volume bars
hline(0)
plot(deltaVolume,title="Delta Volume",style=plot.style_line,color=color.yellow,transp=65,offset=0)
//plot(totalDeltaVolume,title="Total Delta Volume",color=color.blue,offset=0)
//plot(maxDeltaVolume,title="Max Delta Volume",color=color.green,offset=0)
//plot(minDeltaVolume,title="Min Delta Volume",color=color.red,offset=0)
//plot(tickVol,title="Tick Volume",color=color.fuchsia,offset=0)

// computing cumulative delta volume
var float cumDeltaVolume = 0.0
cumDeltaVolume := cumDeltaVolume == 0.0 ? deltaVolume : nz(cumDeltaVolume[1]) + deltaVolume
//plot(cumDeltaVolume,title="Cumulative Delta Volume",color=color.orange,transp=0,offset=0,linewidth=2)

// computing ohlc values to plot cumulative delta candles
var float o = 0.0
var float c = 0.0
o := cumDeltaVolume == deltaVolume ? 0.0 : c[1]
c := o + deltaVolume
float h = c + maxDeltaVolume - deltaVolume
float l = o + minDeltaVolume

// plotting cumulative delta volume candles
plotcandle(o <= c ? o : na,h,l,c,title="Cumulative Delta Green Candle",color = color.teal,wickcolor=color.teal,bordercolor=color.teal)
plotcandle(o >= c ? o : na,title="Cumulative Delta Red Candle",color = color.red,wickcolor=color.red,bordercolor=color.red)

解决方法

是的,这是预期的行为。 Intrabar 段未在实时栏中对齐。请参阅在栏内 TF here 中使用 security() 的限制。 TV 并未正式支持此功能。

披露:此答案中的链接指向 PineCoders 常见问题解答条目。
我是 PineCoders 社区的成员,我很可能写了那个 FAQ 条目。 PineCoders 是由 TradingView 支持的 Pine 编码志愿者小组,PineCoders 的网站具有严格的教育意义。 TradingView 和 PineCoders 都不会从向 pinecoders.com 发送流量中获得经济利益,并且该网站不包含附属/推荐链接。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。