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 andrewtrades, Today, 04:57 PM
    0 responses
    3 views
    0 likes
    Last Post andrewtrades  
    Started by chbruno, Today, 04:10 PM
    0 responses
    3 views
    0 likes
    Last Post chbruno
    by chbruno
     
    Started by josh18955, 03-25-2023, 11:16 AM
    6 responses
    436 views
    0 likes
    Last Post Delerium  
    Started by FAQtrader, Today, 03:35 PM
    0 responses
    6 views
    0 likes
    Last Post FAQtrader  
    Started by rocketman7, Today, 09:41 AM
    5 responses
    19 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Working...
    X