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 Mindset, 04-21-2026, 06:46 AM
      0 responses
      89 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      135 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by M4ndoo, 04-19-2026, 05:54 PM
      0 responses
      68 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      119 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      69 views
      0 likes
      Last Post PaulMohn  
      Working...
      X