Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Enter position after 3 consecutive bars in a direction

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

    Enter position after 3 consecutive bars in a direction

    Hi there,

    I'm trying to have a strategy enter after 3 consectutive closes in a specific direction, and reverse after only after 3 bars have gone the opposite direction. Here was my first attempt at the strategy but it's not working quite right. Hoping someone can take a look to see what my flaw is here:

    Code:
    protected override void OnBarUpdate()
            {
                if (BarsInProgress != 0)
                    return;
    
                if (CurrentBars[0] < 4)
                    return;
    
                 // Set 1
                if ((High[0] > High[1])
                     && (High[1] > High[2])
                     && (High[2] > High[3])
                     && (High[3] < High[4]))
                {
                    EnterLong(Convert.ToInt32(DefaultQuantity), @"long");
                }
    
                 // Set 2
                if ((Low[0] < Low[1])
                     && (Low[1] < Low[2])
                     && (Low[2] < Low[3])
                     && (Low[3] > Low[4]))
                {
                    EnterShort(Convert.ToInt32(DefaultQuantity), @"short");
                }​

    #2
    Hello quicksandatl,

    Thanks for your post.

    To check if the current bar and the prior 2 bars are up bars (total of 3 bars), the condition would look something like this:

    if (Close[0] > Open[0] && Close[1] > Open[1] && Close[2] > Open[2])
    {
    //do something
    }

    To check if the current bar and the prior 2 bars are down bars (total of 3 bars), the condition would look something like this:

    if (Close[0] < Open[0] && Close[1] < Open[1] && Close[2] < Open[2])
    {
    //do something
    }

    Ultimately, if a strategy is not behaving as expected then you should add debugging prints (outside of the condition) that prints out all the values used in the condition to see how they are evaluating.

    In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar. Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).​

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121​​
    <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


      #3
      NinjaTrader_BrandonH that worked perfectly. The customer support on this forum is incredible, thank you!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, 03-13-2026, 05:17 AM
      0 responses
      87 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      151 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      80 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      53 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      62 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X