r - Using Iteration in Data Frames -


i dealing stocks (when buy/sell , how many stocks hold)

i begginer trying move small sample sizes in excel larger sample sizes in r.

i try explain problem reproducible example. if not understandable let me know , try clarify.

i have 2 stocks:

aapl.price <- c(140,145,150,148,152,156,153) msft.price <- c(70,75,73,74,73,76,74) price <- data.frame (aapl.price,msft.price) 

and have data frame tells me when buy , when sell given stock (1 buy , -1 sell)

aapl <- c(1,-1,0,1,0,0,-1) msft <- c(0,0,1,0,-1,0,1) decision <- data.frame (aapl,msft) 

moreover, have created formula tells me how many stocks hold @ each point in time. but, number of stocks depend example of cash available, have created function tell me when buy stock 100 (just simplicity hoping change later).

the function is:

numberstocks <- function(x) {  newvalue <- 0  (i in 1:length(x)) {      if (x[i] == -1) {         newvalue <- -100     } else if (x[i] == 1) {         newvalue <- 100     } else if (x[i] == 0 && newvalue != 100) {         newvalue <- 0     }     x[i] <- newvalue }  return(x)  } 

and applying decision2 <- as.data.frame (lapply(decision,numberstocks)) data frame value 100 when hold stock , -100 when sell it.

after this, proceeded computation of cash. added 2 columns data frame: 1 cash movements (from buying , selling) , 1 actual value in cash.

positivevalues <- function (x) sapply (seq_along(x),function (i) {     if (x[i] < -0.5) {         100     }else{         x[i]     } })  decision3 <- as.data.frame (lapply(decision2[,1:2], positivevalues)) decision2$cashmov <- (decision2, c(rowsums(decision*price*decision3)))  decision2$cash <- (decision2, c(1000000, matrix(0,6,1))) decision2$cash <- 0     (i in 1) {     decision2$cash [i] = 1000000     }     (i in 2:7){     decision2$cash [i] = decision2$cash[i-1]-decision2$cashmov[i] }         

finally, have created column tells me number of stocks buying @ each period.

decision2$numberbuys <- (decision2, c(rowsums(decision>0))) 

what want each point, when buy stock, instead of having "100", have

decision2$cash[i-1]/decision2$numberbuys[i]/price 

which impact cash @ period [i] impacting number of stocks in [i+1] , on.

could me this?

i sorry if confusing, tried clear possible.

thank in advance

if right, have problem next realization:
decision2$cash[i-1]/decision2$numberbuys[i]/price.

input

con <- textconnection("aapl msft    cash cashmov numberbuys 100    0 1000000   14000          1 -100    0 1014500  -14500          0    0  100 1007200    7300          1  100  100  992400   14800          1  100 -100  999700   -7300          0  100    0  999700       0          0 -100  100 1007600   -7900          1")  decision2 <- read.table(con, header = t)  df <- data.frame(cash = decision2$cash, numberbuys = decision2$numberbuys)  #cash.offset column decision2$cash[i-1] df$cash.offset <- c(na, df$cash[1:nrow(df) - 1]) df$res <- df$cash.offset / df$numberbuys  df$aapl <- df$res / decision2$aapl df$msft <- df$res / decision2$msft  df$aapl[is.infinite(df$aapl)] <- 0 df$msft[is.infinite(df$msft)] <- 0  print(df) 

output

     cash numberbuys cash.offset     res  aapl  msft 1 1000000          1          na      na    na    na 2 1014500          0     1000000     inf     0     0 3 1007200          1     1014500 1014500     0 10145 4  992400          1     1007200 1007200 10072 10072 5  999700          0      992400     inf     0     0 6  999700          0      999700     inf     0     0 7 1007600          1      999700  999700 -9997  9997 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -