使用函数计算传播

时间:2018-06-15 12:15:16

标签: r function stocks

我正试图计算两只股票的价差。我的数据由3列组成:#1表示日期,#2表示第一批股票的收盘价,#3表示第二批股票的收盘价。之后,我创建了额外的列,稍后我将需要它。

我一直在运行以下功能,输出如下。

PrepareData <- function(csvData){
#Calculate the Pair Ratio
csvData$pairRatio  <-  csvData[,2] / csvData[,3]  
#Calculate the log prices of the two time series
csvData$LogA <- log10(csvData[,2])
csvData$LogB <- log10(csvData[,3])

#Add columns to the DF
csvData <- AddColumns(csvData)
#Make sure that the date column is not read in as a vector of characters
csvData$Date <- as.Date(csvData$Date)

return(csvData)
}

这是我得到的输出,这很好(除了日期,任何想法如何更改?):

PrepareData(data)
      Date  AALB AEGO pairRatio     LogA      LogB spread adfTest mean stdev zScore signal BuyPrice SellPrice LongReturn ShortReturn Slippage TotalReturn
0001-06-20 28.43 6.84  4.156433 1.453777 0.8350561      0       0    0     0      0      0        0         0          0           0        0           0
0002-06-20 28.21 6.85  4.118248 1.450403 0.8356906      0       0    0     0      0      0        0         0          0           0        0           0

接下来,我想计算点差。因此,我在csvData$LogB行之后加上以下内容:csvData$spread <- csvData$LogA - csvData$LogB,我的差价一直保持为0.任何想法?

0 个答案:

没有答案
相关问题