Big Mike's Trading Forum
 

Go Back   Big Mike's Trading Forum

> Trading Forums > Platforms and Indicators > NinjaTrader > NinjaTrader Programming


Notices
     

Conditional Nested Boolean Statements
Started:November 27th, 2011 (02:18 PM) by serac Views / Replies:720 / 4
Last Reply:November 27th, 2011 (06:44 PM) Attachments:1

Welcome to Big Mike's Trading Forum.

Welcome, Guest!

This forum was established to help traders by openly sharing indicators, strategies, methods, trading journals and discussing the psychology of trading.

BMT is fundamentally different than most other trading forums:
  • We work extremely hard to keep things positive on our forums.
  • We do not tolerate rude behavior, trolling, or vendor advertising in posts.
  • We firmly believe in openness and encourage sharing. The holy grail is within you, it is not something tangible you can download.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.


You'll need to register in order to view the content of the threads and start contributing to our community. It's free and simple, and we will never resell your private information.

-- Big Mike
     

Reply
 
Thread Tools Search this Thread

Conditional Nested Boolean Statements

Old November 27th, 2011, 02:18 PM   #1 (permalink)
Elite Member
Arizona, USA
 
Trading Experience: Beginner
Platform: NinjaTrader
Broker/Data: MB Trading
Favorite Instrument: Cello
 
serac's Avatar
 
Posts: 116 since Jan 2011
Thanks: 320 given, 130 received

Conditional Nested Boolean Statements

Hi all,

This is less of a NinjaTrader specific question and more of a question on how the C# compiler works. It is a pretty nit-picky question best suited for programming experts. Let me explain my question:

I'm trying to make my strategy efficient. As with any programming language, there a lots of if/then statements. My question arises from what I can do in other languages. C# is new to me. My native tongue is Pascal and Ada. In the latter, I can do this:
 
Code
if A and B and C then
DoThis;
end if;
In this case, the compiler always evaluates A, B, and C --- even if A is false. On the other hand, one can write:
 
Code
if A and if B and if C then
DoThis;
end if;
The extra ifs added above function the same. But, if A is false, Boolean statements B and C are never evaluated. The result is the same, but is (very slightly) less computational intensive. The "and if" statement is an elegant way to write the ugly:
 
Code
if A then
if B then
if C then
DoThis;
end if;
end if;
end if;
[The "or" statement equivalent is "or else" (e.g. if A and B or else C then...). ]

Question: Can I do the same sort of thing in C# without resorting to ugly nested if/then statements?

Thanks!

Reply With Quote
     

Old November 27th, 2011, 02:18 PM   #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.

     

Old November 27th, 2011, 04:03 PM   #3 (permalink)
Market Wizard
Berlin, Europe
 
Trading Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker/Data: Interactive Brokers
Favorite Instrument: Keyboard
 
Fat Tails's Avatar
 
Posts: 8,377 since Mar 2010
Thanks: 3,940 given, 19,575 received
Forum Reputation: Legendary


Good question. I think that you can do it.

I have attached two indicators in the zip file below. Both are identical versions of the SuperTrend, but there is a small difference in lines 74 and 75 of the code

The first indicator, SuperTrendErrorCode uses

 
Code
if (ChartControl.ChartStyleType == ChartStyleType.CandleStick)
	candles = true;
If you call this indicator from another indicator, you will get an error, because there is no chart and ChartControl will throw an exception in OnStartUp().

The second indicator, SuperTrendCorrectedCode uses

 
Code
if (ChartControl != null && ChartControl.ChartStyleType == ChartStyleType.CandleStick)
              candles = true;
In this case NinjaTrader will not throw an exception, because the first statement is false and the second statement is not executed.

I would conclude from this behavior that you do not need to use nested statements.

But I am clearly no expert for C#, so you may want to ask somebody else.

Attached Files
Register to download File Type: zip SuperTrendCode.zip (10.5 KB, 11 views)
Reply With Quote
     
The following 3 users say Thank You to Fat Tails for this post:
     

Old November 27th, 2011, 05:17 PM   #4 (permalink)
Elite Member
glostrup, denmark
 
Trading Experience: Advanced
Platform: Sierra Chart
Favorite Instrument: CL, 6E
 
ktrader's Avatar
 
Posts: 200 since Aug 2011
Thanks: 55 given, 169 received


serac View Post
Hi all,

This is less of a NinjaTrader specific question and more of a question on how the C# compiler works. It is a pretty nit-picky question best suited for programming experts. Let me explain my question:

I'm trying to make my strategy efficient. As with any programming language, there a lots of if/then statements. My question arises from what I can do in other languages. C# is new to me. My native tongue is Pascal and Ada. In the latter, I can do this:
 
Code
if A and B and C then
DoThis;
end if;
In this case, the compiler always evaluates A, B, and C --- even if A is false. On the other hand, one can write:
 
Code
if A and if B and if C then
DoThis;
end if;
The extra ifs added above function the same. But, if A is false, Boolean statements B and C are never evaluated. The result is the same, but is (very slightly) less computational intensive. The "and if" statement is an elegant way to write the ugly:
 
Code
if A then
if B then
if C then
DoThis;
end if;
end if;
end if;
[The "or" statement equivalent is "or else" (e.g. if A and B or else C then...). ]

Question: Can I do the same sort of thing in C# without resorting to ugly nested if/then statements?

Thanks!


C# uses short-circuit evaluation for && and ||, so it will only evaluate as much as needed to have the full result. so false && false will only evaluate the first false as it is enough to conclude the entire expression is false. true || true will only evaluate the first true as it is enough.

C# does have binary operators for and/or that evaluates all arguments, & and | these will evaluate both arguments.

Reply With Quote
     
The following 3 users say Thank You to ktrader for this post:
     

Old November 27th, 2011, 06:44 PM   #5 (permalink)
Elite Member
Arizona, USA
 
Trading Experience: Beginner
Platform: NinjaTrader
Broker/Data: MB Trading
Favorite Instrument: Cello
 
serac's Avatar
 
Posts: 116 since Jan 2011
Thanks: 320 given, 130 received

@FatTails and @ktrader: Thanks for the reply! Looks like && is built-in to function as "and if" as I described. Makes sense, I guess. Thanks, again!

Reply With Quote
     

Reply



Tags
binary, boolean, c#, charts, chartstyle, code, control, es, evaluation, file, function, indicators, language, ninja, ninjascript, ninjatrader, onstartup, programming, question, small, strategy, supertrend
     

Big Mike's Trading Forum > Trading Forums > Platforms and Indicators > NinjaTrader > NinjaTrader Programming > Conditional Nested Boolean Statements

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads
Thread Thread Starter Forum Replies Last Post
Three Way Filter with Boolean Setup zeller4 NinjaTrader Programming 3 July 19th, 2011 08:19 PM
Complete noob programming MTF and nested indicator nirajkrishna Traders Hideout 2 March 26th, 2011 07:17 AM
IB: Analyse Statements Henrik Beginners and Introductions 4 March 12th, 2010 06:29 PM
My boolean is crazy! gregid NinjaTrader Programming 3 February 27th, 2010 04:49 PM
conditional logic using a set of values as determinant Saroj NinjaTrader Programming 9 October 5th, 2009 10:30 AM


All times are GMT -4. The time now is 08:50 PM.

Powered by vBulletin®
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
Copyright © 2014 by Big Mike Trading. 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.
 
no new posts

Page generated 2014-04-16 in 0.14 seconds with 20 queries on atlantis via your IP 54.205.173.252