Announcement

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

  • Create global list of variable based on newly generated variables

    Hi,

    I am creating portfolio statistics like mean, sd, Sharpe ratio, maximum drawdown of a portfolio. I need to generate some of these variables with "foreach" loop for all varialbes (assets) in my portfolio. Eventually, I need to report all these statistics in one table with mean, sd, Sharpe ratio, maximum drawdown etc being each one column.

    I am using "collapse" to present them. I would like to be able to create a "global" list (preferred over "local" here) based on the assets I have. I would like to avoid re-writting all variables in collapse, this is why I need this need "global" varaible list. Here is what I have:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double(w_date closefomo closedevelopersentiment closepessimism)
    3212    .07453440740494499    .07453440740494499    .07453440740494499
    3213   -.12104729462116559   -.05201545157504417   -.12104729462116559
    3214  -.034797890992079195   -.05411629014735886   -.11340634820038664
    3215   .011186391042544708   .022268406133403085   .011186391042544708
    3216    .01442807681633651    .01442807681633651    .01442807681633651
    3217    .07321094534982153  -.016004414985879678   -.05587384220797653
    3218   -.05042888582488991   -.08339684986714901  -.006944377096642973
    3219   .008064042791908532  -.037045037442540285   .008064042791908532
    3220    .12677377475687024      .178091619267851    .11909845491875082
    3221    .07834035932331074    .07834035932331074  -.015685290083635728
    3222  -.003303362499604834    .11903634809715102    .08976012322106208
    3223   -.04559286214390024   -.07660821612687418    -.0823828864839693
    3224   -.05161701318027412   .021547596903848998   .021547596903848998
    3225    .02725055517409758    .02725055517409758   -.05707910780475145
    3226   -.14373829377100167    .08334165322072318   -.08448592390017129
    3227    .01950549087114939   .003796932513457696   .003796932513457696
    3228    .07271596820060244    .17665732493033137    .05536087792409927
    3229  -.011911920723855052   -.06304488456870827   .002239803417027675
    3230     .0849388925900818    .10514921612378347    .03464489919375757
    3231     .2050493607730357    .05188387167613029    .13991013702829158
    3232    .19393952803943604   -.23822083732244537    .02101914346846878
    3233   -.15433957052055425     -.126329399135237     -.126329399135237
    3234    .12716529163855683    .01867949139062519    .12716529163855683
    3235   -.01831011454504128   -.01831011454504128   -.01831011454504128
    3236    .03558431752118363    .03558431752118363    .03558431752118363
    3237   .030915859293264347   -.06119444222594983   -.01671360531845656
    3238   -.13017132959121602   -.10649383356950004   -.13017132959121602
    3239  -.008806019483937888  -.008806019483937888    .03262558232454449
    3240    -.1269795770938903   -.11512173861913977   -.16653474360444265
    3241    .14397747230418978  -.009173662350564049    .13200046664218568
    3242    -.0684615636891871   -.13957673415081373   -.25379167823805604
    3243 -.0025321302829600656 -.0025321302829600656 -.0025321302829600656
    3244   .015844499110792887    .10136103797062268   .015844499110792887
    3245   -.07182316496899077   -.07182316496899077   -.07982053845106007
    3246   -.07085856075429131    -.0580232536544697    -.1380754826219186
    3247     .1557392487308189    .07806730828430042     .1557392487308189
    3248    -.1477735357864235   -.09861944322280658   -.15947652997349748
    3249   -.01752091177592452   .002668665407705051   -.04824094141019675
    3250   -.07754247057551322    .09071221534441887   -.10969708920476251
    3251    .08309057192733027    .05539430882162466    -.1347680462146238
    3252   -.06827365688795772      -.14687697956724    -.1402755906003561
    3253   .018492047642685527    .07209301576101934    .17948664237544087
    3254   -.06499132609853475   -.03940453641397523     .0193562259769835
    3255    .12007980064812895   -.02509725660272555    .12007980064812895
    3256    .11103709828468888    .09936186338650721   -.08424940236702583
    3257    .09927327692340171   .003633102772544472   .061695785662832625
    3258   -.05546984445001801  -.023511795899748278   -.01574799537624305
    3259    .22254722595741883  -.007981017482885489    .18878289918137625
    3260     .1159654972598243    .16974198085996672    .17935448687315292
    3261    .09976332276673672   .021585930852165314    .17583417340588636
    3262   -.04810697538296471   -.03365803778112453   .010562292208397217
    end
    format %tw w_date
    Code:
    foreach var in $strat /*mktrf*/ {    
        egen m_`var' = mean(`var')
        egen sd_`var' = sd(`var')
        replace m_`var' = m_`var'*52
        replace sd_`var' = sd_`var'*sqrt(52)
    }
    
    * I need to make sure these variables are ordered consistently
    ** I would like something that gives the following:
    ** global strategy m_closefomo m_closedevelopersentiment m_closepessimism
        order m_closefomo m_closedevelopersentiment m_closepessimism
    
    * This is why I would like to have a global list $ here (and not collapse m_*, so I am certain about this order, instead of re-writting the list
        collapse (mean) m_closefomo m_closedevelopersentiment m_closepessimism

    Someone can help? Thanks!

  • #2
    You can just create the macro within your loop:


    Code:
    global strategy  // initialise as empty
    foreach var in $strat /*mktrf*/ {    
        egen m_`var' = mean(`var')
        egen sd_`var' = sd(`var')
        replace m_`var' = m_`var'*52
        replace sd_`var' = sd_`var'*sqrt(52)
        global strategy $strategy m_`var'
    }
    This code goes with the caveat that local macros should preferably be used instead of global ones.

    Comment


    • #3
      Excellent, thank you so much, Hemanshu!

      Comment


      • #4
        Say that I would like to run "N" regression with "N" being the number of variables in $strategy and each of them being the dependent variable of that regression.

        How would I loop over each strategy in the $strategy global variable? How can I get the count of the strategies in that global variable? Thank you in advance

        I have an incomplete version of the code
        Code:
        foreach n /* missing stuff here */
            local n_strategy : word 1 of $select_strategies
            newey `n_strategy' BTC, lag(2)
            newey `n_strategy' BTC mktrf, lag(2)
            newey `n_strategy' BTC mktrf smb hml rmw cma, lag(2)

        Comment


        • #5
          If you don't really care for the number n per se, you could simply do something like
          Code:
          foreach strat of global select_strategies {
              newey `strat' BTC, lag(2)
             ...
          }
          If you do want to use the number n for any purpose, then another way is something like this:
          Code:
          local n: word count $select_strategies
          dis "We have `n' strategies selected!"
          forval i = 1/`n' {
              local strat: word `i' of $select_strategies
              newey `strat' BTC, lag(2)
              ...
          }

          Comment

          Working...
          X