Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Passing an array as argument to a function

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

    Passing an array as argument to a function

    Hello!

    Have a look please to the following piece of code and I explain afterwards what the problem is.

    HTML Code:
    if (State == State.SetDefaults)
    {AddPlot(new Stroke(Brushes.Orange, 2), PlotStyle.TriangleUp, "Buys");
    AddPlot(new Stroke(Brushes.Orange, 2), PlotStyle.TriangleDown, "Sells");}
    HTML Code:
    protected override void OnBarUpdate()
    {if (myCondition) Step1(Buys, Sells, value1);}
    HTML Code:
    #region Helpers
    int Step1(ref double[] array1, ref double[] array2, int value2)
    {//myCode}
    #endregion
    After compiling, I receive the following error messages:
    1) Not valid arguments. (In the section "protected override void OnBarUpdate()")
    2) Argument 1 and argument 2: Conversion of Ninjatrader.Ninjatrader.Series <douvle> in ref []double not possible.

    I failed here to find a solution.
    If the conversion of ref double[] array1 and ref double[] array2 to Buys and Sells is not possible, what could be the correct way to write the code in the section protected override void OnBarUpdate() oder in the section #region Helpers?

    Thanks in advance!

    #2
    Hello Stanfillirenfro,

    The NinjaScript Series type is not an array so you can't define a method like that if you want to pass a series. You would need to use Series<double> in place of your double[]. You also don't need the ref keyword if you are passing a series to work with it in a local method.

    Please let me know if I may be of further assistance.

    Comment


      #3
      Many thanks Jesse for your reply.

      I try it now and will revert to you for feedback.

      Comment


        #4
        Jesse,as far as I know, the declaration of Series<double> occurs just at he beginning of

        HTML Code:
        public class myIndicator : Indicator
        { #region Variables
           private Series<double> xxx1
          #enregion
        and its initialization occurs in

        HTML Code:
        else if (State == State.DataLoaded)
        {
        xxx1 = new Series<double>(this);}
        In my case,, I am in a function in a section #region Helpers. I guess I should declare the Series<double> in the function int Step1(Series<double> xxx1). Should I initialize it also in this function as follow?
        HTML Code:
        #region Helpers
        int Step1(Series<double> xxx1)
        {xxx1 = new Series<double>(this);}
        #enregion
        Thanks!

        Comment


          #5
          Hello Stanfillirenfro,

          A C# function has any parameters within its parenthesis :
          Code:
          MyFunction(Type1 parameter1, Type2 paramter2, etc..)
          It would look like
          Code:
          MyFunction(Series<double> mySeries)
          Please let me know if I may be of further assistance.

          Comment


            #6
            Thnaks Jesse fo your reply. Nice!
            But Jesse the next step which is usally

            HTML Code:
            mySeries = new Series<double>(this);
            needs to be writing somewhere or I do not need it in my function? Is just the declaration enough?

            Comment


              #7
              Hello Stanfillirenfro,

              You don't need to re define a new series in your function, you would just use the series you had passed in to the method. The series would be defined in your scripts class just as the help guide shows. You could then pass that mySeries into your method
              Code:
              MyFunction(mySeries);
              The definition of the function would look like this:

              Code:
              MyFunction(Series<double> mySeries)
              {
                // use mySeries here
              }


              Please let me know if I may be of further assistance.

              Comment


                #8
                Thanks Jesse for your reply.

                It is a bit confusing for me here.
                What I have learned with Series is that before using them one should make a dynamic allocation with new such as folowed:

                HTML Code:
                mySeries = new Series<double>(this);
                In your reply
                HTML Code:
                "
                MyFunction(Series<double> mySeries) { // use mySeries here }"
                should I make any dynamic allocation of mySeries before using it?

                Thanks!

                Comment


                  #9
                  Hello Stanfillirenfro,

                  You can see the help guide sample for an example of how to make a series, there is not another way to make a series. https://ninjatrader.com/support/help...t8/seriest.htm

                  You can only use the series after it had been created so depending on what your method does would determine where the call to that method goes. It could go on the following line after the series is created or you can use that method from OnBarUpdate or the other areas which follow the creation of the series.

                  Please let me know if I may be of further assistance.

                  Comment


                    #10
                    Thanks you Jesse for your help.
                    It is becoming clearer. I wil revert to you if I face another problem.

                    Many thanks!

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    595 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    343 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
                    556 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    554 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X