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

average / mean of an indicator

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

    average / mean of an indicator

    hi,

    i'm a newbie.

    I have build my own indicator. now i want to have a line with the average/ mean of the indicator values over all values printed (not only over the 256 values).

    like (1+3+4+5+x) / n


    First i thought about sma. but it seems not to work.

    have you an idea?

    thanks

    #2
    Hello nt_dd,

    Thank you for your post and welcome to our forums!

    Since the indicator is plotted as a data series, you would be able to use the GetMedian() function to calculate this data:



    You should also be able to use a SMA on the indicator as well to return these values.



    If you need further assistance with this, can you please clarify which period length you need to calculate?
    MatthewNinjaTrader Product Management

    Comment


      #3
      hi Matthew,

      what i want to code is a line with the same value over the whole width (like an Oscillator line).

      if the value is calculated 1.25, there has to be a line straight line of 1.25 where the original indicator value oscillate.


      the period length should be all the bars i loaded. for example ym 6-11 5min from february 2011 till today (4221 bars). the 1.25 line has to begin february 2011 and ends today.

      For Count i use:

      int value = Count;

      Plot4.Set(value);

      I thougt that it could be SUM (indicator) / (Count)


      thanks

      Comment


        #4
        nt_dd,

        I believe there could be an easier way to achieve this. Instead of using plots, you could just use a DrawHorizontalLine() object which would extend indefinitely left and right so every single bar will always have this line drawn. Please let us know what you think about using this technique instead.

        Josh P.NinjaTrader Customer Service

        Comment


          #5
          hello josh,

          thank you.

          i tried a little bit with your suggestion, but i didn t find a way to code it or that it draw a line at all.

          can you help me?

          thanks

          code:

          protected override void OnBarUpdate()
          {

          if (CurrentBar < 1)

          return;


          DrawHorizontalLine("tag1", 0.1, Color.Black);

          Comment


            #6
            Hello nt_dd ,

            I tested the code you posted and am able to get this line to plot. Are you saying you don't see the line at all? Do you get any errors on the Log tab of the Control Center?

            Are you plotting this indicator on the input series panel, or creating a new panel?
            MatthewNinjaTrader Product Management

            Comment


              #7
              hi,

              i don't see a line at all.

              there is no failure in the log file.

              i used the code for its own in an own plot and within my indicator plot.

              thanks

              code:

              #region Using declarations
              using System;
              using System.ComponentModel;
              using System.Diagnostics;
              using System.Drawing;
              using System.Drawing.Drawing2D;
              using System.Xml.Serialization;
              using NinjaTrader.Cbi;
              using NinjaTrader.Data;
              using NinjaTrader.Gui.Chart;
              #endregion

              // This namespace holds all indicators and is required. Do not change it.
              namespace NinjaTrader.Indicator
              {
              /// <summary>
              /// Enter the description of your new custom indicator here
              /// </summary>
              [Description("Enter the description of your new custom indicator here")]
              public class TestMean : Indicator
              {
              #region Variables
              // Wizard generated variables
              private int period = 100; // Default setting for Period
              // User defined variables (add any user defined variables below)

              private DataSeries myDataSeries;
              #endregion

              /// <summary>
              /// This method is used to configure the indicator and is called once before any bar data is loaded.
              /// </summary>
              protected override void Initialize()
              {
              Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot4"));
              Overlay = false;

              }

              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {

              if (CurrentBar < 1)

              return;


              DrawHorizontalLine("tag1", 0.1, Color.Black);


              }

              #region Properties
              [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
              [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
              public DataSeries Plot4
              {
              get { return Values[0]; }
              }

              [Description("")]
              [GridCategory("Parameters")]
              public int Period
              {
              get { return period; }
              set { period = Math.Max(1, value); }
              }
              #endregion
              }
              }

              #region NinjaScript generated code. Neither change nor remove.
              // This namespace holds all indicators and is required. Do not change it.
              namespace NinjaTrader.Indicator
              {
              public partial class Indicator : IndicatorBase
              {
              private TestMean[] cacheTestMean = null;

              private static TestMean checkTestMean = new TestMean();

              /// <summary>
              /// Enter the description of your new custom indicator here
              /// </summary>
              /// <returns></returns>
              public TestMean TestMean(int period)
              {
              return TestMean(Input, period);
              }

              /// <summary>
              /// Enter the description of your new custom indicator here
              /// </summary>
              /// <returns></returns>
              public TestMean TestMean(Data.IDataSeries input, int period)
              {
              if (cacheTestMean != null)
              for (int idx = 0; idx < cacheTestMean.Length; idx++)
              if (cacheTestMean[idx].Period == period && cacheTestMean[idx].EqualsInput(input))
              return cacheTestMean[idx];

              lock (checkTestMean)
              {
              checkTestMean.Period = period;
              period = checkTestMean.Period;

              if (cacheTestMean != null)
              for (int idx = 0; idx < cacheTestMean.Length; idx++)
              if (cacheTestMean[idx].Period == period && cacheTestMean[idx].EqualsInput(input))
              return cacheTestMean[idx];

              TestMean indicator = new TestMean();
              indicator.BarsRequired = BarsRequired;
              indicator.CalculateOnBarClose = CalculateOnBarClose;
              #if NT7
              indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
              indicator.MaximumBarsLookBack = MaximumBarsLookBack;
              #endif
              indicator.Input = input;
              indicator.Period = period;
              Indicators.Add(indicator);
              indicator.SetUp();

              TestMean[] tmp = new TestMean[cacheTestMean == null ? 1 : cacheTestMean.Length + 1];
              if (cacheTestMean != null)
              cacheTestMean.CopyTo(tmp, 0);
              tmp[tmp.Length - 1] = indicator;
              cacheTestMean = tmp;
              return indicator;
              }
              }
              }
              }

              // This namespace holds all market analyzer column definitions and is required. Do not change it.
              namespace NinjaTrader.MarketAnalyzer
              {
              public partial class Column : ColumnBase
              {
              /// <summary>
              /// Enter the description of your new custom indicator here
              /// </summary>
              /// <returns></returns>
              [Gui.Design.WizardCondition("Indicator")]
              public Indicator.TestMean TestMean(int period)
              {
              return _indicator.TestMean(Input, period);
              }

              /// <summary>
              /// Enter the description of your new custom indicator here
              /// </summary>
              /// <returns></returns>
              public Indicator.TestMean TestMean(Data.IDataSeries input, int period)
              {
              return _indicator.TestMean(input, period);
              }
              }
              }

              // This namespace holds all strategies and is required. Do not change it.
              namespace NinjaTrader.Strategy
              {
              public partial class Strategy : StrategyBase
              {
              /// <summary>
              /// Enter the description of your new custom indicator here
              /// </summary>
              /// <returns></returns>
              [Gui.Design.WizardCondition("Indicator")]
              public Indicator.TestMean TestMean(int period)
              {
              return _indicator.TestMean(Input, period);
              }

              /// <summary>
              /// Enter the description of your new custom indicator here
              /// </summary>
              /// <returns></returns>
              public Indicator.TestMean TestMean(Data.IDataSeries input, int period)
              {
              if (InInitialize && input == null)
              throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

              return _indicator.TestMean(input, period);
              }
              }
              }
              #endregion
              Attached Files

              Comment


                #8
                Hello,

                It is likley that the horizontal line is drawing at a value of 0.1 on the primary data series.

                Please add DrawOnPricePanel = false; to Initialize() in order for this to plot on the indicator panel instead.

                I've attached a working version of this for you to compare.
                Attached Files
                MatthewNinjaTrader Product Management

                Comment


                  #9
                  hi,

                  thank you all!

                  i'm on the best way to achieve my goal.

                  below you can see the code i wrote for my question.

                  thanks!

                  code:

                  #region Variables
                  // Wizard generated variables
                  private int period = 20; // Default setting for Period
                  // User defined variables (add any user defined variables below)
                  private DataSeries myDataSeries;
                  #endregion

                  /// <summary>
                  /// This method is used to configure the indicator and is called once before any bar data is loaded.
                  /// </summary>
                  protected override void Initialize()
                  {
                  Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Line, "Plot11"));
                  Overlay = false;
                  DrawOnPricePanel = false;
                  myDataSeries = new DataSeries(this);
                  }

                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {
                  if (CurrentBar < 1)

                  return;

                  myDataSeries.Set(myIndicator(Period)[0]);

                  double value2 = SUM(myDataSeries, Count)[0];
                  double value3 = value2/ Count;

                  Plot11.Set(myIndicator(Period)[0]);
                  DrawHorizontalLine("mytag",value3,Color.Blue);

                  }

                  Comment


                    #10
                    nt_dd,

                    So is this code working as expected or do you need further assistance with this?
                    MatthewNinjaTrader Product Management

                    Comment


                      #11
                      hi matthew,

                      this works fine.

                      my next project is to integrate more lines.

                      the value of the lines should calculated by the average (already done ) + stdDev.

                      but stdDev is a little bit tricky.

                      as indicator alone and in a separate panel i get a real value. when i put it as DrawHorizontalLine code there is a value of "0".

                      Code:

                      protected override void OnBarUpdate()
                      {
                      if (CurrentBar < 1)

                      return;

                      myDataSeries.Set(myIndicator(Period)[0]);
                      myDataSeries1.Set(StdDev(Count)[0]);

                      double value2 = SUM(myDataSeries, Count)[0];
                      double value3 = value2/ Count;
                      double value4 = StdDev(myDataSeries1, Count)[0];




                      Plot11.Set(myIndicator(Period)[0]);
                      DrawHorizontalLine("mytag",value3,Color.Blue);
                      DrawHorizontalLine("mytag2",value4,Color.Green);

                      Comment


                        #12
                        Hello,

                        Please take a look at the 'count' for value3. When dividing on an int, it will need to be cast as double

                        double value3 = value2 / (double) Count;
                        MatthewNinjaTrader Product Management

                        Comment


                          #13
                          Ok.

                          I had forgotten to input myIndicator in the myDataSeries1.Set line.

                          than it works fine.

                          Thanks



                          Code:

                          protected override void OnBarUpdate()
                          {
                          if (CurrentBar < 1)

                          return;

                          myDataSeries.Set(myIndicator(Period)[0]);
                          myDataSeries1.Set(StdDev(myIndicator(Period), Count)[0]);

                          double value2 = SUM(myDataSeries, Count)[0];
                          double value3 = value2/ Count;
                          double value4 = StdDev(myDataSeries1, Count)[0];




                          Plot11.Set(myIndicator(Period)[0]);
                          DrawHorizontalLine("mytag",value3,Color.Blue);
                          DrawHorizontalLine("mytag2",value4,Color.Green);

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Gerik, Today, 09:40 AM
                          1 response
                          6 views
                          0 likes
                          Last Post NinjaTrader_Gaby  
                          Started by RookieTrader, Today, 09:37 AM
                          1 response
                          10 views
                          0 likes
                          Last Post NinjaTrader_ChelseaB  
                          Started by alifarahani, Today, 09:40 AM
                          0 responses
                          5 views
                          0 likes
                          Last Post alifarahani  
                          Started by KennyK, 05-29-2017, 02:02 AM
                          3 responses
                          1,285 views
                          0 likes
                          Last Post NinjaTrader_Clayton  
                          Started by AttiM, 02-14-2024, 05:20 PM
                          11 responses
                          186 views
                          0 likes
                          Last Post NinjaTrader_ChelseaB  
                          Working...
                          X