Announcement

Collapse
No announcement yet.

Draw a MA for the last X bars

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Draw a MA for the last X bars

    I was wondering how to draw a moving average for only the last 20 bars.

    Probably need to create and array and then return it for plotting.

    Any ideas?

  • #2
    Re: Reply to post 'Draw a MA for the last X bars'

    This code does it.


    if(getCurrentBarIndex()>-21)
    return new
    Array(mean+sdDev,mean+(sdDev*2),mean+(sdDev*3),mea n-sdDev,mean-(2*sdDev),mea
    n-(3*sdDev),mean)
    else return


    ----- Original Message -----
    From: <[email protected]>
    To: <[email protected]>
    Sent: Wednesday, February 05, 2003 4:18 PM
    Subject: Reply to post 'Draw a MA for the last X bars'


    > Hello rhbishop,
    >
    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    >

    Comment


    • #3
      I know I am close, what am I missung?

      var study=new MAStudy(20,0,"Close",MAStudy.EXPONENTIAL);
      var new array ma20;

      function preMain()
      {
      setPriceStudy(true);
      }

      function main()
      {
      ma20=study.getValue(MAStudy.MA,0);
      if(getCurrentBarIndex()>-19)
      return new Array(ma20);
      else return
      }

      Comment


      • #4
        bingo

        var study=new MAStudy(20,0,"Close",MAStudy.EXPONENTIAL);


        function preMain()
        {
        setPriceStudy(true);
        }

        function main()
        {
        ma20=study.getValue(MAStudy.MA,0);
        if(getCurrentBarIndex()>-19)
        return ma20;
        else return
        }

        Comment


        • #5
          hmmm...

          every time a new bar is drawn, the ma line gets longer, doesnt stay at the same length.

          Comment


          • #6
            I think the ONLY two ways you can do what I think you are trying to do is:

            1) Use the drawline command to plot the MA (this will look clunky and not be able to give you the MA values in the cursor window). But you could draw one line between each bar and then delete the oldest (22nd) line as a new line is drawn.

            2) This may or maynot work. You will need 545 I think to even try it.

            setBar(TYPE(Value|Color|Style|Thickness, BarOffset, Series, Value);

            to redraw existing info
            Garth

            Comment


            • #7
              I am trying to draw a ma withonly the las x number of data points, like this



              Do you mean

              setBar(LINE(MAStudy.MA|Color.red|DOTTED|1, 0, 50, vEMA50);

              Comment


              • #8
                Solution....

                only plot MA*2 bars and then use the standard MA function

                for a ma20, only plot 40 bars

                the first 20 dont have the data required, therefore the ma is blank, the rest of thbars have enuf data and they plot normal.

                hehe

                Comment

                Working...
                X