使用数组计算相关比
我正在尝试使用数组计算两个资产之间的相关性比率。但是,我编写的代码产生的相关比仅采用 -1
和 1
的值。我想知道是否有人可以指出我在编写的代码中犯的错误?
//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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用 var 关键字声明数组,否则您的数组将在每个新柱上重新初始化为零,而不是包含正确的值。
像这样管理数组可能会更好。然后,在初始历史柱期间,将使用可用数据执行计算,直到达到回溯柱数,并且从那时起将在数组中维护该数量的值。
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.
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.