Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Enter Position x number of bars after signal

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

    Enter Position x number of bars after signal

    Hi,

    I'm new to ninjascript. I'm building a strategy and have written all conditions so far.

    Everything works fine however i would like to enter long or short 3 bars after my signal occurs. I've looked over CurrentBar but i can't seem to figure it out. I'm thinking i need some sort of counter.

    Could you guys please help me out a bit?

    #2
    Hi Skorpyo,

    Thank you for posting and welcome to the forums!

    You would want to use BarsSinceEntry() to tell your code to wait a certain number of bars before making a move.

    BarsSinceEntry()

    Let me know if I can be of further assistance.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the quick reply. I've tried implementing it but when i backtest, the orders do not seem to go through.

      Here is the code:

      protected override void OnBarUpdate()
      {
      // Condition set 1
      if (Close[-1] < Close[0] && Close[-2] < Low[-1] && Close[-3] < Close[-1] && BarsSinceEntry()==3)
      {
      ExitLong("ExitShort", "EnterLongNew");
      EnterShort(DefaultQuantity, "EnterShortNew");
      }

      // Condition set 2
      if (Close[-1] > Close[0]
      && Close[-2] > High[-1]
      && Close[-3] > Close[-1]&&BarsSinceEntry()==3)
      {

      ExitShort("ExitLong", "EnterShortNew");
      EnterLong(DefaultQuantity, "EnterLongNew");
      }
      }

      Where am i going wrong with this?

      Thanks a lot!

      Comment


        #4
        Skorpyo,

        With the Managed approach of placing orders if you have a long/short position and use a enter in the opposite direction it will close out the current position and enter in the opposite direction.

        Since, you are submitting a Exit and Enter at the same time you can run into situations were you could enter in more than one contract in the same direction or your orders could potentially cancel out.

        I recommend using just the Enter order for your conditions. If you want to exit out at a different time or condition you can still use the Exit orders but in different if statement and condition.

        Let me know if I can be of further assistance,
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          Thanks a lot for the recommendation. I removed the exit order but i see it still does not enter in any of the orders when backtesting. Am i using BarsSinceEntry() correctly?

          protected override void Initialize()
          {
          CalculateOnBarClose = true;
          }

          protected override void OnBarUpdate()
          {
          // Condition set 1
          if (Close[-1] < Close[0] && Close[-2] < Low[-1] && Close[-3] < Close[-1] && BarsSinceEntry()==3)
          {

          EnterShort();
          }

          // Condition set 2
          if (Close[-1] > Close[0] && Close[-2] > High[-1] && Close[-3] > Close[-1] && BarsSinceEntry()==3)
          {
          EnterLong();
          }
          }

          Comment


            #6
            Skorpyo,

            While you can use a negative index value, I wouldn't recommend it as you wouldn't have this work in the real time.

            I recommend using positive numbers for the bar index for the price series.

            Such as,
            Code:
            (Close[1] < Close[0] && Close[2] < Low[1] && Close[3] < Close[1] && BarsSinceEntry()==3)
            Let me know if this helps
            Cal H.NinjaTrader Customer Service

            Comment


              #7
              I've modified the code according to your recommendation but Strategy Analyzer still does not seem to open any orders when using the BarsSinceEntry() command. If i remove it however, the strategy does indeed open and close trades. Is there any other way of doing it?

              Comment


                #8
                Skorpyo,

                Would you mind attaching your strategy so that I may test this out on my end?

                You can add an attachment by clicking Reply -> Go Advanced -> and clicking the paperclip on the top toolbar.

                Additionally, you can find the strategy in (My) Documents -> NinjaTrader 7 -> bin -> Custom -> Strategy
                Cal H.NinjaTrader Customer Service

                Comment


                  #9
                  Done. Thanks a lot!
                  Attached Files

                  Comment


                    #10
                    Skorpyo,

                    Thank you for the code.

                    You are testing the BarsSinceEntry before you even had an entry for the script.

                    I would have a condition that would first enter your position but then have a bool set so that it won't become true again.

                    An example -
                    Code:
                    if(Close[1] < Close[0] && myBool == true)
                    {
                         EnterShort();
                         myBool = false;
                    }
                    You would first declare the variable in the variables region.

                    This would stop this particular condition from becoming true again.
                    Cal H.NinjaTrader Customer Service

                    Comment


                      #11
                      I'm a bit confused now. I understand the principle behind but where do i exactly put the bool?

                      I defined it in the variables region as "bool myBool;"

                      Do i put it in my condition set 1?

                      (Close[1] < Close[0] && Close[2] < Low[1] && Close[3] < Close[1] && BarsSinceEntry()==3 && myBool==true)?

                      Comment


                        #12
                        Skorpyo,

                        See the attached file for a sample of how to use the bool.

                        Let me know if this clarifies things up
                        Attached Files
                        Cal H.NinjaTrader Customer Service

                        Comment


                          #13
                          I see. I have updated the code:


                          Code:
                          // Condition set 1
                                     
                           if (Close[1] < Close[0] && Close[2] < Low[1] && Close[3] < Close[1] && BarsSinceEntry()==3 && myBool == true)
                          			{
                                        	EnterShort();
                                          myBool = false;
                                      }
                          
                          // Condition set 2
                                      if (Close[1] > Close[0] && Close[2] > High[1] && Close[3] > Close[1] && BarsSinceEntry()==3 && myBool == true)
                          			{
                          				EnterLong();
                                          myBool = false;
                                      }
                          However, the strategy still does not enter any orders.

                          Comment


                            #14
                            Skorpyo,

                            That is not what I provided in the sample script that I attached in post #12.

                            You will need to have a starting if condition that will first enter you into the market but will change the bool to false so that the first IF statement does not become true anymore, allowing you to use the BarsSinceEntry for the rest of the conditions.
                            Cal H.NinjaTrader Customer Service

                            Comment


                              #15
                              Skorpyo.......

                              Why don't you just use this code..........

                              (Close[4] < Close[3] && Close[5] < Low[4] && Close[6] < Close[4]

                              That would check your conditions and delay the entry just like you want..........

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              656 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              370 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              109 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              574 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              577 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X