![]() |
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
|
|||||||
| 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. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
Join Date: Jul 2010
Posts: 415
Thanks: 1
Thanked 4 times in 4 posts
|
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 |
|
NinjaTrader Customer Service
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
|
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.
Joydeep M.
NinjaTrader Customer Service |
|
|
|
|
|
#3 | |
|
Senior Member
|
Quote:
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. |
|
|
|
|
|
|
#4 |
|
Senior Member
Join Date: Jul 2010
Posts: 415
Thanks: 1
Thanked 4 times in 4 posts
|
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 |
|
|
|
|
|
#5 |
|
Junior Member
Join Date: Apr 2010
Posts: 6
Thanks: 0
Thanked 3 times in 2 posts
|
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 } |
|
|
|
|
|
#6 | |
|
Senior Member
Join Date: Aug 2010
Location: Washington, D.C.
Posts: 1,184
Thanks: 178
Thanked 301 times in 259 posts
|
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:
|
|
|
|
|
|
|
#7 | |
|
Senior Member
|
Quote:
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
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.
|
|
|
|
|
|
|
#8 |
|
Senior Member
Join Date: Jul 2010
Posts: 415
Thanks: 1
Thanked 4 times in 4 posts
|
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 |
|
|
|
|
|
#9 | |
|
Senior Member
Join Date: Aug 2010
Location: Washington, D.C.
Posts: 1,184
Thanks: 178
Thanked 301 times in 259 posts
|
Quote:
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; |
|
|
|
|
|
The following user says thank you to sledge for this post: |
|
|
|
#10 |
|
Senior Member
Join Date: Jul 2010
Posts: 415
Thanks: 1
Thanked 4 times in 4 posts
|
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.
|
|
|
|
|
|
#11 | |
|
Senior Member
Join Date: Aug 2010
Location: Washington, D.C.
Posts: 1,184
Thanks: 178
Thanked 301 times in 259 posts
|
Quote:
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;
}
|
|
|
|
|
|
|
#12 | |
|
Senior Member
|
Quote:
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.
|
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |