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

Strategy "exited a short position" twice, therefore entering a long position...?

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

    Strategy "exited a short position" twice, therefore entering a long position...?

    My Strategy "exited a short position" twice, and therefore enetered a long position on the 2nd "exit", since the order size is only 1. How do I prevent my strategy from "exiting twice" and thus falsely entering trades..? Is this a known glitch that occurs if you don't write your entries and exits some specific way, or is my instance just being buggy maybe?

    Thanks

    Click image for larger version

Name:	image.png
Views:	47
Size:	21.6 KB
ID:	1300498

    #2
    Hello agclub,

    Thank you for your post.

    Are you perhaps calling an exit order and an entry order on the same pass of OnBarUpdate? This can lead to the strategy position entering the opposite position with double the quantity or will result in an Overfill.

    When calling an entry method in the opposite direction of your position this will cause your position to be reversed. NinjaTrader will automatically submit an order to close your existing position and then enter an order to enter you into the opposite position.

    If you exit and then call an entry method in the same run of OnBarUpdate, the OnPositionUpdate() will not have yet run and NinjaTrader will not have known that your position is closed. This will cause both actions to complete and end up sending 3 orders. The first order is the exit position from your exit method, the second order is to close the position from NinjaTrader automatically reversing your position, the third order is to enter you into the opposite position.

    The result is that either the script will double the quantity when it reverses or it will cause an overfill and stop the script.

    This is discussed further in this post: https://forum.ninjatrader.com/forum/...16#post1036616

    Please let us know if this doesn't guide you in the right direction.
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Gaby View Post
      Hello agclub,

      Thank you for your post.

      Are you perhaps calling an exit order and an entry order on the same pass of OnBarUpdate? This can lead to the strategy position entering the opposite position with double the quantity or will result in an Overfill.

      When calling an entry method in the opposite direction of your position this will cause your position to be reversed. NinjaTrader will automatically submit an order to close your existing position and then enter an order to enter you into the opposite position.

      If you exit and then call an entry method in the same run of OnBarUpdate, the OnPositionUpdate() will not have yet run and NinjaTrader will not have known that your position is closed. This will cause both actions to complete and end up sending 3 orders. The first order is the exit position from your exit method, the second order is to close the position from NinjaTrader automatically reversing your position, the third order is to enter you into the opposite position.

      The result is that either the script will double the quantity when it reverses or it will cause an overfill and stop the script.

      This is discussed further in this post: https://forum.ninjatrader.com/forum/...16#post1036616

      Please let us know if this doesn't guide you in the right direction.
      Hi Gaby. Thanks for the reply.

      ...and I see... Hmmm, so what is the typical solution to this? I need to have multiple runs of OnBarUpdate ?? Like, 1 section for entries and a separate 1 for exits? Or I need to look into using OnOrderUpdate?

      Thanks

      -mike
      Last edited by agclub; 04-22-2024, 09:40 PM.

      Comment


        #4
        Originally posted by agclub View Post

        Hi Gaby. Thanks for the reply.

        ...and I see... Hmmm, so what is the typical solution to this? I need to have multiple runs of OnBarUpdate ?? Like, 1 section for entries and a separate 1 for exits? Or I need to look into using OnOrderUpdate?

        Thanks

        -mike
        you should have a bool to check if you're in 1 position long or short, then another check to see if you're flat and if there are any pending orders. This eliminated my overfill issues.

        Comment


          #5
          Hello agclub,

          If you want to simply reverse the position, call an entry order in the opposite direction of the position without calling an exit method first. For example if the position is long, then call EnterShort(). You don't need to call ExitLong() to reverse the position.

          Please let us know if you have any further questions.
          Gaby V.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_Gaby View Post
            Hello agclub,

            If you want to simply reverse the position, call an entry order in the opposite direction of the position without calling an exit method first. For example if the position is long, then call EnterShort(). You don't need to call ExitLong() to reverse the position.

            Please let us know if you have any further questions.
            Hi gaby. I think its possible my original question was misunderstood. I never wanted to reverse a position. If you see my screenshot above, the "GoS" which is a short entry, exited twice. (ExS, and ExS another time) Thus, on the second time it entered a long trade. I want to prevent this double exiting from occurring at all.

            Comment


              #7
              Hello agclub,

              To understand why the script is behaving as it is, such as placing orders or not placing orders when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

              In the strategy add prints (outside of any conditions) that print the date time of the bar and all values compared in every condition that places an order.

              The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.

              Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

              Further, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

              I am happy to assist you with analyzing the output from the output window.

              Run or backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output text file to your reply.

              Below is a link to a forum post that demonstrates using informative prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.


              Please let me know if I may further assist with analyzing the output or if you need any assistance creating a print or enabling TraceOrders.​
              Gaby V.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Ryan333, Yesterday, 05:25 PM
              1 response
              9 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by hsammons87, Yesterday, 05:21 PM
              2 responses
              20 views
              0 likes
              Last Post NinjaTrader_LuisH  
              Started by PrTester, 12-29-2008, 05:27 PM
              132 responses
              96,706 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by ETFVoyageur, Yesterday, 10:34 AM
              11 responses
              30 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by smartromain, Today, 06:05 AM
              0 responses
              6 views
              0 likes
              Last Post smartromain  
              Working...
              X