Announcement

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

  • Business calender: Get omitted dates using bodf()

    I have a list of dates for specific company events of listed US companies. This variable (reguldate) may include weekends as well as holidays.
    For each of these companies, I have a dataset containing daily stock price data from which I created a business calender:

    Code:
     bcal create biscal, from(date) replace
    I then transformed the regular event dates using:
    Code:
    gen businessdate= bofd("biscal",reguldate)
    The help file states:
    Function bofd() returns missing when the date does not appear on the specified calendar.
    However, if reguldate is a non trading date (according to the business calender I created) I would like to get previous trading date instead of a missing value, so something like: businessdate-1
    Is there a way to include those values what are omited according to my .stbcal file?

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int regulardate float businessdate
    14607 104
    14570  79
    14571  80
    14574  82
    14572  81
    14577  83
    14578  84
    14579  85
    14578  84
    14579  85
    14577  83
    14580  86
    14580  86
    14580  86
    14585  89
    14584  88
    14573   .
    14584  88
    14585  89
    14585  89
    end
    format %tdDD/NN/CCYY regulardate
    format %tbbiscal businessdate
    Thanks

  • #2
    I am still very much interested in a solution to my problem. Please let me know if something is not clear.

    Comment


    • #3
      Since you mentioned that your dataset can have non-trading days, I would first point out that

      Code:
      bcal create biscal, varname(date)
      works if there are no observations in your dataset for business holidays. Business holidays are inferred from gaps in variable date. For example, if date is never a Saturday, then the business calendar file created will contain a line like

      Code:
      omit dayofweek (Sa)
      Code:
      help datetime business calendars creation
      gives more details. You can also use bcal describe and bcal load to check if your business calendar is correct.

      Now, assuming you have the business calendar you wanted, bofd("biscal", regulardate) will rightly return missing if regulardate is a non-trading day. You can force it to be the previous trading day, bearing in mind that you may have different regulardates with the same business day. This is not consistent.

      Anyway, you can force missing business days to the previous trading day as follows. I don't have your business calendar, so I created a simple one, called biscal, where Saturdays and Sundays are holidays. I also added two holidays to test the solution.

      Here are the contents of biscal.stbcal:

      Code:
      * Business calendar "biscal"
      
      version 17
      dateformat ymd
      
      range 1999nov22 1999dec29
      centerdate 1999nov22
      
      omit dayofweek (Sa Su)
      omit date 1999nov25
      omit date 1999nov24
      Here is the complete code with your example. Since your data is not sorted, an index is created so the data can be sorted (to conveniently find the previous trading day) and then restored to the original order at the end. To get the previous trading day, I look back at up to the previous four days. You can change how far you need to look back depending on your business calendar.

      Code:
      clear
      input int regulardate
      14607
      14570
      14571
      14574
      14572
      14577
      14578
      14579
      14578
      14579
      14577
      14580
      14580
      14580
      14585
      14584
      14573
      14584
      14585
      14585
      end
      format %tdDD/NN/CCYY regulardate
      generate businessdate = bofd("biscal",regulardate)
      format %tbbiscal businessdate
      generate index = _n
      sort regulardate
      forvalues i = 1/4 {
         replace businessdate = bofd("biscal",regulardate-`i') if missing(businessdate)
      }
      sort index
      drop index
      I hope I am understanding the problem and this helps.

      -- Kreshna

      Comment


      • #4
        Marc Pelow, in the first line of code in #3, I meant
        Code:
        bcal create biscal, from(date)
        . That is, it should be from(date) instead of varname(date).

        Comment


        • #5
          Thank you very much, that is what I was looking for.

          Comment

          Working...
          X