使用数组计算相关比

发布于 2025-01-11 05:38:10 字数 1551 浏览 0 评论 0原文

我正在尝试使用数组计算两个资产之间的相关性比率。但是,我编写的代码产生的相关比仅采用 -11 的值。我想知道是否有人可以指出我在编写的代码中犯的错误?

//Obtain user inputs

lookback=input.int(20,"Lookback Period",minval=0,maxval=30,step=1)
source=input.source(close,"Source")
referencemarket=input.symbol("FTX:BTCPERP","Reference Market")     
asset=input.symbol("FTX:ETHPERP","Asset")

//Calculate percentage change data for the reference market

referenceprice=request.security(referencemarket,timeframe.period,source)
referencechange= ((referenceprice[1]-referenceprice[2])/referenceprice[2])*100

//Calculate percentage change data for the asset

assetprice=request.security(asset,timeframe.period,source)
assetchange=((assetprice[1]-assetprice[2])/assetprice[2])*100

//Declare arrays

float[] referencearray=array.new_float(20,0)
float[] assetarray=array.new_float(20,0)

//Remove the last value from the array and adopt the FIFO sorting method

array.shift(referencearray)
array.shift(assetarray)

//Add current values to the respective arrays

array.push(referencearray,referencechange)
array.push(assetarray,assetchange)

//Calculate the covariance 

covariance=array.covariance(referencearray,assetarray)
referencestdev=array.stdev(referencearray)
assetstdev=array.stdev(assetarray)

//Calculate the correlation coefficient

correlation=covariance/(referencestdev*assetstdev)


//Calculate ratio of price of asset to price of reference security

ratio=referenceprice/assetprice

//Plot metrics

//plot(referenceprice)
//plot(assetprice)

plot(correlation)

I'm trying to calculate the correlation ratio between two assets using arrays. However, the code that I've written produces a correlation ratio which only takes on the values of -1 and 1. I was wondering if someone could point out the error I've made in the code I've written ?

//Obtain user inputs

lookback=input.int(20,"Lookback Period",minval=0,maxval=30,step=1)
source=input.source(close,"Source")
referencemarket=input.symbol("FTX:BTCPERP","Reference Market")     
asset=input.symbol("FTX:ETHPERP","Asset")

//Calculate percentage change data for the reference market

referenceprice=request.security(referencemarket,timeframe.period,source)
referencechange= ((referenceprice[1]-referenceprice[2])/referenceprice[2])*100

//Calculate percentage change data for the asset

assetprice=request.security(asset,timeframe.period,source)
assetchange=((assetprice[1]-assetprice[2])/assetprice[2])*100

//Declare arrays

float[] referencearray=array.new_float(20,0)
float[] assetarray=array.new_float(20,0)

//Remove the last value from the array and adopt the FIFO sorting method

array.shift(referencearray)
array.shift(assetarray)

//Add current values to the respective arrays

array.push(referencearray,referencechange)
array.push(assetarray,assetchange)

//Calculate the covariance 

covariance=array.covariance(referencearray,assetarray)
referencestdev=array.stdev(referencearray)
assetstdev=array.stdev(assetarray)

//Calculate the correlation coefficient

correlation=covariance/(referencestdev*assetstdev)


//Calculate ratio of price of asset to price of reference security

ratio=referenceprice/assetprice

//Plot metrics

//plot(referenceprice)
//plot(assetprice)

plot(correlation)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤慢歌 2025-01-18 05:38:11

您需要使用 var 关键字声明数组,否则您的数组将在每个新柱上重新初始化为零,而不是包含正确的值。

var float[] referencearray=array.new_float(20,0)
var float[] assetarray=array.new_float(20,0)

像这样管理数组可能会更好。然后,在初始历史柱期间,将使用可用数据执行计算,直到达到回溯柱数,并且从那时起将在数组中维护该数量的值。

var float[] referencearray=array.new_float()
var float[] assetarray=array.new_float()

array.push(referencearray,referencechange)
array.push(assetarray,assetchange)

if array.size(referencearray) > lookback
    array.shift(referencearray)
    array.shift(assetarray)

You need to declare your arrays with the var keyword, otherwise your arrays will be reinitialized to zeros on each new bar rather than contain the correct values.

var float[] referencearray=array.new_float(20,0)
var float[] assetarray=array.new_float(20,0)

It is probably better to manage the arrays like this. Then, during the initial historical bars the calculation will be performed with available data until lookback number of bars is reached, and from that point on will maintain that number of values in the arrays.

var float[] referencearray=array.new_float()
var float[] assetarray=array.new_float()

array.push(referencearray,referencechange)
array.push(assetarray,assetchange)

if array.size(referencearray) > lookback
    array.shift(referencearray)
    array.shift(assetarray)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文