Announcement

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

  • Understanding D1. and lag commands

    Hello Everyone,

    I am having some trouble in understanding the D1. command.
    I currently have a dataset with daily values of the S&P500 (from 1/1/190 to 12/31/2014) and I would like to calculate the compounded returns (which is the diff in log prices from t to t-1). My data do not have saturdays, sundays and holidays (in other words, my data have gaps).

    date s&P500
    1/1/1990 1
    1/2/1990 2

    ....
    12/31/2014 4

    At first I got the log(s&p500 variable). Now have 2 options (let's call the s&p500, x):

    1) gen lag_log_x= log_x[_n-1]
    generate return_x= log_x - lag_log_x

    This will work ok

    2) tsset date, daily
    tsfill //*no gaps now*//
    gen lag_log_x=L1. log_x
    gen return_x=D1. log_x

    In theory these two methodologies should give me the same results, but they don't!!! The second approach deletes my monday observation (reducing the number of observations to 4 for week instead of 5).

    Even if I compute the second approach without filling the gaps, STATA still deletes my monday observation (I don't get it!)

    Can anyone help me in understanding how I can solve this problem? I don't want to lose observations.

    Thank you

    Buffy


  • #2
    In theory these two methodologies should
    No they should not.

    Your first code leads to erros when the two following observations are not concerning the same id (here firm).
    Moreover, it doesn't check whether the lag between the two observation is of one day or more.

    Stick to the second option, with the tsset, L. and D. operators.

    Best,
    Charlie

    Comment


    • #3
      Thank you Charlie. What do you mean with "Moreover, it doesn't check whether the lag between the two observation is of one day or more"?


      However the second option is deleting my monday observations (monday returns should depend on friday's prices).
      This does not seem right!

      (btw I don't need to compute any fe and re. This data don''t have the same id.)
      Last edited by Buffy Summer; 28 Apr 2015, 13:22.

      Comment


      • #4
        If weekends and holidays are to be ignored for the purposes of calculating the differences, then you must account for these if you want to use time-series operators. The following example is based on data downloaded from Yahoo Finance

        Code:
        clear
        * downloaded from https://finance.yahoo.com/q/hp?s=%5EGSPC+Historical+Prices
        import delimited using table.csv
        
        gen obsdate = date(date,"YMD")
        format %td obsdate
        
        * assume no gaps and no duplicates in data
        isid obsdate, sort
        gen x = open - open[_n-1]
        
        * generate a business calendar 
        bcal create mycal, from(obsdate) gen(tradeday) replace
        
        tsset tradeday
        gen xx = D.open
        
        assert x == xx
        and this is the output

        Code:
        . clear
        
        . * downloaded from https://finance.yahoo.com/q/hp?s=%5EGSPC+Historical+Prices
        . import delimited using table.csv
        (7 vars, 6301 obs)
        
        . 
        . gen obsdate = date(date,"YMD")
        
        . format %td obsdate
        
        . 
        . * assume no gaps and no duplicates in data
        . isid obsdate, sort
        (data now sorted by obsdate)
        
        . gen x = open - open[_n-1]
        (1 missing value generated)
        
        . 
        . * generate a business calendar 
        . bcal create mycal, from(obsdate) gen(tradeday) replace
        
          Business calendar mycal (format %tbmycal):
        
            purpose:  
        
              range:  02jan1990  31dec2014
                         10959      20088   in %td units
                             0       6300   in %tbmycal units
        
             center:  02jan1990
                         10959              in %td units
                             0              in %tbmycal units
        
            omitted:     2,829              days
                           113.2            approx. days/year
        
           included:     6,301              days
                           252.1            approx. days/year
        
          Notes:
        
            business calendar file mycal.stbcal saved
        
            variable tradeday created; it contains business dates in %tbmycal format
        
        . 
        . tsset tradeday
                time variable:  tradeday, 02jan1990 to 31dec2014
                        delta:  1 day
        
        . gen xx = D.open
        (1 missing value generated)
        
        . 
        . assert x == xx
        
        .

        Comment

        Working...
        X