Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
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:
In this case, the compiler always evaluates A, B, and C --- even if A is false. On the other hand, one can write:
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:
[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!
Can you help answer these questions from other members on NexusFi?
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
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
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.
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.