R:两个时间段之间的聚合值

时间:2014-12-02 07:37:55

标签: r time between

我有一个包含交易的数据框,以及这些交易的开放时间。我正试图在交易时找到股权。但是,我只在每小时开始时拥有股权,因此我在每小时的开始和交易时间之间添加任何存款/取款。

我有一个使用for循环的方法,如下所示:

 for (i in 1:nrow(tradeData))
        {  
           tradeData$EquityUSD [i] = hourlyData$EquityUSD[which(as.character(hourlyData$Period) ==
             min(hourlyData$Period[as.character(hourlyData$Period) >= as.character(tradeData$OpenTime [i])]))-1] #Find equity as of start of the hour
          tradeData$EquityUSD [i] = tradeData$EquityUSD [i] + sum(tradeData$ProfitUSD[tradeData$CloseTime <= tradeData$OpenTime[i] &
              tradeData$CloseTime >= tradeHour$TradeHour[i]]) #Add any profit realised from closed trades before current trade, but in current hour only
          tradeData$EquityUSD [i] = tradeData$EquityUSD [i] + sum(currentNetDep$DepositWithdrawal[currentNetDep$Time <= tradeData$OpenTime [i] &currentNetDep$Time >= tradeHour$TradeHour[i]])      #Add any deposits/withdrawals from closed trades before current trade, but in current hour only
        }

hourlyData数据框只包含每小时开始时的权益。

这有效,将交易时的权益放入tradeData表。然而,它非常缓慢,特别是如果客户在这段时间内进行了大量交易。

以下是hourlyData数据框的示例:

Login   NetExposure EquityUSD        Period           
173929  108791.2    1000         2014-01-16 18:00:00.000
173929  108792.0    1200         2014-01-16 19:00:00.000
173929  108737.6    1500         2014-01-16 20:00:00.000
173929  108794.4    1300         2014-01-16 21:00:00.000
173929  108913.6    1400         2014-01-16 22:00:00.000
173929  108947.2    1600         2014-01-16 23:00:00.000    

存款数据样本:

 Login    DepositTime             DepositAmount
 173929   2014-01-16 19:30:00      100
 173929   2014-01-16 19:50:00      -200

tradeData数据框的样本:

Login        OpenTime            CloseTime              Equity 
173929      2014-01-16 18:30:00  2014-01-16 33:00:00     1000
173929      2014-01-16 18:35:00  2014-01-16 18:40:00     1000     
173929      2014-01-16 19:38:00  2014-01-16 22:32:00     1200 + 100 deposit    
173929      2014-01-16 19:40:00  2014-01-16 19:50:00     1200 + 100 deposit     
173929      2014-01-16 19:51:30  2014-01-16 19:59:40     1200 + 100 - 200 deposit

第一笔交易的股权在一小时开始时等于股本。 第二次交易也是如此 第三笔交易的股权相当于小时开始时的权益+在同一小时内但在交易前存款。 第四笔交易也是如此,该交易在小时开始时包含两笔存款,但在当前交易之前

请注意,我已经调整了答案,不再需要利润计算

有谁知道更有效的方式来做到这一点? 另外,如果上面有任何不清楚的地方,请告诉我。

谢谢!

麦克

1 个答案:

答案 0 :(得分:1)

我会使用avecumsum来计算每小时内的存款,然后在小时开始时将这些存款添加回权益。

我认为这可以做你想要的。

# hourly data
hourlyData <- read.table(header=TRUE, text="
Login   NetExposure EquityUSD        Period           
173929  108791.2    1000         '2014-01-16 18:00:00.000'
173929  108792.0    1200         '2014-01-16 19:00:00.000'
173929  108737.6    1500         '2014-01-16 20:00:00.000'
173929  108794.4    1300         '2014-01-16 21:00:00.000'
173929  108913.6    1400         '2014-01-16 22:00:00.000'
173929  108947.2    1600         '2014-01-16 23:00:00.000'    
                         ")
hourlyData$Period <- strptime(hourlyData$Period, format="%Y-%m-%d %H:%M:%OS")

# trade data
tradeData <- read.table(header=TRUE, text="
Login        OpenTime            CloseTime              
173929      '2014-01-16 18:30:00'  '2014-01-16 18:33:00'
173929      '2014-01-16 18:35:00'  '2014-01-16 18:40:00'
173929      '2014-01-16 19:38:00'  '2014-01-16 22:32:00'
173929      '2014-01-16 19:40:00'  '2014-01-16 19:50:00'
173929      '2014-01-16 19:51:30'  '2014-01-16 19:59:40'
                         ")
tradeData$OpenTime <- strptime(tradeData$OpenTime, format="%Y-%m-%d %H:%M:%OS")
tradeData$CloseTime <- strptime(tradeData$CloseTime, format="%Y-%m-%d %H:%M:%OS")

# deposit data
depositData <- read.table(header=TRUE, text="
Login DepositTime DepositAmount 
173929 '2014-01-16 19:30:00' 100 
173929 '2014-01-16 19:50:00' -200
                         ")
depositData$DepositTime <- strptime(depositData$DepositTime, format="%Y-%m-%d %H:%M:%OS")

# merge
merged <- merge(x=hourlyData, y=depositData, 
                by.x=c("Login", "Period"), by.y=c("Login", "DepositTime"),
                all=TRUE)

# running sum of deposits within each hour
merged$Running <- ave(merged$DepositAmount,
                      merged$Login, format(merged$Period, "%Y-%m-%d %H"),
                      FUN=function(x) {
                          xx <- ifelse(is.na(x), 0, x)
                          cumsum(xx)
                      })

# carry-forward top-of-the-hour equity
merged$EquityUSD1 <- ave(merged$EquityUSD,
                      merged$Login, format(merged$Period, "%Y-%m-%d %H"),
                      FUN=function(x) ifelse(is.na(x), x[1], x))

# add back running sum
merged$EquityUSD1 <- merged$EquityUSD1 + merged$Running