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 fx.practic, 10-15-2013, 12:53 AM
      5 responses
      5,403 views
      0 likes
      Last Post Bidder
      by Bidder
       
      Started by Shai Samuel, 07-02-2022, 02:46 PM
      4 responses
      95 views
      0 likes
      Last Post Bidder
      by Bidder
       
      Started by DJ888, Yesterday, 10:57 PM
      0 responses
      7 views
      0 likes
      Last Post DJ888
      by DJ888
       
      Started by MacDad, 02-25-2024, 11:48 PM
      7 responses
      159 views
      0 likes
      Last Post loganjarosz123  
      Started by Belfortbucks, Yesterday, 09:29 PM
      0 responses
      8 views
      0 likes
      Last Post Belfortbucks  
      Working...
      X