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

Setting Price type to anything other than close (NT 6.5)

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

    Setting Price type to anything other than close (NT 6.5)

    Hi,

    as i understand it from looking through the forums on NT 6.5 even though a ninja indicator code includes the line :

    PriceTypeSupported = true;

    it will not once compiled and running in mkt analyser prompt for the price type (its fixed on close) and requires hard coding in the indicator itself ??

    I am playing around with a simple regression indicator which i have compiled and seems to work perfectly and now i would like to see the results with the midpoint of a daily range (rather than close) ... How would i go about "hard coding" to replace "close" with another price type (in my case mid ie (close+open)/2 )

    I'm just using daily close data

    I have attached the working indicator code below .. just need to know how to "overide close" in NT 6.5 if thats possible

    thx

    ian



    #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>
    /// Linear regression slope - super fast calc with exponential weighting option
    /// </summary>
    [Description("Linear regression slope - super fast calc with exponential weighting option")]
    public class LinRegSlopeSFX : Indicator
    {
    #region Variables
    // Wizard generated variables
    private int period = 8; // Default setting for Period
    private bool exponential = false; // Default setting for Exponential
    private int returnval = 0; // Default setting for returnval
    // exponential moving regression slope variables
    private int nn = 0;
    private double n, w, sumX, sumX2, sumY1, sumXY1, correction;
    // super fast linear regression slope variables
    private double m1, m2, sumP, sumIP;
    // public results, updated each bar
    public double slope, yvalue, err;
    #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.FromKnownColor(KnownColor.Transparent), PlotStyle.Line, "SlopeSFX"));
    Add(new Line(Color.FromKnownColor(KnownColor.DarkOliveGree n), 0, "Zero"));
    Add(new Plot(Color.FromKnownColor(KnownColor.Lime), PlotStyle.Line, "AboveZero"));
    Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Line, "BelowZero"));
    CalculateOnBarClose = true; // MUST BE TRUE, WILL NOT WORK ON REAL TIME DATA
    Overlay = false;
    PriceTypeSupported = true;
    }
    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if (CurrentBar < 2) return;
    double rtn;
    int ix;
    if (exponential) {
    /*
    Exponential linear regression slope
    */
    double denom, sumY = 0.0, sumXY = 0.0;
    if (period != nn || CurrentBar < 1) { // re-initialize if period changes
    nn = period; // save new period
    n = (nn-1.0)*0.75 + 1.0; // lag correction
    sumX = 0.5 * n*(n-1.0); // sum of x values from 0 to n-1
    sumX2 = (n-1.0)*n*(2.0*n-1.0)/6.0; //sum of x^2 from 0 to n-1
    sumY = Input[0] * n; // initialize sum of Y values
    sumXY = sumX*Input[0]; // initialize sum of X*Y values
    w = 2.0 / (n + 1.0); // exponential weighting factor
    correction = (n+1.0) / (3.0*n); // amplitude correction factor
    if (CurrentBar < 1) {
    sumY1 = sumY; // would be sumY[1] in a series
    sumXY1 = sumXY; // would be sumXY[1] in a series
    }
    } else { // calculate sum of Y values and sum of X*Y values
    sumY = w*Input[0]*n + (1.0-w)*sumY1;
    sumXY = Input[0]*(n-1.0) + sumXY1*(n-2.0)/n;
    sumY1 = sumY; // save for next bar
    sumXY1 = sumXY; // save for next bar
    }
    denom = n*sumX2 - sumX*sumX; // denominator of slope formula
    if (denom < 0.00001) denom = 0.00001;
    slope = correction * n*(n*sumXY - sumX*sumY) / denom;
    yvalue = sumY/n + slope * 0.5*n; // regression value, public variable
    }
    else {

    int ix1;
    // Re-initialize 0th bar, every 10000 bars, or when Period changes
    if (CurrentBar < period || nn != period || CurrentBar % 10000 == 0) {
    nn = period;
    m1 = 6.0 / ((double)period * ((double)period + 1.0));
    m2 = 2.0 / ((double)period - 1.0);
    sumP = 0.0; sumIP = 0.0;
    // this loop is executed only during initialization
    for (ix = 0; ix < period; ++ix) {
    ix1 = (ix < CurrentBar) ? ix : CurrentBar;
    sumP += Input[ix1];
    sumIP += (ix1 * Input[ix1]);
    }
    } else {
    // Linear regression slope super fast calculation
    sumIP += (sumP - period * Input[period]);
    sumP += (Input[0] - Input[period]);
    }
    slope = m1 * (sumP - m2 * sumIP);
    yvalue = sumP/period + slope * period * 0.5;
    }
    switch (returnval) {
    case 3:
    rtn = 0.0;
    if (CurrentBar > 1) {
    for (ix = 0; ix < ((CurrentBar <= period) ? CurrentBar+1 : period); ++ix) {
    rtn = (Input[ix] - (-slope * ix + yvalue));
    err += rtn*rtn;
    }
    rtn = err = Math.Sqrt(err/ix);
    }
    break;
    case 2: rtn = yvalue; break;
    default: rtn = slope; break;
    }
    SlopeSFX.Set(rtn); // result
    if (SlopeSFX[0] > 0)
    { AboveZero.Set(SlopeSFX[0]); AboveZero.Set(1, SlopeSFX[1]); }
    else
    { BelowZero.Set(SlopeSFX[0]); BelowZero.Set(1, SlopeSFX[1]); }
    }
    #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 SlopeSFX
    {
    get { return Values[0]; }
    }
    [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 AboveZero
    {
    get { return Values[1]; }
    }
    [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 BelowZero
    {
    get { return Values[2]; }
    }
    [Description("Lookback interval")]
    [Category("Parameters")]
    public int Period
    {
    get { return period; }
    set { period = Math.Max(1, value); }
    }
    [Description("true=exponential moving slope, false=normal weighting")]
    [Category("Parameters")]
    public bool Exponential
    {
    get { return exponential; }
    set { exponential = value; }
    }
    [Description("Return value 1=slope, 2=LinReg value, 3=average error")]
    [Category("Parameters")]
    public int Returnval
    {
    get { return returnval; }
    set { returnval = Math.Max(1, value); }
    }
    #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 LinRegSlopeSFX[] cacheLinRegSlopeSFX = null;
    private static LinRegSlopeSFX checkLinRegSlopeSFX = new LinRegSlopeSFX();
    /// <summary>
    /// Linear regression slope - super fast calc with exponential weighting option
    /// </summary>
    /// <returns></returns>
    public LinRegSlopeSFX LinRegSlopeSFX(bool exponential, int period, int returnval)
    {
    return LinRegSlopeSFX(Input, exponential, period, returnval);
    }
    /// <summary>
    /// Linear regression slope - super fast calc with exponential weighting option
    /// </summary>
    /// <returns></returns>
    public LinRegSlopeSFX LinRegSlopeSFX(Data.IDataSeries input, bool exponential, int period, int returnval)
    {
    checkLinRegSlopeSFX.Exponential = exponential;
    exponential = checkLinRegSlopeSFX.Exponential;
    checkLinRegSlopeSFX.Period = period;
    period = checkLinRegSlopeSFX.Period;
    checkLinRegSlopeSFX.Returnval = returnval;
    returnval = checkLinRegSlopeSFX.Returnval;
    if (cacheLinRegSlopeSFX != null)
    for (int idx = 0; idx < cacheLinRegSlopeSFX.Length; idx++)
    if (cacheLinRegSlopeSFX[idx].Exponential == exponential && cacheLinRegSlopeSFX[idx].Period == period && cacheLinRegSlopeSFX[idx].Returnval == returnval && cacheLinRegSlopeSFX[idx].EqualsInput(input))
    return cacheLinRegSlopeSFX[idx];
    LinRegSlopeSFX indicator = new LinRegSlopeSFX();
    indicator.BarsRequired = BarsRequired;
    indicator.CalculateOnBarClose = CalculateOnBarClose;
    indicator.Input = input;
    indicator.Exponential = exponential;
    indicator.Period = period;
    indicator.Returnval = returnval;
    indicator.SetUp();
    LinRegSlopeSFX[] tmp = new LinRegSlopeSFX[cacheLinRegSlopeSFX == null ? 1 : cacheLinRegSlopeSFX.Length + 1];
    if (cacheLinRegSlopeSFX != null)
    cacheLinRegSlopeSFX.CopyTo(tmp, 0);
    tmp[tmp.Length - 1] = indicator;
    cacheLinRegSlopeSFX = tmp;
    Indicators.Add(indicator);
    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>
    /// Linear regression slope - super fast calc with exponential weighting option
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.LinRegSlopeSFX LinRegSlopeSFX(bool exponential, int period, int returnval)
    {
    return _indicator.LinRegSlopeSFX(Input, exponential, period, returnval);
    }
    /// <summary>
    /// Linear regression slope - super fast calc with exponential weighting option
    /// </summary>
    /// <returns></returns>
    public Indicator.LinRegSlopeSFX LinRegSlopeSFX(Data.IDataSeries input, bool exponential, int period, int returnval)
    {
    return _indicator.LinRegSlopeSFX(input, exponential, period, returnval);
    }
    }
    }
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    public partial class Strategy : StrategyBase
    {


    <<FORUM CUT OFF >>

    #2
    Hello hyena

    Thank you for your post.

    PriceTypeSupport is a NT V7 property.

    For 6.5, You would want to set PriceType = PriceType.Median property in the Initialize() section:



    You will find a list of all the supported price types in the link above.
    MatthewNinjaTrader Product Management

    Comment


      #3
      Matthew,

      hi

      Many thanks .. for other newbies out these these are the changes i made

      changed existing
      ----
      PriceTypeSupported = true;
      ---

      to

      ---
      PriceTypeSupported = false;
      PriceType = PriceType.Median;
      ----

      and after recompiling it didnt work BUT i remembered i have to delete the indicator off the charts and re-add it again (or NT6.5 doesnt recognise the changed indicator code).

      Also I dont see the word median in the indicator narrative on the chart - by this i mean i see ...(close,5) in the stock CLOSE indicator narrative on the chart and for my changed MEDIAN one i just see ...(5) ... so dont worry as i say i have hand calculated it and it seems to work just fine :-)


      b.r.

      ian

      Comment


        #4
        hi,

        I wonder if i can ask another question (i'm talking daily uploaded close data source at the mmt).

        I now have a great working regression indicator BUT i want to take it a step further and create an indicator that calculates the difference between the 8 day regression slope ending yesterday and that of one ending today.

        I'm guessing that i cant simply call the regression procedure (in my regression indicator) from within a new indicator as it has no means of being passed the day the series ends (ie the objective is to calculate a 8 day regression ending today and yesterday and outputing the difference)

        LinRegSlopeSFXMedian (Input, exponential, period, returnval);

        any tips or pointers on how to reference and carry out calculations on a series ending anything other than close today - I have in mind recycling the calculation logic from my regression indicator if i can work out how to do it on anything other than ending close today


        thx

        ian

        Comment


          #5
          Originally posted by hyena View Post
          ... I'm guessing that i cant simply call the regression procedure (in my regression indicator) from within a new indicator as it has no means of being passed the day the series ends (ie the objective is to calculate a 8 day regression ending today and yesterday and outputing the difference)

          LinRegSlopeSFXMedian (Input, exponential, period, returnval);

          any tips or pointers on how to reference and carry out calculations on a series ending anything other than close today - I have in mind recycling the calculation logic from my regression indicator if i can work out how to do it on anything other than ending close today


          thx

          ian
          Yes, you can. The indicator's value yesterday is Indicator(parameterList[])[1], and the value today is Indcator(parameterList[])[0]. You just need to call the indicator itself with the correct parameters.

          Comment


            #6
            Koganam,

            hi

            I had had a bash at this but being a newbie ran in to problems :-(

            I created a new indicator with the aim of calling the regression indicator to get todays and yesterdays regression value and then do a quick % change between the 2 calculation before making the result a plot and visible in market analyser

            So i thought i would start off with just calling the working regression indicator, passing the necessary inputs and hoping the correct answer was returned .. sadly i just got zero returned every time (though it did plot something which wasnt the correct answer.. i'm suspecting its something to do with the scope of the variables defined in the regression indicator called maybe ?

            #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>
            /// Change in Regression Slope
            /// </summary>
            [Description("Change in Regression Slope")]
            public class RegSlopeChange : Indicator
            {
            #region Variables
            // Wizard generated variables
            private int inputperiod = 5; // Default setting for Inputperiod
            // User defined variables (add any user defined variables below)
            private double Slope;
            #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.FromKnownColor(KnownColor.Orchid), PlotStyle.Line, "SlopeChange"));
            CalculateOnBarClose = false;
            Overlay = false;
            PriceTypeSupported = false;
            }
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
            // Use this method for calculating your indicator values. Assign a value to each
            // plot below by replacing 'Close[0]' with your own formula.
            //SlopeChange.Set(Close[0]);


            Slope = LinRegSlopeSFXMedian (false, 5,1)[0];
            SlopeChange.Set(Slope);




            }
            <CUT OFF>

            Comment


              #7
              hyena,

              I believe the forum cut off the rest of your code. Would you be able to post just the relevant methods and expressions you are using in your calculation? If not, you can always uploaded your script as an attachment and post it this way.
              MatthewNinjaTrader Product Management

              Comment


                #8
                Originally posted by hyena View Post
                Koganam,

                hi

                I had had a bash at this but being a newbie ran in to problems :-(

                I created a new indicator with the aim of calling the regression indicator to get todays and yesterdays regression value and then do a quick % change between the 2 calculation before making the result a plot and visible in market analyser

                So i thought i would start off with just calling the working regression indicator, passing the necessary inputs and hoping the correct answer was returned .. sadly i just got zero returned every time (though it did plot something which wasnt the correct answer.. i'm suspecting its something to do with the scope of the variables defined in the regression indicator called maybe ?

                #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>
                /// Change in Regression Slope
                /// </summary>
                [Description("Change in Regression Slope")]
                public class RegSlopeChange : Indicator
                {
                #region Variables
                // Wizard generated variables
                private int inputperiod = 5; // Default setting for Inputperiod
                // User defined variables (add any user defined variables below)
                private double Slope;
                #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.FromKnownColor(KnownColor.Orchid), PlotStyle.Line, "SlopeChange"));
                CalculateOnBarClose = false;
                Overlay = false;
                PriceTypeSupported = false;
                }
                /// <summary>
                /// Called on each bar update event (incoming tick)
                /// </summary>
                protected override void OnBarUpdate()
                {
                // Use this method for calculating your indicator values. Assign a value to each
                // plot below by replacing 'Close[0]' with your own formula.
                //SlopeChange.Set(Close[0]);


                Slope = LinRegSlopeSFXMedian (false, 5,1)[0];
                SlopeChange.Set(Slope);




                }
                <CUT OFF>
                Any errors in the log?
                Have you checked to be sure that your primary (called) indicator is not returning the same values?

                Comment


                  #9
                  hi,

                  indicator code attached in zip

                  RegSlopeChange - indicator created to make calls to regression indicator and carry out % change calculation and plot and show in mkt analyser

                  LinRegSlopeSFXMedian - regression indicator


                  The RegSlopeChange indicator code is messy as ive being testing with other indicators which i've just commented out

                  thanks for your help :-)


                  best rgds

                  ian
                  Attached Files

                  Comment


                    #10
                    hi guys

                    I have had a look at the data (see image below) where the top plot is the indicator calling the regression indicator and the bottom plot the regression indicator itself.

                    All the values are identical between indicators with the exception of the current day which shows 0 for some reason on the indicator which calls the regression indicator ... hence the 0 also shows up in the market analyser

                    Any ideas how to get the correct value to show up for the current day



                    rgds

                    ian

                    Comment


                      #11
                      Hello Ian,

                      From the source code of the LinRegSlopeSFXMedian, there is line that indicates that this indicator cannot be called in real-time:

                      CalculateOnBarClose = true; // MUST BE TRUE, WILL NOT WORK ON REAL TIME DATA

                      Please try setting your RegSlopeChange indicator to COBC = True and test again for issues.
                      MatthewNinjaTrader Product Management

                      Comment


                        #12
                        Originally posted by hyena View Post
                        hi guys

                        I have had a look at the data (see image below) where the top plot is the indicator calling the regression indicator and the bottom plot the regression indicator itself.

                        All the values are identical between indicators with the exception of the current day which shows 0 for some reason on the indicator which calls the regression indicator ... hence the 0 also shows up in the market analyser

                        Any ideas how to get the correct value to show up for the current day



                        rgds

                        ian
                        The current day value cannot show up because that bar has not closed, and the indicator calculates only on the close of the bar.

                        Comment


                          #13
                          hi guys, many thanks for the help


                          1. changed the base indicator called code to be


                          CalculateOnBarClose = false;

                          and it worked just fine then :-)


                          2. the other problem i has was trying to use the call with [1] instead of [0]

                          i got the error message

                          Error on calling the 'OnBarUpdate' method for indicator 'RegSlopeChange' on bar 0: Index was out of range.

                          So i did a bit of googling and after adding a couple of lines of code it works just fine

                          the code i added was :

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

                          <<ETC>>


                          thanks for all your help

                          rgds

                          ian

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by alifarahani, Today, 09:40 AM
                          6 responses
                          29 views
                          0 likes
                          Last Post alifarahani  
                          Started by Waxavi, Today, 02:10 AM
                          1 response
                          17 views
                          0 likes
                          Last Post NinjaTrader_LuisH  
                          Started by Kaledus, Today, 01:29 PM
                          5 responses
                          13 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by Waxavi, Today, 02:00 AM
                          1 response
                          12 views
                          0 likes
                          Last Post NinjaTrader_LuisH  
                          Started by gentlebenthebear, Today, 01:30 AM
                          3 responses
                          17 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Working...
                          X