NexusFi: Find Your Edge


Home Menu

 





Which OR operator to use in C# please?


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one Al2010 with 7 posts (0 thanks)
    2. looks_two samWest with 5 posts (4 thanks)
    3. looks_3 Big Mike with 2 posts (2 thanks)
    4. looks_4 aslan with 2 posts (4 thanks)
    1. trending_up 4,551 views
    2. thumb_up 10 thanks given
    3. group 2 followers
    1. forum 16 posts
    2. attach_file 1 attachments




Closed Thread
 
Search this Thread

Which OR operator to use in C# please?

  #1 (permalink)
Al2010
 
Posts: 50 since Oct 2010
Thanks Given: 36
Thanks Received: 10

Hi,
There are few OR operators in C# and I'm not sure which one to use. Can anyone help please?
Example, if 50 SMA < 0 (falling) OR 50 WMA < 0 - if either one of these are falling then do smth... Another words if 50 SMA is up/flat but 50 WMA is falling or the other way around then return true as if both are falling.
Thank you very much.


Can you help answer these questions
from other members on NexusFi?
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
ZombieSqueeze
Platforms and Indicators
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
PowerLanguage & EasyLanguage. How to get the platfor …
EasyLanguage Programming
REcommedations for programming help
Sierra Chart
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Spoo-nalysis ES e-mini futures S&P 500
29 thanks
Just another trading journal: PA, Wyckoff & Trends
24 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
21 thanks
GFIs1 1 DAX trade per day journal
17 thanks
  #3 (permalink)
 samWest 
Houston, Texas
 
Experience: Advanced
Platform: NinjaTrader
Posts: 25 since Oct 2010
Thanks Given: 2
Thanks Received: 19



Al2010 View Post
Hi,
There are few OR operators in C# and I'm not sure which one to use. Can anyone help please?
Example, if 50 SMA < 0 (falling) OR 50 WMA < 0 - if either one of these are falling then do smth... Another words if 50 SMA is up/flat but 50 WMA is falling or the other way around then return true as if both are falling.
Thank you very much.

if( (50 SMA < 0 ) || (50 WMA < 0) )
{
return true;
}
else
{
return false;
}

Thanked by:
  #4 (permalink)
 
aslan's Avatar
 aslan 
Madison, WI
 
Experience: Advanced
Platform: ALT
Trading: ES
Posts: 625 since Jan 2010
Thanks Given: 356
Thanks Received: 1,127

Sam has it right.

There are two "or" operators:

