NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > Application Technical Support > Automated Trading

Automated Trading Support for automated trading systems using NinjaScript. Support for our ATI (Automated Trading Interface) used to link an external application such as TradeStation and eSignal to NinjaTrader.

Reply
 
Thread Tools Display Modes
Old 07-20-2012, 12:26 PM   #1
kenb2004
Senior Member
 
Join Date: Jul 2010
Posts: 415
Thanks: 1
Thanked 4 times in 4 posts
Default 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
kenb2004 is offline  
Reply With Quote
Old 07-20-2012, 12:33 PM   #2
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
Default

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.
NinjaTrader_Joydeep is offline  
Reply With Quote
Old 07-20-2012, 08:34 PM   #3
koganam
Senior Member
 
Join Date: Feb 2008
Location: Durham, North Carolina, USA
Posts: 3,201
Thanks: 24
Thanked 1,227 times in 998 posts
Send a message via Skype™ to koganam
Default

Quote:
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.
koganam is offline  
Reply With Quote
Old 07-21-2012, 05:36 AM   #4
kenb2004
Senior Member
 
Join Date: Jul 2010
Posts: 415
Thanks: 1
Thanked 4 times in 4 posts
Default

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
kenb2004 is offline  
Reply With Quote
Old 07-21-2012, 06:29 AM   #5
Hans B
Junior Member
 
Join Date: Apr 2010
Posts: 6
Thanks: 0
Thanked 3 times in 2 posts
Default

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 }
Hans B is offline  
Reply With Quote
Old 07-21-2012, 06:57 AM   #6
sledge
Senior Member
 
Join Date: Aug 2010
Location: Washington, D.C.
Posts: 1,184
Thanks: 178
Thanked 301 times in 259 posts
Default

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;
Quote:
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
sledge is offline  
Reply With Quote
Old 07-21-2012, 07:20 AM   #7
koganam
Senior Member
 
Join Date: Feb 2008
Location: Durham, North Carolina, USA
Posts: 3,201
Thanks: 24
Thanked 1,227 times in 998 posts
Send a message via Skype™ to koganam
Default

Quote:
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 at 08:05 AM.
koganam is offline  
Reply With Quote
Old 07-21-2012, 01:21 PM   #8
kenb2004
Senior Member
 
Join Date: Jul 2010
Posts: 415
Thanks: 1
Thanked 4 times in 4 posts
Default

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
kenb2004 is offline  
Reply With Quote
Old 07-21-2012, 01:39 PM   #9
sledge
Senior Member
 
Join Date: Aug 2010
Location: Washington, D.C.
Posts: 1,184
Thanks: 178
Thanked 301 times in 259 posts
Default

Quote:
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;
sledge is offline  
Reply With Quote
The following user says thank you to sledge for this post:
Old 07-21-2012, 02:57 PM   #10
kenb2004
Senior Member
 
Join Date: Jul 2010
Posts: 415
Thanks: 1
Thanked 4 times in 4 posts
Default

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.
kenb2004 is offline  
Reply With Quote
Old 07-21-2012, 03:26 PM   #11
sledge
Senior Member
 
Join Date: Aug 2010
Location: Washington, D.C.
Posts: 1,184
Thanks: 178
Thanked 301 times in 259 posts
Default

Quote:
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;
}
sledge is offline  
Reply With Quote
Old 07-21-2012, 05:02 PM   #12
koganam
Senior Member
 
Join Date: Feb 2008
Location: Durham, North Carolina, USA
Posts: 3,201
Thanks: 24
Thanked 1,227 times in 998 posts
Send a message via Skype™ to koganam
Default

Quote:
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 at 05:05 PM.
koganam is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Optimize with bools? Here's how! jeronymite General Programming 6 09-20-2012 07:42 AM
Using Bars with Bools CaptainAmericaXX General Programming 2 04-14-2011 11:29 AM
Bools and [] indexing kenb2004 Strategy Development 5 02-28-2011 11:41 AM
NT7B9 Immediately submit live working historical orders not working properly oclop Version 7 Beta General Questions & Bug Reports 2 02-22-2010 07:22 AM
ints and bools??? John833 Strategy Development 2 12-08-2008 09:40 AM


All times are GMT -6. The time now is 10:57 AM.