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

Search Array for value.

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

    Search Array for value.

    Im trying to search an array for a value and I found this on StackOverflow nut wont work in NT can anyone tell me why and if there is a way of doing this,


    In .NET 3.5 or higher, using LINQ:

    bool found = yourArray.Contains(yourValue);

    #2
    Hello GKonheiser

    Thank you for the question,

    You could try using the Array.Find method to search the array for a value and depending on the result you can use an if statement to make a true or false statement for a bool value.

    Here is a example that will run in NinjaScript

    Code:
    string[] names = {"Ninja", "Trader", "Strategy"};
    string result = Array.Find(names, element => element.StartsWith("Strategy", StringComparison.Ordinal));
    if(result != null)
    {
       Print("Found");
    } else {
       Print("Not Found");
    }
    Another way you could write this to directly convert the results to a bool would be using a ternary operator. Here is another example

    Code:
    bool foundOrNotFound = false;
    string[] names = {"Ninja", "Trader", "7"};
    string result = Array.Find(names, element => element.StartsWith("7", StringComparison.Ordinal));
    [B]foundOrNotFound = (result != null) ? true : false;[/B]
    Print(foundOrNotFound);
    Here is more information regarding the Array.Find method with some examples.
    Use the Array.Find and Array.FindLast methods to search arrays with lambda Predicates.


    Please let me know if I may be of additional assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks Jesse,

      I ended up doing this,

      Array.IndexOf(pivots, lev[i+1]) > -1

      haven't compiled yet thou so hope it works

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by rjbtrade1, 11-30-2023, 04:38 PM
      2 responses
      75 views
      0 likes
      Last Post DavidHP
      by DavidHP
       
      Started by Stanfillirenfro, Today, 07:23 AM
      3 responses
      11 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by FitSpressoHonest, Today, 09:14 AM
      0 responses
      1 view
      0 likes
      Last Post FitSpressoHonest  
      Started by Davide999, 05-18-2023, 03:55 AM
      4 responses
      557 views
      1 like
      Last Post kcwasher  
      Started by rexsole, Today, 08:39 AM
      2 responses
      7 views
      0 likes
      Last Post NinjaTrader_Erick  
      Working...
      X