根据符号排名在Quantstrat中调整排名?

时间:2018-10-15 13:39:09

标签: r quantstrat

我正在尝试创建一种基于相对排名进行交易的算法。也就是说,如果Google排名第一,请在本周初购买100,000美元,然后在年底出售。如果Google排名第二,请在开始时购买50,000美元,在结束时出售。

我已经找到了每周进行系统交易的方法,但是我无法确定如何设置动态头寸规模。我已经将$头寸值附加到我的XTS文件中,因此系统需要使用正确的日期/周内联查找这些位置值。

有关如何执行此操作的任何建议?谢谢!

我当前的功能如下。给定特定时间戳,“ symbol [timestamp,26]”试图引用列26处的符号XTS对象。但是,我收到一条错误消息,指出“尺寸错误”。

"osMaxDollar" <- function(data, timestamp, orderqty, ordertype, orderside,
                      portfolio, symbol, prefer="Open", tradeSize,
                      maxSize, integerQty=TRUE,
                      ...) {
  pos <- getPosQty(portfolio, symbol, timestamp)
  if(prefer=="Close") {
    price <- as.numeric(Cl(mktdata[timestamp,]))
  } else {
    price <- as.numeric(Op(mktdata[timestamp,]))
  }
  posVal <- pos*price
  if (orderside=="short") {
    dollarsToTransact = symbol[timestamp,26]
    #If our position is profitable, we don't want to cover needlessly.
    # if(dollarsToTransact > 0) {dollarsToTransact=0}
  } else {
    dollarsToTransact <- -symbol[timestamp,26]
    #If our position is profitable, we don't want to sell needlessly.
    # if(dollarsToTransact < 0) {dollarsToTransact=0}
  }
  qty <- dollarsToTransact/price
  if(integerQty) {
    qty <- trunc(qty)
  }
  return(qty)
}

编辑:这是我对所有好奇的人的最终解决方案

"osMaxDollar" <- function(data, timestamp, orderqty, ordertype, orderside,
                          portfolio, symbol, prefer="Open", tradeSize,
                          maxSize, integerQty=TRUE,
                          ...) {
  pos <- getPosQty(portfolio, symbol, timestamp)
  if(prefer=="Close") {
    price <- as.numeric(Cl(mktdata[timestamp,]))
  } else {
    price <- as.numeric(Op(mktdata[timestamp,]))
  }
  posVal <- pos*price
  #Negative mktdata due to buy bot sell top
  dollarsToTransact = mktdata[timestamp,26]
  qty <- dollarsToTransact/price
  if(integerQty) {
    qty <- trunc(qty)
  }
  return(qty)

0 个答案:

没有答案