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

Flat indicators

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

    Flat indicators

    Hello
    Is there a way I can't think of to capture the flat parts of an indicator as one single reading?
    Something like:

    if DMI1[1] < DMI1[2] && DMI1[2] = DMI1[3] = ...= DMI1[x] && DMI1[x] > DMI1[x-1]

    then DMI1SwingHigh[0] = DMI1[2]
    Last edited by itrader46; 12-02-2019, 09:32 AM.

    #2
    Hello itrader46,

    Thanks for your post.

    I would suggest printing out the values of the indicator on a bar by bar basis and then checking the values in the areas you perceive as flat to valid your concept of equality (IE: DMI1[2] == DMI1[3]). As the indicator is a result of calculations and is of double type, you may find that what shows as flat on the chart may not actually be flat (or exactly equal).

    You may want to look at using the Slope() method: https://ninjatrader.com/support/help...nt8/?slope.htm

    Other alternatives would be to define a range of indicator movement that you can define as flat and check however many bars back to see if the indicator is ranging between the high and low values you establish as the flat range. You would use the MIN() and MAX() methods for this:


    Paul H.NinjaTrader Customer Service

    Comment


      #3
      I found the code below kindda gives me the data I need, but I'm struggling to find the value of DMI at the first bar it occurred (flatDMIbar), as the Get value method gives me the error below.

      Code:
      if (IsFirstTickOfBar)
                          {                    
                              if (Math.Abs(DMI1[1] - DMI1[2]) < 0.0001)   // If DMI[1] = DMI[2] start recording DMI flat parts
                              {
                                  FlatDMI[0] = DMI1[1];                            
                                  FlatInd = true;
                                  if (!FlatIndBar)
                                  {
                                      flatDMIbar = CurrentBar - 2;
                                      FlatIndBar = true;                                
                                  }          
      Print(string.Format(@"
      Flat DMI Bar = {0}, Flat Ind = {1}, FlatDMI[0] = {2 :F4}, FlatDMI[i-1] = {3 :F4}
      i = {4}, CurrentBar = {5}, CurrentBar - 1 = {6}, FlatDMI[1] = {7 :F4}, CurrentBar - i = {8}, DMI[flatDMIBar] = {9 :F4}
      ", flatDMIbar, FlatInd, FlatDMI[0], FlatDMI[i-1], i, CurrentBar, CurrentBar - 1, FlatDMI[1]/*, DMI(14).GetValueAt(flatDMIbar)*/)); // GetValue Error on calling 'OnBarUpdate' method on bar 4518: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
                                     i++;
                              }
                              else
                              {    // Reset the search on first DMI[1] != DMI[2]
                                  FlatInd = false;
                                  FlatIndBar = false;
                                  i = 1;                            
                              }
      ....
                          }

      Comment


        #4
        Hello itrader46,

        Thanks for your reply.

        The error message is advising that the variable value of flatDMIbar must be greater than or equal to 0 and less than the CurrentBar. I would suggest printing the value determined to be flatDMIbar before your print statement.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          The flatFMIbar reading prints OK. The error comes only when I inserted the commented part (
          DMI(14).GetValueAt(flatDMIbar)*/)) in the Print

          Comment


            #6
            Hello itrader46,

            Thanks for your reply.

            You are setting the variable here: flatDMIbar = CurrentBar - 2;

            Is there anywhere else that you are setting the variable value?

            Using Print (DMI(14).GetValueAt(CurrentBar-2).ToString()); I do not get any errors, do you?
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Got it... sort of: I was missing a print argument - trying to print 10 bits of data when I had defined only 9, which probably gave the "Index (zero based) must be greater than or equal to zero and less than the size of the argument list" error.

              The problem is now that, DMI(14).GetValueAt(flatDMIbar is not printing the DMI value at Flat DMI Bar, but the price close (CL 01-20, 25.11.2019, 05:15am bar 902 or 06:00 bar 911 - I am using market replay, obviously) at that bar, as you can see from below:



              Flat DMI Bar = 902, Flat Ind = True, FlatDMI[0] = -0.7778, FlatDMI[i-1] = -0.7778
              i = 1, CurrentBar = 904, CurrentBar - 1 = 903, FlatDMI[1] = 0.0000, CurrentBar - i = 903, DMI[flatDMIBar] = 57.8300
              ...
              Flat DMI Bar = 911, Flat Ind = True, FlatDMI[0] = 0.3684, FlatDMI[i-1] = 0.3684
              i = 1, CurrentBar = 913, CurrentBar - 1 = 912, FlatDMI[1] = 0.0000, CurrentBar - i = 912, DMI[flatDMIBar] = 57.9200

              Flat DMI Bar = 911, Flat Ind = True, FlatDMI[0] = 0.3684, FlatDMI[i-1] = 0.3684
              i = 2, CurrentBar = 914, CurrentBar - 1 = 913, FlatDMI[1] = 0.3684, CurrentBar - i = 912, DMI[flatDMIBar] = 57.9200

              Then I thought the DMI was actually applied to a secondary 5 min series ...

              Code:
              else if (State == State.Configure)                
                          {
                              AddDataSeries(Data.BarsPeriodType.Tick, 1);
                              AddDataSeries(Data.BarsPeriodType.Minute, 5);               
                          }
              So I replaced DMI(14).GetValueAt(flatDMIbar) with DMI(Closes[2], Convert.ToInt32(DmiPeriod)).GetValueAt(flatDMIbar) in my prints and the result is... unchanged:

              Flat DMI Bar = 902, Flat Ind = True, FlatDMI[0] = -0.7778, FlatDMI[i-1] = -0.7778
              i = 1, CurrentBar = 904, CurrentBar - 1 = 903, FlatDMI[1] = 0.0000, CurrentBar - i = 903, DMI[flatDMIBar] = 57.8300

              Why on Earth is it doing THAT?!?

              Comment


                #8
                Fixed it: DMI(Closes[2], Convert.ToInt32(DmiPeriod)).Values [0].GetValueAt(flatDMIbar) gives me the DMI value at flatDMIbar.

                I still don't understand why DMI(Closes[2], Convert.ToInt32(DmiPeriod)).GetValueAt(flatDMIbar) prints the price close, though!

                That is from the Help guide - Language reference - Common - ISeries<T> - GetValuaAt().

                Can you please tell me what does "(Input as Indicator).Values[0].GetValueAt(123) // passed in indicator value" mean? What exactly is "passed in value"?

                Comment


                  #9
                  Hello itrader46,

                  Thanks for your reply.

                  "I still don't understand why DMI(Closes[2], Convert.ToInt32(DmiPeriod)).GetValueAt(flatDMIbar) prints the price close, though!" As the help guide advises, it will return the bar data which is the input (by default is the close data series)

                  As you observed and according to the help guide, using .Values[] pulls the indicator value.

                  Can you please tell me what does "(Input as Indicator).Values[0].GetValueAt(123) // passed in indicator value" mean? What exactly is "passed in value"? If the input series to the script was an indicator (instead of a bar series), then .Values[] would return the indicator value that was the input.
                  Paul H.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by AttiM, 02-14-2024, 05:20 PM
                  12 responses
                  213 views
                  0 likes
                  Last Post DrakeiJosh  
                  Started by cre8able, 02-11-2023, 05:43 PM
                  3 responses
                  237 views
                  0 likes
                  Last Post rhubear
                  by rhubear
                   
                  Started by frslvr, 04-11-2024, 07:26 AM
                  8 responses
                  117 views
                  1 like
                  Last Post NinjaTrader_BrandonH  
                  Started by stafe, 04-15-2024, 08:34 PM
                  10 responses
                  47 views
                  0 likes
                  Last Post stafe
                  by stafe
                   
                  Started by rocketman7, Today, 09:41 AM
                  3 responses
                  12 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Working...
                  X