シグナル集計
前回「シグナルの生成と検証」の続き。
> prev_x_qty<-0 > position<-0 > trade_size<-100 > signal<-as.numeric(data$signal) > signal[is.na(signal)]<-0 > beta<-as.numeric(data$betas) > qty_x<-rep(0, length(signal)) > qty_y<-rep(0, length(signal))
条件に応じてシグナル数カウント。
> for(i in 1:length(signal)){ + if(signal[i] == 1 && position == 0){ + prev_x_qty<-round(beta[i]*trade_size) + qty_x[i] <- prev_x_qty + qty_y[i] <- trade_size + position <- 1 + } + + if(signal[i] == -1 && position == 0){ + prev_x_qty<-round(beta[i] * trade_size) + qty_x[i] <- prev_x_qty + qty_y[i] <- trade_size + position <- -1 + } + + if(signal[i] == 1 && position == -1){ + qty_x[i] <- -(round(beta[i] * trade_size)+prev_x_qty) + prev_x_qty<-round(beta[i] * trade_size) + qty_y[i] <- 2*trade_size + position <- 1 + } + + if(signal[i] == -1 && position == 1){ + qty_x[i] <- round(beta[i] * trade_size)+prev_x_qty + prev_x_qty <- round(beta[i] * trade_size) + qty_y[i] <- -2*trade_size + position <- -1 + } + } > qty_x[length(qty_x)] <- -sum(qty_x) > qty_y[length(qty_y)] <- -sum(qty_y) > head(qty_x) [1] 0 0 0 0 0 0 > head(qty_y) [1] 0 0 0 0 0 0 > qty_x [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [19] 0 0 0 0 0 0 0 54 0 0 0 0 0 0 0 0 0 0 [37] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [55] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [73] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [91] 0 0 0 0 108 0 0 0 0 0 0 0 0 0 0 0 -108 0 [109] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [127] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [145] 0 0 0 0 0 0 0 0 0 0 108 0 0 0 0 0 0 0 [163] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [181] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [199] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [217] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [235] 0 0 0 0 0 0 0 0 0 0 -162 > qty_y [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [19] 0 0 0 0 0 0 0 100 0 0 0 0 0 0 0 0 0 0 [37] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [55] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [73] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [91] 0 0 0 0 -200 0 0 0 0 0 0 0 0 0 0 0 200 0 [109] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [127] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [145] 0 0 0 0 0 0 0 0 0 0 -200 0 0 0 0 0 0 0 [163] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [181] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [199] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [217] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [235] 0 0 0 0 0 0 0 0 0 0 100
> data$qty_x <- qty_x > data$qty_y <- qty_y > data[1:3,] betas TOPIX UFJ spread spreadR signalCount qty_x qty_y 2013-01-04 NA 888.51 484 NA NA NA 0 0 2013-01-07 NA 881.06 476 NA NA NA 0 0 2013-01-08 NA 871.88 462 NA NA NA 0 0 > tail(data, 3) betas TOPIX UFJ spread spreadR signalCount qty_x qty_y 2013-12-26 0.5370756 1279.34 669 -18.102343 0.013538413 0 0 0 2013-12-27 0.5370756 1290.07 677 -15.865165 0.007349778 0 0 0 2013-12-30 0.5370756 1302.29 694 -5.428229 0.019456044 0 -162 100 >
> compute_equity_curve<-function(qty, price){ + cach_buy<-ifelse(sign(qty) == 1, qty*price, 0) + cach_sell<-ifelse(sign(qty) == -1, -qty*price, 0) + position <- cumsum(qty) + cumulative_buy<-cumsum(cach_buy) + cumulative_sell<-cumsum(cach_sell) + equity<-cumulative_sell - cumulative_buy + position*price + return(equity) + } >
> head(data) betas TOPIX UFJ spread spreadR signalCount qty_x qty_y 2013-01-04 NA 888.51 484 NA NA NA 0 0 2013-01-07 NA 881.06 476 NA NA NA 0 0 2013-01-08 NA 871.88 462 NA NA NA 0 0 2013-01-09 NA 879.05 469 NA NA NA 0 0 2013-01-10 NA 889.02 474 NA NA NA 0 0 2013-01-11 NA 898.69 479 NA NA NA 0 0 > data$equity_curve_x<-compute_equity_curve(data$qty_x, data$TOPIX) > data$equity_curve_y<-compute_equity_curve(data$qty_y, data$UFJ) > plot(data$equity_curve_x + data$equity_curve_y, type='l', main="UFJ/TOPIX spread", ylab="P&L") >