NexusFi: Find Your Edge


Home Menu

 





C# inline 'if' with multiple statements


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one Big Mike with 2 posts (0 thanks)
    2. looks_two Quick Summary with 1 posts (0 thanks)
    3. looks_3 dmh24 with 1 posts (0 thanks)
    4. looks_4 Zondor with 1 posts (1 thanks)
    1. trending_up 3,683 views
    2. thumb_up 1 thanks given
    3. group 3 followers
    1. forum 5 posts
    2. attach_file 0 attachments




 
Search this Thread

C# inline 'if' with multiple statements

  #1 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,465 since Jun 2009
Thanks Given: 33,242
Thanks Received: 101,665

I'm trying to do an inline if with multiple statements. I'm not sure this is possible?

int divisor = 1;
data = (!string.IsNullOrEmpty(symbol01) ? Closes[1][0] * Symbol01weight : 0) / divisor;

This is the simplest example, the actual condition is more complex.

What I want to do is change the 'true' condition to multiple statements:

Closes[1][0] * Symbol01weight; divisor++;

But I can't figure out a way to do this. At least not inline...

Thoughts? I tried Google but perhaps I am not wording it correctly, or it is impossible, because I can't find a solution (without rewriting the entire code block to not use an inline if).

Mike



Join the free Markets Chat beta: one platform, all the trade rooms!

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
About a successful futures trader who didnt know anythin …
Psychology and Money Management
What broker to use for trading palladium futures
Commodities
Better Renko Gaps
The Elite Circle
MC PL editor upgrade
MultiCharts
REcommedations for programming help
Sierra Chart
 
  #3 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,465 since Jun 2009
Thanks Given: 33,242
Thanks Received: 101,665


Here is the whole block the way it exists now. Please be kind, I'm 3-years out of practice with C#. Feel free to propose a more efficient block.

 
Code
int divisor = 1;
if (!string.IsNullOrEmpty(symbol01)) divisor++;
if (!string.IsNullOrEmpty(symbol02)) divisor++;
if (!string.IsNullOrEmpty(symbol03)) divisor++;
if (!string.IsNullOrEmpty(symbol04)) divisor++;
if (!string.IsNullOrEmpty(symbol05)) divisor++;
if (!string.IsNullOrEmpty(symbol06)) divisor++;
if (!string.IsNullOrEmpty(symbol07)) divisor++;
if (!string.IsNullOrEmpty(symbol08)) divisor++;
if (!string.IsNullOrEmpty(symbol09)) divisor++;
if (!string.IsNullOrEmpty(symbol10)) divisor++;
data = (Closes[0][0] * Symbol00weight + (!string.IsNullOrEmpty(symbol01) ? Closes[1][0] * Symbol01weight : 0) + (!string.IsNullOrEmpty(symbol02) ? Closes[2][0] * Symbol02weight : 0) + (!string.IsNullOrEmpty(symbol03) ? Closes[3][0] * Symbol03weight : 0) + (!string.IsNullOrEmpty(symbol04) ? Closes[4][0] * Symbol04weight : 0) + (!string.IsNullOrEmpty(symbol05) ? Closes[5][0] * Symbol05weight : 0) + (!string.IsNullOrEmpty(symbol06) ? Closes[6][0] * Symbol06weight : 0) + (!string.IsNullOrEmpty(symbol07) ? Closes[7][0] * Symbol07weight : 0) + (!string.IsNullOrEmpty(symbol08) ? Closes[8][0] * Symbol08weight : 0) + (!string.IsNullOrEmpty(symbol09) ? Closes[9][0] * Symbol09weight : 0) + (!string.IsNullOrEmpty(symbol10) ? Closes[10][0] * Symbol10weight : 0))/divisor;
Aggregate.Set(data);
Mike



Join the free Markets Chat beta: one platform, all the trade rooms!

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #4 (permalink)
 
