Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

MA direction change counter

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

    MA direction change counter

    I want to build an indicator that will count the number of times in a given period that an MA changes direction from a positive slope to a negative slope or otherwise. I want to use 3 MA's of different periods and possibly type, and weight the value of each ones change differently. A greater value from the indicator should mean more volatility exists in the market and trends are more likely to reverse and basically predicts choppiness. Can anyone think of a logic structure or method that would allow this or already know of such an indicator?

    Basically it should be a way to count over a given time how many instances did N[0] > N[1] change to N[1] > N[0] and the opposite.

    Thanks
    Last edited by JamesF; 06-09-2009, 10:26 PM.

    #2
    JamesF, did you already look into CountIf() for this? http://www.ninjatrader-support.com/H...6/CountIf.html
    BertrandNinjaTrader Customer Service

    Comment


      #3
      I looked into the countif method. I don't think it will help me. To determine if the direction of the moving average has changed i would need to employ something like this.
      (if( EMA(ma1period)[1]> EMA(ma1period)[2]){if(EMA(ma1period)[1]>EMA(ma1period)[0]){n=n+1;}else{n=n;};}

      else if( EMA(ma1period)[2]< EMA(ma1period)[1]){if(EMA(ma1period)[0]>EMA(ma1period)[1]){n=n+1;}else{n=n;};}

      so that if there is a change in ma direction, n will be added to by +1. My question is how to do this to a whole series of data over a given period so that the n value will accumulate +1 for ever change in direction and be plottable. Thanks.

      Comment


        #4
        JamesF,

        Your indicator already goes through the whole series of data points. It does so in a sequential manner starting at the oldest bar and moving forward, each time incrementing your count if you want it to.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Yes, but I want it at any given time to only add up the directional changes for a given period, say the last 20 bars. Is this possible?

          Comment


            #6
            You will need to program it. If you want to look at last 20 bars, make a loop that only checks 20 bars.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              What is the syntax for that?

              Comment


                #8
                Please see this article: http://www.ninjatrader-support.com/H...gCommands.html
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  it is not plotting,

                  int n = 0;
                  int x = 0;
                  do{
                  if( EMA(ma1period)[1]> EMA(ma1period)[2]){if(EMA(ma1period)[1]>EMA(ma1period)[0]){n=n+1; x = x+1;}else{n=n; x = x+1;};}
                  else if( EMA(ma1period)[2]< EMA(ma1period)[1]){if(EMA(ma1period)[0]>EMA(ma1period)[1]){n=n+1; x = x+1;}else{n=n; x = x+1;};}
                  }
                  while (x < lookbackperiod);


                  Plot0.Set(n);

                  does n need to be data series or something?

                  Comment


                    #10
                    Please check your Control Center logs for errors. You may have run into the issues discussed here: http://www.ninjatrader-support2.com/...ead.php?t=3170
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      Ok, that made it plot, but it is only making one blip every time there is a directional change in the ma. I want it to tally up all the directional changes in the last 20 bars and plot that number. how do i do this?

                      Comment


                        #12
                        You need to debug your code. You are likely resetting it back to zero on each OnBarUpdate() method. Don't declare your variable there. Do it inside the Variables region of your code.

                        private int n = 0;
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          Josh, Thanks alot, that made it tally them up and they are plotting, there is one more problem, it is adding all the directional changes on the chart up instead of just the last 20 bars.

                          if (CurrentBar < 2)
                          return;



                          int x = 0;
                          do
                          {
                          if( EMA(ma1period)[1]> EMA(ma1period)[2]){if(EMA(ma1period)[1]>EMA(ma1period)[0]){n=n+1; x = x+1;}else{n=n; x = x+1;};}
                          else if( EMA(ma1period)[2]< EMA(ma1period)[1]){if(EMA(ma1period)[0]>EMA(ma1period)[1]){n=n+1; x = x+1;}else{n=n; x = x+1;};}
                          }
                          while(x < lookbackperiod);


                          Plot0.Set(n);
                          }

                          Comment


                            #14
                            JamesF,

                            Unfortunately you will have to take it from here. You should print the value of your variables at each step and see when you want to plot it. When you decide the point of plotting you should do the .Set() right there. After that you should reset the variable back to zero for the next counting.
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              Ok, Josh, I figured it out. To obtain the value for the last 20 bars, I must simply take N[0] - N[lookbackperiod]. How do you obtain the value of a variable from a given amount of bars back? It wont let me apply [0] or [lookbackperiod] to an integer.


                              int draw = n[0] - n[lookbackperiod];

                              Plot0.Set(draw);

                              produces the error message : Cannot apply indexing with [] to an expression of type 'int'

                              Thanks, josh, youve been very helpful

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by bortz, 11-06-2023, 08:04 AM
                              47 responses
                              1,604 views
                              0 likes
                              Last Post aligator  
                              Started by jaybedreamin, Today, 05:56 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post jaybedreamin  
                              Started by DJ888, 04-16-2024, 06:09 PM
                              6 responses
                              18 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by Jon17, Today, 04:33 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post Jon17
                              by Jon17
                               
                              Started by Javierw.ok, Today, 04:12 PM
                              0 responses
                              13 views
                              0 likes
                              Last Post Javierw.ok  
                              Working...
                              X