'Find the first value of a plot line

I have the plot:

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

theline(src, len) => wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len)))

Line = theline(close, 9)

plot(Line, title='Line', color=#0066ff, linewidth=3)

That line will move up or down depending on the close value.

How do I find the first value of that line, when the new bar appears, when the first close==open.

I need that value to compare, to see if the current line is above or under that first value.

Thank you for helping me.



Solution 1:[1]

The answer is the same principle as your last question, you have to refactor the wma equation in order to obtain the sum of the first n-1 values as normal and replace the nth value (current bar's value) calculated using open instead of close.

//@version=5
indicator("hull open", overlay = true)

len = input.int(9)

f_wma_open(_close, _open, _len) =>
    float _wtd_sum = 0.0
    int _denom = 0

    for i = 1 to _len - 1
        _wt = _len - i
        _wtd_sum += _close[i] * _wt
        _denom += _wt

    _wtd_sum += _open * _len
    _denom += _len

    _wma_open = _wtd_sum / _denom
    _wma_open


f_hull_open(_close, _open, _len) =>
    _a = 2 * ta.wma(_close, _len / 2) - ta.wma(_close, _len)
    _b = 2 * f_wma_open(_close, _open, _len / 2) - f_wma_open(_close, _open, _len)
    _slen = math.round(math.sqrt(_len))
    
    float _wtd_sum = 0.0
    float _denom = 0.0
    for i = 1 to _slen - 1    
        _wt = _slen - i
        _wtd_sum += _a[i] * _wt
        _denom += _wt
    
    _wtd_sum += _b * _slen
    _denom += _slen
    
    _hull_open = _wtd_sum / _denom
    _hull_open


f_hull(_src, _len) =>
    ta.wma(2 * ta.wma(_src, _len / 2) - ta.wma(_src, _len), math.round(math.sqrt(_len)))
    
hull = f_hull(close, len)
hull_open = f_hull_open(close, open, len)


plot(hull, color = color.gray)
plot(hull_open, color = color.yellow)

Solution 2:[2]

You can use varip to freeze and hold real time values and update them conditionally. I wrote a custom function for you that will freeze the real time open value of the wma line. Please note this will only work in real time or for alerts. It will only freeze the open if you are watching the open live, and alerts will begin working on the first open after adding to the chart.

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

theline(src, len) => wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len)))

openVal(src) =>
    varip float lineOpen = na
    if barstate.isnew
        lineOpen := src
    result = barstate.islastconfirmedhistory[1] or barstate.isconfirmed ? src : lineOpen

Line = theline(close, 5)

Line2 = openVal(Line)

plot(Line, title='Line', color=#0066ff, linewidth=4)
plot(Line2, title='Alt Line', color=color.white)

cheers and best of luck

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rumpypumpydumpy
Solution 2 Bjorgum