Search code examples
pine-scriptseriespine-script-v5tradingview-apialgorithmic-trading

pinescript to get weekly close price's as series from daily timeframe chart


I am trying to extract weekly close prices from daily chart or daily secuirty-call (request.security(syminfo.ticker , "D", screener_func()) in tradingview.

Based on timeframe change(W), i am able to get previous day close and store into array. Its enough to get 60 weekly close prices. These 60 price values in array need to be converted into series(It can assume na beyond the 60 values). Series of these values are desired so other pine functions can be applied on the new series data.

Here the problem is, Convert array of values into series data. wkseries variable is getting only 'na' values and not elements from closewk array.


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

//series of close prices. except last day of week all other days price are na
closew2 = timeframe.change("W") ? close[1] : na

//going back to 60 weeks(approax) and collecting weekly close into array
closewk = array.new_float()
if barstate.islast
    for i = 0 to 60 * 5
        if not na(closew2[i]) //dont need na values
            array.push(closewk,closew2[i])

//print 3rd value in array on status line to debug array values
plot(array.size(closewk)>0? array.get(closewk,3): na, title='dataFromArray', display=display.status_line)

//THE BELOW LINE NEEDS FIX
//Convert array to series
wkseries = array.size(closewk)>0 and bar_index<62? array.get(closewk,bar_index) : na
//debug print on series values
plot(wkseries[1], title='dataFromSeries', display=display.status_line)


//Draw harizontal lines on last one weekly close price in daily chart
//debug print
line.new(bar_index, closew2[4],bar_index+10,  closew2[4], style = line.style_dashed, color = color.black, extend = extend.both)



Help is appreciated in the code above where comment is in capitals. Thanks


Solution

  • Get weekly close price into an array. By the time it reaches latest bar, the array would have been filled with more than 2000 weeks of weekly close prices.

    closew = timeframe.change("W") ? close[1] : na
    var closewk = array.new_float()
    if closew
        array.push(closewk,closew)
    

    Here is the tricky part. Use last_bar_index and bar_index to start reading from array for the last 60 candles. Its sufficient to get last 60 weekly close price into a series for my usecase. So calculate array index that is needed to start getting values from array and put it into a series

    var arridx = 0
    var count = 0
    if last_bar_index - bar_index == 60
        arridx := array.size(closewk) - 49 // 49 = (60-60/5+1) //5 days/week
    
    if last_bar_index - bar_index < 60
        count := count + 1
    plot(arridx+count, title='incremental array index for last 60 candles', display=display.status_line)
    

    Convert the array values into series starting from last 60 daily candles

    wkseries = array.size(closewk)>0 and (last_bar_index - bar_index) <= 60? array.get(closewk,arridx+count) : na
    plot(wkseries, title='plot weekly data onto daily chart ', style=plot.style_line, display=display.all)
    

    I now have a weekly close prices plotted on daily candles on a continuous basis. i.e., day0 candle/tick plotted along with recent week close, yesterday candle/tick plotted along with previous wk close, day before yesterday candle/tick plotted along with previous to previous wk close....