Announcement

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

  • Daily Stock returns

    Dear all,

    I am entirely new to stata, so in case I am leaving out any information that might be required to help me, I am already apologising in advance.
    I want to calculate the daily stock returns for multiple companies based on the following data:

    Date ISIN 1 ISIN 2 ISIN 3 ISIN 4
    4/28/2021 3.23 15350 .2765 1.5935
    4/27/2021 3.23 15350 .2765 1.5935
    4/26/2021 3.3 15500 .2815 1.59
    4/23/2021 3.28 15170 .28 1.593
    4/22/2021 3.3 14865 .265 1.59
    4/21/2021 3.33 15105 .274 1.56
    4/20/2021 3.31 14895 .282 1.562
    4/19/2021 3.33 15220 .287 1.5785
    4/16/2021 3.33 15300 .2865 1.56


    Just an abstract of course. The numbers you see are the daily stock prices. How can I replace these numbers with the daily stock return (price t+1 - price t) / price t for each variable.

    Any help is highly appreciated.



  • #2
    Welcome to Statalist.

    Code:
    // present the example data in a usable format ready to read into Stata
    
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(date isin1 isin2 isin3 isin4)
    22398 3.23 15350 .2765 1.5935
    22397 3.23 15350 .2765 1.5935
    22396  3.3 15500 .2815   1.59
    22393 3.28 15170   .28  1.593
    22392  3.3 14865  .265   1.59
    22391 3.33 15105  .274   1.56
    22390 3.31 14895  .282  1.562
    22389 3.33 15220  .287 1.5785
    22386 3.33 15300 .2865   1.56
    end
    format %tdnn/dd/CCYY date
    
    // the answer starts here
    
    sort date
    foreach price of varlist isin1-isin4 {
        generate float r_`price' = (`price'[_n+1] - `price')/`price', after(`price')
        format %9.5f r_`price'
    }
    list, clean
    Code:
    . list, clean 
    
                date   isin1    r_isin1   isin2    r_isin2   isin3    r_isin3    isin4    r_isin4  
      1.   4/16/2021    3.33    0.00000   15300   -0.00523   .2865    0.00175     1.56    0.01186  
      2.   4/19/2021    3.33   -0.00601   15220   -0.02135    .287   -0.01742   1.5785   -0.01045  
      3.   4/20/2021    3.31    0.00604   14895    0.01410    .282   -0.02837    1.562   -0.00128  
      4.   4/21/2021    3.33   -0.00901   15105   -0.01589    .274   -0.03285     1.56    0.01923  
      5.   4/22/2021     3.3   -0.00606   14865    0.02052    .265    0.05660     1.59    0.00189  
      6.   4/23/2021    3.28    0.00610   15170    0.02175     .28    0.00536    1.593   -0.00188  
      7.   4/26/2021     3.3   -0.02121   15500   -0.00968   .2815   -0.01776     1.59    0.00220  
      8.   4/27/2021    3.23    0.00000   15350    0.00000   .2765    0.00000   1.5935    0.00000  
      9.   4/28/2021    3.23          .   15350          .   .2765          .   1.5935          .
    With that said, please take a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

    The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

    And, advice I always give to a member who identifies themself as new to Stata.

    I'm sympathetic to you as a new user of Stata - there is quite a lot to absorb.

    When I began using Stata in a serious way, I started, as have others here, by reading my way through the Getting Started with Stata manual relevant to my setup. Chapter 18 then gives suggested further reading, much of which is in the Stata User's Guide, and I worked my way through much of that reading as well. There are a lot of examples to copy and paste into Stata's do-file editor to run yourself, and better yet, to experiment with changing the options to see how the results change.

    All of these manuals are included as PDFs in the Stata installation and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu. The objective in doing the reading was not so much to master Stata - I'm still far from that goal - as to be sure I'd become familiar with a wide variety of important basic techniques, so that when the time came that I needed them, I might recall their existence, if not the full syntax, and know how to find out more about them in the help files and PDF manuals.

    Stata supplies exceptionally good documentation that amply repays the time spent studying it - there's just a lot of it. The path I followed surfaces the things you need to know to get started in a hurry and to work effectively.

    Comment

    Working...
    X