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

Exit on close

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

    Exit on close

    NinjaTrader Community,

    A common inquiry is that the Exit on close didn't work in a NinjaScript Strategy because there is a position after the exit on close should have occurred.

    When viewing the log we often find that the Exit on close does indeed exit the position shortly before the end of the session (based on the Exit on close seconds), however, after the exit the strategy places a new order and re-enters the position.

    The Exit on close trigger will not prevent a NinjaScript Strategy from placing new orders after this has triggered and this can cause your strategy to take a new position before the end of the session (causing a position to be left open when the session is closed).

    I've created a video to demonstrate that first, the Exit on close does work, and second that a new position will be created if the script allows for it. Last, I demonstrate a script that uses a Session Iterator and the Exit on close seconds to set a flag that prevents trades after the exit on close until the new session has started.

    https://drive.google.com/file/d/1KP5...Wh1Zp7s3X/view

    Attached is the PreventEntryAfterExitOnCloseExample tested in the video.
    PreventEntryAfterExitOnCloseExample_NT8.zip
    ExitOnCloseTest_NT8.zip
    Attached Files
    Last edited by NinjaTrader_ChelseaB; 06-02-2021, 08:52 AM.
    Chelsea B.NinjaTrader Customer Service

    #2
    is there a difference to adding all that script and making a session iteration rather than just adding a check for currentbars[0] != bars.isfirstbarofsession into your script.

    all else equal and granted that we are using calculate on bar close. it feels like it would produce the same result with a lot less clutter?

    Comment


      #3
      Hello travelingsalesman,

      Thank you for your note.

      You could prevent a strategy from taking a trade after exit on close was called many different ways.

      Resetting a variable which prevents trades being taken after ExitOnClose using Bars.IsFirstBarOfSession would work too. An example of resetting a variable on first bar of session can be found here,


      You could also use a time filter. An example of using a time filter can be found below,


      Please let us know if you need further assistance.
      Alan P.NinjaTrader Customer Service

      Comment


        #4
        awesome thanks for that info

        Comment


          #5
          just wanted to follow up for anybody else who might stumble upon this thread i was able to fix my problem just by adding this line of code to the beginning of the script BEFORE the actual trading logic.

          if (Bars.IsFirstBarOfSession || Bars.IsLastBarOfSession)
          return;

          this prevents any trades to be entered at the last bar of a session or first bar of a session. for me my problem was the last bar of the session was triggering trades

          hope this helps someone.

          Comment


            #6
            Originally posted by travelingsalesman View Post
            just wanted to follow up for anybody else who might stumble upon this thread i was able to fix my problem just by adding this line of code to the beginning of the script BEFORE the actual trading logic.

            if (Bars.IsFirstBarOfSession || Bars.IsLastBarOfSession)
            return;

            this prevents any trades to be entered at the last bar of a session or first bar of a session. for me my problem was the last bar of the session was triggering trades

            hope this helps someone.
            Thanks for sharing. I assume this will work only for Time based bars. Tick based bars won't work. Can anyone confirm?

            Comment


              #7
              Hi roblogic, thanks for your post.

              Those two properties will still work with tick based charts. I tested with the playback connection and received a session begin and end on 100 tick and 10000 tick charts.

              Best regards.
              Chris L.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_AlanP View Post
                Hello travelingsalesman,

                Resetting a variable which prevents trades being taken after ExitOnClose using Bars.IsFirstBarOfSession would work too. An example of resetting a variable on first bar of session can be found here,

                Hello,

                In this particular situation:

                Resetting a variable in Bars.IsFirstBarOfSession is clear but how a bool can be reset if, IsExitOnSessionCloseStrategy == true and the X seconds of ExitOnSessionCloseSeconds have elapsed to prevent new entries?

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by TraderBCL, Today, 04:38 AM
                2 responses
                7 views
                0 likes
                Last Post TraderBCL  
                Started by martin70, 03-24-2023, 04:58 AM
                14 responses
                105 views
                0 likes
                Last Post martin70  
                Started by Radano, 06-10-2021, 01:40 AM
                19 responses
                606 views
                0 likes
                Last Post Radano
                by Radano
                 
                Started by KenneGaray, Today, 03:48 AM
                0 responses
                4 views
                0 likes
                Last Post KenneGaray  
                Started by thanajo, 05-04-2021, 02:11 AM
                4 responses
                471 views
                0 likes
                Last Post tradingnasdaqprueba  
                Working...
                X