0

我正在尝试使用 R 修改定量交易一章中的一些代码,以使用回报而不是原始价格。除了我的代码的“利润和损失”部分之外,一切都会好起来的。它不断返回“qty_x[i] = (vec[i] + prev_x_qty) 中的错误:替换长度为零”查看我的变量时,我似乎找不到任何问题。我已经包含了复制代码。

# LOAD LIBRARIES
library(quantmod)
library(xts)

# FUNCTIONS

  # ROLLING BETA
  pcbeta = function(dF){
    r = prcomp( ~ dF$x[-1] + dF$y[-1])
    return(r$rotation[2, 1] / r$rotation[1,1])
  }

  rolling_beta = function(z, width){
    rollapply(z, width = width, FUN = pcbeta,
              by.column = FALSE, align = 'right')
  }

# GET TICKER DATA
SPY   = getSymbols('SPY', adjust=T, auto.assign=FALSE)
AAPL  = getSymbols('AAPL', adjust=T, auto.assign=FALSE)

# IN-SAMPLE DATE RANGE
in_start_date = '2011-01-01'
in_end_date   = '2011-12-31'
in_range      = paste(in_start_date, '::', in_end_date, sep='')

# RETRIEVE IN-SAMPLE DATA
x_in = SPY[in_range, 6]
y_in = AAPL[in_range, 6]

dF_in = cbind(x_in, y_in)
names(dF_in) = c('x','y')

# OUT-OF-SAMPLE DATE RANGE
out_start_date= '2012-01-01'
out_end_date  = '2012-12-31'
out_range     = paste(out_start_date, '::', out_end_date, sep='')

# RETRIEVE OUT-OF-SAMPLE DATA
x_out = SPY[out_range, 6]
y_out = AAPL[out_range, 6]

dF_out = cbind(x_out, y_out)
names(dF_out) = c('x', 'y')

# CALCULATE RETURNS (IN AND OUT OF SAMPLE)
returns_in = diff(dF_in) / dF_in 
returns_out = diff(dF_out) / dF_out 

# DEFINE ROLLING WINDOW LENGTH
window_length = 10

# FIND BETAS 
betas_in = rolling_beta(returns_in, window_length)
betas_out = rolling_beta(returns_out, window_length)

# FIND SPREADS
spreadR_in = returns_in$y - betas_in * returns_in$x
spreadR_out = returns_out$y - betas_out * returns_out$x

names(spreadR_in) =  c('spread')
names(spreadR_out) = c('spread')

# FIND THRESHOLD
threshold = sd(spreadR_in, na.rm=TRUE)

# FORM DATA SETS
data_in = merge(returns_in, betas_in, spreadR_in)
data_out = merge(x_out, y_out, returns_out, betas_out, spreadR_out)

names(data_out) = c('xp', 'yp', 'x', 'y', 'betas_out', 'spread')
data_in = data_in[-1]
data_out = data_out[-1]
# GENERATE BUY AND SELL SIGNALS FOR OUT OF SAMPLE
buys = ifelse(data_out$spread > threshold, 1, 0)
sells = ifelse(data_out$spread < -threshold, -1, 0)
data_out$signal = buys+sells

# PROFIT AND LOSS
prev_x_qty = 0
position = 0
trade_size = 100
signal = as.numeric(data_out$signal)
signal[is.na(signal)] = 0
beta = as.numeric(data_out$betas_out)
ratio = (data_out$yp/data_out$xp)
vec = round(beta*trade_size*ratio)

qty_x = rep(0, length(signal))
qty_y = rep(0, length(signal))

for(i in 1:length(signal)){

  if(signal[i] == 1 && position == 0){
    #buy the spread
    prev_x_qty = vec[i]
    qty_x[i] = -prev_x_qty
    qty_y[i] = trade_size
    position = 1
  }

  if(signal[i] == -1 && position == 0){
    #buy the spread
    prev_x_qty = vec[i]
    qty_x[i] = prev_x_qty
    qty_y[i] = -trade_size
    position = -1
  }

  if(signal[i] == 1 && position == -1){
    # we are short the spread and need to buy
    qty_x[i] = -(vec[i] + prev_x_qty)
    prev_x_qty = vec[i]
    qty_y[i] = 2 * trade_size
    position = 1
  }

  if(signal[i] == -1 && position == 1){
    # we are short the spread and need to buy
    qty_x[i] = (vec[i] + prev_x_qty)
    prev_x_qty = vec[i]
    qty_y[i] = -2 * trade_size
    position = -1
  }
}
4

0 回答 0