英文:
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 = "FAQ-HX1", shorttitle ="FAQ-HX1")
// 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, "-")
A_currency = array.new_string(50, "-")
A_timeopen = array.new_int(50, 1)
A_timeclose = array.new_int(50, 1)
mcc = matrix.new<float>(25,25,na) //larger than needed
// function fill arrays with pulled tuples
f_FillArray(Name,TickerNumber)=>
[src_hlcc4, src_close, src_RlTme, src_ticker, src_currency,src_timeopen, src_timeclose] = request.security( Name, "D", [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("NYSE:PFE", 1)
f_FillArray("SIX:ROG", 2)
f_FillArray("SIX:NOVN", 3)
f_FillArray("NYSE:JNJ", 4)
f_FillArray("NYSE:MRK", 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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论