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

n bars need to pass

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

    n bars need to pass

    Hello,

    Attached is a screenshot of the NinjaScript code for the current 5 minute price bar being above the current high/below the current day's low. And below is that code.

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1
    || CurrentBars[1] < 1)
    return;

    // Set 1
    if (Lows[1][0] >= CurrentDayOHL1.CurrentHigh[0])
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), "");
    }

    // Set 2
    if (Highs[1][0] <= CurrentDayOHL1.CurrentLow[0])
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), "");
    }

    }


    What NinjaScript code would be needed to then say that anywhere from 1 to 5 five minute bars need to form and close before my next condition statement?

    #2
    Hello i2w8am9ii2,

    Thanks for your post.

    I think you are asking what you can do to prevent, for example, Set 2 from running until after set 1 has entered an order, and vice versa, for 1 to 5 5-minute bars.

    You could consider using the BarsSinceEntryExecution(): https://ninjatrader.com/support/help...yexecution.htm where you can specify the number of bars to test for (and you will need to test for -1 in case there is no previous entry).

    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hi Paul,

      Thanks for your reply and for the link.

      Actually, I'm wanting to limit the amount of time that elapses after entering a breakout , for a pullback to occur. I'm trying to avoid a pullback entry after 25+ five minute bars have passed.

      So I would not want a trade entry until after a breakout and then pullback. Because of that, BarsSinceEntryExecution() is probably not what I'm looking for.

      Initially I thought about the following code, to indicate that I want 1 five minute bar to have formed and closed but no more than 5 five minute bars to have formed and closed.

      But now I'm thinking that I would need something other than BarsSinceEntryExecution()

      protected override void OnBarUpdate()
      {
      if (CurrentBar < BarsRequiredToTrade)
      return;

      if ((BarsSinceEntryExecution() > 1 && BarsSinceEntryExecution() < 6) && CrossAbove(SMA(10), SMA(20), 1))
      EnterLong();
      }
      Last edited by i2w8am9ii2; 10-30-2018, 07:31 AM.

      Comment


        #4
        Hello i2w8am9ii2,

        Thanks for your reply.

        Almost, you need to account for the condition where there is no previous entry. As is your code would not work when loaded because there would never be an entry.

        try this:

        if (((BarsSinceEntryExecution() > 1 && BarsSinceEntryExecution() < 6) || BarsSinceEntryExecution() == -1) && CrossAbove(SMA(10), SMA(20), 1))

        EnterLong();
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Hi Paul,

          I realized that my post did not indicate what I wanted clearly, so I edited it at 04:31 AM. Then I saw that you replied to my post at 04:31 AM. So maybe you did not see my clarification regarding what I'm trying to do. But then again, maybe you did see it and your reply addressed it.

          Sorry for the confusion.

          Here is what I changed in my post that you might have missed:
          • Actually, I'm wanting to limit the amount of time that elapses after entering a breakout , for a pullback to occur. I'm trying to avoid a pullback entry after 25+ five minute bars have passed.

            So I would not want a trade entry until after a breakout and then pullback. Because of that, BarsSinceEntryExecution() is probably not what I'm looking for.


          In either case, regarding the code you specified, would this code work for what I'm wanting?

          protected override void OnBarUpdate()
          {
          if (BarsInProgress != 0)
          return;

          if (CurrentBars[0] < 1
          || CurrentBars[1] < 1)
          return;

          // Set 1
          if (((BarsSinceEntryExecution() > 1 && BarsSinceEntryExecution() < 6) || BarsSinceEntryExecution() == -1) &&
          (Lows[1][0] >= CurrentDayOHL1.CurrentHigh[0]))
          {
          EnterLong(Convert.ToInt32(DefaultQuantity), "");
          }

          // Set 2
          if (((BarsSinceEntryExecution() > 1 && BarsSinceEntryExecution() < 6) || BarsSinceEntryExecution() == -1) &&
          (Highs[1][0] <= CurrentDayOHL1.CurrentLow[0]))
          {
          EnterShort(Convert.ToInt32(DefaultQuantity), "");
          }

          }

          Comment


            #6
            Hello i2w8am9ii2,

            Thanks for your repies.

            Yes, I missed that you updated so thanks for posting again.

            I'm confused on what you are doing so you may want to create a chart picture and mark on there what you are wanting to do.

            Regarding the code, I see now that you are using a multi-timeframe script so that does change the code requirements. I recommend that you change each of the BarSinceEntryExecution() to BarsSinceEntryExecution(0,"",0) as this is needed in a multi-timeframe environment.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Hi Paul,

              Thanks for your reply. Sorry again for the confusion.

              Attached is an image showing two price action scenarios; the first scenario would result in no trade being entered (because too many price bars had formed after the breakout and before the pullback); and the second scenario would result in a trade being entered (because less than 6 price bars have formed after the breakout and pullback).

              I am wanting the code for the second scenario (breakout > 1 to 5 price bars have closed during pullback to breakout area > trade entry).

              Comment


                #8
                Hello i2w8am9ii2,

                Thanks for your reply and picture.

                If I understand correctly you basically want to set up a 2 step trigger. The first step is a cross above the current day high then if the pullback crosses below over the same level within 6 bars.

                The way you can do this is to check for step 1 with a crossabove and as the action when that condition is true would be to save the CurrentBar into an Int variable. For step 2 you would check that if CurrentBar minus the saved bar is > 1 and < 6 and check for the cross below condition to then place the order.

                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Paul,

                  Thanks for your reply and for the info.

                  I am trying to add the code for my Int variable to check if CurrentBar minus the saved bar is > 1 and < 6 in relation to the cross below condition, but get an error (CS1061).

                  Below is the code I'm using. Attached also is a screenshot of my code.

                  What should I change?

                  protected override void OnBarUpdate()
                  {
                  if (BarsInProgress != 0)
                  return;

                  if (CurrentBars[0] < 1
                  || CurrentBars[1] < 1)
                  return;

                  // Set 1
                  if ((CrossAbove(Lows[1], CurrentDayOHL1.CurrentHigh, 1))
                  && (CurrentBars[0].MyPullbackBar1 > 1 < 6)
                  && (CrossBelow(Lows[1], CurrentDayOHL1.CurrentHigh, 1)))
                  {
                  EnterLong(Convert.ToInt32(DefaultQuantity), "");
                  }

                  // Set 2
                  if ((CrossBelow(Lows[1], CurrentDayOHL1.CurrentLow, 1))
                  && (CurrentBars[0].MyPullbackBar1 > 1 < 6)
                  && (CrossAbove(Lows[1], CurrentDayOHL1.CurrentLow, 1)))
                  {
                  EnterShort(Convert.ToInt32(DefaultQuantity), "");
                  }

                  }

                  Comment


                    #10
                    Hello i2w8am9ii2,

                    Thanks for your reply.

                    You would need to have two separate sections (per side) (In strategy builder terms this would be 4 sets) for the 2 step process.

                    set 1 would check for the crossabove condition and the action would be to assign your variable MyPullbackBar = CurrentBars[0];

                    Set 2 would check for the difference of CurrentBar[0] - MyPullbackBar is greater than 1 and less than 6 And that the cross down condition has occurred. the action of the set would be to place the order

                    It needs to be two separate sets because of the timing from set 1 to set 2 can vary.

                    Something like:

                    //step 1
                    if ((CossAbove(Lows[1], CurrentDayOHL1.CurrentHigh, 1))
                    {
                    MyPullBackBar = CurrentBars[0]; // save the current bar number
                    }

                    // step 2
                    if ((CurrentBar - MyPullBackBar) > 1 && (CurrentBars[0] - MyPullbackBar < 6) && CrossBelow(Lows[1], CurrentDayOHL1.CurrentHigh, 1))
                    {
                    EnterLong();
                    }
                    Paul H.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi Paul,

                      Thanks so much for your help.

                      My strategy did compile with no errors now, so I will be able to test it later on.

                      I will start focusing on learning C# more at this time, as I know that it takes a lot of strategies to find ones that are worth using in the markets, and I know that takes a lot of coding.

                      I appreciate your help.

                      Comment


                        #12
                        Hi Paul,

                        One more thing before I start focusing on learning C# again...

                        The strategy produced no trades when I ran it through the Strategy Analyzer using a 15 minute time-frame and while going 2 years back, and I only have two input parameters, a stop-loss and profit target ($150 to $550 stop-loss and $550 to $950 profit target), and those two parameters and other settings mentioned usually produce many trades, even when I have other input parameters to go along with them.

                        So I'm thinking that there is either something that I did wrong with the order of the code or something like that, or that there is some other issue.

                        If you get a chance and have the time, I would really appreciate it if you could take a quick look at the attached .cs file and see if I did something wrong with the code.
                        Attached Files

                        Comment


                          #13
                          Hello i2w8am9ii2,

                          Thanks for your reply.

                          I don't see anything obvious.

                          We always recommend that when the ninjascript is not performing as expected to first check the log tab of the control center for any related errors. If no errors then the next step would be to debug the script by using print statements to verify the values that the logic is working on. This will be a tremendous aid to you to remove the mystery of why the code works as it does. While debugging is inherently tedious it does clarify what is going on and would be a good technique that will help you in the long run. We recommend that you review the debugging tips here: https://ninjatrader.com/support/help...script_cod.htm
                          Paul H.NinjaTrader Customer Service

                          Comment


                            #14
                            Hi Paul,

                            Thanks so much for finding the time to look at my NinjaScript file.

                            And thanks for the troubleshooting advice.

                            I will check it out.

                            I appreciate the help.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by elirion, Today, 09:32 PM
                            0 responses
                            2 views
                            0 likes
                            Last Post elirion
                            by elirion
                             
                            Started by cre8able, Today, 09:15 PM
                            1 response
                            5 views
                            0 likes
                            Last Post bltdavid  
                            Started by cummish, Today, 08:43 PM
                            0 responses
                            10 views
                            0 likes
                            Last Post cummish
                            by cummish
                             
                            Started by Option Whisperer, Today, 07:58 PM
                            4 responses
                            21 views
                            0 likes
                            Last Post Option Whisperer  
                            Started by ETFVoyageur, 05-07-2024, 07:05 PM
                            13 responses
                            87 views
                            0 likes
                            Last Post ETFVoyageur  
                            Working...
                            X