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

AddOnFramework.cs Orders Collection, example using LINQ?

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

    AddOnFramework.cs Orders Collection, example using LINQ?

    EDITED:
    Here is my example of using LINQ to query the collection of Orders.
    I didn't want to loop thru all orders, if I can get a subset first.

    Code:
    // LINQ Query ?
    var openOrders = from theOrder in accountSelector.SelectedAccount.Orders
    where
    ( theOrder.OrderState == OrderState.Accepted
    ||
    theOrder.OrderState == OrderState.Working
    )
    orderby theOrder.Id
    select theOrder;
    
    foreach (Order openOrder in openOrders)
    {
      // bonk bonk
    }
    thank you

    EDITED:
    My initial confusion was trying to figure out what type of collection the ~Acct~.Orders was.

    I used this code
    Code:
    var ordCollTyep = accountSelector.SelectedAccount.Orders.GetType();
    to return the type as System.Collections.ObjectModel.Collection<NinjaTra der.Cbi.Order>

    but trying to put all that in the type specifier in front of the local var "openOrders" would not compile, giving errors of
    "CS0266

    Cannot implicitly convert type 'System.Linq.IOrderedEnumerable<NinjaTrader.Cbi.Or der>' to 'System.Collections.ObjectModel.Collection<NinjaTr ader.Cbi.Order>'. An explicit conversion exists (are you missing a cast?)

    so after a few drinks, I somehow got the idea to just use the generic type of "var" to recv the results from the LINQ query, because all I really needed is the item inside the filtered LINQ results.

    the code at the beginning of this post is working code.

    any comments for a better method are welcome.

    Last edited by balltrader; 02-12-2021, 09:42 PM.

    #2
    Hello balltrader,

    Thanks for your post.

    This is more of a C# question than a NinjaScript specific question.

    We will leave the thread open for any community members who have written their NinjaScript code to utilize LINQ to share their comments.

    If you have any other NinjaScript specific inquiries, please don't hesitate to open a new thread.
    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by WHICKED, Today, 12:45 PM
    1 response
    10 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by samish18, Today, 01:01 PM
    0 responses
    5 views
    0 likes
    Last Post samish18  
    Started by WHICKED, Today, 12:56 PM
    0 responses
    8 views
    0 likes
    Last Post WHICKED
    by WHICKED
     
    Started by Spiderbird, Today, 12:15 PM
    2 responses
    11 views
    0 likes
    Last Post Spiderbird  
    Started by FrazMann, Today, 11:21 AM
    2 responses
    8 views
    0 likes
    Last Post NinjaTrader_ChristopherJ  
    Working...
    X