Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Consensual indicators

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

    Consensual indicators

    Dear all

    hope you're doing great,

    I'd like somebody to help me coding this indicador in NT8. It is a consensual indicator: these indicators iteratively evaluate whether a population "z" of means with different number of bars satisfy a specific target criterion.

    In this case the code is calculating an ADX consensual indicator but I can't make it work in NT8.

    Could somebody please help?

    The indicator should look something like this here below and it has two parameters: "FinalPeriod" and "Filter":

    Click image for larger version

Name:	CPADX_nt8.PNG
Views:	292
Size:	215.5 KB
ID:	1135672

    The code is:

    nA = 0;

    Ma = 0;

    int step = finalPeriod/20;

    for (int x = 10; x < finalPeriod+1; x+=step)

    {

    if (Slope(ADX(x),filter,0)<0)

    {

    Ma = Ma+1;

    }

    nA = nA+1;

    }
    CSaxd = (((double)Ma/nA)*100);

    MCADX.Set(CSaxd);

    if (CSaxd<80)

    {

    BackColor=Color.Salmon;

    }

    else

    {

    BackColor=Color.Aquamarine;

    }


    Thanks
    regards

    #2
    Hello AlbertP,

    While I cannot assist with the custom logic and calculations, I can assist with helping you understand the behavior.

    This thread will remain open for any community members that would like to assist with the logic and calculations.


    So currently, where you have stated "I can't make it work in NT8", what specifically is not working?

    Is an error appearing on the Log tab of the Control Center when loading the script?

    Are you expecting a plot to be set and no plot is set?
    If so, use AddPlot() in State.SetDefaults to add a plot, then remove and re-add a new instance of the indicator.
    Then set the Value[0] to the calculated plot value.
    Below are links to the help guide on this.



    Is the background of the chart not being set to the expected color?
    If so, print the time of the bar and all values in the condition that when true sets the background color.
    Below is a link to a forum post that demonstrates using prints to understand behavior.

    Save the output to a text file and supply this with your reply and we will be happy to assist with analyzing the output.


    As a heads up, you have some invalid code:
    MCADX.Set(CSaxd);
    This would be appropriate for NinjaTrader 7, but is not appropriate in NinjaTrader 8.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      ​​​Dear Chelsea,

      thanks for your prompt reply!

      Sorry I was not clear at all in my first post. Let me try to explain myself better:

      Using the code I posted, I created a new Ninjascript indicator (trying to replicate the indicator):

      -Created the two parameters and the AddPlot (using the builder):
      ​​​
      FinalPeriod = 14;
      Filter = 50;
      AddPlot(Brushes.Orange, "CSaxd");


      -Created the variables:

      private int nA = 0;
      private int Ma = 0;
      private int x = 0;


      -Within the OnBarUpdate method, I added the calculation logic:

      protected override void OnBarUpdate()

      for (int x = 10; x < finalPeriod+1; x+=step)

      {

      if (Slope(ADX(x),filter,0)<0)

      {

      Ma = Ma+1;

      }

      nA = nA+1;

      }
      CSaxd = (((double)Ma/nA)*100);





      MCADX.Set(CSaxd);

      if (CSaxd<80)

      {

      BackColor=Color.Salmon;

      }

      else

      {

      BackColor=Color.Aquamarine;

      }



      While compiling the error message is: the name 'CSaxd' does not exist in the current context + many others:

      Click image for larger version  Name:	errors1.PNG Views:	0 Size:	21.4 KB ID:	1135852



      Click image for larger version  Name:	errors2.PNG Views:	0 Size:	33.1 KB ID:	1135847


      Does it mean a problem with the brackets {} ?? maybe with the names?

      Re the instruction MCADX.Set(CSaxd); which you say this is not valid for NT7, where can I find assistance to convert it to NT8? I'm a beginner in coding: I can create simple indicators using existing ones as a template but with this one I'm totally lost.

      PD: i checked the "Code breaking Changes" in the help section but I can't find any clue on how to solve it.

      Thanks again for your help

      best regards
      Last edited by AlbertP; 01-08-2021, 12:51 PM.

      Comment


        #4
        Hello AlbertP,

        Thanks for your reply.

        It looks like you can change this line CSaxd = (((double)Ma/nA)*100); to look like this: CSaxd[0] = (((double)Ma/nA)*100); // assign the value to plot

        You can then remove MCADX.Set(CSaxd);

        On this line: if (CSaxd<80) you would need to change that to if (CSaxd[0]<80)

        In both of the cases above you are specifying the current bra of the plot CSaxd

        NinjaTrader8 uses brushes instead of the color, so you can replace Color with Brushes, ie; Brushes.Red;

        Comment


          #5
          Hi Paul

          thanks a lot for your assistance. I made the changes you mentioned. Now the indicator compiles without errors, however when I do insert it in the chart it keeps "calculating" for a long time. As if the loop at some point got stuck and no plot is drawn.

          Could you please let me know where is the error now?

          Thanks again,

          regards


          Click image for larger version

Name:	calculating_error.PNG
Views:	415
Size:	7.8 KB
ID:	1135896


          Find here below the code as it is now:


          protected override void OnBarUpdate()

          {

          int step = FinalPeriod/20;
          for (int x = 10; x < FinalPeriod+1; x+=step)


          if (Slope(ADX(x),Filter,0)<0)
          {
          Ma = Ma+1;
          nA = nA+1;
          }

          Value[0] = (((double)Ma/nA)*100);

          {
          if (Value[0]<80)
          {

          ackBrush=Brushes.Salmon;
          }
          else
          {

          BackBrush=Brushes.Aquamarine;
          }

          }

          }

          Comment


            #6
            Hello AlbertP,

            We need to know which specific line of code is causing an issue.

            If you comment out all of the logic is the behavior the same?

            If you uncomment nt step = FinalPeriod/20; is the behavior the same?

            Uncomment each line one by one until the behavior returns. Which line when uncommented causes the behavior to return?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi ChelseaB

              hope you're doing well!

              I 'commented' one by one each of the logics in the indicator. It seems this the part of the code creating the issue:

              // int step = FinalPeriod/20;
              // for (int x = 10; x < FinalPeriod+1; x+=step)


              this message pops up after inserting the indicator
              Click image for larger version

Name:	error3.PNG
Views:	212
Size:	7.5 KB
ID:	1136248​​

              When commented, the indicator 'works' although it plots nothing (as expected, I think).

              So, how should I write the loop function? Am I missing ​declaring any variable?

              Looking forward to reading from you soon
              Best regards

              Comment


                #8
                Hello AlbertP,

                The error is letting you know you have specified the range for the Period property to be greater than 1, and it has a value of 0. Set it to at least 1 in State.SetDefaults (or if you are calling the ADX, supply a value of greater than 1 as the Period parameter).

                With the for loop, this would depend on the values used in the loop. If the for loop is going through a large amount of iterations, this may take considerable time and CPU.

                To understand the behavior, print the values.
                Print the values of all the variables involved, print the value of x on each iteration of the for loop.
                Print below the loop to have information that the loop completed.

                Below is a link to a forum post that demonstrates using prints to understand behavior.


                Save the output from the output window to a text file and please include this with your reply.
                Chelsea B.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                599 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                344 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                103 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                558 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                557 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X