• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Moving Average of an Indicator

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

    Moving Average of an Indicator

    Hey guys,

    I am trying to standardise the results of a custom indicator, so it can be used in different markets. To do this, I need to create a moving average of that indicator for say the last 100 bars, so I have something to divide by... but as far as i could work out, I needed to add the indicator as a second data series in order to be able to create a SMA of it.

    Below is my formula, it is not currently showing anything, the problem appears to be with setting the second data series ("SlopeData") as the slope value. I am wondering if maybe when I calculate the 'Slope' of this indicator, it is trying to access the first data series instead of the second...?

    Thanks for any help/advice... cheers
    Shane

    namespace NinjaTrader.Indicator
    {
    /// <summary>
    /// Measures the slope of an SMA
    /// </summary>
    [Description("Measures the slope of an SMA")]
    public class SlopeSMA : Indicator
    {
    #region Variables
    private int sMAPeriod = 200; // Default setting for SMAPeriod
    private int startBarsAgo = 5; // Default setting for StartBarsAgo
    private int endBarsAgo = 0; // Default setting for EndBarsAgo
    private double slope;
    private DataSeries SlopeData;

    #endregion

    protected override void Initialize()
    {
    SlopeData = new DataSeries (this);
    Add(new Plot(Color.FromKnownColor(KnownColor.RoyalBlue), PlotStyle.Line, "Plot0"));
    Overlay = false;
    }
    protected override void OnBarUpdate()
    {
    SlopeData.Set(Slope(SMA(sMAPeriod), startBarsAgo, endBarsAgo));
    slope = SMA(SlopeData,100)[0];
    Plot0.Set((Slope(SMA(sMAPeriod), startBarsAgo, endBarsAgo) / slope)*100);


    }

    #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 Plot0
    {
    get { return Values[0]; }
    }
    public DataSeries Plot1
    {
    get { return Values[1]; }
    }
    public DataSeries Plot2
    {
    get { return Values[2]; }
    }

    [Description("Moving Aver Period")]
    [GridCategory("Parameters")]
    public int SMAPeriod
    {
    get { return sMAPeriod; }
    set { sMAPeriod = Math.Max(1, value); }
    }

    [Description("Start Bars Ago")]
    [GridCategory("Parameters")]
    public int StartBarsAgo
    {
    get { return startBarsAgo; }
    set { startBarsAgo = Math.Max(1, value); }
    }

    [Description("End Bars Ago")]
    [GridCategory("Parameters")]
    public int EndBarsAgo
    {
    get { return endBarsAgo; }
    set { endBarsAgo = Math.Max(0, value); }
    }
    #endregion
    }
    }

    #2
    I think the problem is that every bar to which a plot is assigned must have access to all the previous bars needed to make the calculation, or it won't plot at all - for any bar. I've forgotten to do this many times!

    So put this piece of code at the beginning of OnBarUpdate:

    Code:
    if (CurrentBar < SMAPeriod) // The longest length overload
            		return;
    Hope this helps.

    Cheers.

    Comment


      #3
      Hi arbuthnot,

      Thanks for your help on that - it seems to have got it

      The indicator is displaying now, but not with the info I had aimed for though... it's late here so I'll tackle that problem tomorrow.

      Thanks again!
      Shane

      Comment


        #4
        Hello ShaneAU,

        Thank you for your post.

        What values are you seeing? What values are you expecting?
        Patrick H.NinjaTrader Customer Service

        Comment


          #5
          Hi again Shaun

          Looking at your Plot formula below, you have a problem of division by zero, which produces an infinity, which no system can handle.

          Code:
          Plot0.Set((Slope(SMA(sMAPeriod), startBarsAgo, endBarsAgo) / slope)*100);
          Even when 'slope' is near zero, you'll find the result gets very large.

          A way to avoid this is to use a difference rather than division, but the result will be very jagged.
          Last edited by arbuthnot; 04-23-2015, 03:22 PM.

          Comment


            #6
            Another problem is when both slopes get very close to zero. You won't get an exact zero in either case given that your slope functions will be a 'double', but given that 0/0 is undefined mathematically, you'll often get close to this state.

            I tried this once and my PC packed its bags and went into retirement!
            Last edited by arbuthnot; 04-24-2015, 04:44 AM.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by sambo81, Yesterday, 06:57 PM
            1 response
            16 views
            0 likes
            Last Post NinjaTrader_ThomasC  
            Started by tgn55, Today, 08:34 AM
            0 responses
            3 views
            0 likes
            Last Post tgn55
            by tgn55
             
            Started by TazoTodua, Today, 06:00 AM
            0 responses
            7 views
            0 likes
            Last Post TazoTodua  
            Started by gbux11, Today, 02:36 AM
            0 responses
            8 views
            0 likes
            Last Post gbux11
            by gbux11
             
            Started by fx.practic, Today, 01:47 AM
            0 responses
            15 views
            0 likes
            Last Post fx.practic  
            Working...
            X