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

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?

        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) / [B][COLOR="Blue"]slope[/COLOR][/B])*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 jeronymite, 04-12-2024, 04:26 PM
            3 responses
            39 views
            0 likes
            Last Post jeronymite  
            Started by bill2023, Today, 08:51 AM
            2 responses
            15 views
            0 likes
            Last Post bill2023  
            Started by sidlercom80, 10-28-2023, 08:49 AM
            167 responses
            2,260 views
            0 likes
            Last Post jeronymite  
            Started by warreng86, 11-10-2020, 02:04 PM
            7 responses
            1,362 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by Perr0Grande, Today, 08:16 PM
            0 responses
            5 views
            0 likes
            Last Post Perr0Grande  
            Working...
            X