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

Sell Limit order not executing on backtest?

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

    Sell Limit order not executing on backtest?

    So I have a strategy that buys limit when a range candle closes below a moving average, and then exits the long position with a limit order when the candle closes above the moving average.
    EnterLongLimit works fine, all of them execute, but NONE of my ExitLongLimit execute on the backtest on strategy analyzer.
    If I switch the exit order to ExitLong, then the strategy works and all my long position is closed every time a bar closes above the moving average, But I want to exit with a limit order to make sure I'm not paying for the spread.
    Again, NONE of my ExitLongLimit orders execute, only the buys.

    What am I doing wrong?





    #2
    Hello franco12364,

    Thanks for your post.

    Please check the "log" tab of the control center when you run your strategy for any error messages related to your strategy.

    Whenever a script (indicator or strategy for example) is not performing as expected, checking the log tab will usually evidence runtime errors or warnings.

    If you are using the "Managed approach", I suspect you may want to review the "Internal Order Handling Rules that Reduce Unwanted Positions" https://ninjatrader.com/support/help...d_approach.htm



    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Okay, read the rules and I see why the ExitLongLimit() is being ignored, which is marked in red below.

      "Set() methods that generate orders to exit a position will be ignored if:

      •A position is open and an order submitted by an enter method (EnterLongLimit() for example) is active and the order is used to open a position in the opposite direction

      A position is open and an order submitted by a non market order exit method (ExitLongLimit() for example) is active"


      But I am still confused, because doesn't this defeat the entire purpose of order, which is to exit my open long position? What else would ExitLongLimit() be used for but exiting my existing long position? Very odd...
      How could I surpass this? Is a market order ExitLong() the only way to exit my long position? Below is picture of the entry and exit code. It is a very simple strategy, only one lot opened at a time, and only long positions, never am I trying to reverse a position. All I want is to be able to exit my long with a limit order, not a market order.

      Any solutions?


      Attached Files

      Comment


        #4

        Hello franco12364,

        It looks like you copied the Set method rules however I don't see any mention of using the Set (SetStopLoss, SetProfitTarget) here for this to apply. You are more likely instead seeing one of the following for rules that apply toward orders that exit:


        Methods that generate orders to exit a position will be ignored if:

        •A position is open and an order submitted by an enter method (EnterLongLimit() for example) is active and the order is used to open a position in the opposite direction

        •A position is open and an order submitted by a set method (SetStopLoss() for example) is active


        Are you specifically seeing warnings in the log that mention the order handling rules and viewing the help guide or are you seeing something else? If you could open the NinjaScript output window and then re run the test to check for any output that would help me get a better idea of whats happening in that test. I briefly tried what you have in the image but saw a few different messages which could relate to my specific test.

        I look forward to being of further assistance.

        JesseNinjaTrader Customer Service

        Comment


          #5
          Thank you Jesse. It was indeed a SetStopLoss() that was making my ExitLongLimit() not work. So for my last question, is there really no way that a SetStopLoss() and an ExitLongLimit() can coexist? I can get creative to make sure my long position is stopped out by another parameter, but It would be much easier to use SetStopLoss() to make sure I only lose the desired amount. Would I have to unlock the code?

          Comment


            #6
            Hello franco12364,

            Thanks for clarifying it was the SetStopLoss.

            In the managed approach these rules exist to prevent unwanted positions, there is not really any way in the managed approach to have those specific methods coexist. You can use the workaround you mentioned or also use the unmanaged approach as that avoids the managed order handling rule set.



            This would not be available in the builder so if you wanted to use unmanaged you would need to unlock the code.



            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Hello,

              I am using managed approach and trying to reverse position when bar closes in an opposite direction. It works fine with Market orders. However, if I had a position and what to close that position and enter a limit order in the opposite direction with offset on that same bar - Strategy 'XYZ/-1': An Enter() method to submit an entry order at '5/5/2020 11:44:48 AM' has been ignored. Please search on the term 'Internal Order Handling Rules' in the Help Guide for detailed explanation.
              I looked at the help guide, and it seems that the only way to work around this problem is to use unmanaged approach. Is this still the case? Strategy in Strategy Analyzer needs to close open position and enter a limit order with offset at the same moment as position got closed...

              I do use "ChangeOrder" and "CancelOrder" in managed approach (not using SetTarget/Profit methods).

              The other idea I am thinking on trying out in managed approach (since I use multi-series including 1 tick series) - is to cancel all pending orders, close/reverse all market positions, and if I did have limit order as part of setup - to send it against 1 tick chart series (so kind of dirty workaround because I cannot close open limit position and place a new limit order on the same primary series bar)...

              Thanks
              Last edited by music_p13; 06-21-2020, 09:41 PM.

              Comment


                #8
                Hello music_p13,

                In general if you hit a managed approach ignore or warning the unmanaged approach is generally the correct way to approach the problem. The managed approach has not really changed from NT7 so the rules are all still relevant. That is in place to prevent unwanted positions or orders, however you can bypass those rules with unmanaged.

                In some cases you may be able to work around the problem by using logic but if you want to do specifically the action you are asking for it will likely need unmanaged.

                I look forward to being of further assistance.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Thanks Jesse,

                  I have looked at Unmanaged examples that were provided. Do you have any additional examples such as performing Reverse when in position that was filled through the Limit order, and then when performing Reverse it means exiting original position and entering a new Limit order with offset. Another example that would be very useful is on how to use multiple arrays or lists in case I have a group of orders (some filled, some pending) and want to perform reverse on it (essentially closing all open positions and opening a new one).
                  And additional question - assuming I am working on BarClose: if I exit position and submit limit all on that bar close - how should NT8 behave in these scenarios during backtest and during live account? In Backtest it should be able to easily close existing position and put a limit order. However, in live account - there should be some "Delays" - first NT8 should wait for position to be closed, and only then to submit the "Limit" order.. so should I submit that Limit order during "OnExecution" or "OnPosition (verifying that position got flat indeed before entering next limit position)" events?

                  Thanks

                  Comment


                    #10
                    Hello music_p13,

                    Aside from the items uploaded to the forum there are no reference samples of using unmanaged that I am aware of. This is considered an advanced concept so there is really not much documentation or samples that go into much detail here. You can use the existing managed approach samples to get a good idea of how to submit orders or manage orders but beyond that the unmanged approach really relies on your knowledge with both the managed approach along with general trading and the types of orders/how they work in the market.

                    And additional question - assuming I am working on BarClose: if I exit position and submit limit all on that bar close - how should NT8 behave in these scenarios during backtest and during live account? In Backtest it should be able to easily close existing position and put a limit order. However, in live account - there should be some "Delays" - first NT8 should wait for position to be closed, and only then to submit the "Limit" order.. so should I submit that Limit order during "OnExecution" or "OnPosition (verifying that position got flat indeed before entering next limit position)" events?
                    Submitting two orders like this at the same time is bad practice with the managed approach as that does not allow for position updates. If the limit order was intended to be used as an entry, you should do that after confirming the position was actually closed instead of trying to do that at the same time as the exit. OnExecution is generally used for tracking fills so that could be used to know when you exit the position based on the exit order fill. You could also use items like the Position object to make sure you are flat before submitting the limit order when using OnBarUpdate however you would still want to make sure that limit order is submitted at least 1 bar away from the exit for the position to update.




                    I look forward to being of further assistance.
                    JesseNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by andrewtrades, Today, 04:57 PM
                    1 response
                    5 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by chbruno, Today, 04:10 PM
                    0 responses
                    3 views
                    0 likes
                    Last Post chbruno
                    by chbruno
                     
                    Started by josh18955, 03-25-2023, 11:16 AM
                    6 responses
                    436 views
                    0 likes
                    Last Post Delerium  
                    Started by FAQtrader, Today, 03:35 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post FAQtrader  
                    Started by rocketman7, Today, 09:41 AM
                    5 responses
                    19 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Working...
                    X