• 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

  • ShaneAU
    started a topic Moving Average of an Indicator

    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
    }
    }

  • arbuthnot
    replied
    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.

    Leave a comment:


  • arbuthnot
    replied
    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.

    Leave a comment:


  • NinjaTrader_PatrickH
    replied
    Hello ShaneAU,

    Thank you for your post.

    What values are you seeing? What values are you expecting?

    Leave a comment:


  • ShaneAU
    replied
    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

    Leave a comment:


  • arbuthnot
    replied
    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.

    Leave a comment:

Latest Posts

Collapse

Topics Statistics Last Post
Started by StealthM93, Today, 07:58 PM
0 responses
2 views
0 likes
Last Post StealthM93  
Started by Mykro, Today, 11:01 AM
1 response
5 views
0 likes
Last Post NinjaTrader_ChelseaB  
Started by bauerjj10, Today, 12:24 PM
1 response
14 views
0 likes
Last Post NinjaTrader_ChelseaB  
Started by Newtrader101, Today, 06:01 PM
1 response
7 views
0 likes
Last Post NinjaTrader_EricB  
Started by Steve L, Today, 03:50 PM
1 response
9 views
0 likes
Last Post NinjaTrader_ChelseaB  
Working...
X