Announcement

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

  • Forming portfolios and tracking their performance

    Dear Statalist members,

    I am new to Stata and would appreciate any help/insights you could give me, in dealing with the following.

    I am working with a panel data of stocks, their time series of monthly returns and other fundamental data. I am trying to do the following:
    1. Rank stocks on specific dates t (June each year) based on the realization of one of the fundmental variables x
    2. Allocate the stocks to portfolios (i.e. 30% highest realizations in the high_portfolio and 30% lowest realizations in the low_portfolio)
    2. Calculate portfolio returns from t+1 to t+12 (i.e. how does the high_portfolio perform subsequent to formation and how does this compare to the low_portfolio?)

    I have had a look at some previous similar threads but I have not been able to piece everything together.

    For step 1, I am trying to create a local list of all the June observations per year so that I can refer to them in ranking the portfolios.However, I am getting the error message _( invalid name
    r(198);
    Code:
    local(formationt) if month=(06)
    For step 3, I am struggling to figure out how to tell Stata to focus only on the stocks belonging to a certain percentile group as identified in step 2 and use their individual return series to compute a portfolio return series for the next year.

    Thank you in advance for any help!

    Nour



  • #2
    The code you show in #1 indeed contains a syntax error. You must not put parentheses around thename of the local macro when you create it. But even after you fix that, what you will end up with is a local macro named formationt that contains the string "if month = (06)". I should also point out that if, perhaps, that is actually what you want, when you try to use it later on you will get another syntax error because "if month = (06)" is itself incorrect syntax.

    I don't think anybody will be able to help you without having an example of your data to work with. Please use the -dataex- command to post a relevant example. By a relevant example I mean one which has enough different stocks in it that it is possible to create the kind of portfolios you are talking about, and enough observations on those stocks so that it is possible to calculate the forward 12 month returns.

    If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Also, when posting back, please explain what you want to do with the middle 40% of the stocks that do not fall into your high and low portfolios. Just ignore them? Treat them as a third portfolio? Delete them from the data? Something else?
    Last edited by Clyde Schechter; 28 Jul 2019, 14:21.

    Comment


    • #3
      Thank you very much for the tip! Here is an example of the data generated using the dataex command:

      The idea would be to sort by the variable PE into high/medium/low portfolios, and then track portfolios' performance post formation.

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input int Date str18 Stock double(Price Return PE)
      20482 "LANXESS"            38.014  .036835320226005434  43.7
      20513 "LANXESS"            38.131   .30730866656415257  43.8
      20544 "LANXESS"            42.252   10.262409036662842  23.4
      20573 "LANXESS"            46.187    8.904668324912567  25.6
      20605 "LANXESS"                43   -7.149825751647907  20.1
      20635 "LANXESS"            39.185   -9.290609518873447  18.3
      20664 "LANXESS"            42.161     7.32016026120693  19.7
      20697 "LANXESS"            47.589   12.110601897753043  23.7
      20727 "LANXESS"             55.37    15.14362887404479  27.5
      20758 "LANXESS"            58.074    4.768012856013065  28.9
      20788 "LANXESS"            58.249   .30088655214679355    26
      20818 "LANXESS"              61.8    5.917643954717048  27.6
      20850 "LANXESS"            67.275    8.488533214932453    30
      20878 "LANXESS"            63.866   -5.200155821216089  28.5
      20909 "LANXESS"            63.075   -1.246264387368717  30.1
      20937 "LANXESS"            66.049    4.607239628118123  31.5
      20970 "LANXESS"            66.384    .5059172782209553    28
      21000 "LANXESS"            66.259  -.18847588942242302  27.9
      21031 "LANXESS"             65.83   -.6495643216502637  27.8
      21062 "LANXESS"            62.586   -5.053405055789626  39.5
      21091 "LANXESS"            66.499    6.064529903286857    42
      21123 "LANXESS"            66.988    .7326588917474119  42.3
      21153 "LANXESS"            64.118   -4.378836313325276  42.5
      21182 "LANXESS"             66.38    3.467067055284957    44
      21215 "LANXESS"             70.34    5.794482075400794  46.7
      21243 "LANXESS"                69  -1.9234122448577073  45.8
      21273 "LANXESS"              62.2  -10.375150485212558  65.4
      21304 "LANXESS"             61.82   -.6128063041163582    65
      21335 "LANXESS"             67.52    8.819691358373127  58.8
      21364 "LANXESS"             66.64  -1.3118852429114307    58
      21396 "LANXESS"             70.38    5.460413403485174  61.3
      21427 "LANXESS"             68.14  -3.2344719660618493  27.4
      21455 "LANXESS"             63.12   -7.652673569520174  25.3
      21488 "LANXESS"             54.98  -13.806819380051913  22.1
      21518 "LANXESS"              48.5   -12.54056847936621  15.7
      21549 "LANXESS"              40.5    -18.0261823830944  13.1
      20482 "INNOGY"                  .                    .     .
      20513 "INNOGY"                  .                    .     .
      20544 "INNOGY"                  .                    .     .
      20573 "INNOGY"                  .                    .     .
      20605 "INNOGY"                  .                    .     .
      20635 "INNOGY"                  .                    .     .
      20664 "INNOGY"                  .                    .     .
      20697 "INNOGY"                  .                    .     .
      20727 "INNOGY"                  .                    .     .
      20758 "INNOGY"             36.325                    .     .
      20788 "INNOGY"             33.269   -8.788017698619143  14.1
      20818 "INNOGY"              33.17  -.29801795111941004    14
      20850 "INNOGY"             31.935  -3.7943265945408617  13.5
      20878 "INNOGY"             33.961   6.1510220403595515  14.4
      20909 "INNOGY"              35.18   3.5264931892264206   8.5
      20937 "INNOGY"               33.9  -3.7062724923007604   8.2
      20970 "INNOGY"              36.53    7.471882642078671  11.9
      21000 "INNOGY"             34.805   -4.837278618656141  11.3
      21031 "INNOGY"             35.507   1.9968805521299804  11.5
      21062 "INNOGY"             37.104    4.399492037124621  10.7
      21091 "INNOGY"             37.805   1.8716588497057491  10.9
      21123 "INNOGY"             39.935    5.481176335911904  11.5
      21153 "INNOGY"             38.805  -2.8704028063250195  18.6
      21182 "INNOGY"             32.697  -17.126577373081776  15.7
      21215 "INNOGY"              30.61   -6.595657761796328  14.7
      21243 "INNOGY"              32.85    7.062499197329846  15.8
      21273 "INNOGY"              38.49   15.844672236503527  27.5
      21304 "INNOGY"              36.57   -5.117023513436489  26.1
      21335 "INNOGY"              36.19  -1.0445394585638812  25.6
      21364 "INNOGY"              36.61   1.1538589556493806  25.9
      21396 "INNOGY"              38.08    3.936778279101959  26.9
      21427 "INNOGY"              37.25  -2.2037265097595964  25.5
      21455 "INNOGY"              38.38   2.9884545753985403  26.3
      21488 "INNOGY"              38.95    1.474228173720343  26.7
      21518 "INNOGY"              40.48   3.8529252662452733  36.4
      21549 "INNOGY"              40.43  -.12359413265305118  36.4
      20482 "GEA GROUP"          38.515  .038953451119057875  24.6
      20513 "GEA GROUP"          40.766    5.680062485660935  21.7
      20544 "GEA GROUP"           42.89    5.079029786725048  22.8
      20573 "GEA GROUP"          40.819   -4.949103924236462  21.7
      20605 "GEA GROUP"          41.555   1.7870191718565254  22.5
      20635 "GEA GROUP"          42.599    2.481292781640848  23.1
      20664 "GEA GROUP"          47.625    11.15270546254669  19.9
      20697 "GEA GROUP"          48.015    .8155628643071073  20.1
      20727 "GEA GROUP"          49.525   3.0964130494678526  20.7
      20758 "GEA GROUP"          35.168   -34.23410143634038  20.2
      20788 "GEA GROUP"          35.172  .011373329554475701  20.2
      20818 "GEA GROUP"           38.17    8.179955524038288    22
      20850 "GEA GROUP"          38.265   .24857735047461144    22
      20878 "GEA GROUP"           36.75  -4.0397414603038735  24.9
      20909 "GEA GROUP"           39.77    7.897463356075374  26.9
      20937 "GEA GROUP"           39.04  -1.8526097674707613  26.4
      20970 "GEA GROUP"           36.62   -6.399222229852715  23.1
      21000 "GEA GROUP"           36.05  -1.5687675515406556  22.7
      21031 "GEA GROUP"            34.1   -5.560947944148683  23.5
      21062 "GEA GROUP"          36.961      8.0565918395054  25.5
      21091 "GEA GROUP"           38.38   3.7673187895028626  26.4
      21123 "GEA GROUP"          41.445    7.683075646393567  28.6
      21153 "GEA GROUP"          40.538   -2.212744122771695  27.3
      21182 "GEA GROUP"          39.836  -1.7468779746379608  26.8
      21215 "GEA GROUP"           39.96    .3107927710959674  30.6
      21243 "GEA GROUP"           39.06  -2.2780028331819886  29.9
      21273 "GEA GROUP"           34.62  -12.066737570046984  26.5
      21304 "GEA GROUP"           32.17   -7.339720849712814  30.8
      21335 "GEA GROUP"           31.72   -1.408694448428785  30.4
      21364 "GEA GROUP"           29.19   -8.312121190062383    28
      21396 "GEA GROUP"           33.38   13.413073336766573  30.2
      21427 "GEA GROUP"           32.78  -1.8138344918005451  29.7
      21455 "GEA GROUP"           31.09  -5.2932349245021255  28.2
      21488 "GEA GROUP"            26.9  -14.475993746286905  22.4
      21518 "GEA GROUP"           23.24    -14.6251354624083  19.3
      21549 "GEA GROUP"           22.59   -2.836760150379851  18.8
      20482 "FIELMANN"           69.389   1.8310708723578883  34.6
      20513 "FIELMANN"             63.8   -8.397516315841152  32.4
      20544 "FIELMANN"             66.7    4.445176257083401  33.8
      20573 "FIELMANN"            64.53  -3.3074720973829708    34
      20605 "FIELMANN"            65.38   1.3086169348316152  34.5
      20635 "FIELMANN"            65.58    .3054370120437595  34.6
      20664 "FIELMANN"               69    5.083573318075731  36.4
      20697 "FIELMANN"             70.1   1.5816289443285008  35.4
      20727 "FIELMANN"           72.958   3.9961139069106464  36.8
      20758 "FIELMANN"             63.6  -13.727046276357429  33.1
      20788 "FIELMANN"           59.547   -6.584755364752631    31
      20818 "FIELMANN"           62.456    4.769639211943972  32.5
      20850 "FIELMANN"            64.81   3.6997603621634885  33.7
      20878 "FIELMANN"           69.797    7.413111646234446  35.3
      20909 "FIELMANN"           72.287   3.5053277733268917  36.5
      20937 "FIELMANN"           70.527   -2.464869141124335  34.7
      20970 "FIELMANN"            71.83    1.830660086271549  35.4
      21000 "FIELMANN"           67.593   -6.079778869535176  33.3
      21031 "FIELMANN"            67.72   .18771298735760822  33.4
      21062 "FIELMANN"           69.699   2.8804413196325984  34.3
      21091 "FIELMANN"           73.354    5.111106570152499  36.1
      21123 "FIELMANN"            74.84   2.0055465243103434  36.9
      21153 "FIELMANN"            73.41  -1.9292335283199014  35.2
      21182 "FIELMANN"           73.393 -.023160289743379657  35.2
      21215 "FIELMANN"            70.65  -3.8090454094719672  33.9
      21243 "FIELMANN"            67.75   -4.191364937072623  33.9
      21273 "FIELMANN"             65.8  -2.9204621428538817    33
      21304 "FIELMANN"             68.6    4.167269640056808    34
      21335 "FIELMANN"               68   -.8784829555732803  33.7
      21364 "FIELMANN"             59.6  -13.185213110480262  29.6
      21396 "FIELMANN"             59.6                    0  29.6
      21427 "FIELMANN"            57.95  -2.8075004285543335  29.9
      21455 "FIELMANN"             52.1   -10.64156210264395  26.9
      21488 "FIELMANN"               55    5.416823647314968  28.4
      21518 "FIELMANN"             55.3    .5439723295818122  28.3
      21549 "FIELMANN"             54.4  -1.6408754666392098  27.8
      20482 "EVOTEC"              3.414  -2.7731413105525475  38.4
      20513 "EVOTEC"              3.146    -8.17528197361629  35.4
      20544 "EVOTEC"              3.154    .2539683904760534  25.2
      20573 "EVOTEC"              3.497   10.323396954039215  27.9
      20605 "EVOTEC"              4.012   13.738441201845017  31.4
      20635 "EVOTEC"               3.82   -4.903944748120518  29.9
      20664 "EVOTEC"              4.108    7.268586944782789  32.2
      20697 "EVOTEC"               4.48    8.668675436058221  94.7
      20727 "EVOTEC"               5.05   11.976519686037461 106.8
      20758 "EVOTEC"              4.993  -1.1351311768796124 105.6
      20788 "EVOTEC"               6.15   20.841515029995428  45.3
      20818 "EVOTEC"              7.457   19.270110674323117    55
      20850 "EVOTEC"              6.937    -7.22837841584935  51.1
      20878 "EVOTEC"              7.266   4.6336529395845885  53.5
      20909 "EVOTEC"              9.128    22.81406787607644  43.9
      20937 "EVOTEC"              10.69   15.796211247717928  51.5
      20970 "EVOTEC"             12.534   15.913622676552997  47.5
      21000 "EVOTEC"             14.032   11.289548382734345  53.2
      21031 "EVOTEC"             12.272   -13.40201910049269  46.5
      21062 "EVOTEC"              16.17   27.583742896411522  63.4
      21091 "EVOTEC"              20.14    21.95502137014007  78.9
      21123 "EVOTEC"             18.049  -10.961760570830497  70.8
      21153 "EVOTEC"              12.19   -39.24743380889312  58.5
      21182 "EVOTEC"             13.545    10.54015320438784  65.1
      21215 "EVOTEC"              14.92    9.668511923855636  71.7
      21243 "EVOTEC"              14.44  -3.2700461310934648  69.4
      21273 "EVOTEC"             15.875    9.474341131407526  94.9
      21304 "EVOTEC"              13.47  -16.428055435648265  80.6
      21335 "EVOTEC"             14.835    9.652426332051249 105.5
      21364 "EVOTEC"              14.76   -.5068434570458729   105
      21396 "EVOTEC"             17.755    18.47463472227247 126.3
      21427 "EVOTEC"              21.98   21.346578258042413 101.7
      21455 "EVOTEC"             18.195   -18.89861179113522  84.2
      21488 "EVOTEC"              17.62    -3.21122105560896  81.5
      21518 "EVOTEC"              20.66   15.916484318345903    48
      21549 "EVOTEC"              17.18  -18.445354713538322    40
      20482 "DUERR"              31.111    7.026562441160718  13.6
      20513 "DUERR"               27.46  -12.483105405825619  11.8
      20544 "DUERR"               34.51   22.851873677008687  14.8
      20573 "DUERR"              34.943   1.2469025386930261    15
      20605 "DUERR"               34.94 -.008585779093183797  13.2
      20635 "DUERR"              34.221  -2.0792813992040005  12.9
      20664 "DUERR"              38.508   11.802652120184968  14.6
      20697 "DUERR"              38.385  -.31992536358583723  14.3
      20727 "DUERR"              37.185  -3.1761304124598153  13.8
      20758 "DUERR"               33.93   -9.160587535912477  12.6
      20788 "DUERR"              37.595   10.257148403385138  15.1
      20818 "DUERR"              38.025   1.1372775315366424  15.3
      20850 "DUERR"                  40    5.063561596857972  16.1
      20878 "DUERR"              39.081   -2.324303873039957  14.9
      20909 "DUERR"              41.755   6.6182789221656115  15.9
      20937 "DUERR"              45.816    9.281417048636339  17.4
      20970 "DUERR"              48.716    6.137414313330787  13.3
      21000 "DUERR"              52.343    7.181069477371545  14.3
      21031 "DUERR"              51.226   -2.157099734625108    14
      21062 "DUERR"              48.972   -4.499850944065934  16.7
      21091 "DUERR"              56.405   14.130910084521469  19.2
      21123 "DUERR"              58.875     4.28587452796981    20
      21153 "DUERR"                52.6  -11.270043259291763  17.4
      21182 "DUERR"               53.05    .8518745316327955  17.5
      21215 "DUERR"                55.4    4.334472869324596  18.3
      21243 "DUERR"                50.3   -9.657451664754458  17.9
      21273 "DUERR"               44.67  -11.870294174166611  15.9
      21304 "DUERR"                41.1   -8.329401386183834  14.6
      21335 "DUERR"               42.86   4.1930868543242035    18
      21364 "DUERR"               39.87   -7.231482865216739  16.7
      21396 "DUERR"                37.3   -6.663083474349375  15.6
      21427 "DUERR"               39.61     6.00882849840554  16.9
      21455 "DUERR"                38.8  -2.0661365004597547  16.6
      21488 "DUERR"               31.44  -21.033927906822182  13.4
      21518 "DUERR"               32.98    4.782034957044697  15.3
      21549 "DUERR"               30.79   -6.871135519504383  14.3
      20482 "CARL ZEISS MEDITEC" 26.795    6.933947807861291    35
      20513 "CARL ZEISS MEDITEC" 29.104    8.266031882895282  39.1
      20544 "CARL ZEISS MEDITEC" 26.987   -7.552035320234723  36.2
      20573 "CARL ZEISS MEDITEC" 28.994    7.173364345528643  38.9
      20605 "CARL ZEISS MEDITEC" 35.077    19.04567330041951  35.6
      20635 "CARL ZEISS MEDITEC" 36.407   3.7215418813347863  36.9
      20664 "CARL ZEISS MEDITEC" 33.195   -9.236180167512599  33.7
      20697 "CARL ZEISS MEDITEC" 32.644  -1.6738190564126307  34.2
      20727 "CARL ZEISS MEDITEC"  33.82   3.5391271863437836  35.4
      20758 "CARL ZEISS MEDITEC"  32.64  -3.5513813374527943  34.2
      20788 "CARL ZEISS MEDITEC" 32.378   -.8059350276592202  33.9
      20818 "CARL ZEISS MEDITEC" 34.701    6.928932516206018  28.7
      20850 "CARL ZEISS MEDITEC"   34.9    .5718324227007268  28.9
      20878 "CARL ZEISS MEDITEC" 41.143    16.45669740884453  29.8
      20909 "CARL ZEISS MEDITEC"     40  -2.8174349182890372    29
      20937 "CARL ZEISS MEDITEC" 42.178    5.301930397563628  30.6
      20970 "CARL ZEISS MEDITEC"  45.86    8.369451934018574  33.2
      21000 "CARL ZEISS MEDITEC"  45.69  -.37138218545782964  33.1
      21031 "CARL ZEISS MEDITEC" 45.315   -.8241352068435132  32.9
      21062 "CARL ZEISS MEDITEC" 41.425   -8.975354019411844    28
      21091 "CARL ZEISS MEDITEC"  44.04    6.121374854185261  29.7
      21123 "CARL ZEISS MEDITEC"  45.77     3.85305428110715  30.9
      21153 "CARL ZEISS MEDITEC" 49.693    8.223522344701138  33.6
      21182 "CARL ZEISS MEDITEC"  51.79      4.1333002323238    33
      21215 "CARL ZEISS MEDITEC"  52.55   1.4568016887159907  33.5
      21243 "CARL ZEISS MEDITEC"     51   -2.993946459863431  33.8
      21273 "CARL ZEISS MEDITEC"   51.7   1.3632148790057628  34.3
      21304 "CARL ZEISS MEDITEC"   56.3    8.523675363126124  37.3
      21335 "CARL ZEISS MEDITEC"   59.3    5.191477085803512  41.2
      21364 "CARL ZEISS MEDITEC"   57.9   -2.389192142473024  40.2
      21396 "CARL ZEISS MEDITEC"     66   13.093735744747603  45.8
      21427 "CARL ZEISS MEDITEC"   76.2   14.370672066617503    55
      21455 "CARL ZEISS MEDITEC"     72    -5.66953436765453    52
      21488 "CARL ZEISS MEDITEC"  72.55    .7609860314141406  52.4
      21518 "CARL ZEISS MEDITEC"   61.1   -17.17641131526472  44.1
      21549 "CARL ZEISS MEDITEC"   68.5   11.432187909062986  48.4
      20482 "BECHTLE"            39.314    .8019043405960501  20.2
      20513 "BECHTLE"            39.353    .0991521303599316  20.2
      20544 "BECHTLE"              45.1   13.631003566942654  20.4
      20573 "BECHTLE"             45.43    .7290433262679274  20.5
      20605 "BECHTLE"            49.935    9.454947992378598  21.8
      20635 "BECHTLE"             47.25   -5.526950575534626  20.6
      20664 "BECHTLE"              51.7    9.000512757463165  22.6
      20697 "BECHTLE"            50.255   -2.834773703768023  21.1
      20727 "BECHTLE"            50.219  -.07166033310221734    21
      20758 "BECHTLE"            47.628   -5.297261755675234    20
      20788 "BECHTLE"            44.034   -7.845876079794796  18.1
      20818 "BECHTLE"             49.61   11.923036352197686  20.3
      20850 "BECHTLE"            47.858  -3.5954133255833374  19.6
      20878 "BECHTLE"            48.753     1.85284409548572  19.8
      20909 "BECHTLE"            50.685   3.8863274822381855  20.6
      20937 "BECHTLE"            52.809    4.105162189758192  21.5
      20970 "BECHTLE"            57.406    8.346719674746094  22.7
      21000 "BECHTLE"             56.45  -1.6793536883734677  22.3
      21031 "BECHTLE"                61    7.751857357764015  24.1
      21062 "BECHTLE"            57.717   -5.532210667036604  22.2
      21091 "BECHTLE"            63.375    9.351770440840207  24.4
      21123 "BECHTLE"              68.8    8.213428302795078  26.5
      21153 "BECHTLE"            71.699    4.127305556762173  26.7
      21182 "BECHTLE"              69.9   -2.541115126715499    26
      21215 "BECHTLE"              73.6    5.157937649506588  27.4
      21243 "BECHTLE"             70.65   -4.090691660429393  25.9
      21273 "BECHTLE"             65.95   -6.884122996721291  24.2
      21304 "BECHTLE"             69.75    5.602054153815127  25.6
      21335 "BECHTLE"              75.1    7.390313806861387  26.9
      21364 "BECHTLE"             66.05  -12.840852780175476  23.6
      21396 "BECHTLE"              76.8   15.079260918529217  27.5
      21427 "BECHTLE"              88.4   14.066732948997132  30.3
      21455 "BECHTLE"             87.85   -.6241155010491644  30.1
      21488 "BECHTLE"              78.2  -11.636116708184066  26.8
      21518 "BECHTLE"              71.6    -8.81745735846656    24
      21549 "BECHTLE"              68.1   -5.011786081113324  22.8
      20482 "AXEL SPRINGER"      48.081   -3.913591674859801  23.2
      20513 "AXEL SPRINGER"      46.345  -3.6773677351752125  22.4
      20544 "AXEL SPRINGER"      47.447   2.3499887282605245  18.7
      20573 "AXEL SPRINGER"      48.992    3.204372086487185  19.3
      20605 "AXEL SPRINGER"      51.305   4.6131193838899245  12.5
      20635 "AXEL SPRINGER"      47.332   -8.060161379185999  11.5
      20664 "AXEL SPRINGER"      49.292    4.057519655356442    12
      20697 "AXEL SPRINGER"      45.875   -7.184148970114237  11.1
      20727 "AXEL SPRINGER"      44.957  -2.0213828994223504  10.9
      20758 "AXEL SPRINGER"      45.533   1.2730860417056522    11
      20788 "AXEL SPRINGER"      40.335  -12.121775940862964  11.3
      20818 "AXEL SPRINGER"      45.977   13.092169305847484  12.9
      20850 "AXEL SPRINGER"      47.681   3.6391724244110484  13.4
      20878 "AXEL SPRINGER"        49.3    3.339108535712125  13.8
      20909 "AXEL SPRINGER"      51.441    4.251143881617959    13
      20937 "AXEL SPRINGER"      51.878    .8459288339172959  13.1
      20970 "AXEL SPRINGER"      56.267    8.121340944281846    23
      21000 "AXEL SPRINGER"      53.237   -5.535457431761467  21.8
      21031 "AXEL SPRINGER"       53.61    .6981974510245517    22
      21062 "AXEL SPRINGER"      51.963   -3.120369098319357  20.8
      21091 "AXEL SPRINGER"      53.598    3.097982709150413  21.4
      21123 "AXEL SPRINGER"      58.058    7.993075693174593  23.2
      21153 "AXEL SPRINGER"       66.15   13.048237968146198  32.6
      21182 "AXEL SPRINGER"      65.268  -1.3423020332140774  32.2
      21215 "AXEL SPRINGER"       70.35   7.4980913331573875  34.7
      21243 "AXEL SPRINGER"        73.5    4.380262265839305  36.2
      21273 "AXEL SPRINGER"       67.35   -8.738250336241812    21
      21304 "AXEL SPRINGER"       67.75     .592155690343748  21.2
      21335 "AXEL SPRINGER"        62.3   -8.386303396640285  17.6
      21364 "AXEL SPRINGER"        62.1  -.32154368539743544  17.6
      21396 "AXEL SPRINGER"        64.2    3.325722175648234  16.8
      21427 "AXEL SPRINGER"        62.7  -2.3641763057040426  16.4
      21455 "AXEL SPRINGER"        57.6   -8.483887993702943    15
      21488 "AXEL SPRINGER"       56.95  -1.1348877808654396  14.9
      21518 "AXEL SPRINGER"        56.5   -.7933051740795989  14.1
      21549 "AXEL SPRINGER"       49.62  -12.984665988874038  12.4
      end
      format %tdnn/YY Date

      Comment


      • #4
        Thanks for the data example. Here's code to create the portfolios. (In your example data the portfolio sizes are not particularly in accord with the 30% and 70% figures because there are only a small number of stocks in each year and they do not evenly divide up at 30% and 70%. In a larger data set this should not be a problem.

        Code:
        //  DIVIDE DATA INTO TIME PERIODS BEGINNING IN JUNE OF EACH YEAR
        gen period = year(Date)
        replace period = period-1 if month(Date) < 6
        gen byte base_month = month(Date) == 6
        
        //  IDENTIFY STOCK BASED ON REALIZATION OF PRICE AT START OF EACH PERIOD
        by Stock period (Date), sort: egen base_price = max(cond(base_month, Price, .))
        by Stock period (Date): replace base_price = . if _n > 1
        
        //  CREATE PORTFOLIOS
        gen thresh30 = .
        gen thresh70 = .
        label define portfolio  1   "Low"   2   "Medium"    3   "High"
        gen byte portfolio = .
        levelsof period, local(periods)
        foreach p of local periods {
            _pctile base_price, percentiles(30 70) 
            replace thresh30 = r(r1) if period == `p' & base_month
            replace thresh70 = r(r2) if period == `p' & base_month
            replace portfolio = 1 if period == `p' & Price < thresh30 & base_month
            replace portfolio = 2 if period == `p' & inrange(Price, thresh30, thresh70) ///
                & base_month
            replace portfolio = 3 if period == `p' & inrange(Price, thresh70, .) & base_month
        }
        by period Stock (Date), sort: replace portfolio = portfolio[1]
        As for calculating the return on the portfolio, I do not know how that is done. I don't work in finance. I know how to calculate the return on a single stock. But as a portfolio is a mix of stocks, each of which can perform differently, I don't know how you arrive at the overall return from the individual stock returns. What makes sense to me is to assume purchase of a single share of each stock, calculate the total initial value, then calculate the total value of those same shares at the end of the period and get the return from the ratio. That's what the code below does, but if this isn't how financiers thing about it, I'll leave it to you to implement the correct definition.

        Code:
        //  CALCULATE EACH PORTFOLIO'S RETURN
        //  START BY GETTING THE INITIAL TOTAL VALUE
        sort period portfolio Date
        by period portfolio (Date), sort: egen initial_value = total(base_price)
        by period portfolio (Date): egen final_value = total(cond(final_month, Price, .))
        by period portfolio (Date): replace final_value = . if !base_month
        gen pct_return = (final_value/initial_value - 1) * 100
        Evidently if you want to base your portfolios on some other attribute of the stock than its initial price, it's a simple matter of changing variables in some of the commands, and I leave that to you as well.

        Comment

        Working...
        X