Tradingview Pine脚本-如何使自定义交易量指标的行为类似于内置的Vol

时间:2020-02-21 21:31:15

标签: pine-script

我做了一个自定义的音量指示器,但是当应用于图表时,它不会像内置的那样自动缩放,不会自动粘在图表面板的底部,也不能没有它自己的显式显示侧面有多余的刻度。

有没有办法做所有这些事情?

我也没有在原始指标代码中找到帮助。例如。当我尝试应用format.volume时,它根本拒绝编译。

下面是我的代码,原始代码在下面:

//This inddicator will show volume inversely. So if you are looking at an Altcoin, it will show volume in BTC, if you are looking at for example BTC/USD it will show volume in USD and so on. Works with all altcoins and fiat pairs.
//I find this most useful when shopping for alts to quickly get an idea of their liquidity.

//title the indicator and dont use decimals. (Otherwise when viewing fiat volume you get unreadably large numbers) you can change this in the settings dialog tho if you want.
study("Vol in Base asset 20MA", precision=0)

//Make the moving average user configurable
showMA = input(true)

//Get volume for current bar and multiply with vwap
vInverse = volume * vwap

//Plot fiat volume.
plot(vInverse, color = orange, title="VolumeBTC", style=columns, transp=65)

//Plot 20 candle moving average (changable in settings)
plot(showMA ? sma(vInverse,20) : na, color = white, title="Volume MA", style=area, transp=65)

原始代码:

//@version=4
study(title="Volume", shorttitle="Vol", format=format.volume)
showMA = input(true)
barColorsOnPrevClose = input(title="Color bars based on previous close", type=input.bool, defval=false)

palette = barColorsOnPrevClose ? close[1] > close ? color.red : color.green : open > close ? color.red : color.green

plot(volume, color = palette, style=plot.style_columns, title="Volume", transp=65)
plot(showMA ? sma(volume,20) : na, style=plot.style_area, color=color.blue, title="Volume MA", transp=65)

2 个答案:

答案 0 :(得分:3)

您问题的简短答案是“否”,目前尚无法做您想做的所有事情。要完成内置的音量独立程序,我们需要做以下两件事:

  1. 在图表上显示哪些条形图的松动可见性(因此我们可以动态缩放音量列的高度)。
  2. 一种在图表底部锚固独立指标的方法,同时仍允许指标的比例自动缩放。

此代码还使用了Michel的scale.none的想法,但是它在顶部添加了一个不可见的图,使您可以确定垂直空间中列的最大高度,而无需缩放列本身的值,因此您仍然获得正确的读数。使用*设置/输入”,您可以:

  • 指定您希望最多的列占用的垂直空间的最大百分比。
  • 指定如何确定用于缩放列的最大过去值,即使用:
    • 历史最高点,或者
    • 最后 n 条中的最高列( n = 400 为默认列)。第二种方法可以更好地适应最近的图表上下文。

enter image description here

//This indicator will show volume inversely. So if you are looking at an Altcoin, it will show volume in BTC, if you are looking at for example BTC/USD it will show volume in USD and so on. Works with all altcoins and fiat pairs.
//I find this most useful when shopping for alts to quickly get an idea of their liquidity.

//title the indicator and dont use decimals. (Otherwise when viewing fiat volume you get unreadably large numbers) you can change this in the settings dialog tho if you want.
//@version=4
study("Vol in Base asset 20MA", "", true, format.volume, 0, scale.none)

//Make the moving average user configurable
HIM1 = "1. Historical High"
HIM2 = "2. Highest in last..."
showMA = input(true)
scaleFactor = 100 / input(30, "% of vertical space used", step = 10, maxval = 100)
hiMethod = input(HIM2, "High point method", options = [HIM1, HIM2])
hiMethod2Len = input(400, "  2. Length", minval = 2, step = 100)

//Get volume for current bar and multiply with vwap
vInverse = volume * vwap

//Plot fiat volume.
plot(vInverse, color = color.orange, title="VolumeBTC", style=plot.style_columns, transp=0) //Originally: transp=65

//Plot 20 candle moving average (changable in settings)
plot(showMA ? sma(vInverse,20) : na, color = color.white, title="Volume MA", style=plot.style_area, transp=65)

//Plot high line to scale down the columns.
var histHi = 0.
histHi := max(histHi, nz(vInverse, histHi))
limit = hiMethod == HIM1 ? histHi : highest(vInverse, hiMethod2Len)
plot(limit * scaleFactor, "Historical High", #00000000)

这将给出如下信息: enter image description here

有两种方法可以将列的底部置于图表的底部,但没有一种是理想的:

  1. 使用图表的 Settings / Appearance / Bottom margin 将图表的底部边距设置为0%,但是图表栏继续与列重叠。
  2. 通过向上/向下拖动其缩放比例(红色箭头)来稍微压缩独立的缩放比例。然后抓住图表上的列(绿色箭头)并将其降低,但这会冻结独立程序的比例,因此当您更改符号并增加/减少音量时,它不会调整大小。

enter image description here

电视已将指示符与垂直空间底部对齐的功能视为一种潜在的改进,但尚无预计到达时间。

答案 1 :(得分:0)

您还缺少其他一些东西。

首先,您应该放置二手松木的版本。有一个特殊的字符串://@version=4如果未设置该字符串,则该代码被认为是版本1,没有format参数。

要关闭自动缩放,应将scale参数设置为scale.none。请注意,它仅在overlay参数为true

时有效

要使用版本4,颜色应在实际颜色名称color.之前加上color.orange前缀,并且绘图样式应在前缀plot.style_之前加上plot.style_area < / p>

// NOTE: THE STRING WITH VERSION BELOW IS IMPORTANT!
//@version=4

// to turn the scale off scale.none is used. Note, that it might be only applied
// if the overlay param is 'true'
study(title="Volume", shorttitle="Vol", format=format.volume, overlay=true, scale=scale.none)

showMA = input(true)

vInverse = volume * vwap

// colors must have the prefix 'color.' before the color names
plot(vInverse, color = color.orange, title="VolumeBTC", style=plot.style_columns, transp=65)
plot(showMA ? sma(vInverse,20) : na, color = color.white, title="Volume MA", style=plot.style_area, transp=65)