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 md4866, 05-01-2024, 08:15 PM
    2 responses
    18 views
    0 likes
    Last Post md4866
    by md4866
     
    Started by samish18, Today, 12:20 PM
    0 responses
    7 views
    0 likes
    Last Post samish18  
    Started by thread, 04-15-2024, 11:58 PM
    3 responses
    48 views
    0 likes
    Last Post Georg1o
    by Georg1o
     
    Started by leojimenezp, 04-20-2024, 05:49 PM
    4 responses
    50 views
    0 likes
    Last Post leojimenezp  
    Started by nicthe, Today, 09:24 AM
    1 response
    9 views
    0 likes
    Last Post nicthe
    by nicthe
     
    Working...
    X