A Simple (?) R Question

How tough would it be to create a function in R that takes two inputs (an array of stock symbols, and a weight in each symbol) and outputs daily p/l over a period of time?

Pretty tough. That’s not enough input to write the function.

I think it would be pretty easy, but you would need to have a starting dollar value. If you just want to calculate the portfolio returns, you can use the PerformanceAnalytics package if you don’t want to calculate it yourself. Then the dollar value p/l is just the dollar value of the day before times the return of the portfolio.

None of the individual steps are hard, but it would get somewhat intricate to make sure that the looking up of values and such are handled correctly. It depends on the source of your data and how complex it is to get that into R in a useable format. If it’s all in one giant excel spreadsheet that you import, it’s not that tricky. If you are extracting it from yahoo finance or something, it’s trickier. Presumably the function could only give you an array of returns; you’d need to give it more data to do more than that.

Here is my brainstorming so far. I’ll try and put this thing together when I get home tonight. # need # number of periods to backtest with # array of symbols # array of cash investeds # # example # # portfolio