Hi all,
I want to calculate the average trading volume (30 days) for each trading day per stock. I have the variables date, ticker, volume (per day per company). The problem is that i cant use rolling, because i have multiple dates that are the same due to the fact that each company was trading that day of course. I hope someone can help me with my problem. below you will find a sneak peak of how my data looks like. Logical would be that the first 29 days dont have an average, because those are the days that the first average volume is being calculated with.
I also tried this, but this gives the same value for all the observations:
sort TICKER date
by TICKER: gen avg_volume = VOL[_n - 29] / 30 if _n >= 30
by TICKER: replace avg_volume = sum(VOL[_n - 29])/30 if _n >= 30
I want to calculate the average trading volume (30 days) for each trading day per stock. I have the variables date, ticker, volume (per day per company). The problem is that i cant use rolling, because i have multiple dates that are the same due to the fact that each company was trading that day of course. I hope someone can help me with my problem. below you will find a sneak peak of how my data looks like. Logical would be that the first 29 days dont have an average, because those are the days that the first average volume is being calculated with.
I also tried this, but this gives the same value for all the observations:
sort TICKER date
by TICKER: gen avg_volume = VOL[_n - 29] / 30 if _n >= 30
by TICKER: replace avg_volume = sum(VOL[_n - 29])/30 if _n >= 30
Comment