Announcement

Collapse
No announcement yet.

moving trend indicator

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

  • moving trend indicator

    in the following formula by ts support how can i turn the trend line into a variable which can then be used in a statement . ?
    if you know what i mean,

    /************************************************** *****************
    Provided By : TS Support, LLC for eSignal. (c) Copyright 2002
    ************************************************** ******************/


    function preMain()
    {
    setStudyTitle("Movtrend");
    setCursorLabelName("Movtrend", 0);
    setDefaultBarFgColor(Color.blue, 0);
    setDefaultBarThickness(2);
    setPriceStudy(true);

    }

    function main(n) {
    if(n == null)
    n = 20;
    var sum = 0;
    var i = 0;
    var mt = 0;

    for(i = n; i > 0; i--)
    sum += (i - (n + 1) / 3) * close(i - n);
    wt = 6 / (n * (n + 1)) * sum

    return wt;
    }

  • #2
    shogun
    First of all you need to change one thing in the efs and that is
    var mt = 0; should be var wt = 0;
    Once you have done that wt is the variable you would use in the conditions. For example
    if(close()>wt)
    setBarFgColor(Color.lime);

    However as it is written now you cannot address historical values for wt so you would need to use ref() or create an array depending on the lookback you need. There is also a simpler method if all you need is two or three bars back
    Alex.

    Comment


    • #3
      alexis many thanks i was wondering what the variable "mt" was for, in the original code,

      the following code works fine even with the backtester, my next problem is i am not sure how to use "ref()" within the efs as i would like to be able to use the following statement "

      ( wt > wt -1 ) && ( wt -1 > wt -2) " this currently does not run as you have already mentioned it would not.


      /************************************************** *****************
      Provided By : TS Support, LLC for eSignal. (c) Copyright 2002
      ************************************************** ******************/
      var vLastAlert = -1;

      function preMain()
      {
      setStudyTitle("Movtrend js");
      setCursorLabelName("Movtrend js", 0);
      setDefaultBarFgColor(Color.blue, 0);
      setDefaultBarThickness(2);
      setPriceStudy(true);

      }

      function main(n) {
      if(n == null)
      n = 20;
      var sum = 0;
      var i = 0;
      var wt = 0;

      for(i = n; i > 0; i--)
      sum += (i - (n + 1) / 3) * close(i - n);
      wt = 6 / (n * (n + 1)) * sum

      if (wt < close()){
      setPriceBarColor(Color.RGB(0,255,0));
      if (vLastAlert != 1) Strategy.doLong("", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT, 0);
      if (vLastAlert != 1) drawShapeRelative(1, low(), Shape.UPARROW, "", Color.RGB(0,0,0), Shape.LEFT);
      vLastAlert = 1;
      }
      if (wt > close()){
      setPriceBarColor(Color.RGB(255,0,0));
      if (vLastAlert != 2) Strategy.doShort("", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT, 0);
      if (vLastAlert != 2) drawShapeRelative(1, high(), Shape.DOWNARROW, "", Color.RGB(0,0,0), Shape.LEFT);
      vLastAlert = 2;
      }

      return wt;
      }

      Comment


      • #4
        shogun
        If all you need is two or three historical values then it is probably easier to use the following
        Declare the wt variable outside of main and at the same time declare two new variables called wt1 and wt2 (also outside of main) and set them all equal to null. Remember then to delete the var wt = 0; that is in main.
        Then at the very beginning of the main section add
        if(getBarState()==BARSTATE_NEWBAR){
        wt2=wt1;
        wt1=wt;
        }

        At this point you have created the historical values for wt for one bar ago (wt1) and two bars ago (wt2)
        Hope this helps
        Alex

        Comment


        • #5
          i think i have worked it out by adding the following ,

          var BarCntr = 0;
          if (getBarState() == BARSTATE_NEWBAR) {
          if (BarCntr > 20) {



          is this the correct way to use "ref() " i have run it through the backtester and it seems to work ok, the finished workings are below,

          /************************************************** *****************
          Provided By : TS Support, LLC for eSignal. (c) Copyright 2002
          ************************************************** ******************/
          var vLastAlert = -1;

          function preMain()
          {
          setStudyTitle("Movtrend js");
          setCursorLabelName("Movtrend js", 0);
          setDefaultBarFgColor(Color.blue, 0);
          setDefaultBarThickness(2);
          setPriceStudy(true);

          }

          var BarCntr = 0;

          function main(n) {

          if (getBarState() == BARSTATE_NEWBAR) {

          BarCntr += 1;

          }


          if(n == null)
          n = 20;
          var sum = 0;
          var i = 0;
          var wt = 0;

          for(i = n; i > 0; i--)
          sum += (i - (n + 1) / 3) * close(i - n);
          wt = 6 / (n * (n + 1)) * sum

          if (BarCntr > 20) {
          var wt1 = ref(-1);
          var wt2 = ref(-2);
          }



          if ((wt > wt1) && (wt1 > wt2)){
          setPriceBarColor(Color.RGB(0,255,0));
          if (vLastAlert != 1) Strategy.doLong("", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT, 0);
          if (vLastAlert != 1) drawShapeRelative(1, low(), Shape.UPARROW, "", Color.RGB(0,0,0), Shape.LEFT);
          vLastAlert = 1;
          }
          if ((wt < wt1) && (wt1 < wt2)){
          setPriceBarColor(Color.RGB(255,0,0));
          if (vLastAlert != 2) Strategy.doShort("", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT, 0);
          if (vLastAlert != 2) drawShapeRelative(1, high(), Shape.DOWNARROW, "", Color.RGB(0,0,0), Shape.LEFT);
          vLastAlert = 2;
          }

          return wt;
          }
          Last edited by shogun; 05-11-2004, 05:03 AM.

          Comment


          • #6
            alexis many thanks i think i must have been writing my last post at the same time as you, i will rewrite the efs in the format you have just stated,

            Comment


            • #7
              shogun
              The ref() method you used works just as well. The solution I proposed is just an easier way to grab a few bars back.
              One thing you may want to change in your efs is the following
              if (BarCntr > 20) {
              replace with
              if (BarCntr > n+1) {
              This way if you change the length of wt you are always ensured to have the appropriate amount of data otherwise you may get an error
              Alex

              Comment


              • #8
                alex i have changed the (BarCntr > 20) to (BarCntr > n+1) and the efs runs fine, thanks.

                how many bars back could i go before the efs would have problems running,

                Comment


                • #9
                  shogun
                  You mean using ref()?
                  I believe you should be able to retrieve the value of any past bar with ref() so long as there are at least n+1 bars in the chart (in your efs that would correspond to 21 bars as default).
                  As an example I set the counter to be > n+10 which means there need to be at least 30 bars. In the image below notice in fact that the Csr# is at 30 (it was zeroed on the first bar) before the efs actually begins painting the bars even though there are already more than three values of wt before bar index 30.
                  Alex

                  Comment


                  • #10
                    alex thats great if i need to ever go back more bars than "n" i will change the counter as you have done ,

                    Comment


                    • #11
                      shogun
                      You don't need to do that in as much as n is already defined by the length of wt in if(n==null) n=20. The moment you change the length of wt through Edit Studies you are also changing the value needed in the counter (plus 1). That is why I suggested a change from the fixed value you had set at 20 to one that changes according to your needs.
                      Alex

                      Comment


                      • #12
                        ok thanks for your time alex, you have been a great help in helping me to understand the " ref() " function in efs,

                        i have another topic on how to create a signal of the 2% move from the swing low/high i don't suppose you have any thoughts on how i should go about it.

                        Comment


                        • #13
                          shogun
                          I have not looked into that. Irrespective you may want to check realtimeswings.efs that JasonK wrote and use that as the base for what you want to do.
                          Alex

                          Comment


                          • #14
                            thanks i will do

                            Comment

                            Working...
                            X