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

i need help to get a simple average inside another indicator to work.

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

    i need help to get a simple average inside another indicator to work.



    people with nt,


    regards. i hope everything is going good.


    i have created a very simple indicator that would calculate and plot the average of the close minus the open for n periods. i really want two indicators that will use this structure as a starting point to report this average in terms of ticks and monetary value. that last part is extremely easy if one uses Instrument.MasterInstrument.TickSize and Instrument.MasterInstrument.PointValue, however this indicator below will compile without problem but then will not plot or calculate everything.


    Code:
    public class simpleaverage : Indicator
        {
    
            private Series<double>        clmiop;
            private SMA siavrasma;
    
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"simple average.";
                    Name                                        = "simpleaverage";
                    Calculate                                    = Calculate.OnBarClose;
                    IsOverlay                                    = false;
                    DisplayInDataBox                            = true;
                    DrawOnPricePanel                            = true;
                    DrawHorizontalGridLines                        = true;
                    DrawVerticalGridLines                        = true;
                    PaintPriceMarkers                            = true;
                    ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive                    = true;
                    Period                    = 14;
                    AddLine(Brushes.Blue, 3, "Simpleaveragerangel");
                }
                else if (State == State.Configure)
                {
                }
                else if (State == State.DataLoaded)
                {
    
                himilo                = new Series<double>(this);
                siavrasma            = SMA(clmiop, Period);
    
                }
            }
    
            protected override void OnBarUpdate()
            {
    
                if (CurrentBar < ( Period + 2 ) ) return;
    
    
                clmiop[0] = ( ( Close[0] - Open[0] ) );
    
                double siavrava    = siavrasma[0];
    
                Value[0]        = siavrava;
    
            }

    i have tried several different things i have thought of to fix this but nothing has worked so far. ¿could the people with nt support help me with this indicator? thanks.

    #2
    Hello rtwave,

    Thanks for your post.

    At the top you have: private Series<double> clmiop;

    In State.DataLoaded you have: himilo = new Series<double>(this);

    In OnBarUpdate() you have: clmiop[0] = ( ( Close[0] - Open[0] ) );

    You need to initialize clmiop in state data loaded like: climiop = new Series<double>(this);
    Paul H.NinjaTrader Customer Service

    Comment


      #3



      Paul,


      you are right. i didn't copy and paste correctly the version of this indicator that does compile, it still doesn't calculate or plot anything.


      Code:
      {
          public class simpleaverage : Indicator
          {
      
              private Series<double>        clmiop;
              private SMA siavrasma;
      
      
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description                                    = @"simple average.";
                      Name                                        = "simpleaverage";
                      Calculate                                    = Calculate.OnBarClose;
                      IsOverlay                                    = false;
                      DisplayInDataBox                            = true;
                      DrawOnPricePanel                            = true;
                      DrawHorizontalGridLines                        = true;
                      DrawVerticalGridLines                        = true;
                      PaintPriceMarkers                            = true;
                      ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                      //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                      //See Help Guide for additional information.
                      IsSuspendedWhileInactive                    = true;
                      Period                    = 14;
                      AddLine(Brushes.Blue, 3, "Simpleaveragel");
                  }
                  else if (State == State.Configure)
                  {
                  }
                  else if (State == State.DataLoaded)
                  {
      
                  clmiop                = new Series<double>(this);
                  siavrasma            = SMA(clmiop, Period);
      
                  }
              }
      
              protected override void OnBarUpdate()
              {
      
                  if (CurrentBar < ( Period + 2 ) ) return;
      
      
                  clmiop[0] = ( ( Close[0] - Open[0] ) );
      
                  double siavrava    = siavrasma[0];
      
                  Value[0]        = siavrava;
      
              }

      even when i have included a check to verify that the number of bars is greater than the period it still does not work. if i remove the sma then the calculation for close minus open for every single trading day works without issue and i can then use Instrument.MasterInstrument.TickSize and Instrument.MasterInstrument.PointValue to report this value as number of ticks or monetary value but i want to calculate a simple average for all of these values as it far more reliable and informative.


      very well, thanks, regards.

      Comment


        #4
        Hello rtwave,

        Thanks for your reply.

        I overlooked that you are using AddLine() and what you really need is to use AddPlot(): https://ninjatrader.com/support/help...8/?addplot.htm

        AddLine is a static line value (such as a zero line).
        Paul H.NinjaTrader Customer Service

        Comment


          #5



          Paul, people with nt,


          thanks.


          the modification you indicated solved this issue.


          ¿how should i do if i want the width for this plot to be set to 3? i found one line of code that could work but i have no idea in which of the several different protected override voids i should place it.



          thanks.

          Comment


            #6
            Hello rtwave,

            Thanks for your reply.

            When you create the plot, you can specify the stroke at the time, for example: AddPlot(new Stroke(Brushes.Gold, DashStyleHelper.Solid, 2), PlotStyle.Line, "R4"); Would provide a line width of 2 for a solid line plot that is gold in color.

            Alternately, in State.DataLoaded, or in the OnbarUpdate()you could also use Plots[0].Width = 2;

            Please see: https://ninjatrader.com/support/help...nt8/?plots.htm
            Paul H.NinjaTrader Customer Service

            Comment


              #7


              Paul, people with nt,



              thanks for your assistance in this matter.


              i have been able to create the indicators i had in mind. mission accomplished. thanks again, regards.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by judysamnt7, 03-13-2023, 09:11 AM
              4 responses
              59 views
              0 likes
              Last Post DynamicTest  
              Started by ScottWalsh, Today, 06:52 PM
              4 responses
              36 views
              0 likes
              Last Post ScottWalsh  
              Started by olisav57, Today, 07:39 PM
              0 responses
              7 views
              0 likes
              Last Post olisav57  
              Started by trilliantrader, Today, 03:01 PM
              2 responses
              21 views
              0 likes
              Last Post helpwanted  
              Started by cre8able, Today, 07:24 PM
              0 responses
              10 views
              0 likes
              Last Post cre8able  
              Working...
              X