"||" -> this is the logical operator, which is normally what you want to use in boolean expressions (like if's)

"|" -> this is the binary operator, which is used for doing bit operations (i.e. masking)

People sometimes get confused, because the binary operator can still work in logical expressions, just because of the way boolean true/false expressions are represented with numbers 0 & 1. While this is true, you should always use the proper logical or operator to avoid anything the compiler might try to optimize.

The same is true for the "and" operators "&&" and "&".

Thanked by:
  #5 (permalink)
Al2010
 
Posts: 50 since Oct 2010
Thanks Given: 36
Thanks Received: 10


samWest View Post
if( (50 SMA < 0 ) || (50 WMA < 0) )
{
return true;
}
else
{
return false;
}

Hi,
If SMA 50 > 0 and 50 WMA < 0 will this return false? How to make it return true?
Thank you very much.

  #6 (permalink)
Al2010
 
Posts: 50 since Oct 2010
Thanks Given: 36
Thanks Received: 10


aslan View Post
Sam has it right.

There are two "or" operators:

"||" -> this is the logical operator, which is normally what you want to use in boolean expressions (like if's)

"|" -> this is the binary operator, which is used for doing bit operations (i.e. masking)

People sometimes get confused, because the binary operator can still work in logical expressions, just because of the way boolean true/false expressions are represented with numbers 0 & 1. While this is true, you should always use the proper logical or operator to avoid anything the compiler might try to optimize.

The same is true for the "and" operators "&&" and "&".

Hi,
Is there an inclusive OR so to speak as in my previous reply to samWest - if 50 SMA> 0 and 50 WMA < 0 then return true, e.g. if one of those true then return true?
Thank you very much.

  #7 (permalink)
 
aslan's Avatar
 aslan 
Madison, WI
 
Experience: Advanced
Platform: ALT
Trading: ES
Posts: 625 since Jan 2010
Thanks Given: 356
Thanks Received: 1,127


Al2010 View Post
Hi,
Is there an inclusive OR so to speak as in my previous reply to samWest - if 50 SMA> 0 and 50 WMA < 0 then return true, e.g. if one of those true then return true?
Thank you very much.

Thats the logical and operator ("&&").

Thanked by:
  #8 (permalink)
 samWest 
Houston, Texas
 
Experience: Advanced
Platform: NinjaTrader
Posts: 25 since Oct 2010
Thanks Given: 2
Thanks Received: 19


Al2010 View Post
Hi,
Is there an inclusive OR so to speak as in my previous reply to samWest - if 50 SMA> 0 and 50 WMA < 0 then return true, e.g. if one of those true then return true?
Thank you very much.

Ok Al2010. You seem to be a little weak in you basic logic knowledge. I'm not trying to be critical, just an observation. A basic understanding of the logical AND, and OR are essential skills for programming. Slightly less essential but important are the concepts of EXCLUSIVE OR often notated as XOR. I would suggestion studying what are know as the logical truth tables.

End of lecture . To answer you specific question, as I think I understand it:
The way the logical OR ( || in C# or C++) works is if either side of the OR symbol is true or both sides of the OR symbol are true then the whole statement will evaluate as true. I guess you could call this an "inclusive" OR.

Thanked by:
  #9 (permalink)
Al2010
 
Posts: 50 since Oct 2010
Thanks Given: 36
Thanks Received: 10


samWest View Post
Ok Al2010. You seem to be a little weak in you basic logic knowledge. I'm not trying to be critical, just an observation. A basic understanding of the logical AND, and OR are essential skills for programming. Slightly less essential but important are the concepts of EXCLUSIVE OR often notated as XOR. I would suggestion studying what are know as the logical truth tables.

End of lecture . To answer you specific question, as I think I understand it:
The way the logical OR ( || in C# or C++) works is if either side of the OR symbol is true or both sides of the OR symbol are true then the whole statement will evaluate as true. I guess you could call this an "inclusive" OR.


Thanks aslan and samWest.
Thanks for the lecture, samWest. Wish i was in my 20s again to learn all i can learn...

That's exactly what I would expect from || as you descr. above. I lost that example that confused me with ORs... If I have another one I will post it.
Maybe you guys can help me with other small problem I have. I have a simple program that draws arrows whenever the Stochastic crosses above/below 20-80. I'm checking for multiple crosses but I want my program draw arrows only once per cross. Here's the small check program I'm running:

int crsAbvCheck = 0;
if (Stochastics(periodD, periodK, smooth).K[0] > 20 && Stochastics(periodD, periodK, smooth).K[0] < 30)
{
if (CrossAbove(Stochastics(periodD, periodK, smooth).K, 20, 1))
{
crsAbvCheck = 20;
Print("Cross abv = " +crsAbvCheck+ "---Time:......"+Time[0]);
}
}
else if (Stochastics(periodD, periodK, smooth).K[0] > 30 && Stochastics(periodD, periodK, smooth).K[0] < 40)
{
if (CrossAbove(Stochastics(periodD, periodK, smooth).K, 30, 1))
{
crsAbvCheck = 30;
Print("Cross abv = " +crsAbvCheck+ "---Time:......"+Time[0]);
}
}
else { crsAbvCheck = 0; }

Here's the output - 6E 300 volume chart:

Cross abv = 20---Time:......12/9/2010 10:21:32 AM
Cross abv = 30---Time:......12/9/2010 10:21:32 AM

// Getting both messages for both crosses 20 and 30 whenever the Stoch is below 20 and crossing up...
Cross abv = 20---Time:......12/9/2010 12:32:26 PM
Cross abv = 30---Time:......12/9/2010 12:34:49 PM

As you can see the program fails. When the Stoch is below 20 and crossing up it prints messages for both crosses 20 and 30. I would get two arrows up on my chart -20 and 30 arrows whenever cross above 20=true. I expected to get only one message/arrow for the lowest cross above, e.g. crs. abv 20. Yes there was a price jump at that time... But still, I'd like to stop my program somehow if the lowest cross above is true. Is this my fuzzy logic again? Another lecture maybe? I will gladly take it, honestly.
How to fix it please? How to get only one message/arrow per cross, e.g. if above 20 and below 30 then only cross abv 20=true, if above 30 but below 40 then only cross abv 30=true...
Thank you very much.

  #10 (permalink)
 samWest 
Houston, Texas
 
Experience: Advanced
Platform: NinjaTrader
Posts: 25 since Oct 2010
Thanks Given: 2
Thanks Received: 19


I'm sorry Al2010, I'm not going to write your code for you. My suggestion would be to go over to the indicator download area and look for something that is similar to what you want to do. Downloaded it, and look at the code. Studying other peoples code is great way to improve you coding skills.

Thanked by:

Closed Thread



Last Updated on December 11, 2010


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts