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

Using bools to improve code efficiency

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

    Using bools to improve code efficiency

    Hi

    Suppose I have:

    If( XYZ bool= true)
    {do abc}

    Vs

    {do abc}

    In the first scenario, if I set the bool to false, will NT essentially "skip" over the code {do abc} or will it still be going through the code. Because In the second scenario, NT will read the entire {do abc}. I'm asking this because my code is really long and I'm trying to improve the run time during strategy analyzer. Within this particular strategies, I have many scenarios where I can essentially "ignore to run" a particular piece of the code. So I was thinking maybe I can use bools. This way I can make the strategy run faster. Problem is, my strategy has 10 instruments, im loading last tick, bid tick and ask tick data. I've got 4 CPU and 8GB of ram and 500 GB of disk storage but still it's taking my up to 1 hour to go through 1 year worth of data in strategy analyzer. And if I have to optimize, then I'm looking at, at the least, 1 day run time.

    Thanks.

    #2
    It will skip over.

    This is any programming logic.

    Comment


      #3
      Originally posted by sledge View Post
      It will skip over.

      This is any programming logic.
      Thank you!

      Comment


        #4
        I have an elementary bool question. Please ignore the OP. But this is related to bools so I don't want to create another post.

        declare a bool

        Code:
        		private bool EnvironmentIsLong;
        call a bool

        Code:
        			if (XYZ[0] >= ABC)
        				{EnvironmentIsLong = true;}
        Once the above condition comes true; EnvirnmentIsLong is set to true.

        However, what seems to be confusing is when the above condition becomes untrue:

        XYZ[0] <= ABC

        EnvirnmentIsLong is becoming false. Why? I have no logic that saying that once XYZ <= ABC.. set EnvirnmentIsLong to false.

        Do bools inherently reset on their own if the condition is not true any longer?

        Comment


          #5
          Originally posted by staycool3_a View Post
          I have an elementary bool question. Please ignore the OP. But this is related to bools so I don't want to create another post.

          declare a bool

          Code:
          		private bool EnvironmentIsLong;
          call a bool

          Code:
          			if (XYZ[0] >= ABC)
          				{EnvironmentIsLong = true;}
          Once the above condition comes true; EnvirnmentIsLong is set to true.

          However, what seems to be confusing is when the above condition becomes untrue:

          XYZ[0] <= ABC

          EnvirnmentIsLong is becoming false. Why? I have no logic that saying that once XYZ <= ABC.. set EnvirnmentIsLong to false.

          Do bools inherently reset on their own if the condition is not true any longer?
          Looks like restarting NT fixed this issue. What's the best practice for dealing with bools? Looks like a bool can get stuck?

          Basically; I'm declaring a bool. and then I'm setting a bool to true; then I am setting it to false when additional conditions comes true. Isn't that enough?

          Comment


            #6
            Originally posted by staycool3_a View Post
            Looks like restarting NT fixed this issue. What's the best practice for dealing with bools?
            bool variables are best used as on/off 'knobs' -- they are commonly used as gatekeeper variables that control 'access' to some bit of code.

            To initialize the bool variable along with its definition is a very good practice.

            To set it to true or false later in the code to alter the access to that some bit of code are decisions completely dependent upon the situation at hand -- which is at the very core of what programming is all about.

            If restarting NT fixed everything, that's probably because everything was reset to a 'known' state, including your bool variable.

            Originally posted by staycool3_a View Post
            Looks like a bool can get stuck?
            Woah woah ... let's get real.

            That is not possible. There is no such thing as a 'bool variable getting stuck'.

            Any thing, or any variable, that gets 'stuck' is, 9,999,999 times out of a million, simply the result of the programmer's skills and abilities.

            Most programmer's don't use expressions like 'my bool variable is stuck'.

            Code that produces an incorrect or incomplete or undesired result is simply called 'defective' -- and most of these defects are simply called bugs.

            If you are wanting to use a description like my 'bool variable got stuck' then I know two things about you and about your code:

            1. You are relatively new to programming.
            2. Your code has a bug.

            #1 is perfectly fine.
            #2 is a matter of opinion, because like I said, variables don't get 'stuck' -- that is simply not a 'thing'. Therefore, you almost assuredly have a bug who's symptoms manifest behavior that makes you feel like (to your inexperienced mind) it 'gets stuck'.

            What to do?
            1. The problem is more likely you need to reset your bool variable in more than one place.
            2. Or, the code that decides to reset the variable is defective.
            3. Or, perhaps you're simply not handling all possible cases where reset is needed.

            But, I digress, #3 is the same as #1.

            Originally posted by staycool3_a View Post
            Basically; I'm declaring a bool. and then I'm setting a bool to true; then I am setting it to false when additional conditions comes true. Isn't that enough?
            Define 'enough'.

            I could say dozens of things to this. All the way from 'of course' to 'sounds fine' .. ending up at the best answer, which is: "How the heck should we know? We're not mind readers, we can't see your code, we don't know your requirements, we haven't read your specifications, etc, etc."

            Sorry, there is simply no way for anyone to know if your code is 'enough'. Every situation is unique, there could be dozens (or hundreds) of considerations.

            Is it 'enough' to be able to run 25 miles per hour when the bear chasing you can run 26 miles per hour?

            It depends ...
            Last edited by bltdavid; 09-03-2018, 01:38 PM.

            Comment


              #7
              Originally posted by staycool3_a View Post
              I have an elementary bool question. Please ignore the OP. But this is related to bools so I don't want to create another post.
              Hijacking this thread to ask your question is the wrong way to go about getting an answer.

              You should ALWAYS start a new thread for NEW questions, especially for an elementary question.

              If everyone followed your example here ... this support forum would degenerate into chaos and anarchy -- hijacking threads like this must be avoided.

              Now, back to our regular broadcast ...

              Comment


                #8
                Hello staycool3_a,

                Thank you for your post.
                Originally posted by staycool3_a View Post
                Basically; I'm declaring a bool. and then I'm setting a bool to true; then I am setting it to false when additional conditions comes true. Isn't that enough?
                Correct, this should be enough. Do you have a use case you can detail where you can reproduce the bool getting stuck?

                I look forward to your response.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Kaledus, Today, 01:29 PM
                1 response
                6 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by frankthearm, Yesterday, 09:08 AM
                13 responses
                45 views
                0 likes
                Last Post frankthearm  
                Started by PaulMohn, Today, 12:36 PM
                2 responses
                16 views
                0 likes
                Last Post PaulMohn  
                Started by Conceptzx, 10-11-2022, 06:38 AM
                2 responses
                55 views
                0 likes
                Last Post PhillT
                by PhillT
                 
                Started by yertle, Yesterday, 08:38 AM
                8 responses
                37 views
                0 likes
                Last Post ryjoga
                by ryjoga
                 
                Working...
                X