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 wzgy0920, 04-20-2024, 06:09 PM
    2 responses
    27 views
    0 likes
    Last Post wzgy0920  
    Started by wzgy0920, 02-22-2024, 01:11 AM
    5 responses
    32 views
    0 likes
    Last Post wzgy0920  
    Started by wzgy0920, 04-23-2024, 09:53 PM
    2 responses
    49 views
    0 likes
    Last Post wzgy0920  
    Started by Kensonprib, 04-28-2021, 10:11 AM
    5 responses
    193 views
    0 likes
    Last Post Hasadafa  
    Started by GussJ, 03-04-2020, 03:11 PM
    11 responses
    3,235 views
    0 likes
    Last Post xiinteractive  
    Working...
    X