TradingView Pine脚本:绘制价格收盘价的百分比

时间:2019-09-18 19:58:49

标签: finance trading pine-script

我正在尝试为TradvingView编写一个指标脚本,其中价格收盘价(或任何一组值)的第80个百分位值和第20个百分位值绘制在图表上(如下代码)。

基于“回溯期”输入(假设为x),它应从当前柱向后看x条,并计算并绘制价格收盘价(或开盘价或任何指定的用户输入)的第20和第80个百分位数)。它应该对每个酒吧都执行此操作。

虽然确实在图​​表上画了线,但它绘制的百分位数值似乎不准确。

将感谢您对我哪里出错了或者是否有一种方法可以使用函数手动计算百分位数值的反馈,因为内置函数似乎在错误地计算它们。

谢谢! :)

//@version=3
study(title="Percentiles", shorttitle="Percentiles", overlay=true)

// ----- DESCRIPTION
//
// Given a set of values, this indicator plots 2 lines: the upper percentile and the lower percentile
// It was made to be overlaid on the 'Bitmex Funding' indicator but could work with any set of values
//
//
//----- PRINTED/RENDERED VALUES
//
// Upper Percentile Plot Threshold | Lower Percentile Plot Threshold
// Upper Percentile Plot (Green Line)
// Lower Percentile Plot (Red Line)
//
//
// ----- OPTIONS
//
// Lookback Period: The number of values to look at plotting the percentiles thresholds.
// Upper Percentile Plot: The percentile plot i.e. 80th percentile plot of a set of values. By default, the value is 80 is used.
// Lower Percentile Plot: The percentile plot i.e. 20th percentile plot of a set of values. By default, the value is 20 is used.


length = input(100, minval=1, title="Lookback Period")
upperPercentilePlot = input (80, title="Upper Percentile Plot")
lowerPercentilePlot = input(20, title="Lower Percentile Plot")
src = input(close, title="Source")

median_high = percentile_nearest_rank(src, length, upperPercentilePlot)
median_low = percentile_nearest_rank(src, length, lowerPercentilePlot)

plot(median_high, color=blue, title="Upper Percentile Plot", linewidth=2)
plot(median_low, color=blue, title="Lower Percentile Plot", linewidth=2)

1 个答案:

答案 0 :(得分:0)

使用该功能的方式似乎没有任何问题,并且似乎可以正常工作。用条形索引的模100替换src行会产生正确的结果:

src = n%100