Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Autoenveloppe : Custom Indicator not compiling

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

    Autoenveloppe : Custom Indicator not compiling

    Hello,

    I'm a new NinjaTrader user, and I'm trying to create an indicator (the Elder Autoenveloppe).
    But the language bothers me as I can't manage to compile my code.

    I'm a user of ProrealTime and the line I can't translate to NinjaTrader is the following :
    STD[NbBars](2*Max(Abs(High-avg) ,Abs(Low-avg)) / avg)
    where
    - STD : the "standard deviation" mathematical formula
    - NbBars is the number of bars on which to calculate the standard deviation
    - Max : a function that returns the greatest of the argument (Exemple : Max(X,Y) returns X if X>Y)
    - Abs : the "absolute value" of a parameter : Example : Abs(-5) = 5 or Abs(6) = 6
    - avg : it's the value of a exponential moving average.

    So, what I want to calculate is just the standard deviation, measured on "NbBars" days, of "the greatest distance between the high and the mean, or the low and mean, divided by the mean".

    As I could not retrieve the "max" (which has a different meaning in Ninja Trader) and "abs" functions, I had to re-do everything by hand.
    So far, I have :
    double myEMA = 0;
    double ABS1 = 0;
    double ABS2 = 0;
    double dif1 = 0;
    double dif2 = 0;
    double ABS = 0;
    double DistanceRatio =0;

    myEMA = EMA(Close, EMALength)[0];

    dif1 = High[0] - myEMA;
    if (dif1 >= 0)
    { ABS1 = (High[0] - myEMA); }
    else
    {ABS1 = (myEMA - High[0]);}

    if (Low[0] - myEMA > 0)
    {ABS2 = Low[0] - myEMA;}
    else
    {ABS2 = myEMA - Low[0];}

    if (ABS1 >= ABS2)
    {ABS = ABS1;}
    else
    {ABS = ABS2;}

    DistanceRatio = 2* (ABS/myEMA);

    ChannelSize=StdDev(DistanceRatio,NbBars)[0];

    But it won't compile it .... error CS1502 on the last line (StdDev).
    When I look at the doc, I don't understand why my parameters are incorrect.

    Can somebody help ?

    Second question : is there an equivalent to the metastock "lastValue" function ?

    Many thanks in advance !

    #2
    Hello Jeriz10,

    Welcome to the NinjaTrader forums!

    NinjaTrader uses the standard C# math methods for these. For help with their usage you can see a full list available here:
    Provides constants and static methods for trigonometric, logarithmic, and other common mathematical functions.


    In your snippet, I don't see where NbBars is declared at all so that is likely contributing. You can type in an integer value in its place or declare in the same place with the rest of your variables.

    Unfortunately I'm not familiar with MetaStocks lastValue, but hopefully another community member can offer input on that.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Hello,
      The "NbBars" is a parameter.
      I've changed it, and made the last line :
      ChannelSize=StdDev(DistanceRatio,100)[0];
      in order to remove the doubt.

      I get the same result ..
      Any other idea ?

      Concerning the lastValue function, it's kind of hard to explain ..
      Imagine you calculate, on 100 bars, the value of an indicator.
      For the first day, the indicator will have the "value01" value
      For the second day, the indicator will have the "value02" value
      For the third day, value03 ..... untill day 100, where the indicator will have "value100"

      lastvalue is a function that sets the value for the indicator at the last value that was calculated...
      So on day one, the value of the indicator will be "value100"
      on day two, "value100" ; on day three, "value100" ; etc ....
      In the case of the autoenveloppe, it helps create a smooth enveloppe around an EMA, calculated only on the last bars, but applied to all the bars .... (it recalculates the past !)
      Last edited by Jeriz10; 01-25-2011, 04:06 PM. Reason: More info on "lastValue()"

      Comment


        #4
        What is the descriptive error message you're getting? You may have to resize the Error column to see the whole message.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          The error is in french, but doesn't mean anything in french .. I can translate word-by-word ... maybe it could help:
          - the supercharged method corresponding the best to 'NinjaTrader.Indicator.Indicator.StdDev(NinjaTrade r.Data.|Dataseries, int)' has invalid arguments
          - Argument 1 : impossible to convert from 'double' to "NinjaTrader.Data.|Dataseries"

          Translating it enables me to understand the error I think : maybe I need to create a data series, representing the hundred values of "DistanceRatio" ?
          If this is the case, how can I do it ?

          Comment


            #6
            Yes, inputs for other indicators will have to be data series. There is a sample available on data series here:


            Please see here for additional help working with DataSeries class
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              Thanks,
              I'll look at it tomorrow - time to go to bed in France now ;o)

              Nobody for the metastock equivalent of LastValue() ?

              Comment


                #8
                I finally managed to make something :

                protected override void OnBarUpdate()
                {
                double myEMA = 0;
                double DistanceRatio =0;
                double Channel=0;

                myEMA = EMA(Close, eMALength)[0];

                myDataSeries.Set((2*Math.Max(Math.Abs(High[0]-myEMA),Math.Abs(Low[0]-myEMA))) / (myEMA)) ;
                ChannelSize.Set(StdDev(myDataSeries, lookBack)[0]*(stdDev/10));
                Channel = ChannelSize[0]*myEMA;

                Upper.Set(myEMA + Channel/2);
                Lower.Set(myEMA - Channel/2);
                }

                eMALength, stdDev and lookBack are parameters set to 22, 27 and 100.
                The indicator compiles and draws an autoenveloppe, but the channel size if a bit off.

                It seems to me (hard to test) that the "ChannelSize.Set()" doesn't use the last 100 values of "MyDataSeries" to calculate the Standard Deviation, but rather the first 100 (lookBack is set to 100).

                Can someone help ?
                Thanks !

                PS : Still nobody for the Metastock equivalent of LastValue() ?

                Comment


                  #9
                  StdDev does start some calculations at the first bar of the lookback, but not the first bar of the series. You can review the source code to see how it works through Tools > Edit NinjaScript > indicator. I would start simple and print values to see how this works.

                  You're also doing additional math on channel size which seems redundant to the StdDev method.
                  Ryan M.NinjaTrader Customer Service

                  Comment


                    #10
                    There is no redundancy in the maths, as I calculate the standard deviation of a channel length around an EMA... and I need to multiply that value by the value of the EMA and by 2.7, to get approximately 95% of all the values.

                    I think I can solve my problem, or at least understand it better, if I can revert the order of the values in "myDataSeries" :
                    For example, myDataSeries contains : "1, 2, 3, .., 100, 101, 102". I want it to contain : "102, 101, 100, ..., 3, 2, 1". Is there a simple function to do that (I would like to avoid creating another data series and a FOR loop) ?

                    Comment


                      #11
                      Unfortunately there's no built in method that reverses the order of data series inputs. It would need to be custom coded.
                      Ryan M.NinjaTrader Customer Service

                      Comment


                        #12
                        lastvalue is a function that sets the value for the indicator at the last value that was calculated...
                        So on day one, the value of the indicator will be "value100"
                        on day two, "value100" ; on day three, "value100" ; etc ....
                        In the case of the autoenveloppe, it helps create a smooth enveloppe around an EMA, calculated only on the last bars, but applied to all the bars .... (it recalculates the past !)
                        To do that in Ninjascript, you will need a recursive loop that sets the DataSeries backwards on each bar update.

                        One way might be:

                        Code:
                        double lastValue = SomeValue; // as determined by your code
                        int count = SomeIntValue; // as determined by you
                         		for (int index = 0; index < count; index++) 
                        		{
                        			PlotSomething.Set(index, lastValue);
                        		}

                        Comment


                          #13
                          I finally found out why the calculation was off !
                          When the standard deviation of the channel around the EMA is calculated, I need to multiply it by 2.7 (to get 95% of the values) ... but it seems that I would only get integers on this part (multiply by 3 or by 2, not 2.7)
                          I changed it to make it a double : now, it works great !
                          I'll try to smooth the autoenveloppe using the provided loop.
                          I'll let you know !

                          Comment


                            #14
                            Hi koganam
                            I can't manage to smooth it .. I've tried different things, but nothing works.
                            It's the "upper" and "lower" that are printed : I've tried to print them outisde and inside the recursive loop, but it's always the same result.

                            Here is what I get:

                            protected override void OnBarUpdate()
                            {

                            double myEMA = 0;
                            double DistanceRatio =0;

                            double Channel=0;
                            int length = 0;

                            myEMA = EMA(Close, eMALength)[0];
                            myDataSeries.Set((2*Math.Max(Math.Abs(High[0]-myEMA),Math.Abs(Low[0]-myEMA))) / (myEMA)) ;
                            ChannelSize.Set(StdDev(myDataSeries, lookBack)[0]*(nbStdDev));

                            if (Time[0].DayOfWeek == DayOfWeek.Monday)
                            {ChannelBSize.Set(ChannelSize[1]);}
                            else
                            {ChannelBSize.Set(ChannelBSize[1]);}

                            double lastValue = ChannelBSize[0]; // as determined by your code
                            int count = 256;
                            for (int index = 0; index < count; index++)
                            {
                            // SmoothChannel.Set(index, lastValue);
                            Upper.Set(myEMA + lastValue*myEMA/2);
                            Lower.Set(myEMA - lastValue*myEMA/2);
                            }

                            //Channel = SmoothChannel[0]*myEMA;
                            //Channel = lastValue * myEMA;
                            //Upper.Set(myEMA + Channel/2);
                            //Lower.Set(myEMA - Channel/2);

                            }

                            Any idea ?

                            Comment


                              #15
                              I am not sure what you really want to code, so it is hard to say what is wrong with the code last provided. I really just responded to your query about how to equivalently code the lastValue function from MetaStock.

                              However, even given your description, these last 2 statements will only do pretty much nothing in the loop, as they are setting only the current values on each pass through the loop. To adjust values in the past, the Set() function must be indexed. You have no index. If, as it looks like you want to do, you are setting values in the past, the myEMA will also need to be a dataSeties, and indexed too.

                              Upper.Set(myEMA + lastValue*myEMA/2);
                              Lower.Set(myEMA - lastValue*myEMA/2);
                              Then I guess your expressions would be:

                              Code:
                              Upper.Set(index, myEMA[index] + lastValue*myEMA[index]/2);
                              Lower.Set(index, myEMA[index] - lastValue*myEMA[index]/2);
                              where myEMA is itself a dataSeries, not a double. That means that you may have to adjust other places where you used myEMA as a double to also be specifically indexed some way, most probably as myEMA[0].

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              608 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              355 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              105 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              560 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              561 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X