Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Programming question

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

    Programming question

    As usual, I am reverse engineering learning Ninjascript instead of properly from the start of a tutorial, but this is how I learn.

    My question is how to program if then events that are "nested" so that if one condition (step 1) is true I then do step 2, and within step 2 are other conditions that if met do things. Like this sample of a multi time frame strategy:

    {

    if (BarsInProgress != 0)
    return;

    {
    // Checks if the 5 period SMA is below the 15 period SMA on the 3, 8, 13, 60 minute time frames
    if (SMA(BarsArray[1], sMA5)[0] < SMA(BarsArray[1], sMA15)[0] && SMA(BarsArray[2], sMA5)[0] < SMA(BarsArray[2], sMA15)[0] && SMA(BarsArray[3], sMA5)[0] < SMA(BarsArray[3], sMA15)[0] && SMA(BarsArray[4], sMA5)[0] < SMA(BarsArray[4], sMA15)[0])

    {
    // Checks for a cross below condition of the 5 and 15 period SMA on the primary Bars object and enters short
    if (CrossBelow(Stochastics(PeriodD, PeriodK, Smooth).D, Stochastics(PeriodD, PeriodK, Smooth).K, 1)
    && Stochastics(PeriodD, PeriodK, Smooth).D[0] > TopLine)
    EnterShort(sHAREposition1, "SHAREposition1signal");
    }
    }
    {

    if (SMA(BarsArray[2], sMA5)[0] < SMA(BarsArray[2], sMA15)[0] && SMA(BarsArray[3], sMA5)[0] < SMA(BarsArray[3], sMA15)[0] && SMA(BarsArray[4], sMA5)[0] < SMA(BarsArray[4], sMA15)[0])
    // Checks for a cross below condition on the 3min Bars object and enters short

    {
    if (CrossBelow(Stochastics(BarsArray[1], PeriodD, PeriodK, Smooth).D, Stochastics(BarsArray[1],PeriodD, PeriodK, Smooth).K, 1)
    && Stochastics(BarsArray[1],PeriodD, PeriodK, Smooth).D[0] > TopLine)
    EnterShort(sHAREposition2, "SHAREposition2signal");
    }
    }

    // Checks for a cross above condition on the primary Bars object and exits short
    if (CrossAbove(Stochastics(PeriodD, PeriodK, Smooth).K, Stochastics(PeriodD, PeriodK, Smooth).D, 1)
    && Stochastics(PeriodD, PeriodK, Smooth).D[0] < BottomLine)
    ExitShort("SHAREposition1exit","SHAREposition1sign al");

    // Checks for a cross above condition 3min Bars object and exits short
    if (CrossAbove(Stochastics(BarsArray[1],PeriodD, PeriodK, Smooth).K, Stochastics(BarsArray[1],PeriodD, PeriodK, Smooth).D, 1)
    && Stochastics(BarsArray[1],PeriodD, PeriodK, Smooth).D[0] < BottomLine)
    ExitShort("SHAREposition2exit","SHAREposition2sign al");





    }

    For instance, where I do this:
    // Checks for a cross below condition of the 5 and 15 period SMA on the primary Bars object and enters short
    if (CrossBelow(Stochastics(PeriodD, PeriodK, Smooth).D, Stochastics(PeriodD, PeriodK, Smooth).K, 1)
    && Stochastics(PeriodD, PeriodK, Smooth).D[0] > TopLine)
    EnterShort(sHAREposition1, "SHAREposition1signal");

    that will excecute when the higher level condition below is met:
    // Checks if the 5 period SMA is below the 15 period SMA on the 3, 8, 13, 60 minute time frames
    if (SMA(BarsArray[1], sMA5)[0] < SMA(BarsArray[1], sMA15)[0] && SMA(BarsArray[2], sMA5)[0] < SMA(BarsArray[2], sMA15)[0] && SMA(BarsArray[3], sMA5)[0] < SMA(BarsArray[3], sMA15)[0] && SMA(BarsArray[4], sMA5)[0] < SMA(BarsArray[4], sMA15)[0])

    Ok, so my question is how many of these can I nest or have in some hiearchy? I notice in the Wizard I can have sets of conditions. With not using the wizard, can I have sets of conditions within conditions, like this

    {
    if (X<Z)
    { if (S<D) go short
    {if (S<G) go short
    }
    }
    }

    What I would like is to filter down when sets of conditions are met, so that at some level certain trades can be taken, this would mean I could more or less run multiple entries and exits on the same stock as certain conditions are met or expire.

    My apologies for this very long winded explanation. I am reading some tutorials and any links would be appreciated.

    #2
    Hi sauer11155,

    I think you already got most of it. What you want to do is done by nesting if statements. You can nest as many if statements together as you want.

    For example:
    Code:
    if (Close[0] > Open[0])
    {
         if (High[0] > High[1])
         {
              if (High[1] > High[2])
              {
                   EnterLong();
               }
          }
    }
    The key thing to notice is that you will generally want a set of { } brackets per if statement.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Thanks. Is there somewhere I can learn the proper syntax for using those brackets? I think I'm a bit muddled.

      Comment


        #4
        You've got it right already. The idea is just you want a pair of { } brackets for each if statement.

        The indentations are there to help you keep track of the nesting. You can see the brackets lined up on the same level with the first if-statement. Those brackets are associated with each other. All sublevel brackets are lined up with their own if statement that they are corresponding with.

        The key thing to remember is that brackets come in pairs.

        Google is a great resource if you wish to search more. Maybe try searching coding conventions or something along those lines.
        Josh P.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        574 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        333 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        101 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        553 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        551 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X