vvhg's Avatar
 vvhg 
Northern Germany
 
Experience: Intermediate
Platform: NT
Trading: FDAX, CL
Posts: 1,583 since Mar 2011
Thanks Given: 1,016
Thanks Received: 2,824


Big Mike View Post
Here is the whole block the way it exists now. Please be kind, I'm 3-years out of practice with C#. Feel free to propose a more efficient block.

 
Code
int divisor = 1;
if (!string.IsNullOrEmpty(symbol01)) divisor++;
if (!string.IsNullOrEmpty(symbol02)) divisor++;
if (!string.IsNullOrEmpty(symbol03)) divisor++;
if (!string.IsNullOrEmpty(symbol04)) divisor++;
if (!string.IsNullOrEmpty(symbol05)) divisor++;
if (!string.IsNullOrEmpty(symbol06)) divisor++;
if (!string.IsNullOrEmpty(symbol07)) divisor++;
if (!string.IsNullOrEmpty(symbol08)) divisor++;
if (!string.IsNullOrEmpty(symbol09)) divisor++;
if (!string.IsNullOrEmpty(symbol10)) divisor++;
data = (Closes[0][0] * Symbol00weight + (!string.IsNullOrEmpty(symbol01) ? Closes[1][0] * Symbol01weight : 0) + (!string.IsNullOrEmpty(symbol02) ? Closes[2][0] * Symbol02weight : 0) + (!string.IsNullOrEmpty(symbol03) ? Closes[3][0] * Symbol03weight : 0) + (!string.IsNullOrEmpty(symbol04) ? Closes[4][0] * Symbol04weight : 0) + (!string.IsNullOrEmpty(symbol05) ? Closes[5][0] * Symbol05weight : 0) + (!string.IsNullOrEmpty(symbol06) ? Closes[6][0] * Symbol06weight : 0) + (!string.IsNullOrEmpty(symbol07) ? Closes[7][0] * Symbol07weight : 0) + (!string.IsNullOrEmpty(symbol08) ? Closes[8][0] * Symbol08weight : 0) + (!string.IsNullOrEmpty(symbol09) ? Closes[9][0] * Symbol09weight : 0) + (!string.IsNullOrEmpty(symbol10) ? Closes[10][0] * Symbol10weight : 0))/divisor;
Aggregate.Set(data);
Mike

You could stick it into a loop like for each symbol is empty reduce divisor by one.
But I think it would not be any more efficient, so it would probably only be for the looks.

vvhg

Hic Rhodos, hic salta.
Reply With Quote
  #5 (permalink)
 dmh24 
Denver, CO
 
Experience: Advanced
Platform: SierraChart
Broker: Optimus, Rithmic
Trading: ES
Posts: 67 since Sep 2011
Thanks Given: 6
Thanks Received: 34

 
Code
int divsor = 1 + new[] { symbol01, symbol02, symbol03, symbol04, symbol05, symbol06, symbol07,
symbol08, symbol09, symbol10 }.Count(s => !string.IsNullOrEmpty(s));
could you consider changing symbol01..symbol10 to string[] symbols = new string[10] ? it would simplify above to "divisor = 1 + symbols.Count(s => !string.IsNullOrEmpty(s));" :P

Reply With Quote
  #6 (permalink)
 
Zondor's Avatar
 Zondor 
Portland Oregon, United States
 
Experience: Beginner
Platform: NinjatraderŽ
Broker: CQG, Kinetick
Trading: Gameplay KlownbineŽ Trading of Globex
Posts: 1,333 since Jul 2009
Thanks Given: 1,246
Thanks Received: 2,731

Give some thought to how many times this test needs to be made and where this code is located.

Does it need to execute on every tick, on every bar, or just once (which is what it looks like to me, in which case it should be located in OnStartUp. Looks like that would require a different approach regarding what to do in response to the test.

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
Thanked by:




Last Updated on August 9, 2012


© 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