• 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

Polynomial Regression Indicator conversion from NT7 to NT8

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

    Polynomial Regression Indicator conversion from NT7 to NT8

    I need to convert the Polynomial Regression Indicator from NT7 to NT8. I am also adding a second set of PRC bands which is not included in the original version of the indicator. I have attached the original NT7 version of the Polynomial Regression Channel. (A big thank you to fxcanada who made this indicator available for free!)
    The converted code compiles and plots on charts without throwing up an error in the log but it is definitely NOT looking like it should.
    Could someone please take a read through my code and see where I have gone wrong?
    Many thanks

    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class PRC : Indicator
    {
    private double[,] ai = new double[10, 10];
    private double[] b = new double[10];
    private double[] x = new double[10];
    private double[] sx = new double[10];
    private double sum;
    private int ip;
    private int p;
    private int n;
    private int f;
    private double qq;
    private double mm;
    private double tt;
    private int ii;
    private int jj;
    private int kk;
    private int ll;
    private int nn;
    private double sq;
    private double sq2;
    private int i0 = 0;
    private int mi;

    private Series <double> median;
    private Series <double> sqh;
    private Series <double> sqh2;
    private Series <double> sql;
    private Series <double> sql2;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"PRC";
    Name = "PRC";
    Calculate = Calculate.OnEachTick;
    IsOverlay = true;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    DrawHorizontalGridLines = true;
    DrawVerticalGridLines = true;
    PaintPriceMarkers = true;
    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
    //See Help Guide for additional information.
    IsSuspendedWhileInactive = true;
    Degree = 3;
    Period = 120;
    StrdDev = 1.62;
    StrdDev2 = 2;
    AddPlot(new Stroke(Brushes.DarkGray), PlotStyle.Line, "Median");
    AddPlot(new Stroke(Brushes.Red), PlotStyle.Line, "SQH");
    AddPlot(new Stroke(Brushes.Red), PlotStyle.Line, "SQH2");
    AddPlot(new Stroke(Brushes.Blue), PlotStyle.Line, "SQL");
    AddPlot(new Stroke(Brushes.Blue), PlotStyle.Line, "SQL2");
    }
    else if (State == State.DataLoaded)
    {
    median = new Series<double>(this);
    sqh = new Series<double>(this);
    sqh2 = new Series<double>(this);
    sql = new Series<double>(this);
    sql2 = new Series<double>(this);
    }
    }

    protected override void OnBarUpdate()
    {
    if( CurrentBar < Period) return;

    ip = Period;
    p = ip;
    sx[1] = p + 1;
    nn = Degree + 1;
    //----------------------sx-------------------------------------------------------------------
    //
    for (mi = 1; mi <= nn * 2 - 2; mi++)
    {
    sum = 0;
    for (n = i0; n <= i0 + p; n++)
    {
    sum += Math.Pow(n, mi);
    }
    sx[mi + 1] = sum;
    }
    //----------------------syx-----------
    for (mi = 1; mi <= nn; mi++)
    {
    sum = 0.0;
    for (n = i0; n <= i0 + p; n++)
    {
    if (mi == 1)
    sum += Close[n];
    else
    sum += Close[n] * Math.Pow(n, mi - 1);
    }
    b[mi] = sum;
    }
    //===============Matrix============================= ================================================== ========================
    for (jj = 1; jj <= nn; jj++)
    {
    for (ii = 1; ii <= nn; ii++)
    {
    kk = ii + jj - 1;
    ai[ii, jj] = sx[kk];
    }
    }
    //===============Gauss============================== ================================================== ========================
    for (kk = 1; kk <= nn - 1; kk++)
    {
    ll = 0;
    mm = 0;
    for (ii = kk; ii <= nn; ii++)
    {
    if (Math.Abs(ai[ii, kk]) > mm)
    {
    mm = Math.Abs(ai[ii, kk]);
    ll = ii;
    }
    }
    if (ll == 0)
    return;
    if (ll != kk)
    {
    for (jj = 1; jj <= nn; jj++)
    {
    tt = ai[kk, jj];
    ai[kk, jj] = ai[ll, jj];
    ai[ll, jj] = tt;
    }
    tt = b[kk];
    b[kk] = b[ll];
    b[ll] = tt;
    }
    for (ii = kk + 1; ii <= nn; ii++)
    {
    qq = ai[ii, kk] / ai[kk, kk];
    for (jj = 1; jj <= nn; jj++)
    {
    if (jj == kk)
    ai[ii, jj] = 0;
    else
    ai[ii, jj] = ai[ii, jj] - qq * ai[kk, jj];
    }
    b[ii] = b[ii] - qq * b[kk];
    }
    }
    x[nn] = b[nn] / ai[nn, nn];
    for (ii = nn - 1; ii >= 1; ii--)
    {
    tt = 0;
    for (jj = 1; jj <= nn - ii; jj++)
    {
    tt = tt + ai[ii, ii + jj] * x[ii + jj];
    x[ii] = (1 / ai[ii, ii]) * (b[ii] - tt);
    }
    }
    sq = 0.0;
    sq2 = 0.0;
    for (n = i0; n <= i0 + p; n++)
    {
    sum = 0;
    for (kk = 1; kk <= Degree; kk++)
    {
    sum += x[kk + 1] * Math.Pow(n, kk);
    }
    median[n] = (x[1] + sum);
    sq += Math.Pow(Close[n] - Median[n], 2);
    sq2 += Math.Pow(Close[n] - Median[n], 2);
    }
    sq = Math.Sqrt(sq / (p + 1)) * StrdDev;
    sq2 = Math.Sqrt(sq2 / (p + 1)) * StrdDev2;
    for (n = i0; n <= i0 + p; n++)
    {
    sqh[n] = median[n] + sq;
    sql[n] = (median[n] - sq);
    sqh2[n] = (median[n] + sq2);
    sql2[n] = (median[n] - sq2);
    }

    Median[0] = median[0];
    SQH[0] = sqh[0];
    SQH2[0] = sqh2[0];
    SQL[0] = sql[0];
    SQL2[0] = sql2[0];
    }

    #region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="Degree", Description="Degree", Order=1, GroupName="Parameters")]
    public int Degree
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="Period", Description="Period", Order=2, GroupName="Parameters")]
    public int Period
    { get; set; }

    [NinjaScriptProperty]
    [Range(0.1, double.MaxValue)]
    [Display(Name="StrdDev", Description="StrdDev", Order=3, GroupName="Parameters")]
    public double StrdDev
    { get; set; }

    [NinjaScriptProperty]
    [Range(0.1, double.MaxValue)]
    [Display(Name="StrdDev2", Description="StrdDev2", Order=4, GroupName="Parameters")]
    public double StrdDev2
    { get; set; }

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> Median
    {
    get { return Values[0]; }
    }

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> SQH
    {
    get { return Values[1]; }
    }

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> SQH2
    {
    get { return Values[2]; }
    }

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> SQL
    {
    get { return Values[3]; }
    }

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> SQL2
    {
    get { return Values[4]; }
    }
    #endregion

    }
    }

    Attached Files

    #2
    Hello JulieV,
    Thanks for your post.

    The first thing that I see that would effect the way things look is your Calculate setting.

    For the NT7 version this is set to CalculateOnBarClose = true

    For the NT8 version this is set to Calculate = Calculate.OnEachTick

    If you want these settings to be the same you should use Calculate = Calculate.OnBarClose for your converted script.

    Please see the following help guide documentation on Calculate for more information: https://ninjatrader.com/support/help...?calculate.htm
    JoshNinjaTrader Customer Service

    Comment


      #3
      Hi _JoshG
      Thanks for your response. I am in the process of moving all my algos from cTrader to NinjaTrader hence why there is some minor code changes that does not exactly reflect the NT7 PRC indicator such as the choice of calculating OnTick rather than on BarClose.
      I did find a series assignment issue in my code that seem to have fixed the plotting on the chart. The only difference now that I see is that on cTrader the PRC indicator plots with what appears to be "smoothed" lines whereas in NinjaTrader the lines are not. I can't seem to explain this change. I did look again at the code from cTrader and the indicator is definitely being plotted as a line with no smoothing factor included.

      Comment


        #4
        Originally posted by JulieV View Post
        Hi _JoshG
        Thanks for your response. I am in the process of moving all my algos from cTrader to NinjaTrader hence why there is some minor code changes that does not exactly reflect the NT7 PRC indicator such as the choice of calculating OnTick rather than on BarClose.
        I did find a series assignment issue in my code that seem to have fixed the plotting on the chart. The only difference now that I see is that on cTrader the PRC indicator plots with what appears to be "smoothed" lines whereas in NinjaTrader the lines are not. I can't seem to explain this change. I did look again at the code from cTrader and the indicator is definitely being plotted as a line with no smoothing factor included.
        If you do a search on Ninja or Futures.IO you will find NT8 versions of this indicator.

        Hopefully you are aware that this indicator "constantly recalculates and adjusts as price progresses" badly and only seems to project the highs and lows perfectly after the fact. In real time, it is a mouse trap and untradeable.

        Unfortunately, because it looks so good when charted it is popular with the Forex sim traders, thanks to a certain Egyptian snake oil salesman that popularized this "pig with lipstick on."

        Not recommended for trading.

        Cheers!

        Comment


          #5
          Hi aligator

          Thanks for your response. I have searched on Ninja and Futures.IO and could not find an NT8 version. No point recreating something if it is already freely available, right?! If you happen to stumble across the link again, I would appreciate if you could pass it on.

          I am aware that this version of the PRC indicator repaints. My current trading strategy only looks at the final values.

          Thanks for your advice on using this particular indicator. I will definitely keep it in mind as I develop strategies for futures contracts. What, in your opinion, is a better alternative?

          Many thanks

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by jrp2099, 01-19-2019, 11:20 AM
          4 responses
          26 views
          0 likes
          Last Post jrp2099
          by jrp2099
           
          Started by jxz1995, 12-23-2018, 10:44 PM
          4 responses
          26 views
          0 likes
          Last Post NinjaTrader_PatrickG  
          Started by sergey_z, Today, 03:42 AM
          5 responses
          12 views
          0 likes
          Last Post NinjaTrader_PaulH  
          Started by U0000999, Today, 04:24 AM
          0 responses
          5 views
          0 likes
          Last Post U0000999  
          Started by lehula, 01-18-2019, 08:13 PM
          1 response
          18 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Working...
          X