futures.io futures trading
 

Go Back   futures.io

> Futures Trading, News, Charts and Platforms > Platforms and Indicators > NinjaTrader > NinjaTrader Programming


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

Welcome to futures.io.

Welcome, Guest!

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

We are 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
 
Futures Experience: Intermediate
Platform: NinjaTrader
Broker/Data: MB Trading
Favorite Futures: Cello
 
serac's Avatar
 
Posts: 116 since Jan 2011
Thanks: 321 given, 135 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)
Elite Member
Berlin, Europe
 
Futures Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker/Data: Interactive Brokers
Favorite Futures: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,515 since Mar 2010
Thanks: 4,212 given, 24,672 received


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, 19 views)
Reply With Quote
     
The following 4 users say Thank You to Fat Tails for this post:
     

Old November 27th, 2011, 05:17 PM   #4 (permalink)
Elite Member
glostrup, denmark
 
Futures Experience: Advanced
Platform: Custom platform
Broker/Data: CQG
Favorite Futures: Futures, Options, Stocks
 
ktrader's Avatar
 
Posts: 233 since Aug 2011
Thanks: 148 given, 260 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 4 users say Thank You to ktrader for this post:
     

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

Futures Edge on FIO

How long have you been an Elite Member of futures.io?

 
@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



futures.io > Futures Trading, News, Charts and Platforms > Platforms and Indicators > NinjaTrader > NinjaTrader Programming > Conditional Nested Boolean Statements

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Events (4:30PM ET unless noted)
 

Peter Davies: Getting Started in Scalping/Ultra Short Term Trading

Aug 25

FIO Journal Contest with over $2000 in prizes [entire month]

September
 

John Hoagland (TopstepTrader): From The Pits to The Screens

Sep 1
 

John Grady (sponsored by Jigsaw Trading): Choosing the right market

Elite only
 

Ferran Font Ramentol (sponsored by Stage 5): Scalping the Order Book w/HFT's

Elite only

Brannigan Barret: agenda TBA

Elite only
 

Tradovate: the modern, cloud-based futures trading platform

Sep 22
 

Jigsaw Trading: Standalone Tools now available

Sep 26

Suri Duddella: The 10 Basic Trading concepts to follow in the Markets (part 2 of 2) [w/book giveaway]

Elite only

Linda Bradford Raschke: agenda TBA [w/book giveaway]

Elite only

Raymond Deux (NinjaTrader): NinjaTrader 8 overview and Q&A

Elite only

FuturesTrader71: agenda TBA (part 4 of 4) [w/book giveaway]

Elite only

Corey Rosenbloom: agenda TBA [w/book giveaway]

Elite only

An Afternoon with FIO trader bobwest

Elite only

NinjaTrader 8: Programming Profitable Trading Edges w/FIO member shodson

Elite only

An Afternoon with FIO trader GFIs1

Elite only

MarketDelta Cloud Platform: Announcing new mobile features

Oct TBD
     

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 02:24 PM.

Copyright © 2016 by futures.io. 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 2016-08-25 in 0.09 seconds with 20 queries on phoenix via your IP 23.20.6.115