使用具有可变长度的 percentile_nearest_rank 来计算运行中位数 [英] Using percentile_nearest_rank with variable length to calculate running median

查看:64
本文介绍了使用具有可变长度的 percentile_nearest_rank 来计算运行中位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 pine-script 中创建一个运行中值函数.
目的是计算估计的股票代码值和实际股票代码值之间的中值误差.
请参阅绘制每日高、低、收盘的手动水平关于这个估计的一些历史.
对于每一个新的估计,中值误差都会随着时间的推移而变化,因为它必须考虑所有历史误差才能找到中值误差.

I'm trying to create a running median function in pine-script.
The purpose is to calculate a median error between an estimated ticker value and the actual ticker value.
See Plotting manual levels for daily high,low,close for a bit of history on this estimation.
For each new estimation, the median error will change over time, because it has to take into account all historical errors to find the median error.

要计算中位数,我们必须使用 percentile_nearest_rank(source, length, percent) 函数和 percentage=50.
例如,percentile_nearest_rank(close, 100, 50) 将给出最近 100 个收盘价的中位数.
这将计算向后 100 根柱线的滑动窗口的中值.
然而,这不是我要找的.

To calculate a median we have to use the percentile_nearest_rank(source, length, percentage) function with percentage=50.
For example, percentile_nearest_rank(close, 100, 50) will give the median of the last 100 close prices.
This will calculate the median of a sliding window of 100 bars back.
However, that's not what I'm looking for.

我想要做的是计算固定起始柱线(固定柱线编号或日期)和当前柱线之间的中位数.
假设我们每天都有酒吧,我的开始日期是 3 月 5 日.
3 月 6 日,我的中位数计算长度为 2 个柱:percentile_nearest_rank(close, 2, 50)
3 月 7 日,我的中位数计算长度为 3 个柱:percentile_nearest_rank(close, 3, 50)
3 月 8 日,我的中位数计算长度为 4 个柱:percentile_nearest_rank(close, 4, 50)
等等...
这意味着 length 参数将在每个条形上增加.
所以,我想知道是否可以在 pine 脚本中使用 percentile_nearest_rank 函数,其中 length 参数不是固定的,而是在每个条形上发生变化.
如果没有,我愿意接受替代方案(如果有的话).

What I'd like to do is calculate the median between a fixed starting bar (fixed bar number or date) and the current bar.
Say that we have daily bars and my starting date is March 05.
On March 06, I have a length of 2 bars for my median calculation: percentile_nearest_rank(close, 2, 50)
On March 07, I have a length of 3 bars for my median calculation: percentile_nearest_rank(close, 3, 50)
On March 08, I have a length of 4 bars for my median calculation: percentile_nearest_rank(close, 4, 50)
etc...
This means that the length parameter will increase on each bar.
So, I'm wondering if it's possible in pine script to use the percentile_nearest_rank function where the length parameter is not fixed, but changes on each bar.
If not, I'm open to alternatives if there are any.

推荐答案

使用 n 个数据点的窗口大小计算滚动中值将需要这些数据点按升序排列的有序列表订购.

The computation of a rolling median using a window size of n data points will require an ordered list of these data points in an ascending order.

由于您想使用增加的窗口大小,因此您需要使用循环,但是您不能在循环内排序(据我所知).所以我认为这是不可能的.

Since you want to use an increasing window size you would need to use a loop, however you can't sort within a loop (as far as i'am aware). So i think its not possible to do that.

您可以使用累积平均值 cum(x)/bar_index 作为替代,我不知道在收盘价的情况下差异是否显着.

You could use a cumulative mean cum(x)/bar_index as a replacement, i don't know if the difference would be significant in the case of closing prices.

这篇关于使用具有可变长度的 percentile_nearest_rank 来计算运行中位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