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

How to do a modification to the StdDev() function?

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

    How to do a modification to the StdDev() function?

    I'm currently making a modification to the StdDev() function, calling it StdDevPlus(). Right now, StdDev() can take one input data series and a period. I want it to take 2 input data series, with the 2nd series being a moving average of my choice which will be used in the calculation of the standard deviation.

    The relevant section of code for StdDev() is here:

    protected override void OnBarUpdate()
    {
    if (CurrentBar < 1)
    {
    Value.Set(0);
    sumSeries.Set(Input[0]);
    }
    else
    {
    sumSeries.Set(Input[0] + sumSeries[1] - (CurrentBar >= Period ? Input[Period] : 0));
    double avg = sumSeries[0] / Math.Min(CurrentBar + 1, Period);
    double sum = 0;
    for (int barsBack = Math.Min(CurrentBar, Period - 1); barsBack >= 0; barsBack--)
    sum += (Input[barsBack] - avg) * (Input[barsBack] - avg);

    Value.Set(Math.Sqrt(sum / Math.Min(CurrentBar + 1, Period)));
    }
    }

    The bolded section is the section I want to change. Rather than using the calculated simple average, I want to pass the average from a series (like TEMA) and use that value.

    Let's say the second input is called Input2. So the bolded section would now look like this:

    sum += (Input[barsBack] - input2[barsBack]) * (Input[barsBack] - input2[barsBack]);

    What I can't figure out is to how to have an iDataSeries object passed as input2 as an argument in the function and then use it in this manner.


    Here's an example of MLQ4 code that does this and I'm trying to do the 2nd option:


    //
    //
    //
    // In the original article the second method is how deviation is
    // calculated . It is a questionableble method , but if we apply
    // standard calculation the bands tend to be "spiky".That is the
    // reason to have the "UseClassicalDeviations" option
    //
    //
    //

    double iDeviationPlus(double& array[], double& ma[], int period, int i)
    {
    double sum = 0;
    if (UseClassicalDeviations)
    for(int k=0; k<period; k++) sum += (array[i+k]-ma[i]) *(array[i+k]-ma[i]);
    else for( k=0; k<period; k++) sum += (array[i+k]-ma[i+k])*(array[i+k]-ma[i+k]);
    return(MathSqrt(sum/period));
    }

    #2
    I've also tried just to get the standard deviation of the zero lag TEMA indicator (posted in a recent thread) using the StdDev function but I get an error message when I try to compile.

    double stdDevValue = StdDev(ZeroLagTEMA, Period)[0];

    ...gives me two compiling error messages CS1502 and NT1503

    Argument 1 cannot convert from 'method group' to NinjaTrader.Data.IDataSeries

    Comment


      #3
      Hello,
      Thank you for your post.
      What you are wanting to do would be possible through using enumerations. We actually have a reference file on creating a moving average indicator that allows you to select different moving average. You would be able to add this into your StdDev indicator. Please see the reference at the following link,




      The compiler error that you are receiving is because you will need to define a period for the ZeroLagTEMA and enter in its overloads.
      Cody B.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by YngvaiMalmsteve View Post
        I've also tried just to get the standard deviation of the zero lag TEMA indicator (posted in a recent thread) using the StdDev function but I get an error message when I try to compile.

        double stdDevValue = StdDev(ZeroLagTEMA, Period)[0];

        ...gives me two compiling error messages CS1502 and NT1503

        Argument 1 cannot convert from 'method group' to NinjaTrader.Data.IDataSeries

        You cannot call the ZerolagTEMA without parameters. The correct syntax would be one of

        Code:
        double stdDevValue = StdDev(ZeroLagTEMA (temaPeriod), stdDevPeriod)[0];
        double stdDevValue = StdDev(ZeroLagTEMA (Input, temaPeriod), stdDevPeriod)[0];
        where temaPeriod is the period selected for the ZeroLagTEMA, and stdDevPeriod is the period selected for the standard deviation.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Javierw.ok, Today, 04:12 PM
        0 responses
        1 view
        0 likes
        Last Post Javierw.ok  
        Started by timmbbo, Today, 08:59 AM
        2 responses
        10 views
        0 likes
        Last Post bltdavid  
        Started by alifarahani, Today, 09:40 AM
        6 responses
        40 views
        0 likes
        Last Post alifarahani  
        Started by Waxavi, Today, 02:10 AM
        1 response
        18 views
        0 likes
        Last Post NinjaTrader_LuisH  
        Started by Kaledus, Today, 01:29 PM
        5 responses
        15 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Working...
        X