2017-10-12 76 views
1

我有一些財務數據剔,head(df_xts)如何使用有條件的價格點對R中的不規則日內財務數據進行分組?

     price volume 
2016-06-01 09:30:00 1073  1 
2016-06-01 09:30:00 1073  1 
2016-06-01 09:30:00 1073  1 
2016-06-01 09:30:00 1073  1 
2016-06-01 09:30:00 1073  1 
2016-06-01 09:30:00 1073  5 

我想看看這個數據價格已經移到高於其開度範圍內有一定的距離之後。我限定開口範圍作爲第15分鐘:

df_open <- df_xts["T09:30/T09:44"] 

df_main,這將在下文中引用,是一天中的剩餘時間幀。)

以下發現最大價格,或「開口範圍高」的每一天:

orh <- apply.daily(df_open$price, max) 
> orh 
         price 
2016-06-01 09:44:55 1083.75 
2016-06-02 09:44:59 1119.25 
2016-06-03 09:44:59 1169.00 
2016-06-06 09:44:53 1155.00 

和這個發現價格在一個預先定義的距離breakout上方的開口範圍內的高:

orh_bo <- orh + breakout 

然後我發現每一天這裏的價格已經上升到這個突破點,先觀察,這給了我的「突破」的索引值,相對於天:

orh_bo_index <- apply.daily(df_main, FUN = function(X) first(which(X %in% orh_bo))) 
> orh_bo_index 
        [,1] 
2016-06-01 14:14:59 2074 
2016-06-02 14:14:59 10693 
2016-06-03 14:14:59 2351 
2016-06-06 14:14:59 1224 

orh_bo_matrix <- coredata(orh_bo_index) 

我結合這突破指數每日端點的索引創建一個數據幀,df_bo_indexes

ep_daily <- endpoints(df_main, on = "days") 
daily_last_index <- ep_daily[-1] 
daily_last_matrix <- t(t(daily_last_index)) 
df_bo_indexes <- bind_cols(data.frame(orh_bo_matrix), data.frame(daily_last_matrix)) 

> df_bo_indexes 
    orh_bo_matrix daily_last_matrix 
1   2074    52155 
2   10693   126623 
3   2351   181408 
4   1224   221002 

如何使用子集上述指標的數據?這是我到目前爲止已經試過:

df_bo_day1 <- df_main[df_bo_indexes[1,1]:df_bo_indexes[1,2]] 
i <- 2 
for(i in 2:ndays(df_main)) { 
df_bo_all <- df_main[(df_bo_indexes[i-1,2]+df_bo_indexes[i,1]):df_bo_indexes[i,2]] 
} 

split a time series by another irregular time series是有益的,但我不知道如何將其應用到盤中的數據。

我的完整代碼和數據集可以在https://github.com/blottb7/tick-data找到。我對從事時間系列投入工作的人感興趣。

回答

1

split(), lapply(,cummax), do.call(rbind,), merge()爲開放範圍。整個數據框都一樣。整個範圍合併開放範圍。然後使用breakout <- ifelse(cummax - breakout > opening_range_cummax, TRUE, FALSE), then subset by breakout == 1

相關問題