0
//@version=5
indicator("test V/TWAP + Close", overlay = true)

//string tfInput = input.timeframe("W", "Timeframe")
tf1 = input.timeframe('W', '1st T/Vwap timeframe') 
bool showVwaps = input.bool(true, 'Show VWAP and TWAP')
bool showClose = input.bool(true, 'Show Support and resistance levels')
bool showMitigated = input.bool(true, 'Show Mitigated Levels in gray')
int maxLinesback = input.int(50, 'Max Lines displayed')
var line [] _support = array.new_line(0)
var line [] _resistance = array.new_line(0)
var line [] _resMitigated = array.new_line(0)
var line [] _supMitigated = array.new_line(0)

get_ohlc()=> [close, open, high, low]
[src_c, src_o, src_h, src_l] = request.security(syminfo.tickerid, '', get_ohlc())

//VWAP & TWAP {
_get_WVWAP(_res)=>
    time2 = time(_res)
    data2 = na(time2[1]) or time2 > time2[1]
    source2 = hlc3 * volume
    volume2 = volume
    source2 := data2 ? source2 : source2 + source2[1]
    volume2 := data2 ? volume2 : volume2 + volume2[1]
    vwap2 = source2 / volume2
    vwap2

_get_WTWAP(_res)=>
    weight1 = ta.barssince(ta.change(request.security(syminfo.tickerid, _res, time, lookahead=barmerge.lookahead_on)))
    price1 = 0.0
    price1 := weight1 == 0 ? hlc3 : hlc3 + nz(price1[1])
    twap2 = price1 / (weight1 + 1)
    twap2
//end }

cond_wap = ta.change(time(tf1)) != 0
twap_close = ta.valuewhen(cond_wap, _get_WTWAP(tf1)[1], 0)
vwap_close = ta.valuewhen(cond_wap, _get_WVWAP(tf1)[1], 0)
cond_cross = ta.cross(src_c, vwap_close)

_controlline(_linesArray, _mitCond)=>
    
    if array.size(_linesArray) > 0
            
        for i = array.size(_linesArray) -1 to 0 by 1
            _line = array.get(_linesArray, i)
            _linePosiotion = line.get_y2(_line)
            _lineRight = line.get_x2(_line)

            cond = ta.cross(src_c, _linePosiotion)

            if _mitCond
                if na or (bar_index == _lineRight and not cond)
                    line.set_x2(_line, bar_index + 1)
                else   
                    line.set_color(_line, color.gray)
            else
                if na or (bar_index == _lineRight and not cond) 
                    line.set_x2(_line, bar_index + 1)

plot(showVwaps ? _get_WVWAP(tf1) : na, title='Daily VWAP', color=color.new(color.blue, 15), linewidth = 1)
//plot(showVwaps ? _get_WTWAP(tf1): na, 'Daily TWAP', color.new(color.blue, 15), linewidth = 1)

if ta.change(time(tf1)) and showVwaps
    if close > vwap_close
        supportLine = line.new(bar_index - 1, vwap_close, bar_index, vwap_close, color = color.green, style = line.style_solid, width = 1)
        if cond_cross
            if array.size(_supMitigated) > maxLinesback
                line.delete(array.shift(_supMitigated))
            array.push(_supMitigated, supportLine)
        else
            if array.size(_support) > maxLinesback
                line.delete(array.shift(_support))
            array.push(_support, supportLine)
    else 
        resistanceLine = line.new(bar_index - 1, vwap_close, bar_index, vwap_close, color = color.red, style = line.style_solid, width = 1)
        if cond_cross
            if array.size(_resMitigated) > maxLinesback
                line.delete(array.shift(_resMitigated))
            array.push(_resMitigated, resistanceLine)
        else
            if array.size(_resistance) > maxLinesback
                line.delete(array.shift(_resistance))
            array.push(_resistance, resistanceLine)
if showVwaps
    _controlline(_support, showMitigated)
    _controlline(_resistance, showMitigated)
    _controlline(_supMitigated, showMitigated)
    _controlline(_resMitigated, showMitigated)

https://www.tradingview.com/x/1gQrENSH/

Hi guys, i cant solve the error that is occuring in this script... i am trying to print support and ressistance line at the closing price of weekly vwap. The problem is that not all the lines are displayed, the mitigated Levels seem to be fine but if the Indicator shows the support levels than it doesnt show the resistance levels as shown on the picture above. Could you guys help me with that? Thanks a lot

0 Answers0