Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Create business calendar for stock returns & price

    Hi,

    I would like to create a business calendar in a way that Stata creates lead/lag price/returns. In particular, I would like Stata to price/returns on Friday to be considered the previous price/return of Monday. Same for price/returns when there is a day off in the middle of the week. I have checked several Statalist posts & doc about "bcal create" but could not figure out the solution to my problem. Here are exemples of posts I have read:
    http://www.stata.com/features/overvi...ess-calendars/
    https://www.statalist.org/forums/for...xt-trading-day

    Here is what I have so far. The 2 lag "close" price created are the same and do not address the problem:

    Code:
    sysuse sp500.dta , clear
    
    * Generate wrong lagged price:
    tsset date
    gen L_close_w = L.close
    
    * Now creat the right lagged price
    bcal create sp500, from(date)
    gen L_close_r = L.close
    
    * Compare both of them (same at the moment):
    browse date L_close_w L_close_r

  • #2
    I don't use business calendars so I looked up the help files. You replicated an example in the bcal help file but you omitted the generate(bizdate) option. So you created a business calendar but the dataset contains no date variable in the sp500 business calendar format. If you want Stata to know that you want to use a business calendar date for lags instead of the regular calendar date, you have to redefine the tsset. Here's a quick example that shows various ways to do this (copies of the business calendar date variables are also created to show the numerical values for each date).

    Code:
    * lag based on calendar date
    sysuse sp500.dta , clear
    tsset date
    gen L_close = L.close
    
    * neanderthal business calendar
    gen neanderthal_date = _n
    tsset neanderthal_date
    gen L_close2 = L.close
    
    * create a business calendar; generate date variable separately
    bcal create sp500, from(date) replace
    gen mydate = bofd("sp500", date)
    clonevar mydate0 = mydate
    format mydate %tbsp500
    tsset mydate
    gen L_close3 = L.close
    
    * create a business calendar, including a new business calendar variable
    bcal create sp500b, from(date) generate(bizdate) replace
    tsset bizdate
    gen L_close4 = L.close
    gen bizdate0 = bizdate
    
    assert L_close2 == L_close3
    assert L_close2 == L_close4

    Comment


    • #3
      Thank you so much for your response Robert. If I understand well, the "neanderthal" approach is strictly equivalent the "business calendar" approach as long as they are generated from the same "date variable". Is there any way for Stata to recognize when there is effectively missing data (one return/price has not been recorded in the dataset for some random reasons) as opposed to weekends/day off.

      This is important because in some cases, you may/may not want to create the lags. Any idea? Thanks!

      Comment


      • #4
        I think that's right, a calendar date variable is no different than a simple observation counter with respect to the dataset that created the business calendar. If this dataset contains real missing dates, then you need to either fine tune the business calendar or define a business calendar using another dataset known to have no missing dates.

        In the following example, I create a calendar after dropping the Jan. 5 observation:
        Code:
        sysuse sp500.dta , clear
        drop if date == mdy(1,5,2001)
        bcal create sp500c, from(date) replace
        Stata's business calendars are stored as text files that can be opened in Stata's do-file editor. Here's the content of the sp500c.stbcal calendar:
        Code:
        * Business calendar "sp500c" created by -bcal create-
        * Created/replaced on 18 Jul 2017
        
        version 15
        dateformat ymd
        
        range 2001jan02 2001dec31
        centerdate 2001jan02
        
        omit dayofweek (Sa Su)
        omit date 2001jan05
        omit date 2001jan15
        omit date 2001feb19
        omit date 2001apr13
        omit date 2001may28
        omit date 2001jul04
        omit date 2001sep03
        omit date 2001sep11
        omit date 2001sep12
        omit date 2001sep13
        omit date 2001sep14
        omit date 2001nov22
        omit date 2001dec25
        As you can see, weekend days are omitted in a single instruction. Since there is no Jan. 5 observation in the dataset, that date is omitted as well.

        The following is a modified version of the above with the 2001jan05 and 2001dec25 omits removed. It is saved as sp500d.stbcal
        Code:
        * Business calendar "sp500d" created by RP
        * Modified on 18 Jul 2017
        
        version 15
        dateformat ymd
        
        range 2001jan02 2001dec31
        centerdate 2001jan02
        
        omit dayofweek (Sa Su)
        omit date 2001jan15
        omit date 2001feb19
        omit date 2001apr13
        omit date 2001may28
        omit date 2001jul04
        omit date 2001sep03
        omit date 2001sep11
        omit date 2001sep12
        omit date 2001sep13
        omit date 2001sep14
        omit date 2001nov22
        If you now load the data, drop the 2001jan05, create a business calendar date from the modified calendar, and redo the lag test:
        Code:
        sysuse sp500.dta , clear
        drop if date == mdy(1,5,2001)
        gen mydate = bofd("sp500d", date)
        format mydate %tbsp500d
        tsset mydate
        gen L_close = L.close
        
        list date close mydate L_close if mi(L_close)
        you'll find that 2001jan05 is considered missing as well as 2001dec25:
        Code:
        . list date close mydate L_close if mi(L_close)
        
             +-------------------------------------------+
             |      date     close      mydate   L_close |
             |-------------------------------------------|
          1. | 02jan2001   1283.27   02jan2001         . |
          4. | 08jan2001   1295.86   08jan2001         . |
        244. | 26dec2001   1149.37   26dec2001         . |
             +-------------------------------------------+
        
        .
        More info is available by reading carefully help datetime_business_calendars_creation.

        Comment


        • #5
          Thanks a lot Robert, this is exactly what I needed, it really helps!

          Comment

          Working...
          X