Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Autoenveloppe : Custom Indicator not compiling

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

    #16
    It's kind of difficult to explain.
    All the calculations described only have this one goal : calculate the values of the "ChannelBSize" dataSeries for each bar.

    Imagine I have 10 values in ChannelBSize :
    3-5-4-7-2-4-6-8-2-3
    each of these values are calculated for each bar, using the values of the preceding 100 bars.

    What I would like to have is a "SmoothChannel" DataSeries, where the ten values should be :
    3-3-3-3-3-3-3-3-3-3
    because 3 is the last value of ChannelBSize

    Imagine that the day after (or, the bar after), I get 11 values in ChannelBSize : imagine it is a 4 (the values can only change once a week in the code, but let's suppose I obtain a 4). ChannelBSize contains now :
    3-5-4-7-2-4-6-8-2-3-4
    And I want SmoothChannel to contain 11 values :
    4-4-4-4-4-4-4-4-4-4-4

    Once I have this "SmoothChannel", the calculations are simple :
    the upper channel is : myEMA(today) + SmoothChannel(today)*myEMA(today)/2
    the lower channel is : myEMA(today) - SmoothChannel(today)*myEMA(today)/2

    I don't think it is necessary to index myEMA, as it is normal that it changes everyday.
    It's really the "SmoothChannel" DataSeries that need to contain, for each historical bar, the value of "ChannelBSize", calculated for the last bar.

    Comment


      #17
      You are either setting values in the past, or you are not. If you are setting values only for today, what then is the point of the lastValue calculation at all? From what you describe, SmoothChannel(today) is the same as ChannelBSize for today, so everything else seems rather redundant.

      In other words, if you are setting values in the past, then those values must be indexed to the past. If you want to change the value of a dataSeries for 3 days ago, then you need to specify that you are changing the bar that is indexed to 3 days ago, etc.,

      Comment


        #18
        To make sure that everything is clear, and to show the necessity of the "lastValue" thing, I'll just take the upper channel, and discuss only daily charts :

        1. The value of the indicator for today is :
        myEMA(today) + ChannelBSize(today)*myEMA(today)/2

        2. The value of the indicator for yesterday is :
        myEMA(yesterday) + ChannelBSize(today)*myEMA(yesterday)/2

        3. The value of the indicator three days ago is :
        myEMA(threeDaysAgo) + ChannelBSize(today)*myEMA(threeDaysAgo)/2

        4. The value of the indicator four days ago is :
        myEMA(fourDaysAgo) + ChannelBSize(today)*myEMA(fourDaysAgo)/2

        I tried to create the "SmoothChannel" DataSeries, that contains, for all its values, the value of "ChannelBSize(today)", that should be recalculated everyday : So that, for all the bars, the formula should be :
        myEMA(today) + SmoothChannel(today)*myEMA(today)/2


        I've tried a number of things, including an index in the loop, but everything fails :

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

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



        I think I understand why it doesn't work : it's because the calculations and prints are made in the OnBarUpdate() procedure.
        If the indicator is calculated and plotted day after day (day1 is plotted, then day2 is plotted, then day3 is plotted, etc ...), then the first day is already calculated and plotted before doing the most recent bars : everything is done and finished with day 1, although I need the result of day 100 to calculate and print day 1 properly (on day 100, I will actually "recalculate the past 99 days") !
        I guess I should remove the "lastvalue" loop from the OnBarUpdate() ... or maybe remove the upper.set or lower.set from it.
        It seems to me that, to obtain the desired results, I only need to make the calculations for the last day, and, on this last day, calculate the whole indicator for all the previous days and print it.

        Is there a way to do this ? (I definitely know what I want to do, I try to explain the best I can, but I'm no expert on C#)

        Comment


          #19
          I tried to create the "SmoothChannel" DataSeries, that contains, for all its values, the value of "ChannelBSize(today)", that should be recalculated everyday : So that, for all the bars, the formula should be :
          myEMA(today) + SmoothChannel(today)*myEMA(today)/2
          As today is fixed at "0", every term in that expression is the constant fixed value from today, so that formula will produce a constant, which will plot as a horizontal line. Is that really what you want? In which, case, once again, as the expression evaluates to a constant, the lastValue calculation is rather to little purpose.

          Comment


            #20
            ... although I need the result of day 100 to calculate and print day 1 properly (on day 100, I will actually "recalculate the past 99 days") !
            In order to recalculate the value 100 days ago, you need to index the dataSeries to 100 days ago. That is what I have been saying all along.

            When you recalculate the value for 100 days ago, You MUST write:

            Code:
            Upper.Set(100, [I]recalculatedValue[/I]);
            and for 99 days ago,

            Code:
            Upper.Set(99, [I]recalculatedValue[/I]);
            etc.,

            You can manually code them once for each bar, or you can use a loop. Your choice.
            Last edited by koganam; 03-27-2011, 03:40 PM.

            Comment


              #21
              I tried to do it.
              The end of the code is (up and low are doubles) :

              //-----------------------------------
              int count = 256;
              for (int index = 0; index < count; index++)
              {
              up = EMA(Close, eMALength)[index] + lastValue*EMA(Close, eMALength)[index]/2;
              low = EMA(Close, eMALength)[index] - lastValue*EMA(Close, eMALength)[index]/2;
              Upper.set(index, up);
              Lower.set(index, low);
              }
              //-----------------------------------

              But I obtain an error on each "Upper.Set()" and "Lower.Set()" line :
              Translated from French, it approximately says :
              'NinjaTrader.Data.DataSeries' doesn't contain any definition for 'set' and no 'set' extension method accepting a first argument with type 'NinjaTrader.Data.DataSeries' has been found (is a using directive or an assembly reference missing ?)

              Comment


                #22
                You have: ...
                Upper.set(index, up);
                Lower.set(index, low);
                C# is case sensitive: set is not the same as Set.
                Last edited by koganam; 03-28-2011, 06:47 AM.

                Comment


                  #23
                  Ok it compiles now !
                  But it doesn't print anything anymore.... it's getting me mad.
                  Unless you see something wrong in the code (not in the formula, but in the way it is coded), I guess I will give up : it seems there is no way to smooth the whole channel.


                  protected override void OnBarUpdate()
                  {

                  double DistanceRatio =0;

                  double Channel=0;
                  double up=0;
                  double low = 0;
                  int length = 0;
                  double myEMA = 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];

                  int count = 256;
                  for (int index = 0; index < count; index++)
                  {
                  up = EMA(Close, eMALength)[index] + lastValue*EMA(Close, eMALength)[index]/2;
                  low = EMA(Close, eMALength)[index] - lastValue*EMA(Close, eMALength)[index]/2;
                  Upper.Set(index, up);
                  Lower.Set(index, low);
                  }

                  }

                  Comment


                    #24
                    You would probably have a log entry telling what the problem likely is.

                    Comment


                      #25
                      the log says :
                      Error on calling 'OnBarUpdate' method for indicator 'AutoEnveloppe' on bar 0: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

                      I think I need to set "count" to the number of bars that exist before the one on which the calculation "onbarupdate()" is made.
                      Is there an "easy way" to do it ?

                      Comment


                        #26
                        I think I need to set "count" to the number of bars that exist before the one on which the calculation "onbarupdate()" is made.
                        Is there an "easy way" to do it ?
                        CurrentBar is a 0 based index that increments with each new bar. You may be able find a use for it here.
                        Ryan M.NinjaTrader Customer Service

                        Comment


                          #27
                          It compiles and prints something good !!
                          YEAH !
                          Thanks a lot !

                          Comment


                            #28
                            Originally posted by Jeriz10 View Post
                            the log says :
                            Error on calling 'OnBarUpdate' method for indicator 'AutoEnveloppe' on bar 0: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

                            I think I need to set "count" to the number of bars that exist before the one on which the calculation "onbarupdate()" is made.
                            Is there an "easy way" to do it ?
                            OK. Kind of what I figured.

                            You have 2 viable choices.

                            1. You want to go back and recalculate 256 bars, so do not calculate until you have 256 bars. That means you put at the top of OnBarUpdate()

                            Code:
                            if (CurrentBar < 256) return;
                            That seems rather a lot of bars to escape. Hence the second alternative.

                            2. Go back and recalculate only as many bars are in the chart until you reach 256, after which recalculate only 256. That translates to "calculate up to a maximum of 256 bars", which we would code by changing:

                            Code:
                            int count = 256;
                            to

                            Code:
                            int count = Math.Min(CurrentBar, 256);
                            You may still have to escape the lookBack value with a

                            Code:
                            if (CurrentBar < lookBack) return;
                            at the start of OnBarUpdate().

                            Comment


                              #29
                              I did something simpler :
                              I had no real reason to recalculate only 256 bars ... it was chosen mainly because I usuallly have between 100 and 300 bars on my charts.

                              The last part of the code is just :

                              int count = CurrentBar; // this is what have changed
                              for (int index = 0; index < count; index++)
                              {
                              up = EMA(Close, eMALength)[index] + lastValue*EMA(Close, eMALength)[index]/2;
                              low = EMA(Close, eMALength)[index] - lastValue*EMA(Close, eMALength)[index]/2;
                              Upper.Set(index, up);
                              Lower.Set(index, low);
                              }

                              I would not say it is optimized, as on bar 50, it will recalculate bar 1, bar 2, bar 3, ..., bar 49 and bar 50 ; and then on bar 51, it will recalculate bar 1, bar 2, ...., bar 50, bar 51, etc till the more recent bar on the chart... so the first bar will be recalculated as many times as there are bars on the chart ... but at least it works !
                              If it can be optimized (I need at least 100 bars - Lookback is the parameter that contains 100), I'll do it ... but now that it works, it doesn't matter so much to me now

                              Comment


                                #30
                                Well, good enough. However, be aware that that recalculates the entire chart, no matter how many bars. As long as your maximum number of bars is 256, that is pretty much the same thing as the line that I gave you. Just remember that if you ever decide to use an infinite number of bars, well ...

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                                0 responses
                                574 views
                                0 likes
                                Last Post Geovanny Suaza  
                                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                                0 responses
                                333 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by Mindset, 02-09-2026, 11:44 AM
                                0 responses
                                101 views
                                0 likes
                                Last Post Mindset
                                by Mindset
                                 
                                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                                0 responses
                                553 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by RFrosty, 01-28-2026, 06:49 PM
                                0 responses
                                551 views
                                1 like
                                Last Post RFrosty
                                by RFrosty
                                 
                                Working...
                                X