2

Stata の指値注文書から高頻度データを取得しました。時間には一定の間隔がなく、一部の観測は同時に発生します (ミリ秒単位)。観測ごとに、5 分後に別の列で中間点を取得する必要があります。09:05:02.579 に最も近い最後の中間点は 10.49 であるため、観察 1 の中間点は 10.49 になります。

Stataでこれを行う方法は?

datetime                         midpoint
12/02/2012 09:00:02.579          10.5125
12/02/2012 09:00:03.471          10.5125
12/02/2012 09:00:03.471          10.5125
12/02/2012 09:00:03.471          10.51
12/02/2012 09:00:03.471          10.51
12/02/2012 09:00:03.549          10.505
12/02/2012 09:00:03.549          10.5075
   ......
12/02/2012 09:04:59.785          10.495
12/02/2012 09:05:00.829          10.4925
12/02/2012 09:05:01.209          10.49
12/02/2012 09:05:03.057          10.4875
12/02/2012 09:05:05.055          10.485
 .....
4

2 に答える 2

1
// Construct a variable to look for in the dataset
gen double midpoint_5 = (datetime + 5*60000)    
format midpoint_5 %tcNN/DD/CCYY_HH:MM:SS.sss

// will contain the closest observation number and midpoint 5 minutes a head
gen _t = .
gen double midpoint_at5 = . 

// How many observations in the sample?
local N = _N

// We will use these variables to skip some observations in the loop
egen obs_in_minute = count(minutes_filter), by(minutes_filter)
egen max_obs_in_minute = max(obs_in_minute)
set more off 

// For each observation
forvalues i = 1/`N' {

    // If it is a trade
    if type[`i'] == "Trade" {

        // Set the time to lookup in the data
        local lookup = midpoint_5[`i']

        // The time should be between the min and max(*5)
        local min = `i' + obs_in_minute[`i'] // this might cause errors
        local max = `i' + max_obs_in_minute[`i']*5

        // For each of these observations
        forvalues j = `min'/`max' {

            // Check if the lookup date is smaller than the datetime of the observation
            if `lookup' < datetime[`j'] {

                // Set the observation ID at the lookup ID 1 observation before
                quietly replace _t = `j'-1 in `i'
                // Set the midpoint at the lookup ID 1 observation before
                quietly replace midpoint_at5 = midpoint[`j'-1] in `i'

                // We have found the closest 5th min ahead... now stop loop and continue to next observation.
                continue, break
            }
        }

        // This is to indicate where we are in the loop
        display "`i'/`N'"
    }
}
于 2013-01-17T07:20:02.940 に答える
1

私のアプローチは

  1. 5 分ずらした新しいデータ セットを生成する
  2. appendこのシフター データ セット
  3. 5 分間のデルタに最も近い前後の観測を見つける
  4. いくつかの基準を使用して、これら 2 つの値の良い方を選択します

最寄りを指定しましたが、本によっては他の条件を追加することもできます。また、特定のミリ秒刻みで複数の値について言及しましたが、それ以上の情報がないと、それを処理する方法がわかりません。最初にそれらの中間点を結合しますか? それとも違う株ですか?

上記のアプローチの基本を実装するコードを次に示します。

clear
version 11.2
set seed 2001

* generate some data
set obs 100000
generate double dt = ///
    tc(02dec2012 09:00:00.000) + 1000*_n + int(100*rnormal())
format dt %tcDDmonCCYY_HH:MM:SS.sss
sort dt
generate midpt = 100
replace midpt = ///
    round(midpt[_n - 1] + 0.1*rnormal(), 0.005) if (_n != 1)

* add back future midpts
preserve
tempfile future
rename midpt fmidpt
rename dt fdt
generate double dt = fdt - tc(00:05:00.000)
save `future'
restore
append using `future'

* generate midpoints before and after 5 minutes in the future
sort dt
foreach v of varlist fdt fmidpt {
    clonevar `v'_b = `v'
    replace `v'_b = `v'_b[_n - 1] if missing(`v'_b)
}

gsort -dt
foreach v of varlist fdt fmidpt {
    clonevar `v'_a = `v'
    replace `v'_a = `v'_a[_n - 1] if missing(`v'_a)
}

format fdt* %tcDDmonCCYY_HH:MM:SS.sss

* use some algorithm to pick correct value
sort dt    
generate choose_b = ///
    ((dt + tc(00:05:00.000)) - fdt_b) < (fdt_a - (dt + tc(00:05:00.000))) 
generate fdt_c = cond(choose_b, fdt_b, fdt_a)
generate fmidpt_c = cond(choose_b, fmidpt_b, fmidpt_a)
format fdt_c %tcDDmonCCYY_HH:MM:SS.sss
于 2013-01-16T15:14:02.987 に答える