Announcement

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

  • Analyst Revisions Count

    Dear Statlisters,

    I am working on the following data to calculate the missing variables and need your help. The missing variables are defined as follows:

    revision_5: Number of Revisions (of recommendation) in the past 5 days.
    bts_5: Number of Revision (of recommendation) from BUY to SELL in the past 5 days.
    stb_5: Number of Revision (of recommendation) from SELL to BUY in the past 5 days.

    Required:
    Please guide me how can I count the revisions (as explained in the definitions of the variables) by the analysts in the past 5 days.

    Your help will be highly appreciated.



    [CODE]
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str18 id int date str4 recom byte(revision_5 bts_5 stb_5)
    "153497582541357056" 21186 "BUY" . . .
    "153497582541357056" 21187 "BUY" . . .
    "153497582541357056" 21188 "SELL" . . .
    "153497582541357056" 21189 "BUY" . . .
    "154004513710276609" 21190 "SELL" . . .
    "154004513710276609" 21191 "BUY" . . .
    "154004513710276609" 21192 "BUY" . . .
    "154004513710276609" 21193 "BUY" . . .
    "154188116649984000" 21194 "BUY" . . .
    "154188116649984000" 21195 "SELL" . . .
    "154188116649984000" 21196 "BUY" . . .
    "154188116649984000" 21197 "SELL" . . .
    "154206859409108992" 21198 "BUY" . . .
    "154206859409108992" 21199 "BUY" . . .
    "154206859409108992" 21200 "SELL" . . .
    "154206859409108992" 21201 "SELL" . . .
    "154676532508508160" 21202 "SELL" . . .
    "154676532508508160" 21203 "BUY" . . .
    "154676532508508160" 21204 "BUY" . . .
    "154728793330814976" 21205 "BUY" . . .
    "154728793330814976" 21206 "SELL" . . .
    "154728793330814976" 21207 "SELL" . . .
    "154933705763340288" 21208 "SELL" . . .
    "154933705763340288" 21209 "BUY" . . .
    "154933705763340288" 21210 "SELL" . . .
    end
    format %tddd-Mon-YY date

  • #2
    Code:
    by id (date), sort: gen byte revised = recom != recom[_n-1] & _n > 1
    
    gen byte revised_to_buy = revised & recom == "BUY"
    gen byte revised_to_sell = revised & recom == "SELL"
    
    rangestat (sum) revision_5 = revised ///
                    bts_5 = revised_to_sell ///
                    stb_5 = revised_to_buy, interval(date -5 -1) by(id)
    Notes:
    1. -rangestat- is written by Robert Picard, Nick Cox, and Roberto Ferrer, and is available from SSC.

    2. You do not make clear what you mean by "the past 5 days." I have taken it literally, so the code starts with the day before the current date, and goes back through 5 days before the current date. If you meant to start with the current date and go back through 4 days before the current date, change the -interval()- option to -interval(date -4 0)-.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Code:
      by id (date), sort: gen byte revised = recom != recom[_n-1] & _n > 1
      
      gen byte revised_to_buy = revised & recom == "BUY"
      gen byte revised_to_sell = revised & recom == "SELL"
      
      rangestat (sum) revision_5 = revised ///
      bts_5 = revised_to_sell ///
      stb_5 = revised_to_buy, interval(date -5 -1) by(id)
      Notes:
      1. -rangestat- is written by Robert Picard, Nick Cox, and Roberto Ferrer, and is available from SSC.

      2. You do not make clear what you mean by "the past 5 days." I have taken it literally, so the code starts with the day before the current date, and goes back through 5 days before the current date. If you meant to start with the current date and go back through 4 days before the current date, change the -interval()- option to -interval(date -4 0)-.
      Hi Clyde,

      Thanks for your response. When I use your code it gives me the following output:

      clear
      input str18 id int date str4 recom byte(revised revised_to_buy revised_to_sell) double(revision_5 bts_5 stb_5)
      "153497582541357056" 21186 "BUY" 0 0 0 . . .
      "153497582541357056" 21187 "BUY" 0 0 0 0 0 0
      "153497582541357056" 21188 "SELL" 1 0 1 0 0 0
      "153497582541357056" 21189 "BUY" 1 1 0 1 1 0
      "154004513710276609" 21190 "SELL" 0 0 0 . . .
      "154004513710276609" 21191 "BUY" 1 1 0 0 0 0
      "154004513710276609" 21192 "BUY" 0 0 0 1 0 1
      "154004513710276609" 21193 "BUY" 0 0 0 1 0 1
      "154188116649984000" 21194 "BUY" 0 0 0 . . .
      "154188116649984000" 21195 "SELL" 1 0 1 0 0 0
      "154188116649984000" 21196 "BUY" 1 1 0 1 1 0
      "154188116649984000" 21197 "SELL" 1 0 1 2 1 1
      "154206859409108992" 21198 "BUY" 0 0 0 . . .
      "154206859409108992" 21199 "BUY" 0 0 0 0 0 0
      "154206859409108992" 21200 "SELL" 1 0 1 0 0 0
      "154206859409108992" 21201 "SELL" 0 0 0 1 1 0
      "154676532508508160" 21202 "SELL" 0 0 0 . . .
      "154676532508508160" 21203 "BUY" 1 1 0 0 0 0
      "154676532508508160" 21204 "BUY" 0 0 0 1 0 1
      "154728793330814976" 21205 "BUY" 0 0 0 . . .
      "154728793330814976" 21206 "SELL" 1 0 1 0 0 0
      "154728793330814976" 21207 "SELL" 0 0 0 1 1 0
      "154933705763340288" 21208 "SELL" 0 0 0 . . .
      "154933705763340288" 21209 "BUY" 1 1 0 0 0 0
      "154933705763340288" 21210 "SELL" 1 0 1 1 0 1
      end
      format %tddd-Mon-YY date

      However, for id = 153497582541357056, revision_5 should be 2 instead of 1 (two revisions in recommendations). bts_5 output is correct. stb_5 should be 1 (because there is one revision from Sell to Buy).

      I think you got the right logic but the sum is not doing its magic properly.

      Looking Forward

      Comment


      • #4
        Please re-read my response in #2, specifically Note #2 which deals with precisely this problem. Using my definition of "past 5 days" the results given by this code is correct because there is only one revision in the five days preceding date 21189. Apparently you want the other definition of "past 5 days" and Note 2 in #2 explains how to do that.

        Comment


        • #5

          Hi Clyde, That is indeed true. I am now able to calculate what I wanted. Thanks for your help.

          Comment

          Working...
          X