1

I have a data set of monthly stock returns of 572 stocks for 192 months. I have to make momentum based portfolio. For each point of portfolio formation, i only want those firms to be taken into calculation, who have non-zero returns in at least one of the past 3 months. Can someone guide me how to exclude firms at each point of portfolio formation??Note that portfolios are formed in each month starting third month of the first year.

  • Welcome to SO. Please read [(1)](http://stackoverflow.com/help/how-to-ask) how do I ask a good question, [(2)](http://stackoverflow.com/help/mcve) How to create a MCVE as well as [(3)](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#answer-5963610) how to provide a minimal reproducible example in R. – Christoph Oct 25 '16 at 19:01
  • You can probably just read the third item in @Christoph's list. Basically, we need a sample of your data (pasted into your question using `dput(my_data_sample)` sufficient to capture the essentials of your problem, as well as the expected output. In your case, you should probably also provide code for how you form portfolios and calculate returns for a given firm. – eipi10 Oct 25 '16 at 19:12

1 Answers1

1

Are you familiar with following notation:

x=seq(1:10)
x
x[3:5]

This creates smaller part of vector. If you do something like:

for (i in 1:8){
print(x[i:(i+2)])
}

You will have subsets for each new 3 month period. Now have to just to evaluate if all the 3 values are greater then 0.

Piotr Siejda
  • 197
  • 8