Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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​​
    Brandon H.NinjaTrader Customer Service

    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 snoussi, Today, 12:26 PM
      0 responses
      5 views
      0 likes
      Last Post snoussi
      by snoussi
       
      Started by SnailHorn, 05-02-2024, 10:49 PM
      3 responses
      28 views
      0 likes
      Last Post SnailHorn  
      Started by giulyko00, Today, 11:49 AM
      1 response
      6 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by marksingh87, 02-13-2024, 04:55 PM
      6 responses
      213 views
      1 like
      Last Post mshrstv
      by mshrstv
       
      Started by SnailHorn, Today, 08:38 AM
      2 responses
      5 views
      0 likes
      Last Post SnailHorn  
      Working...
      X