Global allocation - Issue to construct a payout function

Hello

I have an issue on a payout code. eval function does not succeed for myPayout_GMV because of unused argument (returns). I have put below all the R code.

Thks.

PS: other issue on testGlobalAllocationSubmission which does not succeed even for myPayout_EW function.


#packages

.sourceQlib()

library(quantmod)

library(quadprog)


allocation = getTickersGlobalAllocationChallenge()

portAllocation = portfolio(as.list(allocation[allocation$field==“bb live”, “ticker”]))


myPayout_EW = function(features)

{

 # Getting data from dataFeatures

 df = getTickersGlobalAllocationChallenge()

 bbLive = subset(features, tickers=df[df$field==“bb live”, “ticker”], fields=“bb_live”)

 bbLive = bbLive@pxs


 # Equally-weighted

 signal = xts(x = matrix(0, nrow(bbLive), ncol(bbLive)), order.by = index(bbLive))

 for (i in 1:nrow(bbLive))

 {

   for(j in 1:ncol(bbLive)) signal[i, j] = 1/ncol(bbLive)

 }

 colnames(signal) = df[df$field==“bb live”, “ticker”]

 return(signal)

}


myPayout_GMV = function(features)

{

 # Getting data from dataFeatures

 df = getTickersGlobalAllocationChallenge()

 bbLive = subset(features, tickers=df[df$field==“bb live”, “ticker”], fields=“bb_live”)

 bbLive = bbLive@pxs


 # Covariance matrix

 n <- ncol(bbLive)

 T <- nrow(bbLive)

 e <- rep(1, n)

 returns <- matrix(0, T, n)

 for (j in 1:n) returns[, j] <- Delt(bbLive[, j], type = ‘arithmetic’)

 Sigma <- cov(returns[2:T,]) * (T - 1) / (T - n - 2)


 # Empirical constrained GMV with no short selling and a maximum weight of 0.2 by asset

 # min(-d^T b + 1/2 b^T D b) with the constraints A^T b >= b_0

 dvec <- numeric(n)

 # Constraints of sum of the weights equal to 1 and weights between 0 and 0.2

 Amat <- cbind(e, diag(1, n, n), -diag(1, n, n))

 bvec <- cbind(1, t(numeric(n)), t(rep(-0.2, n)))

 omega <- solve.QP(Sigma, dvec, Amat, bvec, meq = 1)$solution

 signal = xts(x = matrix(0, nrow(bbLive), ncol(bbLive)), order.by = index(bbLive))

 for(i in 1:T) signal[i, ] = omega

 colnames(signal) = df[df$field==“bb live”, “ticker”]

 return(signal)

}


# portAllocation = payout(portAllocation, myPayout_EW)

portAllocation = payout(portAllocation, myPayout_GMV)

portAllocation = eval(portAllocation, zoom=“2003::2015”)

backtest(portAllocation, tc=0.01, rebalancePenalty=0.02, rebalancePenaltyPerAsset=FALSE)

Hello Pierre,


Thanks for your question. myPayout_GMV is working well on my side though. Can you try to run restartSession() on your console if you’re using alphienstudio or restart your kernel if youre using notebooks and try again?


Regarding the part about failing the testGlobalAllocationSubmission - I just ran it and it seems like its failing the part where we simulate missing data and test the payout. The payout should not be assigning weights to assets which are missing. The below code should help you recreate the issue.


# this assigns leading NAs to some assets to simulate missing data
portTest = portfolio.createMissingData(portAllocation)
head(portTest@features@pxs)
portTest = eval(portTest)
head(portTest@weightsMatrix)
head(portTest@returnsMatrix)


Also, please note that the portfolio object has to be recreated (code snippet below) before testing another payout.


# recreate object using this
portAllocation = portfolio(as.list(allocation[allocation$field==“bb live”, “ticker”]))

payout only added after above line is ran

portAllocation = payout(portAllocation, myPayout_EW)


Hope this answers your questions. Please feel free to clarify if anything is unclear.

Thks Yongcheng! The first debug helps me (.rs.restartR() or restartSession()).

For the NA treatment, I have to improve the payout function.

Best,

Pierre.

I need again your help - I do not see an answer in the submission topics of your library.

To tackle the NA issue, I tried to be inspired by the simple function equalWeight:

function (rets)
{
rets[is.na(rets)] = 0
rets = rets * 0 + (1/ncol(rets))
return(rets)
}
<environment: namespace:SinglePackageAl>

with an update on myPayout_EW with the following code for instance where the returns are not missing:

 returns <- matrix(0, nrow(bbLive), ncol(bbLive))

 for (j in 1:ncol(bbLive))

 {

  returns[, j] <- Delt(bbLive[, j], type = ‘arithmetic’)

  returns[is.na(returns[, j])] = 0

 }

But the slot portAllocation@returnsMatrix contains NA returns even after this treatment. Do you have a tutorial to help me to understand how to treat the NA stress you do on the portfolio?

Best regards,

Pierre.

Hi Pierre,


If I can suggest an edit to the payout so that it do not assign weights to assets which have NA:


myPayout_EW = function(features){
 
  # Getting data from dataFeatures
  df = getTickersGlobalAllocationChallenge()
  bbLive = subset(features, tickers=df[df$field==“bb live”, “ticker”], fields=“bb_live”)
  bbLive = bbLive@pxs
 
  # Find out which assets are tradeable
  tradeable = !is.na(bbLive)
  # Converting to numeric
  tradeable = tradeable*1
 
  # Construct xts
  signal = xts(tradeable, order.by = index(bbLive))
  # Convert to equally weighted
  signal = signal/rowSums(signal)
  colnames(signal) = df[df$field==“bb live”, “ticker”]
 
  return(signal)
}


Hope this helps!

Thanks a lot Reiyun - it helps me.

Best,

Pierre.