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

Account.Name Contains a certain character

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

    Account.Name Contains a certain character

    I use the following script to run my strategy on certain accounts:

    if (Account.Name != "Demo539891" || Account.Name != "Playback101" || Account.Name != "201331")
    {
    Print("Strategy running on account: " + Account.Name + " Closing strategy.");
    CloseStrategy("XYZ");
    }​

    If I wanted to run the strategy on all Accounts that contain the letter "D" instead of the specific account "Demo539891" how would I change the above script to run all accounts that contain the character "D".... sorry, I am not very good with C#

    #2
    For just a single letter, you have lots of choices.

    One of the more versatile is the StartsWith method.

    Code:
    if (Account.Name.StartsWith("D"))
    {
        ...
    }
    For more information, try this Google search.

    Comment


      #3
      Oh, sorry, you said 'contains letter D', not 'starts with letter D'.

      No problem. Fortunately, there is the Contains method.

      Code:
      if (Account.Name.Contains("D"))
      {
          ...
      }
      For more information, try this Google search.

      Comment


        #4
        Will this work?

        f (Account.Name.StartsWith ("D" ) || Account.Name != "Playback101" || Account.Name != "201331")
        {
        Print("Strategy running on account: " + Account.Name + " Closing strategy.");
        CloseStrategy("XYZ");
        }​​

        Comment


          #5
          Well, yeeessss ... but you have excess logic in the
          if statement that provides no value.

          Think about it, if you're testing the start of the string
          and confirming that its starts with letter 'D', the
          remaining tests are completely unnecessary.

          -=o=-

          Let me explain.

          An if statement provides what is sometimes call a 'filter'.

          You have 3 filters in your if statement.

          The if statement begins by guaranteeing that the string
          Account.Name starts with letter 'D' -- that's the first filter.

          The 2nd filter does not refine the 1st filter, so it is useless
          and provides no benefit. Same for the 3rd filter.

          Why?
          Because when a string starts with 'D' it cannot possibly
          also start with a 'P' -- so the 2nd filter is useless, and
          is simply never used. Do you see that?

          For that if statement, that 1st filter is all you need.

          Subsequent filters in an if statement are used to refine
          the filters before it -- this is the logic of what you have
          written, 'filtering' is what the if statement is all about.

          Study bool logic to understand more about what the
          if statement is really doing under the hood.

          Comment


            #6
            Well the problem is that I need the Playback 101 and 201331 also included so that the strategy will run on any account that starts with D which there may be many and the other two specific accounts.

            Comment


              #7
              Interestingly, I use the exact same logic as EminiMES often. It's useful because the || operator (the conditional logical OR operator) short-circuits the testing of sequential tests as soon as the first test in the sequence is true. So, it effectively becomes a series of "independent" boolean tests where the first test to be true ends the series of potential tests. Of course, if none of the tests is true, it's like any other boolean outcome that is false.

              The advantage of this approach is to eliminate unnecessary boolean tests when any true result is found. It's particularly useful for a series of either unrelated tests, or tests that progressively narrow scope, that end up needing to execute the same outcomes (whether that be true or false).

              For example
              Code:
              if (A || B || C || D || E)
                  DoThisTrueResultStuff();
              else
                  DoThisFalseResultStuff();
              ​
              where A, B, C, D and E may have nothing in common except the need to do the same true/false outcomes. On the other hand, A may eliminate a condition that is common, B refines on the basis of a slightly less common, related condition, etc until E is reached for a very specific condition that is essentially a limited scope condition reached by a process of elimination.

              This latter scope reduction approach is very useful for things like WasThisFantastic || WasThisSufficientlyGreat || WasThisAcceptable || WasThisJustSatisfactory ... etc, where the tests are seeking the outcome on the basis of the decreasing probability of mutually exclusive, yet related, test conditions; e.g. a series of related flags from most likely to least.

              A practical use in NinjaTrader could be along the lines of testing related AccountItems such as various margin requirements, where the actions would be similarly of the nature of "good to go" or "not possible".

              The descriptions here may be nebulous, but the usage can be well-defined. The same logic obviously also can apply to the conditional logical AND operator (&&).

              Thanks.
              Multi-Dimensional Managed Trading
              jeronymite
              NinjaTrader Ecosystem Vendor - Mizpah Software

              Comment


                #8
                Final solution:

                if (Account.Name.Contains("DEMO") != true && Account.Name != "Playback101" && Account.Name != "201331")
                {
                Print("Strategy running on account: " + Account.Name + " Closing strategy.");
                CloseStrategy("XYZ");
                }

                Thanks for all the help!​

                Comment


                  #9
                  EminiMES You can simplify your final approach just a little, if you wish:
                  Code:
                  Account.Name.Contains("DEMO") != true
                  … could be …
                  Account.Name.Contains("DEMO") == false
                  … or, my preferred approach …
                  !Account.Name.Contains("DEMO")
                  Entirely up to you and your preferred coding conventions, but good to consider alternatives that might read better or be more efficient.

                  One final comment on the whole discussion. Using conditional logical OR with most probable to least in order is like an ANY function, and using conditional logical AND with least probable to most in order is like an ALL function, both being as efficient as you can make them.

                  Good luck with all your programming and trading.

                  Thanks.
                  Multi-Dimensional Managed Trading
                  jeronymite
                  NinjaTrader Ecosystem Vendor - Mizpah Software

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by DJ888, Today, 10:57 PM
                  0 responses
                  6 views
                  0 likes
                  Last Post DJ888
                  by DJ888
                   
                  Started by MacDad, 02-25-2024, 11:48 PM
                  7 responses
                  158 views
                  0 likes
                  Last Post loganjarosz123  
                  Started by Belfortbucks, Today, 09:29 PM
                  0 responses
                  7 views
                  0 likes
                  Last Post Belfortbucks  
                  Started by zstheorist, Today, 07:52 PM
                  0 responses
                  7 views
                  0 likes
                  Last Post zstheorist  
                  Started by pmachiraju, 11-01-2023, 04:46 AM
                  8 responses
                  151 views
                  0 likes
                  Last Post rehmans
                  by rehmans
                   
                  Working...
                  X