Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Passing a BarsArray as a function parameter

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

    Passing a BarsArray as a function parameter

    How can I pass a specific BarsArray to a function? Here is what I am trying:

    Code:
            public void test(double[] myBarsArray){
    
                Print(myBarsArray[1].Count);
    
    
            }

    #2
    Hello swcooke,

    Thank you for your post.

    Are you getting an error when you try to compile this?

    A BarsArray object would be of the type NinjaTrader.Data.Bars - not a double. Try this:

    Code:
            public void test(NinjaTrader.Data.Bars[] myBarsArray){
                   Print(myBarsArray[1].Count);
            }
    Please let us know if we may be of further assistance to you.

    Comment


      #3
      I am getting an error. Here is my revised code and the error:

      Code:
      namespace NinjaTrader.NinjaScript.Indicators
      {
          public class MyCustomIndicator : Indicator
          {
              public void test(NinjaTrader.Data.Bars[] myBarsArray){
                  Print(myBarsArray[1].Count);
              }
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description                                    = @"Enter the description for your new custom Indicator here.";
                      Name                                        = "MyCustomIndicator";
                      Calculate                                    = Calculate.OnBarClose;
                      IsOverlay                                    = false;
                      DisplayInDataBox                            = true;
                      DrawOnPricePanel                            = true;
                      DrawHorizontalGridLines                        = true;
                      DrawVerticalGridLines                        = true;
                      PaintPriceMarkers                            = true;
                      ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                      //Disable this property if your indicator requires custom values that cumulate with each new market data event. 
                      //See Help Guide for additional information.
                      IsSuspendedWhileInactive                    = true;
                  }
                  else if (State == State.Configure)
                  {
                      AddDataSeries("AAPL", new BarsPeriod {BarsPeriodType = BarsPeriodType.Day, Value = 1}, 30, "US Equities RTH", true);
                  }
                  else if (State == State.DataLoaded)
                  {
                      test(BarsArray[1]);
                  }            
              }
          }
      }
      Error:
      Code:
       [TABLE]
      [TR]
      NinjaScript File 			Error 			Code 			Line 			Column 		[/TR]
      [TR]
      [TD]MyCustomIndicator.cs[/TD]
       			[TD]The best overloaded method match for 'NinjaTrader.NinjaScript.Indicators.MyCustomIndicator.test(NinjaTrader.Data.Bars[])' has some invalid arguments[/TD]
       			[TD]CS1502[/TD]
       			[TD]56[/TD]
       			[TD]5[/TD]
       		[/TR]
      [/TABLE]
      [TABLE]
      [TR]
      NinjaScript File 			Error 			Code 			Line 			Column 		[/TR]
      [TR]
      [TD]MyCustomIndicator.cs[/TD]
       			[TD]Argument 1: cannot convert from 'NinjaTrader.Data.Bars' to 'NinjaTrader.Data.Bars[]'[/TD]
       			[TD]CS1503[/TD]
       			[TD]56[/TD]
       			[TD]10[/TD]
       		[/TR]
      [/TABLE]

      Comment


        #4
        Hello swcooke,

        Thank you for your reply.

        You're not passing the entire array to test().

        Try this:

        Code:
                    else if (State == State.DataLoaded) 
                     {                 test(BarsArray);             }
        Please let us know if we may be of further assistance to you.

        Comment


          #5
          Originally posted by swcooke View Post
          How can I pass a specific BarsArray to a function? Here is what I am trying:

          Code:
           public void test(double[] myBarsArray){
          
          Print(myBarsArray[1].Count);
          
          
          }
          You may be making things harder than necessary.

          My suggestion:
          Don't pass the array. BarsArray is already global.
          Pass the index instead.

          Code:
          public void test(int index)
          {
              Print(BarsArray[index].Count);
          }
          then just call test(1) where '1' is index of the BarsArray you wish to print the Count of.

          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