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

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'.
      BertrandNinjaTrader Customer Service

      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,
          BertrandNinjaTrader Customer Service

          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 Graci117, Today, 09:02 PM
                1 response
                8 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by ETFVoyageur, Today, 07:55 PM
                0 responses
                7 views
                0 likes
                Last Post ETFVoyageur  
                Started by janio973, Today, 07:24 PM
                1 response
                7 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by aligator, 01-06-2022, 12:14 PM
                4 responses
                248 views
                0 likes
                Last Post john_44573  
                Started by reynoldsn, Today, 05:56 PM
                0 responses
                15 views
                0 likes
                Last Post reynoldsn  
                Working...
                X