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

BarsSinceEntryExecution is broken

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

    BarsSinceEntryExecution is broken

    All strategies containing an exit using BarsSinceEntryExecution are exiting 2 bars later than specified. Please advise.

    #2
    Hello, thanks for writing in. My test script works using this method. See my screenshot below. The condition becomes true on the 9:26:50 bar and the signal is processed on the first tick of the next bar. This may be what you are seeing. If so, reduce the value compared against BarsSinceEntryExecution, e.g. instead of 3 use 1 to exit on the first tick of the 2nd bar.



    Attached Files
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      I want to exit after x bars. If (BarsSinceEntryExecution() == 20) {ExitLong()}
      This has always worked, with bars in the trade showing as 20 and suddenly all of my scripts no longer work

      Comment


        #4
        Hi, have you used Prints to debug your strategy? Using Print should be the first thing you do to debug any issues you are having with your strategy code. What do the Prints say when you print out:
        if(Postion.MarketPostion == MarketPosition.Long)
        Print(BarsSinceEntryExecution());

        My example works, so this means that BarsSinceEntryExecution() works in all cases, so if you are getting unexpected behavior, you need to debug your script.

        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Hi Chris,
          I set the Bars to "if greater than 19, exit". The output window says 20, but the strategy analyzer "Trades" output says 22, and if I count the bars on the chart, There are 21. Lots of variety
          David

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

          if (CurrentBars[0] < 5)
          return;

          // Set 1
          if ((Close[0] > Close[5])
          && (Position.MarketPosition == MarketPosition.Flat))
          {
          EnterLong(Convert.ToInt32(DefaultQuantity), "");
          }

          // Set 2
          if ((BarsSinceEntryExecution(0, "", 0) > BarsToHold)
          && (Position.MarketPosition == MarketPosition.Long))
          {
          ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
          }

          if(Position.MarketPosition == MarketPosition.Long)
          Print(BarsSinceEntryExecution());


          At most, the trades should be closing on the open of the 21st bar, not the open of the 22nd bar, which is what's happening.
          Attached Files
          Last edited by trader3000a; 05-19-2023, 11:22 AM.

          Comment


            #6
            Hi, it's exiting on the 21st bar which is expected. Again, going back to the example I posted, the order is processed on the first tick of the next bar, so the exit will occur on the 21st bar.
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Hi Chris,
              It's exiting on the 22nd bar. Please see the new picture where i've counted them out. That's 1 bar too many.
              David
              Attached Files
              Last edited by trader3000a; 05-19-2023, 02:19 PM.

              Comment


                #8
                another example with attached strategy and settings. Exit on 22nd bar open, not 21st.
                Attached Files

                Comment


                  #9
                  Hi, you are counting the bar where the entry is made, which is not correct. The first bar is the bar directly after the bar where the entry took place.

                  Chris L.NinjaTrader Customer Service

                  Comment


                    #10
                    Hi Chris,
                    If i enter on the open of a bar, I'm 100% in the trade on that bar. Please explain how entering a trade on the open of a bar and being in that trade for a full day doesn't count as being in the trade? The entryexecution is on the open of that bar on the chart. I think I understand what you are saying, but it's definitely counterintuitive.
                    David
                    Last edited by trader3000a; 05-19-2023, 03:20 PM.

                    Comment


                      #11
                      Hi, The BarsSinceEntryExecution method will return '0' when you call it on the bar where you entered at, so this must be accounted for in your script. After that bar closes, then BarsSinceEntryExecution will return 1, then 2, and so on.
                      Chris L.NinjaTrader Customer Service

                      Comment


                        #12
                        Hi Chris,
                        The function should absolutely be re-named BarsAgoSinceEntryExecution or something more clever, since a full bar elapses on the entry bar AFTER the execution. The intent of the user is to use this function to address the number of bars IN trade. Please add this as a feature request?
                        thanks,
                        David

                        Comment


                          #13
                          Hi, If we change any NinjaScript code like this it would be a very serious code-breaking change to the entire platform. All developers that ever used this method would need to change their code, so there is a low likelihood that this request would be fulfilled.
                          Chris L.NinjaTrader Customer Service

                          Comment


                            #14
                            Hi Chris,
                            Understood. Please add this feature request for me: Please add a function "BarsInTrade". If I use 20 as an input variable, for instance, I could simply write:

                            if(BarsInTrade == 20) {ExitLong(); ExitShort();}

                            and I could be assured that from entry to exit, my trade will include 20 bars. Simple and intuitive.

                            Unless of course, you already have something like that, and were waiting to tell me because you love these long rants of mine...

                            Comment


                              #15
                              Hi, this can be created in your script, you can create an integer variable at the class level, set it to 1 once a trade has been entered, then increment the variable after every call to OnBarUpdate, or after every time CurrentBar increments.
                              Chris L.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by pibrew, Today, 06:37 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post pibrew
                              by pibrew
                               
                              Started by rbeckmann05, Yesterday, 06:48 PM
                              1 response
                              14 views
                              0 likes
                              Last Post bltdavid  
                              Started by llanqui, Today, 03:53 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post llanqui
                              by llanqui
                               
                              Started by burtoninlondon, Today, 12:38 AM
                              0 responses
                              11 views
                              0 likes
                              Last Post burtoninlondon  
                              Started by AaronKoRn, Yesterday, 09:49 PM
                              0 responses
                              15 views
                              0 likes
                              Last Post AaronKoRn  
                              Working...
                              X