Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Data Series as Parameters to Functions

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

    Data Series as Parameters to Functions

    HI,

    I am building the code for an indicator and am passing a custom data series as a parameter to a custom function. Unfortunately only the current calue of the series (i.e MySeries{0}) is being passed but not the previous series values - i.e. I can access MySeries[1] in the main but in the function the value of MySeries[1] is zero. The MySeries[0] value is correct in the main as well as in the function. Can you please provide a suggestion as to how I can pass an entire custom series as a parameter to a function? Any suggestions would be greatly appreciated. Thank you.

    #2
    Hi Zeos6,

    What you can do is move your function to its own indicator script. You can then pass in a data series as parameter. Otherwise you can build a function accepting DataSeries as input.
    Last edited by NinjaTrader_RyanM1; 06-09-2011, 01:40 PM.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Hi Ryan,

      Thanks for the reply. Wow, this seems like a very clunky approach - to build a new indicator just to hold custom series values. If a series can be an input to an indicator why can it not be an input to a method? I can understand that perhaps you don't want it to be a value type input but how about a pointer or a reference? Is this perhaps the approach taken to allow series to be inputs to other series and indicators?

      Comment


        #4
        Originally posted by Zeos6 View Post
        Hi Ryan,

        Thanks for the reply. Wow, this seems like a very clunky approach - to build a new indicator just to hold custom series values. If a series can be an input to an indicator why can it not be an input to a method? I can understand that perhaps you don't want it to be a value type input but how about a pointer or a reference? Is this perhaps the approach taken to allow series to be inputs to other series and indicators?
        You can pass a DataSeries to a function, and get the correct values from the passed series. It all depends on how the function is written. Unfortunately, that means that I would have to see the function implementation, before I could possibly see what might not be quite kosher.

        Comment


          #5
          Yes, it is possible besides creating a separate indicator. You just have to build the function accepting DataSeries as input. Thanks for the reply and hopefully this example helps you out.

          public int myCustomMethod (DataSeries myDataSeries)
          {
          if (myDataSeries[0] > 0)
          return 1;

          else
          return 0;
          }

          Last edited by NinjaTrader_RyanM1; 06-09-2011, 01:44 PM.
          Ryan M.NinjaTrader Customer Service

          Comment


            #6
            Hi Koganam,

            The calling statemnet in OnBarUpdate() is:

            if (fastAverageType == FastAverageTypes.fastEMA) fastAverage_Down.Set(EMA(trailingStopInputSeries, fastAveragePeriod)[0]);
            double? tStop = TDown(fastAverage_Down, slowAverage_Down);

            The function is:
            private double? TDown(DataSeries fastAverage, DataSeries slowAverage)
            {
            //find previous values of fast moving average
            double fastAvg = fastAverage[0];
            double fastAvg1 = fastAverage[1];
            double fastAvg3 = fastAverage[3];
            double fastAvg5 = fastAverage[5];
            ...
            }

            Thanks for your help.

            Comment


              #7
              Thanks Ryan,

              That is exactly what I have done but my previous values of the series do not seem to be coming through to the function.
              Attached Files
              Last edited by Zeos6; 06-09-2011, 01:52 PM.

              Comment


                #8
                Our support for this is limited -- This is more C# than NinjaScript. My suspicion is there is an issue with the return type you're expecting for this method. You're returning a double, which is only one value. If you need to return a series of values, would have to look into a different type, like a list.

                If you aren't really producing a value from the method, but want a routine to assign values, look at structuring as void.
                Last edited by NinjaTrader_RyanM1; 06-09-2011, 02:44 PM.
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  Thank you for the reply Ryan. This actually is a Ninjascript question. The question simply is whether past values of a DataSeries used as an input to a function can be retrieved in the function; i.e. Can I get a value for say MySeries{3} in the function.

                  Comment


                    #10
                    Yes, you can. Attached is a simple example you can work from.
                    Attached Files
                    Ryan M.NinjaTrader Customer Service

                    Comment


                      #11
                      Thank you very much Ryan.

                      Comment


                        #12
                        Something seems to be wrong with your method's signature, at the very least.

                        Code:
                        private double? TDown(DataSeries fastAverage, DataSeries slowAverage)
                        seems incorrect.

                        Correct would be:

                        Code:
                        private double? TDown([COLOR="Red"][B]I[/B][/COLOR]DataSeries fastAverage, [COLOR="red"][B]I[/B][/COLOR]DataSeries slowAverage)
                        I am actually a bit surprised that what you had even compiled at all.

                        ref: http://www.ninjatrader.com/support/h...dataseries.htm
                        Last edited by koganam; 06-09-2011, 07:24 PM.

                        Comment


                          #13
                          Hi Koganam,

                          Thanks for your reply. It is appreciated. The signature of the function seems correct. The question I have based on what you posted is whether I need to use DataSeries or IDataSeries in the function. I declare the custom series as DataSeries in Variables and I initialize them in Initialize(). I have tried to use IDataSeries and although I can declare them in Variables, I cannot create a new instance of them in Initialize (I get an error on compilation). So, do you declare and initialize as DataSeries and in the function use IDataSeries? The way I have it now is that I use DataSeries everywhere and it seems to be working fine.

                          On a side note, the thread was initially posted on the basis of whether one can obtain past values of a custom series in a custom function. The answer is yes and I have found my error. It turns out that you need to load the appropriate number of bars first to calculate all the parameters required by the custom series. I am creating a custom price type series as well for the custom average series and I was not loading sufficent number of bars to have the custom price type data available to perform the calculation so my values were coming out as 0.

                          Any clarification of when and how to use IDataSeries you may be able to provide would be greatly appreciated. Thank you very much.

                          P.

                          Comment


                            #14
                            So, do you declare and initialize as DataSeries and in the function use IDataSeries?
                            Pretty much, yes. IDataSeries is the interface object; DataSeries is just a collection.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                            0 responses
                            599 views
                            0 likes
                            Last Post Geovanny Suaza  
                            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                            0 responses
                            344 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by Mindset, 02-09-2026, 11:44 AM
                            0 responses
                            103 views
                            0 likes
                            Last Post Mindset
                            by Mindset
                             
                            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                            0 responses
                            558 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by RFrosty, 01-28-2026, 06:49 PM
                            0 responses
                            557 views
                            1 like
                            Last Post RFrosty
                            by RFrosty
                             
                            Working...
                            X