如何在for循环上下文中正确使用`ta.correlation()`?

huangapple go评论49阅读模式
英文:

how to use ta.correlation() correctly in for_loop context

问题

I am facing a problem with ta.correlation() within two for-loops. The warning provided by the compiler reads:

"The function 'ta.correlation' should be called on each calculation for consistency. It is recommended to extract the call from this scope."

As pointed out "mandatory" in the documentation, the ta.correlation() call should be used on every bar for data consistency, and should NOT be limited by any condition. In my script (as I understand the Pine5 docu), my for_loop cycles through all 5x5 security-combos, and this is (correctly?) done on EVERY bar. Although the warning above is provided.

But the plot (1) of the >>ta.correlation() for_loop output stored in matrix_mcc<< shows that something is wrong with my looping script results.

A direct plot (2) of ta.correlation() using my arrays is proving that correct data is available in the arrays for ta.correlation().

I already asked the Tradingview-Pine5-Support-guys for help. The answer here was, I should run ta.correlation() in the global context. Unfortunately, they are not allowed to provide code examples.

Trying to initiate all variables getting in touch with ta.correlation() via 'var' or 'varip' shows the same (wrong) results and compiler warning.

Maybe someone can help with the correct usage of ta.correlation() in for-loops.

Best Regards
Hendrix

英文:

I am facing a problem with ta.correlation() within two for-loops. The warning provided by the compiler reads :

The function 'ta.correlation' should be called on each calculation for consistency. It is recommended to extract the call from this scope

As pointed out „mandatory“ in the documentation, the ta.correaltion() call should be used on every bar for data_consistency, and should NOT be limited by any condition. In my script (as I understand the Pine5 docu), my for_loop cycles through all 5x5 security-combos, and this is (correctly?) done on EVERY bar. Allthough the warning above is provided.

But the plot (1) of the >>ta.correlation() for_loop output stored to matrix_mcc<< shows, that something is wrong with my looping script results.

A direct plot (2) of ta.correlation() using my arrays is proofing, that correct datas are avail in the arrays for the ta.correlation().

//@version=5
indicator(title = &quot;FAQ-HX1&quot;, shorttitle =&quot;FAQ-HX1&quot;)

// init a few arrays + one matrix
A_hlcc4 = array.new_float(50, 0)
A_close = array.new_float(50, 0)
A_RlTme = array.new_bool(50, false)
A_ticker = array.new_string(50, &quot;-&quot;)
A_currency = array.new_string(50, &quot;-&quot;)
A_timeopen = array.new_int(50, 1)
A_timeclose = array.new_int(50, 1)

mcc = matrix.new&lt;float&gt;(25,25,na) //larger than needed



// function fill arrays with pulled tuples
f_FillArray(Name,TickerNumber)=&gt; 
    [src_hlcc4, src_close, src_RlTme, src_ticker, src_currency,src_timeopen, src_timeclose] = request.security( Name, &quot;D&quot;, [hlcc4,close,barstate.isrealtime,syminfo.ticker,syminfo.currency,time,time_close])
    array.set(A_hlcc4,TickerNumber,src_hlcc4)
    array.set(A_close,TickerNumber,src_close)
    array.set(A_RlTme,TickerNumber,src_RlTme)
    array.set(A_ticker,TickerNumber,src_ticker)
    array.set(A_currency,TickerNumber,src_currency)
    array.set(A_timeopen,TickerNumber,src_timeopen)
    array.set(A_timeclose,TickerNumber,src_timeclose)
//

//run functions above
f_FillArray(&quot;NYSE:PFE&quot;,         1)
f_FillArray(&quot;SIX:ROG&quot;,          2)
f_FillArray(&quot;SIX:NOVN&quot;,         3)
f_FillArray(&quot;NYSE:JNJ&quot;,         4)
f_FillArray(&quot;NYSE:MRK&quot;,         5)

float TEMP = 0
float ValueIn = 0
float ValueOut = 0



for counterOut = 1 to 5
    for counterIn = 1 to 5
        ValueIn:=array.get(A_close,counterIn)
        ValueOut:=array.get(A_close,counterOut)
        TEMP:=ta.correlation(ValueIn,ValueOut,5)
        matrix.set(mcc,counterIn,counterOut,TEMP)
//

// (1)
plot(matrix.get(mcc,2,3), color=color.orange)

// (2)
plot( ta.correlation(array.get(A_close,2),array.get(A_close,3),5)  , color=color.black)

I already asked the Tradingview-Pine5-Support-guys for help. The answer here was, I should run ta.correlation() in global context, unfortunately they are not allowed to provide code examples.
Trying to initiate all variables getting in touch with ta.correlation() via >var< or >varip< shows the same (wrong) results + compiler warning.

Maybe someone can help with correct usage off ta.correlation() in for-loops.

Best Regards
Hendrix

答案1

得分: 0

You can't use ta.correlation() in local block. It is due to the way pinescript are compiled before execution.
In your case, you can expand your two loops, it is a workaround, not very 'beautiful', but it works:

