计算松树系列中最后 N 个元素的标准偏差 [英] Calculating standard deviation of last N elements of in a pine series

查看:69
本文介绍了计算松树系列中最后 N 个元素的标准偏差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试计算一个系列的最后 5 个元素的标准偏差下面是我从 TradingView 官方的 ADX 实现中复制的实现

Try to calculate the standard deviation of last 5 elements of a series below is the implementation I copied from TradingView official's ADX implementation

//@version=4
study(title="DMI movement out of 2SD in last 10 points", shorttitle="StrategyX", format=format.price, precision=4, resolution="")
lensig = input(14, title="ADX Smoothing", minval=1, maxval=50)
len = input(14, minval=1, title="DI Length")

up = change(high)
down = -change(low)
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
trur = rma(tr, len)
plus = fixnan(100 * rma(plusDM, len) / trur)
minus = fixnan(100 * rma(minusDM, len) / trur)
sum = plus + minus
adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), lensig)

我试图做的是获得最新的 5 加 (DMI+) 数据点的标准偏差,我尝试了多种方法..

What I was trying to do is to get the standard deviation of the latest 5 plus (DMI+) data points, I tried multiple approaches..

  1. 创建一个数组,然后将数据点一个一个推入

但结果我无法从 plus 系列中获取数据点.它应该返回一个变量(倒数第五个元素)而不是另一个系列(根据官方文档:https://www.tradingview.com/pine-script-reference/v4/#op_[])

But turned out I can't get the data point from the plus series. It should return a variable (the fifth last element) but not another series (according to the official doc: https://www.tradingview.com/pine-script-reference/v4/#op_[])

plus[5] // do not understand why it is still a series but not a float

  1. 将系列转换为数组,然后执行reverseslice

再次失败,因为无法将系列转换为数组

which again failed as there is no way to convert series to array

非常感谢任何帮助:祈祷谢谢

any help would be greatly appreciated :pray thanks

推荐答案

您是否尝试过内置的 stdev 功能?

Have you tried the built-in stdev function?

plus2StDev = 2 * stdev(plus, 5)
plot(plus2StDev)

但结果我无法从 plus 系列中获取数据点可能是在运行的早期阶段,数据尚不存在,例如在第一个柱形上没有 plus 系列的过去值.您可以使用nz 函数来安全访问系列历史数据 - nz(plus[5]).

But turned out I can't the data point from the plus series Could be during the early stages of runtime the data doesn't exists yet, for example on the first bar there is no past values of plus series. You can use nz function to safely access the series history data - nz(plus[5]).

由tradeview用户alexgrover共享的自定义stdev函数:

custom stdev function shared by tradingview user alexgrover:

f_stdev(src, length) => length == 1 ? 0 : sqrt(sma(pow(src, 2), length) - pow(sma(src, length), 2)) 
plus2StDev = 2 * f_stdev(plus, 5)
plot(plus2StDev)

这篇关于计算松树系列中最后 N 个元素的标准偏差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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