mercredi 30 décembre 2020

Powering the positive returns of a column in R depending on it's value between 0 and 100

I am trying to create a risk/return plot. The return is the mean daily values. It isn't in percentage. I want it kind-of in percentage. When a stock goes up 100%, it is equivalent to a stock falling 50%. If the stock goes up 400%, it is the same as a stock falling 87.5%.

I'm trying to multiply my positive Returns by the following, depending on the value each return has:

  • Returns between 0 and 49.99 multiply the return value by 2
  • Returns between 50 and 74.99 multiply the return value by 4
  • Returns between 75 and 87.49 multiply the return value by 8
  • Returns between 87.5 and 93.74 multiply the return value by 16
  • Returns between 93.75 and 93.74 multiply the return value by 32 ...

Below is the code of the if statement I can't make work:

if (( 0 <= Poi[,2]) & (Poi[,2] <= 49.99999999)){
  Poi[,2] <- Poi[,2]*MyReturn*2
} else if (( 50 <= Poi[,2]) & (Poi[,2] <= 74.99999999)){
  Poi[,2] <- Poi[,2]*MyReturn*4
} else if (( 75 <= Poi[,2]) & (Poi[,2] <= 87.49999999)){
  Poi[,2] <- Poi[,2]*MyReturn*8
} else if (( 87.5 <= Poi[,2]) & (Poi[,2] <= 93.74999999)){
  Poi[,2] <- Poi[,2]*MyReturn*16
} else if (( 93.75 <= Poi[,2]) & (Poi[,2] <= 96.87499999)){
  Poi[,2] <- Poi[,2]*MyReturn*32
} else if (( 96.875 <= Poi[,2]) & (Poi[,2] <= 98.43749999)){
  Poi[,2] <- Poi[,2]*MyReturn*64
} else if (( 98.4375 <= Poi[,2]) & (Poi[,2] <= 99.21874999)){
  Poi[,2] <- Poi[,2]*MyReturn*128
} else if (( 99.21875 <= Poi[,2]) & (Poi[,2] <= 99.60937499)){
  Poi[,2] <- Poi[,2]*MyReturn*256
} else if (( 99.609375 <= Poi[,2]) & (Poi[,2] <= 99.80468749)){
  Poi[,2] <- Poi[,2]*MyReturn*512
} else if (( 99.8046875 <= Poi[,2]) & (Poi[,2] <= 99.90234374)){
  Poi[,2] <- Poi[,2]*MyReturn*1024
} else if (( 99.90234375 <= Poi[,2]) & (Poi[,2] <= 99.951171865)){
  Poi[,2] <- Poi[,2]*MyReturn*2048
} else if ( 99.951171875 <= Poi[,2]){
  Poi[,2] <- Poi[,2]*MyReturn*4096
} else {
  Poi[,2] <- Poi[,2]*MyReturn
}

dput(Poi):

structure(list(targetRisk = c(93.969862047238, 76.9768091680639, 
63.5982003855003, 54.5228767287575, 46.0821549451386, 37.6476764384589, 
29.2248472581225, 20.8278052738397, 12.5085917276091, 4.66122739912144, 
1.70147647969736, 4.34078819570542, 7.31911601372062, 10.4618350712519, 
13.948425299842, 17.515722070763, 21.228074686994, 25.1663931325116, 
29.261108015602, 33.5927110975208, 38.2162769926765, 43.116215831914, 
48.2106070287435, 53.5716998572496), targetReturn = c(-42.7922615561753, 
-38.2096263740965, -33.6269911920178, -29.0443560099391, -24.4617208278605, 
-19.8790856457821, -15.2964504637031, -10.7138152816245, -6.13118009954602, 
-1.54854491746736, 3.03409026461127, 7.6167254466899, 12.1993606287685, 
16.7819958108472, 21.3646309929257, 25.9472661750043, 30.5299013570829, 
35.1125365391614, 39.6951717212402, 44.2778069033188, 48.8604420853973, 
53.443077267476, 58.0257124495546, 62.6083466986961)), row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24"
), class = "data.frame")

Aucun commentaire:

Enregistrer un commentaire