Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Working with bools that latch

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

    Working with bools that latch

    if (Condition 1) {
    Bool1= true;
    Bool2= false;
    Bool3= false;
    Bool4= false;
    Bool5= false;
    }
    if (Condition 2) {
    Bool1= false;
    Bool2= true;
    Bool3= false;
    Bool4= false;
    Bool5= false;
    }
    if (Condition 3) {
    Bool1= false;
    Bool2= false;
    Bool3= true;
    Bool4= false;
    Bool5= false;
    }
    if (Condition 4) {
    Bool1= false;
    Bool2= false;
    Bool3= false;
    Bool4= true;
    Bool5= false;
    }
    if (Condition 5) {
    Bool1= false;;
    Bool2= false;
    Bool3= false;
    Bool4= false;
    Bool5= true;
    }
    I'm trying to create 5 latching bools. The condition only needs to be true momentarily to make the 1 and only 1 bool true and all the others false, and stay that way until another bool becomes true, making all the others false, and so on.

    I'm sure this code will work but I was wondering if there was a specific c# bool function that would do this better?

    Thanks

    #2
    Hello kenb2004,
    Looking from the code snippet I suppose this is the only way.

    I will however leave the thread open for fellow member if they can offer any more insight.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Originally posted by kenb2004 View Post
      if (Condition 1) {
      Bool1= true;
      Bool2= false;
      Bool3= false;
      Bool4= false;
      Bool5= false;
      }
      if (Condition 2) {
      Bool1= false;
      Bool2= true;
      Bool3= false;
      Bool4= false;
      Bool5= false;
      }
      if (Condition 3) {
      Bool1= false;
      Bool2= false;
      Bool3= true;
      Bool4= false;
      Bool5= false;
      }
      if (Condition 4) {
      Bool1= false;
      Bool2= false;
      Bool3= false;
      Bool4= true;
      Bool5= false;
      }
      if (Condition 5) {
      Bool1= false;;
      Bool2= false;
      Bool3= false;
      Bool4= false;
      Bool5= true;
      }
      I'm trying to create 5 latching bools. The condition only needs to be true momentarily to make the 1 and only 1 bool true and all the others false, and stay that way until another bool becomes true, making all the others false, and so on.

      I'm sure this code will work but I was wondering if there was a specific c# bool function that would do this better?

      Thanks
      That looks the easiest to read, but another way is to use your conditions to set the bits of a byte variable based on your conditions. You would use bits 0 to 4 in this case. So the 5 condition each set or not a particular bit. Then have just one statement that sets the bools according to which bit is set. In other words, your bools each query only one bit, and are set to true only if that bit is set.

      As I said, very efficient, but also somewhat hard to read. Then again, for those of us who actually had to code in Assembler, that coding style of using bits to hold code/variable states is practically de rigueur.

      Comment


        #4
        Thanks koganam

        In this case it's going to get awfully bulky, so I'm actually looking for someway to streamline this function. I don't know how to do what your saying, but I am interested if you could provide an example, I would appreciate it.

        thanks

        Comment


          #5
          Not the C# expert, but I used something similar in the past, taking the binary sequence, but using it in numbers, where you use AND-in to get

          if (condition1) boolvalue= 1; // 0000 0001
          if (condition2) boolvalue= 2; // 0000 0010
          if (condition3) boolvalue= 4; // 0000 0100
          if (condition4) boolvalue= 8; // 0000 1000
          if (condition5) boolvalue=16 // 0001 0000

          and then in the code test on (boolvalue AND 8) for instance when boolvalue is 8 then AND-ing with 8 will give TRUE; any other value will give FALSE. - if (boolvalue & 8) { condition met statements }

          Comment


            #6
            I agree with the others about the bit string.

            But, are these conditions mutually exclusive?

            In your code, if condition 1 is set, condition 5 can override that since it is last checked.

            Does one condition have greater precedence over the other condition?

            If not:

            Code:
            Bool1=false;
            Bool2=false;
            Bool3=false;
            Bool4=false;
            Bool5=false;
            
            if (condition1) Bool1=true
            else if (condition2) Bool2=true;
            else if (condition3) Bool3=true;
            else if (condition4) Bool4=true;
            else if (condition5) Bool5=true;
            end if;
            Originally posted by kenb2004 View Post
            if (Condition 1) {
            Bool1= true;
            Bool2= false;
            Bool3= false;
            Bool4= false;
            Bool5= false;
            }
            if (Condition 2) {
            Bool1= false;
            Bool2= true;
            Bool3= false;
            Bool4= false;
            Bool5= false;
            }
            if (Condition 3) {
            Bool1= false;
            Bool2= false;
            Bool3= true;
            Bool4= false;
            Bool5= false;
            }
            if (Condition 4) {
            Bool1= false;
            Bool2= false;
            Bool3= false;
            Bool4= true;
            Bool5= false;
            }
            if (Condition 5) {
            Bool1= false;;
            Bool2= false;
            Bool3= false;
            Bool4= false;
            Bool5= true;
            }
            I'm trying to create 5 latching bools. The condition only needs to be true momentarily to make the 1 and only 1 bool true and all the others false, and stay that way until another bool becomes true, making all the others false, and so on.

            I'm sure this code will work but I was wondering if there was a specific c# bool function that would do this better?

            Thanks

            Comment


              #7
              Originally posted by kenb2004 View Post
              if (Condition 1) {
              Bool1= true;
              Bool2= false;
              Bool3= false;
              Bool4= false;
              Bool5= false;
              }
              if (Condition 2) {
              Bool1= false;
              Bool2= true;
              Bool3= false;
              Bool4= false;
              Bool5= false;
              }
              if (Condition 3) {
              Bool1= false;
              Bool2= false;
              Bool3= true;
              Bool4= false;
              Bool5= false;
              }
              if (Condition 4) {
              Bool1= false;
              Bool2= false;
              Bool3= false;
              Bool4= true;
              Bool5= false;
              }
              if (Condition 5) {
              Bool1= false;;
              Bool2= false;
              Bool3= false;
              Bool4= false;
              Bool5= true;
              }
              I'm trying to create 5 latching bools. The condition only needs to be true momentarily to make the 1 and only 1 bool true and all the others false, and stay that way until another bool becomes true, making all the others false, and so on.

              I'm sure this code will work but I was wondering if there was a specific c# bool function that would do this better?

              Thanks
              An alternative, looking at the code, if all you want is to compact it, there are options made available simply by the logic itself. You can use a function/method to set all bools false, then set only the one of interest, using the function to set all false false first at each condition, or you can just use a round-robin ripple scheme, as only one bool is set by each condition.

              Method 1

              Code:
              private void SetAllFalse()
              {
              Bool1= false;
              Bool2= false;
              Bool3= false; 
              Bool4= false;
              Bool5= false;
              return;
              }
              
              if (Condition 1) {
              SetAllFalse();
               Bool1= true; 
               }
              if (Condition 2) {
              SetAllFalse();
               Bool2= true; 
               } 
              
              //etc
              Method 2

              Code:
              Bool1= false;
              Bool2= false;
              Bool3= false;
              Bool4= false;
              Bool5= false;
              
              if (Condition 1) {
              Bool1= true;
              }
              if (Condition 2) {
              Bool1= false;
              Bool2= true;
              }
              if (Condition 3) {
              Bool2= false;
              Bool3= true;
              }
              if (Condition 4) {
              Bool3= false;
              Bool4= true;
              }
              if (Condition 5) {
              Bool4= false;
              Bool5= true;
              }
              Last edited by koganam; 07-21-2012, 08:05 AM.

              Comment


                #8
                sledge

                Yes the conditions are mutually exclusive.

                Thanks for the input, this was my very first thought. But it does not work as a latch because the conditions are momentary. The bool that becomes true needs to stay true until one of the other bools become true, regardless of the condition that made it true. It's basically a one-shot, like a pushbutton start and a pushbutton stop. Momentary...like a CrossAbove or CrossBelow.

                koganam

                Thanks for your input, but Method 1 won't work because it does not latch. As soon as condition 1 stops being true, bool 1 becomes false, even though no other condition became true. 1 bool has to remain true after a momentary condition becomes true until one of the other conditions become true, making it's bool true and all others false.
                The key is: mometary condition true latches bool to true.

                Method 2, same. No latch. As soon as momentary condition 1 is not true, nothing is true. Again, the key is the latch. Each bool needs to remain true, until one of the other bools becomes true, but only for a moment in time.

                Thanks again

                Comment


                  #9
                  Originally posted by kenb2004 View Post
                  sledge

                  Yes the conditions are mutually exclusive.

                  Thanks for the input, this was my very first thought. But it does not work as a latch because the conditions are momentary. The bool that becomes true needs to stay true until one of the other bools become true, regardless of the condition that made it true. It's basically a one-shot, like a pushbutton start and a pushbutton stop. Momentary...like a CrossAbove or CrossBelow.

                  koganam

                  Thanks for your input, but Method 1 won't work because it does not latch. As soon as condition 1 stops being true, bool 1 becomes false, even though no other condition became true. 1 bool has to remain true after a momentary condition becomes true until one of the other conditions become true, making it's bool true and all others false.
                  The key is: mometary condition true latches bool to true.

                  Method 2, same. No latch. As soon as momentary condition 1 is not true, nothing is true. Again, the key is the latch. Each bool needs to remain true, until one of the other bools becomes true, but only for a moment in time.

                  Thanks again
                  I see now. (Sorry, never heard of a latch until today lol).

                  I would throw in the towel and use #s, or static ints to make it a little readable...

                  public static int LATCH1 = 1;
                  ...

                  psuedo code:

                  Code:
                  latch number :=0;
                  
                  if (condition1 ) then latch := LATCH1;
                  if (condition2 ) then latch := 2;
                  if (condition3 ) then latch := 3;
                  if (condition4 ) then latch := 4;
                  if (condition5 ) then latch := 5;

                  Comment


                    #10
                    That's the idea, but how would you write it in real code? Momentary true conditions latch the last latch, bool, # to true? ...see original code.

                    Comment


                      #11
                      Originally posted by kenb2004 View Post
                      That's the idea, but how would you write it in real code? Momentary true conditions latch the last latch, bool, # to true? ...see original code.
                      So it sounds like you really need this in each bool and it won't work with #s?

                      I'm not sure you are going to get past your original code.

                      You could shorten like this?

                      Are you doing REAL concurrent stuff with latches, or is this a result of some other piece of software forcing you to work with bools?

                      I'm not seeing how this applies NT strategies.

                      Code:
                      if (Condition 1) {
                      Bool1= true; 
                      Bool2= Bool3= Bool4= Bool5= false;
                      }

                      Comment


                        #12
                        Originally posted by kenb2004 View Post
                        sledge

                        Yes the conditions are mutually exclusive.

                        Thanks for the input, this was my very first thought. But it does not work as a latch because the conditions are momentary. The bool that becomes true needs to stay true until one of the other bools become true, regardless of the condition that made it true. It's basically a one-shot, like a pushbutton start and a pushbutton stop. Momentary...like a CrossAbove or CrossBelow.

                        koganam

                        Thanks for your input, but Method 1 won't work because it does not latch. As soon as condition 1 stops being true, bool 1 becomes false, even though no other condition became true. 1 bool has to remain true after a momentary condition becomes true until one of the other conditions become true, making it's bool true and all others false.
                        The key is: mometary condition true latches bool to true.

                        Method 2, same. No latch. As soon as momentary condition 1 is not true, nothing is true. Again, the key is the latch. Each bool needs to remain true, until one of the other bools becomes true, but only for a moment in time.

                        Thanks again
                        I think you may be over-analyzing this. Look at the code again. Every bool, once made true, is true UNTIL it is reset by some condition. That is the definition of a latch. No bool is ever made false by negating any condition: the only way to make a bool false is for an explicit condition to be triggered.

                        But you are right, for mutual exclusivity, method 2 will not work, but method 1 is just an exact, more compact rewrite of what you wrote. Have you even tested it? Given a condition. it explicitly sets all bools false, then sets ONLY the required bool to true. How is that any different from your longer code?

                        The idea behind all functions is to avoid repeating code that will be used very often, by putting all that code into a single line so to speak.
                        Last edited by koganam; 07-21-2012, 05:05 PM.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by GussJ, 03-04-2020, 03:11 PM
                        16 responses
                        3,279 views
                        0 likes
                        Last Post Leafcutter  
                        Started by WHICKED, Today, 12:45 PM
                        2 responses
                        19 views
                        0 likes
                        Last Post WHICKED
                        by WHICKED
                         
                        Started by Tim-c, Today, 02:10 PM
                        1 response
                        9 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by Taddypole, Today, 02:47 PM
                        0 responses
                        5 views
                        0 likes
                        Last Post Taddypole  
                        Started by chbruno, 04-24-2024, 04:10 PM
                        4 responses
                        52 views
                        0 likes
                        Last Post chbruno
                        by chbruno
                         
                        Working...
                        X