Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error in StartsWith or Instrument.FullName

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

    Error in StartsWith or Instrument.FullName

    Hi,

    I have a strange behaviour in using the String-function StartsWith.
    I save the Instrument.FullName in a String variable:

    Code:
    private String futureCode = null;
    In OnStartUp() ...

    Code:
    futureCode = Instrument.FullName;
    Later in Code, I want to enter an IF statement, when CL occurs first in this string:

    Code:
    print ("futureCode.StartsWith(CL) = " = futureCode.StartsWith("CL"));
    if (futureCode.StartsWith("CL) && 
        (<secondCondition> || <thirdCondition>)) 
    {
     ...
    }
    But the code-snippet is called even when I'm trading for example FDAX .... The print statement says, that the string has the value: FDAX 06-11, but nevertheless the IF statement is entered.

    Are there any issues with String and StartsWith? I use this very often and in other places in my strategy it works without a problem.

    Is there a better, recommended way to select the correct instrument?

    DT
    Last edited by DarthTraderson; 05-13-2011, 01:51 AM.

    #2
    I figured out, that the condition alone works in IF-statement.
    But with <secondCondition> or <thirdCondition> together in the same IF
    it doesn't work .... strange ....

    Is && an exclusive AND? I thought, that alle conditions must be validated to true in the IF with an &&?

    DT

    Comment


      #3
      DT, please have a look here - http://msdn.microsoft.com/en-us/libr...v=VS.100).aspx

      Not all conditions have to be 'true' in order for the logical AND in C# to return a 'true'.

      Comment


        #4
        Thank you, Bertrand, thats clear for me. When first operator is FALSE, second do not have to be validated.

        But in my case, see code above, only the second condition is evaluated to TRUE, first is FALSE and the IF is entered.

        Keep in mind, that I trade the FDAX with the above code an IF should never entered.
        You can programm it in your own code, it simple to code and print out the statements and conditions.

        In my opinion and years of experience with programming language an IF-statement can only be TRUE if all conditions are true ....

        Comment


          #5
          DT, would you share a script that I could run here to reproduce and look into? You can also email me directly to support at ninjatrader dot com - witih the info you provided here I was not able reproduce on my end but I'm unsure about your second / third conditions used.

          Thanks,

          Comment


            #6
            Bertrand, it was a bit tricky to identixe the issue. I tailored the code to one snippet and this on does not work ... trading market is FDAX for example ...

            Code:
            Print("Instrument.FullName                        : " +Instrument.FullName);
            Print("Instrument.FullName.StartsWith(CL) : " +Instrument.FullName.StartsWith("CL"));
                        
             if (Instrument.FullName.StartsWith("CL") &&
                (false) || (true))
             {
                Print("Inside ... CL market found ...");                
             }
              else {
                Print("Outside ... no CL ...");
              }
            My IF-clause in live code was a little bit mor complex so I guess the brackets were wrong so that at the end above code was produced.

            If you change the code to the following it seems to work:

            Code:
            Print("Instrument.FullName                        : " +Instrument.FullName);
            Print("Instrument.FullName.StartsWith(CL) : " +Instrument.FullName.StartsWith("CL"));
                        
             if (Instrument.FullName.StartsWith("CL") &&
                ((false) || (true)))
             {
                Print("Inside ... CL market found ...");                
             }
              else {
                Print("Outside ... no CL ...");
              }
            Thx
            DT

            Comment


              #7
              Precedence rules for logical operators means that your condition that does not give you the wanted result is thus.

              Instrument.FullName.StartsWith("CL") && (false) || (true)
              and would be evaluated as:

              (Instrument.FullName.StartsWith("CL") && (false)) || (true)
              which, of course, always evaluates to true.

              i.e., <any bool> OR true = true.
              Last edited by koganam; 09-11-2014, 05:11 PM.

              Comment


                #8
                @koganam: Thank you, I already found the issue as written above. But through the more complicated conditions I did not see the forgotten brackets.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                648 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                369 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                108 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                572 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                573 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X