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

Smoothed StdError Development 2

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

    Smoothed StdError Development 2

    I got the Linear Regression line of the StdError Indicator smoothed by an EMA working. Now I'd like to get the actual StdError part working. So this time I took the code from the StdError Indicator and added the 'average' to the 'linReg'. It didn't like that. How can I change it to work? The 'double average = EMA(linReg, 20)[0];' seems to be the problem. I get CS1502 and NT 1503 errors.
    Thanks again for the help.

    #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>
    /// EMA20 of Srandard Error Indicator
    /// </summary>
    [Description("EMA20 of Srandard Error Indicator")]
    public class StdErrorSmoothed2 : Indicator
    {
    #region Variables
    private int period = 14;
    private DataSeries y;
    #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.Orange, "LinReg"));
    Add(new Plot(Color.DarkViolet, "Upper"));
    Add(new Plot(Color.DarkViolet, "Lower"));

    y = new DataSeries(this);
    Overlay = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // calculate Linear Regression
    double sumX = (double) Period * (Period - 1) * 0.5;
    double divisor = sumX * sumX - (double) Period * Period * (Period - 1) * (2 * Period - 1) / 6;
    double sumXY = 0;

    for (int count = 0; count < Period && CurrentBar - count >= 0; count++)
    sumXY += count * Input[count];

    y.Set(Input[0]);
    double slope = ((double) Period * sumXY - sumX * SUM(y, Period)[0]) / divisor;
    double intercept = (SUM(y, Period)[0] - slope * sumX) / Period;
    double linReg = intercept + slope * (Period - 1);
    double average = EMA(linReg, 20)[0];


    // Calculate Standard Error
    double sumSquares = 0;
    for (int count = 0; count < Period && CurrentBar - count >= 0; count++)
    {
    double linRegX = intercept + slope * (Period - 1 - count);
    double valueX = Input[count];
    double diff = Math.Abs(valueX - linRegX);

    sumSquares += diff * diff;
    }
    double stdErr = Math.Sqrt(sumSquares / Period);

    Middle.Set(average);
    Upper.Set(average + stdErr);
    Lower.Set(average - stdErr);
    }

    #region Properties
    /// <summary>
    /// </summary>
    [Description("Numbers of bars used for calculations")]
    [GridCategory("Parameters")]
    public int Period
    {
    get { return period; }
    set { period = Math.Max(2, value); }
    }

    /// <summary>
    /// Lower band of Standard Error
    /// </summary>
    [Browsable(false)]
    [XmlIgnore]
    public Data.DataSeries Lower
    {
    get { return Values[2]; }
    }

    [Browsable(false)]
    [XmlIgnore]
    public Data.DataSeries Middle
    {
    get { return Values[0]; }
    }

    /// <summary>
    /// Upper band of Standard Error
    /// </summary>
    [Browsable(false)]
    [XmlIgnore]
    public Data.DataSeries Upper
    {
    get { return Values[1]; }
    }

    #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 StdErrorSmoothed2[] cacheStdErrorSmoothed2 = null;

    private static StdErrorSmoothed2 checkStdErrorSmoothed2 = new StdErrorSmoothed2();

    /// <summary>
    /// EMA20 of Srandard Error Indicator
    /// </summary>
    /// <returns></returns>
    public StdErrorSmoothed2 StdErrorSmoothed2(int period)
    {
    return StdErrorSmoothed2(Input, period);
    }

    /// <summary>
    /// EMA20 of Srandard Error Indicator
    /// </summary>
    /// <returns></returns>
    public StdErrorSmoothed2 StdErrorSmoothed2(Data.IDataSeries input, int period)
    {
    if (cacheStdErrorSmoothed2 != null)
    for (int idx = 0; idx < cacheStdErrorSmoothed2.Length; idx++)
    if (cacheStdErrorSmoothed2[idx].Period == period && cacheStdErrorSmoothed2[idx].EqualsInput(input))
    return cacheStdErrorSmoothed2[idx];

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

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

    StdErrorSmoothed2 indicator = new StdErrorSmoothed2();
    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();

    StdErrorSmoothed2[] tmp = new StdErrorSmoothed2[cacheStdErrorSmoothed2 == null ? 1 : cacheStdErrorSmoothed2.Length + 1];
    if (cacheStdErrorSmoothed2 != null)
    cacheStdErrorSmoothed2.CopyTo(tmp, 0);
    tmp[tmp.Length - 1] = indicator;
    cacheStdErrorSmoothed2 = 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>
    /// EMA20 of Srandard Error Indicator
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.StdErrorSmoothed2 StdErrorSmoothed2(int period)
    {
    return _indicator.StdErrorSmoothed2(Input, period);
    }

    /// <summary>
    /// EMA20 of Srandard Error Indicator
    /// </summary>
    /// <returns></returns>
    public Indicator.StdErrorSmoothed2 StdErrorSmoothed2(Data.IDataSeries input, int period)
    {
    return _indicator.StdErrorSmoothed2(input, period);
    }
    }
    }

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    public partial class Strategy : StrategyBase
    {
    /// <summary>
    /// EMA20 of Srandard Error Indicator
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.StdErrorSmoothed2 StdErrorSmoothed2(int period)
    {
    return _indicator.StdErrorSmoothed2(Input, period);
    }

    /// <summary>
    /// EMA20 of Srandard Error Indicator
    /// </summary>
    /// <returns></returns>
    public Indicator.StdErrorSmoothed2 StdErrorSmoothed2(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.StdErrorSmoothed2(input, period);
    }
    }
    }
    #endregion

    #2
    Originally posted by pjwinner View Post
    I got the Linear Regression line of the StdError Indicator smoothed by an EMA working. Now I'd like to get the actual StdError part working. So this time I took the code from the StdError Indicator and added the 'average' to the 'linReg'. It didn't like that. How can I change it to work? The 'double average = EMA(linReg, 20)[0];' seems to be the problem. I get CS1502 and NT 1503 errors.
    Thanks again for the help.

    #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>
    /// EMA20 of Srandard Error Indicator
    /// </summary>
    [Description("EMA20 of Srandard Error Indicator")]
    public class StdErrorSmoothed2 : Indicator
    {
    #region Variables
    private int period = 14;
    private DataSeries y;
    #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.Orange, "LinReg"));
    Add(new Plot(Color.DarkViolet, "Upper"));
    Add(new Plot(Color.DarkViolet, "Lower"));

    y = new DataSeries(this);
    Overlay = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // calculate Linear Regression
    double sumX = (double) Period * (Period - 1) * 0.5;
    double divisor = sumX * sumX - (double) Period * Period * (Period - 1) * (2 * Period - 1) / 6;
    double sumXY = 0;

    for (int count = 0; count < Period && CurrentBar - count >= 0; count++)
    sumXY += count * Input[count];

    y.Set(Input[0]);
    double slope = ((double) Period * sumXY - sumX * SUM(y, Period)[0]) / divisor;
    double intercept = (SUM(y, Period)[0] - slope * sumX) / Period;
    double linReg = intercept + slope * (Period - 1);
    double average = EMA(linReg, 20)[0];


    // Calculate Standard Error
    double sumSquares = 0;
    for (int count = 0; count < Period && CurrentBar - count >= 0; count++)
    {
    double linRegX = intercept + slope * (Period - 1 - count);
    double valueX = Input[count];
    double diff = Math.Abs(valueX - linRegX);

    sumSquares += diff * diff;
    }
    double stdErr = Math.Sqrt(sumSquares / Period);

    Middle.Set(average);
    Upper.Set(average + stdErr);
    Lower.Set(average - stdErr);
    }

    #region Properties
    /// <summary>
    /// </summary>
    [Description("Numbers of bars used for calculations")]
    [GridCategory("Parameters")]
    public int Period
    {
    get { return period; }
    set { period = Math.Max(2, value); }
    }

    /// <summary>
    /// Lower band of Standard Error
    /// </summary>
    [Browsable(false)]
    [XmlIgnore]
    public Data.DataSeries Lower
    {
    get { return Values[2]; }
    }

    [Browsable(false)]
    [XmlIgnore]
    public Data.DataSeries Middle
    {
    get { return Values[0]; }
    }

    /// <summary>
    /// Upper band of Standard Error
    /// </summary>
    [Browsable(false)]
    [XmlIgnore]
    public Data.DataSeries Upper
    {
    get { return Values[1]; }
    }

    #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 StdErrorSmoothed2[] cacheStdErrorSmoothed2 = null;

    private static StdErrorSmoothed2 checkStdErrorSmoothed2 = new StdErrorSmoothed2();

    /// <summary>
    /// EMA20 of Srandard Error Indicator
    /// </summary>
    /// <returns></returns>
    public StdErrorSmoothed2 StdErrorSmoothed2(int period)
    {
    return StdErrorSmoothed2(Input, period);
    }

    /// <summary>
    /// EMA20 of Srandard Error Indicator
    /// </summary>
    /// <returns></returns>
    public StdErrorSmoothed2 StdErrorSmoothed2(Data.IDataSeries input, int period)
    {
    if (cacheStdErrorSmoothed2 != null)
    for (int idx = 0; idx < cacheStdErrorSmoothed2.Length; idx++)
    if (cacheStdErrorSmoothed2[idx].Period == period && cacheStdErrorSmoothed2[idx].EqualsInput(input))
    return cacheStdErrorSmoothed2[idx];

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

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

    StdErrorSmoothed2 indicator = new StdErrorSmoothed2();
    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();

    StdErrorSmoothed2[] tmp = new StdErrorSmoothed2[cacheStdErrorSmoothed2 == null ? 1 : cacheStdErrorSmoothed2.Length + 1];
    if (cacheStdErrorSmoothed2 != null)
    cacheStdErrorSmoothed2.CopyTo(tmp, 0);
    tmp[tmp.Length - 1] = indicator;
    cacheStdErrorSmoothed2 = 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>
    /// EMA20 of Srandard Error Indicator
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.StdErrorSmoothed2 StdErrorSmoothed2(int period)
    {
    return _indicator.StdErrorSmoothed2(Input, period);
    }

    /// <summary>
    /// EMA20 of Srandard Error Indicator
    /// </summary>
    /// <returns></returns>
    public Indicator.StdErrorSmoothed2 StdErrorSmoothed2(Data.IDataSeries input, int period)
    {
    return _indicator.StdErrorSmoothed2(input, period);
    }
    }
    }

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    public partial class Strategy : StrategyBase
    {
    /// <summary>
    /// EMA20 of Srandard Error Indicator
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.StdErrorSmoothed2 StdErrorSmoothed2(int period)
    {
    return _indicator.StdErrorSmoothed2(Input, period);
    }

    /// <summary>
    /// EMA20 of Srandard Error Indicator
    /// </summary>
    /// <returns></returns>
    public Indicator.StdErrorSmoothed2 StdErrorSmoothed2(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.StdErrorSmoothed2(input, period);
    }
    }
    }
    #endregion
    You cannot take the EMA of a double. You likely want to turn linReg into a DataSeries and populate it, in order to be able to take any average.
    Last edited by koganam; 06-23-2013, 11:44 AM.

    Comment


      #3
      Thanks. I'll see what I can do.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Haiasi, 04-25-2024, 06:53 PM
      2 responses
      16 views
      0 likes
      Last Post Massinisa  
      Started by Creamers, Today, 05:32 AM
      0 responses
      5 views
      0 likes
      Last Post Creamers  
      Started by Segwin, 05-07-2018, 02:15 PM
      12 responses
      1,786 views
      0 likes
      Last Post Leafcutter  
      Started by poplagelu, Today, 05:00 AM
      0 responses
      3 views
      0 likes
      Last Post poplagelu  
      Started by fx.practic, 10-15-2013, 12:53 AM
      5 responses
      5,407 views
      0 likes
      Last Post Bidder
      by Bidder
       
      Working...
      X