I am trying to figure out how to best write code for a rolling average and I cant quite figure it out.
I am trying to create a new variable, "average", which represents the average of the last 250 observations of an existing variable, "price".
So essentially, I need the 251st observation of "Average" to be based on values 1-250 of "Price" ... and then the 252nd observation of "Average" to be based on values 2-251 of "Price".
Any suggestions on how to go about doing this? Thank you in advance for your help.
I am trying to create a new variable, "average", which represents the average of the last 250 observations of an existing variable, "price".
So essentially, I need the 251st observation of "Average" to be based on values 1-250 of "Price" ... and then the 252nd observation of "Average" to be based on values 2-251 of "Price".
Any suggestions on how to go about doing this? Thank you in advance for your help.