hello,
currently I work with stata on my master thesis - unfortunately I struggle to get my panel data into the correct date formats in order to calculate weekly stock returns (friday to friday) from daily returns. Maybe you can help me to get this correct. After sorting out my raw data problems I want to set the xtset​ command. My current data looks like this:
What I want to do with this data is calculating returns per id from friday-to-friday and generate a date variable that gives me the right calender week per day. I tried to use the daily date variable (long, %d) to generate a weekly variable which gives me the right week for a day:
gen dw = wofd(date)
format dw %tw
By checking the dates I realized that the 30sep1986 and 01oct1986 are Tuesday and Wednesday of the same week which ends (in trading terms) with Friday 03oct1986 - thus all three dates should have the same calender week, the 40th. Do you know why this conversion does not work?
If I manage to get the calender week variable right, I want to calculate weekly returns from friday to friday, e.g. the return for the 41th week for id 1 is calculated by (1+x4)*(1+x5)*(1+x6)*(1+x7)*(1+x8). I generated two variables, one that counts the days per week and one that gives me the 5th return of one week (so if no public holidays occur, this should be friday every week):
gen week_d=dow(date)
gen fret=(1+ret) if week_d==5
I want to use the value of fret as starting point for my implementation of the above return formula, do you know how to do this?
Your help will be very much appreciated!
Best Nicolai
currently I work with stata on my master thesis - unfortunately I struggle to get my panel data into the correct date formats in order to calculate weekly stock returns (friday to friday) from daily returns. Maybe you can help me to get this correct. After sorting out my raw data problems I want to set the xtset​ command. My current data looks like this:
id | date | ret | day | price | dw | week_d | fret | |
1 | 30sep1986 | x1 | 1 | a1 | 1986w39 | 2 | . | |
1 | 01oct1986 | x2 | 2 | a2 | 1986w40 | 3 | . | |
1 | 02oct1986 | x3 | 3 | a3 | 1986w40 | 4 | . | |
1 | 03oct1986 | x4 | 4 | a4 | 1986w40 | 5 | 1+x4 | |
1 | 06oct1986 | x5 | 5 | a5 | 1986w40 | 1 | . | |
1 | 07oct1986 | x6 | 6 | a6 | 1986w40 | 2 | . | |
1 | 08oct1986 | x7 | 7 | a7 | 1986w41 | 3 | . | |
1 | 09oct1986 | x8 | 8 | a8 | 1986w41 | 4 | . | |
1 | 10oct1986 | x9 | 9 | a9 | 1986w41 | 5 | 1+x9 | |
2 | 30sep1986 | y1 | 1 | b1 | 1986w39 | 2 | . | |
2 | 01oct1986 | y2 | 2 | b2 | 1986w40 | 3 | . | |
2 | 02oct1986 | y3 | 3 | b3 | 1986w40 | 4 | . | |
2 | 03oct1986 | y4 | 4 | b4 | 1986w40 | 5 | 1+y4 | |
2 | 06oct1986 | y5 | 5 | b5 | 1986w40 | 1 | . | |
2 | 07oct1986 | y6 | 6 | b6 | 1986w40 | 2 | . | |
2 | 08oct1986 | y7 | 7 | b7 | 1986w41 | 3 | . | |
2 | 09oct1986 | y8 | 8 | b8 | 1986w41 | 4 | . | |
2 | 10oct1986 | y9 | 9 | b9 | 1986w41 | 5 | 1+y9 | |
gen dw = wofd(date)
format dw %tw
By checking the dates I realized that the 30sep1986 and 01oct1986 are Tuesday and Wednesday of the same week which ends (in trading terms) with Friday 03oct1986 - thus all three dates should have the same calender week, the 40th. Do you know why this conversion does not work?
If I manage to get the calender week variable right, I want to calculate weekly returns from friday to friday, e.g. the return for the 41th week for id 1 is calculated by (1+x4)*(1+x5)*(1+x6)*(1+x7)*(1+x8). I generated two variables, one that counts the days per week and one that gives me the 5th return of one week (so if no public holidays occur, this should be friday every week):
gen week_d=dow(date)
gen fret=(1+ret) if week_d==5
I want to use the value of fret as starting point for my implementation of the above return formula, do you know how to do this?
Your help will be very much appreciated!
Best Nicolai
Comment