Announcement

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

  • Moving Averages

    Hi,

    I am newly stuck on something I am working on. And so i really need your precious help.
    I have a dataset of closing prices of a stock(nbmps) on which i want to calculate the moving averages and the upper and lower bands such as the bollinger band indicator.
    I found somewhere few hints about what i am trying to do and i write my code as follow:

    gen moveave1= (F1.nbmps+nbmps+L1.nbmps)/4

    gen upperband= moveave1*1.04

    gen lowerband= moveave 1*0.96


    Here's the problem:

    line nbmps moveave1 uppperband lowerband Data

    when I plot the drawing shows me 4 lines (1 of prices and it's ok) and the other 3 are give or take equal, they do not differ in position, they are overlaid.


    I read that usually to generate the upper band somebody use 0.04+SMA.
    But i don't know, even because I am a newbie user of stata so maybe I am writing something wrong.

    All answers are welcome
    Thank for your collaboration

    Chris

  • #2
    It's hard to see that you have a real problem here. A moving average with weights 1:2:1 (Hanning) may well be close to the original data. It's really not smoothing very much. Similarly, your extra variables are by design just 4% larger and 4% smaller than the smooth, so why be surprised that they are close?

    EDIT: You omitted the factor 2 on the middle value. Dividing the sum of 3 values by 4 makes no sense otherwise.
    Last edited by Nick Cox; 03 Sep 2016, 08:58.

    Comment


    • #3
      HI Chris. You can use the filter function from the egenmore commmand by Nic Cox.
      Install egenmore from ssc (ssc install egenmore). If your dataset has a variable called price and another time variable called period, you can do the following:

      tsset period
      egen ma=filter( price), lags(0/3) c(0.25 0.25 0.25 0.25)
      tsline period ma

      Now you can create other 2 variables for the upper and lower bands by adding/substracting any factor you want. If you want to use the standard deviation for the upper/lower bands, you can use the sd function from egen:
      egen sd=sd(price)

      I hope this help.

      Alberto

      Comment


      • #4
        Alberto: Thanks for the publicity, but the code in #1 looks fine for smoothing (with the important correction of a factor of 2: see my EDIT please!!!).

        Chris appears puzzled that three lines are close together but the values of two variables are by design just 4% larger or smaller.

        Comment


        • #5
          Thank you for the answers.
          As you can notice probably i made some mistakes in calculating the bands. Hence, if there's a package that can I use to exclude my own fault it's better. I am going to try what Alberto said about the egenmore command by Nick Cox, and i will share with you my results.
          Thank you both again for the help.



          P.S: egen ma=filter( price), lags(0/3) c(0.25 0.25 0.25 0.25)
          What the bold part stands for?

          P.p.s: Nick you were right i divided by 4 that was nosense, probably i misclick on the keyboard
          Last edited by Chris Nova; 04 Sep 2016, 04:01.

          Comment


          • #6
            The filter() function in egenmore was of some use about 15 years ago before Stata introduced better smoothing commands but it has no advantages for you here. I am not disparaging others' work here, as I wrote it.


            The only problem I can detect in your approach is that you omitted a factor 2.

            Code:
            gen moveave1= (F1.nbmps+2 * nbmps+L1.nbmps)/4
            is fine as a 1:2:1 weighted smoother.

            Whether it's good for your purposes I can't say. I don't know what a "bollinger [?Bollinger] band" is.

            But it's not at all the same smoother as Alberto is discussing. I imagine he was just giving you an example, and not at all saying "This is the smoother you should use".

            Comment


            • #7
              Click image for larger version

Name:	statalistexample.png
Views:	1
Size:	13.3 KB
ID:	1355406

              Ok thanks that's the final result obtained with your suggestion, it seems great.

              Another little question, if i would like to zoom in a specific portion(e.g. 1 oct 15 to 1 jan 16), where can I find the option?

              Sorry for the easy question but i didn't find any similar on the web.

              Thanks again

              Comment


              • #8
                Why look on the web? Help is on your system bundled with Stata. For example, the help for tsline gives an example of selecting part of a series from which it follows that something like

                Code:
                tsline nbmps moveave1 upperband lowerband if tin(1oct2015,1jan2016)
                is what you want, but you must tsset first if you have not yet done so.

                Code:
                help tsset

                Comment


                • #9
                  Ok, now i can continue my work.
                  Thank you for your support.

                  Best regards

                  Chris

                  Comment

                  Working...
                  X