matrix.set(mcc,1,1,ta.correlation(array.get(A_close,2),array.get(A_close,1),5))
matrix.set(mcc,2,1,ta.correlation(array.get(A_close,2),array.get(A_close,1),5))
matrix.set(mcc,3,1,ta.correlation(array.get(A_close,3),array.get(A_close,1),5))
matrix.set(mcc,4,1,ta.correlation(array.get(A_close,4),array.get(A_close,1),5))
matrix.set(mcc,5,1,ta.correlation(array.get(A_close,5),array.get(A_close,1),5))
matrix.set(mcc,1,2,ta.correlation(array.get(A_close,1),array.get(A_close,2),5))
matrix.set(mcc,2,2,ta.correlation(array.get(A_close,2),array.get(A_close,2),5))
matrix.set(mcc,3,2,ta.correlation(array.get(A_close,3),array.get(A_close,2),5))
matrix.set(mcc,4,2,ta.correlation(array.get(A_close,4),array.get(A_close,2),5))
matrix.set(mcc,5,2,ta.correlation(array.get(A_close,5),array.get(A_close,2),5))
matrix.set(mcc,1,3,ta.correlation(array.get(A_close,1),array.get(A_close,3),5))
matrix.set(mcc,2,3,ta.correlation(array.get(A_close,2),array.get(A_close,3),5))
matrix.set(mcc,3,3,ta.correlation(array.get(A_close,3),array.get(A_close,3),5))
matrix.set(mcc,4,3,ta.correlation(array.get(A_close,4),array.get(A_close,3),5))
matrix.set(mcc,5,3,ta.correlation(array.get(A_close,5),array.get(A_close,3),5))
matrix.set(mcc,1,4,ta.correlation(array.get(A_close,1),array.get(A_close,4),5))
matrix.set(mcc,2,4,ta.correlation(array.get(A_close,2),array.get(A_close,4),5))
matrix.set(mcc,3,4,ta.correlation(array.get(A_close,4),array.get(A_close,4),5))
matrix.set(mcc,4,4,ta.correlation(array.get(A_close,4),array.get(A_close,4),5))
matrix.set(mcc,5,4,ta.correlation(array.get(A_close,5),array.get(A_close,4),5))
matrix.set(mcc,1,5,ta.correlation(array.get(A_close,1),array.get(A_close,5),5))
matrix.set(mcc,2,5,ta.correlation(array.get(A_close,2),array.get(A_close,5),5))
matrix.set(mcc,3,5,ta.correlation(array.get(A_close,3),array.get(A_close,5),5))
matrix.set(mcc,4,5,ta.correlation(array.get(A_close,4),array.get(A_close,5),5))
matrix.set(mcc,5,5,ta.correlation(array.get(A_close,5),array.get(A_close,5),5))
英文:

You can't use ta.correlation() in local block. It is due to the way pinescript are compiled before execution.
In your case, you can expand your two loops, it is a workaround, not very 'beautiful', but it works :

matrix.set(mcc,1,1,ta.correlation(array.get(A_close,2),array.get(A_close,1),5))
matrix.set(mcc,2,1,ta.correlation(array.get(A_close,2),array.get(A_close,1),5))
matrix.set(mcc,3,1,ta.correlation(array.get(A_close,3),array.get(A_close,1),5))
matrix.set(mcc,4,1,ta.correlation(array.get(A_close,4),array.get(A_close,1),5))
matrix.set(mcc,5,1,ta.correlation(array.get(A_close,5),array.get(A_close,1),5))
matrix.set(mcc,1,2,ta.correlation(array.get(A_close,1),array.get(A_close,2),5))
matrix.set(mcc,2,2,ta.correlation(array.get(A_close,2),array.get(A_close,2),5))
matrix.set(mcc,3,2,ta.correlation(array.get(A_close,3),array.get(A_close,2),5))
matrix.set(mcc,4,2,ta.correlation(array.get(A_close,4),array.get(A_close,2),5))
matrix.set(mcc,5,2,ta.correlation(array.get(A_close,5),array.get(A_close,2),5))
matrix.set(mcc,1,3,ta.correlation(array.get(A_close,1),array.get(A_close,3),5))
matrix.set(mcc,2,3,ta.correlation(array.get(A_close,2),array.get(A_close,3),5))
matrix.set(mcc,3,3,ta.correlation(array.get(A_close,3),array.get(A_close,3),5))
matrix.set(mcc,4,3,ta.correlation(array.get(A_close,4),array.get(A_close,3),5))
matrix.set(mcc,5,3,ta.correlation(array.get(A_close,5),array.get(A_close,3),5))
matrix.set(mcc,1,4,ta.correlation(array.get(A_close,1),array.get(A_close,4),5))
matrix.set(mcc,2,4,ta.correlation(array.get(A_close,2),array.get(A_close,4),5))
matrix.set(mcc,3,4,ta.correlation(array.get(A_close,4),array.get(A_close,4),5))
matrix.set(mcc,4,4,ta.correlation(array.get(A_close,4),array.get(A_close,4),5))
matrix.set(mcc,5,4,ta.correlation(array.get(A_close,5),array.get(A_close,4),5))
matrix.set(mcc,1,5,ta.correlation(array.get(A_close,1),array.get(A_close,5),5))
matrix.set(mcc,2,5,ta.correlation(array.get(A_close,2),array.get(A_close,5),5))
matrix.set(mcc,3,5,ta.correlation(array.get(A_close,3),array.get(A_close,5),5))
matrix.set(mcc,4,5,ta.correlation(array.get(A_close,4),array.get(A_close,5),5))
matrix.set(mcc,5,5,ta.correlation(array.get(A_close,5),array.get(A_close,5),5))

huangapple
  • 本文由 发表于 2023年4月4日 04:57:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定