Announcement

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

  • Creation of Compound Interest variable

    Good evening everyone,

    I do have the closing prices NAV (net asset value) and I calculate the return as follows: nav_ret=ln(nav/nav[_n-1]. I try to find a way to create a new variable that compounds the interest on daily basis starting as base the value 100. In the data example variable base has to start with 100 and continue till the end. When I try to loop like this all values in base are reported as missing.

    Code:
    levelsof n, local(ns)
    foreach x in `ns' {
    replace base=base[_n-1]*nav_ret+base[_n-1]
    }

    Code:
    input str7 ticker double nav float(datenum nav_ret n base)
    ""             .     .            .  1       100
    "CBDJIUS" 106.12 18415    .02239362  2 102.23936
    "CBDJIUS" 101.64 18420   -.01163998  3  101.0493
    "CBDJIUS" 102.95 18421   .012806275  4 102.34337
    "CBDJIUS" 102.52 18422 -.0041855318  5         0
    "CBDJIUS" 105.34 18423    .02713531  6         0
    "CBDJIUS" 105.77 18424   .004073711  7         0
    "CBDJIUS" 105.55 18427 -.0020821511  8         0
    "CBDJIUS" 107.76 18428    .02072176  9         0
    "CBDJIUS" 107.81 18429 .00046388645 10         0
    "CBDJIUS" 108.07 18430   .002408747 11         0
    end
    format %td datenum
    In the code above the base the obsevations 2/5 are calculated manually by command:

    Code:
    replace base=base[_n-1]*nav_ret+base[_n-1] in 2
    replace base=base[_n-1]*nav_ret+base[_n-1] in 3
    replace base=base[_n-1]*nav_ret+base[_n-1] in 4
    Is there any way to fix the loop?

  • #2
    Christov:
    welcoem to this forum.
    Probably a -loop- is not necessary:
    Code:
    . replace base=(base[_n-1]*nav_ret)+base[_n-1] if n>1
    (8 real changes made)
    
    . list
    
         +----------------------------------------------------------+
         |  ticker      nav     datenum     nav_ret    n       base |
         |----------------------------------------------------------|
      1. |                .           .           .    1        100 |
      2. | CBDJIUS   106.12   02jun2010    .0223936    2   102.2394 |
      3. | CBDJIUS   101.64   07jun2010     -.01164    3   101.0493 |
      4. | CBDJIUS   102.95   08jun2010    .0128063    4   102.3434 |
      5. | CBDJIUS   102.52   09jun2010   -.0041855    5    101.915 |
         |----------------------------------------------------------|
      6. | CBDJIUS   105.34   10jun2010    .0271353    6   104.6805 |
      7. | CBDJIUS   105.77   11jun2010    .0040737    7   105.1069 |
      8. | CBDJIUS   105.55   14jun2010   -.0020822    8   104.8881 |
      9. | CBDJIUS   107.76   15jun2010    .0207218    9   107.0616 |
     10. | CBDJIUS   107.81   16jun2010    .0004639   10   107.1112 |
         |----------------------------------------------------------|
     11. | CBDJIUS   108.07   17jun2010    .0024087   11   107.3692 |
         +----------------------------------------------------------+
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Thank you very much Carlo, it worked perfectly

      Comment

      Working...
      X