柱线打开时如何找到布林带的第一个值

How to find the first value of Bollinger Bands when bar open

实际上,布林带代码是:

//@version=4
study(title="AAAA", shorttitle="AAAA", overlay=true)

len = 5
multi = 2

bb5med = sma(close, len)
devBB5 =  mult2 * stdev(close, len)
bb5top =  bb5med + devBB5
bb5bot =  bb5med - devBB5

我想在新柱出现时找到这 3 行的第一个值,也就是说,当收盘==开盘时。

此外,当我将 len 更改为 20、50 and/or 当我将 multi 更改为 3

时,我需要它工作

请帮帮我。谢谢。

//@version=5
indicator("BB Open", overlay = true)

len = input.int(20)
mult = input.float(2.000)

basis = (math.sum(close, len - 1)[1] + open) / len

float dev_sum = 0.0

for i = 1 to len - 1
    dev_sum += math.pow(basis - close[i], 2)

dev_sum += math.pow(basis - open, 2)

stdev = math.sqrt(dev_sum / len)

up = basis + stdev * mult
dn = basis - stdev * mult

plot(basis, color = color.yellow)
plot(up)
plot(dn)

函数:

f_BBopen(_close, _open, _len, _mult) =>
    _basis = (math.sum(_close, _len - 1)[1] + _open) / _len

    float _dev_sum = 0.0

    for i = 1 to _len - 1
        _dev_sum += math.pow(_basis - _close[i], 2)

    _dev_sum += math.pow(_basis - _open, 2)

    _stdev = math.sqrt(_dev_sum / _len)

    _up = _basis + _stdev * _mult
    _dn = _basis - _stdev * _mult
    [_basis, _up, _dn]

[basis, up, dn] = f_BBopen(close, open, len, mult)