Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

array of most recent bars for all instruments?

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

    array of most recent bars for all instruments?

    I have a function in one of my scripts that takes for arguments the most recent close for all bars on the chart. So, if for instance I have 3 instruments, the function call would look like:
    Code:
    MyFunction(Closes[0][0], Closes[1][0], Closes[2][0])
    However, sometimes there will be more bars on a chart, sometimes less. Therefore I can't hard code the function arguments as above. Instead I'd like to pass a 1-dimensional array into the function, with each element representing the current close for each instrument.
    Code:
    MyFunction(my_array_of_current_bar_closes)
    Is there an easy way to represent this array, or would I have to create it. Something like Closes[:][0]?

    #2
    Hello,

    Thank you for the question.

    In this case, you could pass the whole Closes collection to the method and allow the method to separate the data as needed. You could also pass any type of object you want such as an array. I composed a small sample of both ways below.


    Code:
    protected override void OnBarUpdate()
    {
            MyFunction(Closes);
    	double[] myArr = new [] {Closes[0][0], Closes[1][0], Closes[2][0]};
    	MyFunction(myArr);
    }
    
    void MyFunction(PriceSeries[] closes)
    {
        if (closes.Length == 3)
        {
            Print(string.Format("Found 2 series: {0} - {1} - {2}", closes[0],closes[1]));
        } else if (closes.Length == 4) 
        {
    		Print(string.Format("Found 3 series: {0} - {1} - {2}", closes[0],closes[1], closes[2]));
        }
    }
    
    void MyFunction(double[] closes)
    {
       foreach(double close in closes)
       {
    		Print(close);   
       }
    }


    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Haiasi, Today, 06:53 PM
    1 response
    4 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by ScottWalsh, Today, 06:52 PM
    1 response
    12 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by ScottW, Today, 06:09 PM
    1 response
    5 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by ftsc2022, 10-25-2022, 12:03 PM
    5 responses
    256 views
    0 likes
    Last Post KeyonMatthews  
    Started by Board game geek, 10-29-2023, 12:00 PM
    14 responses
    244 views
    0 likes
    Last Post DJ888
    by DJ888
     
    Working...
    X