1

私はペア取引データに取り組んでおり、次の関数は値 "k" の total.profit を与える必要があります。

optimal.k = function (k) {

u = m + k * s                           
l = m - k * s                                      
profit = 0                                               
profit = 0
total.profit = 0
i = 1
p = 0.001


while ( i <= length(r) ) {
    if ( r[i] >= u ) {
        buy.unit  = 1/East$Close[i]
        sell.unit = 1/South$Close[i]
            if ( i == length(r) ) {
                buy.price    = buy.unit  * East$Close[i]
                sell.price   = sell.unit * South$Close[i]
                profit       = sell.price - buy.price
                costs        = (sell.price + buy.price) * p
                total.profit = total.profit + profit - costs
                break
            }
            while ( r[i] > m ) {  ####################################   here
                i = i + 1
            }
        buy.price    = buy.unit  * East$Close[i]
        sell.price   = sell.unit * South$Close[i]
        profit       = sell.price - buy.price
        costs        = (sell.price + buy.price) * p
        total.profit = total.profit + profit - costs
    }
    if ( r[i] <= l ) {
        buy.unit  = 1/South$Close[i]
        sell.unit = 1/East$Close[i]
            if ( i == length(r) ) {
                buy.price    = buy.unit  * South$Close[i]
                sell.price   = sell.unit * East$Close[i]
                profit       = sell.price - buy.price
                costs        = (sell.price + buy.price) * p
                total.profit = total.profit + profit - costs
                break
            }
            while ( r[i] < m ) { 
                i = i + 1
            }
        buy.price    = buy.unit  * East$Close[i]
        sell.price   = sell.unit * South$Close[i]
        profit       = sell.price - buy.price
        costs        = (sell.price + buy.price) * p
        total.profit = total.profit + profit - costs
    }
    if ( i == length(r) ) stop
i = i + 1
}
print(total.profit)
}

関数を実行すると、このエラー メッセージが表示されます。

optimal.k(1) while (r[i] > m) { のエラー: TRUE/FALSE が必要な場所に値がありません

(r[i] > m) が NA である理由がわかりません。

4

0 に答える 0