Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Creating an Avg of an Indicator

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

    Creating an Avg of an Indicator

    I'm sure this isn't as hard as I am making it but I am having a devil of a time to get an avg of an indicator working. I have modified the Dynamic S/R indicator to calculate the difference betwen the DynamicR and the DynamicS. It works and the plot looks like the image attached and below is the code that works. The problem I have is I can't seem to create an avg that I can then link to the second plot that I have already defined. I know that I need a dataseries to calculate the avg from but do I need to create a new dataseries to do that or is there a way that I can construct a variable, based on the first dataseries in order to construct the avg.? This seems simple enough but I am getting jumbled up on the creation of the avg I guess. The image is of this indicator code below, it does plots the difference I am looking for but not the average of the difference.
    Thanks
    DaveN

    #region Variables
    // Wizard generated variables
    private int period = 14;
    private DataSeries HMASeries;
    private DataSeries HMA_DiffSeries;
    // private DataSeries Diff_AvgSeries;

    // User defined variables (add any user defined variables below)

    private double DynamicR = 0;
    private double DynamicS = 0;
    private double SR_Diff = 0;
    // private double DiffAvg = 0;
    private double HH = 0;
    private double LL = 0;
    private int barcounter = 0;
    #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(new Pen(Color.Black,3), PlotStyle.Bar, "RTM"));
    Add(new Plot(new Pen(Color.Magenta,2), PlotStyle.Line, "RTM_Avg"));

    HMASeries = new DataSeries(this);
    HMA_DiffSeries = new DataSeries(this);
    // Diff_AvgSeries = new DataSeries(this);


    CalculateOnBarClose = true;
    Overlay = false;
    PriceTypeSupported = false;
    PaintPriceMarkers = true;
    AutoScale = false;
    BarsRequired = 14;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    HMASeries.Set(HMA(period)[0]);

    if ( CurrentBar < 2 ) return;
    {
    HH = High[Bars.HighestBar(10)];
    if (HMASeries[1] > HMASeries[2] && HMASeries[0] < HMASeries[1])
    DynamicR=HH;
    LL = Low[Bars.LowestBar(10)];
    if(HMASeries[1]< HMASeries[2] && HMASeries[0] > HMASeries[1])
    DynamicS=LL;
    }
    if ( CurrentBar < period) return;
    SR_Diff = (DynamicR - DynamicS);
    HMA_DiffSeries.Set(SR_Diff);
    RTM.Set(SR_Diff);
    // RTM_Avg.Set(SMA(SR_Diff(), period)[0]);

    //
    // Diff_AvgSeries.Set(SMA(SR_Diff, period)[0]);



    }

    #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 RTM
    {
    get { return Values[0]; }
    }

    // [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 RTM_Avg
    // {
    // get { return Values[1]; }
    // }

    [Description("Numbers of bars used for calculations")]
    [Category("Parameters")]
    public int Period
    {
    get { return period; }
    set { period = Math.Max(1, value); }
    }
    #endregion
    Attached Files
    Last edited by daven; 07-27-2010, 11:06 AM. Reason: Left out image.

    #2
    Hello,

    Just so I understand correctly your trying to create and average of the last bars value of the indicator is this correct?

    I look forward to assisting you further.

    Comment


      #3
      I probably put this in the wrong category. I am working on an automated system based on expansion and contraction of range, and this indicator is the enabler for that range detection function. Should I move this question to another forum, ( the forum on indicators for example):
      DaveN

      Comment


        #4
        Yes, you are correct. I know it is pretty simple and I should be able to do it easily but everything I try gives me error messages.

        Comment


          #5
          Hello,

          Here is an Simple MA method I coded some time ago and does the same thing as SMA(). This will not work cut and paste into your code however this should show you how to write this and integrate something like this in your own code to do this. In stead of adding the close price together you would add your indicator value.

          Code:
          [FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]
          double[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=#000000] customSMA([/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=#000000] smaVariable)[/COLOR]
          {
          [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] average = [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2];
          [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] addingVariable = [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2];
          
          [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] i=[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2];i < smaVariable;i++)
          {
          addingVariable += Close[(smaVariable - i)];
          }
          
          average = addingVariable / smaVariable;
          [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] average; 
          }
          [/SIZE][/FONT][/SIZE][/FONT]
          Let me know if you have any questions.

          Comment


            #6
            That first line is creating errors in my code. It looks like you are declaring a double variable (customSMA) which is actually based on an integer variable(int smaVariable). Shouldn't there be a semicolon after that statement?

            Comment


              #7
              Also, you stated "instead of adding the close price you would add the indicator value". Which indicator value did you mean, SR_Diff, HMA_DiffSeries or RTM? Also when I use whichever one I use do I index it (for example SR_Diff[0}, index the whole statement (SR_Diff(), period)[0], or just use the straight variable? Is there a reason I can't just use the SMA function as it currently exists or do I have to use your alternate construct? If I can't use the straight SMA function, could you tell me why not. This all seems like pretty fundamental stuff which would be incredible helpful for me to understand better.
              I appreciate your help.
              DaveN

              Comment


                #8
                Hello,

                Yes you can also use the SMA() function as well you would specify an alternate data series. This is an example of there is always multiple ways to do things in code.

                As far as my code there is no semi colen since this is a method declaration.

                You will be unable to copy my code segment into your code since your missing variable declations. However if you werent looking for the information on how to use the SMA and are just looking to get the calculation out you can use the SMA and pass in your data series instead of the Close data series.

                SMA(HMA_DiffSeries , Period);

                This would output the average as well.

                As far as your question as to which data series/variable this is up to you and what logic you are looking for in your code.You just need to make sure it is a dataSeries and is not a variable.



                Let me know if I can be of further assistance.

                Comment


                  #9
                  I got it running. I tried doing your method and frankly I got really confused, so I just did strategy wizard, creating a command to see when RTM crossed over the avg of RTM (using the SMA indicator with PT_RTM embedded as the default input. I then looked at the code and copied it as best I could into the new indicator including the average. It worked. Here is the code that was added to the existing code that created the avg, (in case anyone else is interested). The key was to create a new double variable (DiffAvg), make it equal to the SMA of the first indicator (RTM), properly indexed

                  double DiffAvg = (SMA(RTM, period)[0]);

                  Then tieing it to the new output plot variable (RTM_Avg) and making sure that second plot variable showed up in properties tied to Value[1].

                  Thanks for pointing me in the right direction.
                  DaveN
                  Original Code Block
                  if ( CurrentBar < period) return;
                  SR_Diff = (DynamicR - DynamicS);
                  HMA_DiffSeries.Set(SR_Diff);
                  RTM.Set(SR_Diff);
                  New Code Block
                  if ( CurrentBar < period) return;
                  SR_Diff = (DynamicR - DynamicS);
                  HMA_DiffSeries.Set(SR_Diff);
                  RTM.Set(SR_Diff);

                  double DiffAvg = (SMA(RTM, period)[0]);
                  RTM_Avg.Set(DiffAvg);
                  Properties Code needed to tie it all together
                  [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 RTM_Avg
                  {
                  get { return Values[1]; }
                  }

                  Comment


                    #10
                    just a quick question, is there a command for "equal to +/- x%" exe. if variable y is + or - X% from 100? is there a coding trick for it or do I have to go through OR statements?

                    Comment


                      #11
                      Hello,

                      Best way that I can think of currently with what you described is OR statements.

                      Comment


                        #12
                        How can I loop through a DataSeries to calculate an average?

                        For example, I have an indicator which plots an value. This plot is derived from the Plot0 DataSeries. How can I loop through this DataSeries to calculate various metrics?

                        This is what I have so far which illustrates what I’m trying to accomplish:

                        Code:
                          int i;
                          double totalSum = 0;
                          foreach (Value d in Plot0)
                          {
                          totalSum = totalSum + d.Plot0[0];
                          i++;
                          }
                          double averagePlot0 = totalSum / i;
                        This gives the following error for the ‘Value’:
                        'NinjaTrader.Indicator.IndicatorBase.Value' is a 'property' but is used like a 'type'

                        And the ‘foreach’ error:
                        foreach statement cannot operate on variables of type 'NinjaTrader.Data.DataSeries' because 'NinjaTrader.Data.DataSeries' does not contain a public definition for 'GetEnumerator'

                        So, how can I loop through this Plot0 DataSeries?


                        Any suggestions would be highly appreciated.


                        Regards,

                        Comment


                          #13
                          J_o_s, unfortunately it would not be suppored to access the plot values collection in this way, you can try looping over the Value series for all bars on your chart.

                          BertrandNinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Barry Milan, Yesterday, 10:35 PM
                          7 responses
                          19 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by AttiM, 02-14-2024, 05:20 PM
                          10 responses
                          179 views
                          0 likes
                          Last Post jeronymite  
                          Started by ghoul, Today, 06:02 PM
                          0 responses
                          9 views
                          0 likes
                          Last Post ghoul
                          by ghoul
                           
                          Started by DanielSanMartin, Yesterday, 02:37 PM
                          2 responses
                          13 views
                          0 likes
                          Last Post DanielSanMartin  
                          Started by DJ888, 04-16-2024, 06:09 PM
                          4 responses
                          13 views
                          0 likes
                          Last Post DJ888
                          by DJ888
                           
                          Working...
                          X