Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

If statements

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    If statements

    I'm having trouble with "If" statements. Typically "If" statements are multiple choice but I need an "If" "And If" "Else" statement. Can you please tell me how to do this. I have 2 overlapping time frames and 2 different strategies in one. Strategy 1 needs to start at 9:00am and end at 12:00pm. Then Strategy 2 needs to start at 10:00am and end at 2:00pm. So they will both be trading (overlapping) from 10:00am to 12:00pm. But the way I now have the code written Strategy 2 does not start utill Strategy 1 stops and I understand it is because the first "If" is still True. So how should this code be written to overlap the 2 strategies with 2 unique time frames. Thanks

    protected override void OnBarUpdate()
    {
    if (TimeToTrade1)// 9:00am - 12:00pm Strategy 1
    {
    BackColor = Color.FromArgb(10,255,0,0);

    Buy_1();
    Buy_1_Profit();
    Buy_1_Loss();
    Buy_1_Exit();

    Sell_1();
    Sell_1_Profit();
    Sell_1_Loss();
    Sell_1_Exit();
    }
    else if (TimeToTrade2) // 10:00am - 2:00pm Strategy 2
    {
    BackColor = Color.FromArgb(10,0,0,255);

    Buy_2();
    Buy_2_Profit();
    Buy_2_Loss();
    Buy_2_Exit();

    Sell_2();
    Sell_2_Profit();
    Sell_2_Loss();
    Sell_2_Exit();
    }
    else // If not Time To Trade
    {
    SessionExit(); // Exit all trades...
    }
    }

    #2
    Hi kenb2004,

    If you're looking for help with branching commands, you may want to look into a general programming guide. Our documentation on their usage in NinjaScript is limited to this page, but you should be able to get more out of a general purpose guide.

    The way your code is structured now, it doesn't evaluate your else if line, unless the first if statement is also false. That means it only evaluates from 12:01 - 2:00. If you want it to evaluate the condition independently of the previous if statement, then use if instead of else if.

    else if (TimeToTrade2) // 10:00am - 2:00pm Strategy 2
    Ryan M.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    646 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    367 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    107 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    569 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    573 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X