Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Exit Position Seconds Before Bar Close

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

    Exit Position Seconds Before Bar Close

    I want to use the NinjaScript Wizard to create a way to exit (Exit Long or Exit Short) an open position (whether long or short) 30 seconds before the current bar closes (for example, 30 seconds before a 60 minute bar closes). I've used used the Wizard to say "if the Bar Timer = Numeric Value of 000030, then ExitLong and Exit Short" but it does not seem to work. What's the best way to set this up?

    #2
    Be sure you are using CalculateOnBarClose = false.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the reply. I've done that. Something else I'm doing must be wrong.

      I'm trying to cancel any open positions 30 seconds before the current bar closes and then enter a market buy at the close of the current bar if the previous bar's close is lower than it's open. I have unchecked CaclulateOnBarClose so it is false and have Set 1 in the Wizard showing: BarTimer[][0] == 30, then ExitLong["",""] and ExitShort["",""]. Set 2 shows: Close[0] < Open[0] and BarTimer[][0] == 0, then EnterLongQuantity, "").

      Where did I go wrong?

      Comment


        #4
        Are you backtesting or are you live? Your logic will only work in real-time.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          What do you mean? I am connected live to my ZenFire data stream and applying a Strategy to the current chart. When I do so, I see what the effect of the strategy would have been in the past. Is that what you mean?

          Comment


            #6
            You are processing historical bars and as such that is backtesting. BarTimer does not work in a backtesting environment. It will only work on real-time bars.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Sorry--I made a mistake in my statement. In the case of the strategy I'm trying to create, when I apply it to a chart, no history shows up. When I apply other strategies I have created successfully to any chart, I see a history of the effect the strategy would have had.

              Comment


                #8
                When you look at the historical bars all of those bars are always processed as CalculateOnBarClose = true. Any order that is filled is actually placed the bar before that at the close. The order then goes in at the open of the next bar. If during that bar a stop/target would have filled then you can exit on the same bar, but there is for sure no "30 seconds before bar close" logic.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Is there another way to accomplish what I want to do, then, besides using BarTimer? Again, I want to cancel any orders outstanding or in process 30 seconds before the close of the current bar and, also, enter a buy order, for example, at the close of the current bar. How would I do that if I have CalculateOnBarClose unchecked (false)? Is there a way to accomplish this with Calculate OnBarClose checked (true)?

                  Comment


                    #10
                    djdinut1,

                    What I have been trying to explain is that you cannot do this in backtesting. You just do not have the granularity levels to allow you such intrabar logic. In backtesting you are always CalculateOnBarClose = true.

                    What you want to do is only possible in real-time with CalculateOnBarClose = false and can be done with BarTimer.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      The Bar Timer is not working for me. I created an elementary strategy titled "Test" where I have asked an alarm to sound whenever the bar timer reaches 30 seconds and another when it reaches one second. Neither alarm is being activated. I've attached the .cs file in case you want to take a look to see what I may be doing wrong.
                      Attached Files

                      Comment


                        #12
                        Looking into BarTimer you get this: "The indicator 'BarTimer' is intended for visualization and can only be used on a chart."

                        It would appear this would not fly then. The only way you can proceed now is to use DateTime.Now which is literally to look at your PC clock and compare it to the timestamps of your bars. Unfortunately this is more advanced C# than what we can offer indepth support for. There are many articles on google discussing how to program with DateTime objects though.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          If I understand correctly, you're saying the Bar Timer cannot be used to trigger actions. It is only a visual indicator?

                          Is there, then, any other way to set up closing all orders in process x number of minutes or seconds prior to the close of the current bar?

                          Also, can I run two different strategies at once on one chart?

                          Comment


                            #14
                            djdinut1,

                            BarTimer appears to be a visualization indicator only so you cannot programmatically access values from it. DateTime.Now is the way to proceed as suggested in my previous post.

                            You can run two strategies on one chart, but this may be confusing. I would suggest two charts for simplicity.
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              You've been very helpful. Another question, please.

                              If I apply two strategies to one chart and the Strategy #1 is CalculateOnBarClose true and triggers a long entry and Strategy #2 is CalculateOnBarClose false and says to that at 1:59 PM to ExitLong and ExitShort, would this close the long entry if it has not been filled?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              647 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              368 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              108 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              571 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              573 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X