1

In Pine V5, I have used array in one indicator. array.get() , array.set(), mattrix.get() these kind of functions are used in that code. I am capturing values of different indicators, across different time frames, inside this array. This indicator working fine in 1 min, 5 min and 15 min time frames. But as soon as I am shifting to hourly time frame, it is showing following error and indicator is not being displayed,

Study Error - Error on bar 0: Cannot call array methods when id of array is na.

//@version=5
indicator("Learning Array V3", shorttitle = "Learning Array V3", overlay = true)

_sma_source    = close

_sma01_Length  = 10
_sma02_Length  = 30
_sma03_Length  = 100
_sma04_Length  = 200

_my_array_func() =>
    array_output = array.new<float>(4, float(na))
    
    _sma10  = ta.sma(_sma_source, _sma01_Length)
    _sma30  = ta.sma(_sma_source, _sma02_Length)
    _sma100 = ta.sma(_sma_source, _sma03_Length)
    _sma200 = ta.sma(_sma_source, _sma04_Length)
   
    [_sma10, _sma30, _sma100, _sma200] 
    
    array.set(array_output, 0, _sma10)
    array.set(array_output, 1, _sma30)
    array.set(array_output, 2, _sma100)
    array.set(array_output, 3, _sma200)
    
    array_output

//========================== Getting Data inside Array  ===========

_data_mtly = request.security(syminfo.tickerid, "1M", _my_array_func())
_data_wkly = request.security(syminfo.tickerid, "1W", _my_array_func())
_data_daly = request.security(syminfo.tickerid, "1D", _my_array_func())
_data_hrly = request.security(syminfo.tickerid, "60", _my_array_func())
_data_15mn = request.security(syminfo.tickerid, "15", _my_array_func())

My_Array = matrix.new<float>(5, 4, na)

matuu(mtx1, _row1, _data) =>
    if not na(array.get(_data, 0))
        matrix.set(mtx1, _row1,  0, array.get(_data, 0)) // SMA10
        matrix.set(mtx1, _row1,  1, array.get(_data, 1)) // SMA50
        matrix.set(mtx1, _row1,  2, array.get(_data, 2)) // SMA100
        matrix.set(mtx1, _row1,  3, array.get(_data, 3)) // SMA200
        

matuu(My_Array, 0, _data_mtly)
matuu(My_Array, 1, _data_wkly)
matuu(My_Array, 2, _data_daly)
matuu(My_Array, 3, _data_hrly)
matuu(My_Array, 4, _data_15mn)

// Just to check what value is being stored in the Array  ....

Show_Array_Table = table.new(position.top_right, 4, 5, #000000, #000000, 1, #000000, 1)
for i = 0 to 4  
    table.cell(Show_Array_Table, 0, i, str.tostring(matrix.get(My_Array, i, 0)), text_size = size.small, bgcolor = #000000, text_color = #FFFFFF)
    table.cell(Show_Array_Table, 1, i, str.tostring(matrix.get(My_Array, i, 1)), text_size = size.small, bgcolor = #000000, text_color = #FFFFFF)
    table.cell(Show_Array_Table, 2, i, str.tostring(matrix.get(My_Array, i, 2)), text_size = size.small, bgcolor = #000000, text_color = #FFFFFF)
    table.cell(Show_Array_Table, 3, i, str.tostring(matrix.get(My_Array, i, 3)), text_size = size.small, bgcolor = #000000, text_color = #FFFFFF)

I don't understand how come same piece of code is working fine in 1 min / 5 min / 15 min but the same is failing in hourly TF.

KalC
  • 11
  • 2
  • without code it is hard to help you. – G.Lebret May 01 '23 at 06:05
  • Hi G.Lebert, thanks for your kind revert. Pls find the code below, – KalC May 01 '23 at 08:21
  • No idea what I am looking at here but could it be that you iterate 5 times instead of 4? `for i = 0 to 4` should result in 5 roundtrips and not 4 as could result in the missing id of array? Just shoot in the dark – Denny Mueller May 01 '23 at 09:40
  • Thanks Denny Mueller for trying to help me out. Loop iteration for 5 times is not the cause of this problem. I checked it by editing that part but the isuues remained as it is. – KalC May 01 '23 at 12:40
  • Thanks G.Lebret for giving me this detail revert. I tried the solution you suggested but could not sort this out. I draw this conclusion that it is not possible to catch 15min TF indicator values into an array when chart TF is Hourly. By the by I am not only failing to catch values in Hourly TF but also failing if the chart has less number of candles. In less number of candles ( like monthly option chart ) similar error message is coming. G.Lebret you are quite knowledgeable in Pine hence request you to give it another try, if you can get some free time. Thanks a lot ! – KalC May 01 '23 at 13:19
  • G.Lebert, kindly note I am catching a Float array whereas request.security_lower_tf can be used with simple int expression only. Otherwise this could work ...if timeframe.period == "60" _data_15mn = request.security_lower_tf(syminfo.tickerid, "15", my_array_func()) else _data_15mn = request.security(syminfo.tickerid, "15", _my_array_func()) – KalC May 01 '23 at 13:42

1 Answers1

0

in your script, you ask for those request.security :

_data_mtly = request.security(syminfo.tickerid, "1M", _my_array_func())
_data_wkly = request.security(syminfo.tickerid, "1W", _my_array_func())
_data_daly = request.security(syminfo.tickerid, "1D", _my_array_func())
_data_hrly = request.security(syminfo.tickerid, "60", _my_array_func())
_data_15mn = request.security(syminfo.tickerid, "15", _my_array_func())

So if you set your timeframe above 15min (30 min or 1 hour), your _data_15mn will create problems.
In pinescript to use request.security on a lower timeframe, you use a special function (request.security_lower_tf) that send you back an array (because there are several values in the 15min timeframe when you are on the 1 hour timeframe on your chart).

From the pinescript manual :

Scripts not written specifically to use lower timeframe data, when they are published for a broader audience, should ideally include protection against running them on chart timeframes where request.security() would be accessing lower timeframes than the chart’s, as it will not produce reliable results in those cases. See the Comparing timeframes section for a code example providing error-checking to avoid just that.

The link to deal with the control of timeframe here :
https://www.tradingview.com/pine-script-docs/en/v5/concepts/Timeframes.html#pagetimeframes-comparingtimeframes

G.Lebret
  • 2,826
  • 2
  • 16
  • 27