Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to check whether a condition is true within x bars ago?

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

    How to check whether a condition is true within x bars ago?


    For example, if the following condition (below) is true within the last 10 bars.

    <Condition>
    if ((MACD1.Avg[0] >= 10)
    && (Close[0] > SMA2[0])
    )



    Execute the following script (if the condition above is true as of 10 bars ago):

    <Action to execute, if Condition is True>
    Draw.TriangleDown(xxxxxxxxxxxxxxxxx);

    #2
    Hello cryfgg,

    Thanks for your post.

    You could use a loop to change the BarsAgo value to look back 10 bars to see if the condition is true in the last 10 bars.

    Note that you would need to make sure to use a CurrentBar check for 10 bars to avoid an index out-of-range error.

    See the help guide documentation below.
    LoopingCommands: https://ninjatrader.com/support/help...g_commands.htm
    CurrentBar: https://ninjatrader.com/support/help...currentbar.htm
    Make sure you have enough bars in the data series you are accessing: https://ninjatrader.com/support/help...nough_bars.htm

    Let us know if we may assist further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    566 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    330 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
    547 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    548 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X