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

If limit order gets rejected change to place market order

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

    If limit order gets rejected change to place market order

    If limit order gets rejected change to place market order

    Hello, I was wondering if it was possible to change a limit order to market order if it gets rejected. My strategy occasionally gets the limit order rejected due to fast moving market conditions and was wondering if this type of order error handling was possible.

    Thanks!
    Regards​

    #2
    Hello mintos,

    You can detect the limit order.OrderState being OrderState.Rejected in OnOrderUpdate().


    And send a market order with EnterLong() or EnterShort().


    You will need to have the RealtimeErrorHandling set to StopCancelCloseIgnoreRejects.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello, I tried using the order.OrderState being OrderState.Rejected in OnOrderUpdate() to detect when it was rejected and submit a market order. I have confirmed that the code does get executed, however the market order never gets submitted. It still gives the same error.
      Code:
      protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
      {
          // Rejection handling
          if (error == ErrorCode.OrderRejected)
          {
              if (order.Name == "Stop Market Long")
              {
                  // Stop market order was rejected, attempt submit market order
                  EnterLong(1, "Market Long");
              }
          }        
      }​
      Click image for larger version

Name:	sn2.png
Views:	41
Size:	7.8 KB
ID:	1302080 Click image for larger version

Name:	sn.png
Views:	33
Size:	112.7 KB
ID:	1302081

      Comment


        #4
        Hello mintos,

        The error is not for the market order. The error in your screenshot is for the stop order only.

        Print the order.ToString() object at the top of OnOrderUpdate().

        Re-run the strategy and provide the output from the output window saved to a text file attached to your next post.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Code:
          orderId='3ebae0be9af1434893c1cfd05819d85a' account='Playback101' name='Stop Market Long' orderState=Submitted instrument='NQ 06-24' orderAction=Buy orderType='Stop Market' limitPrice=0 stopPrice=17612 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=7 time='2024-05-02 13:20:01' gtd='2099-12-01' statementDate='2024-05-02'
          orderId='3ebae0be9af1434893c1cfd05819d85a' account='Playback101' name='Stop Market Long' orderState=Rejected instrument='NQ 06-24' orderAction=Buy orderType='Stop Market' limitPrice=0 stopPrice=17612 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=7 time='2024-05-02 13:20:01' gtd='2099-12-01' statementDate='2024-05-02'​
          This was what was printed out. Like I mentioned. The Market buy isn't getting executed despite the code running through the line.

          If you check the attachment in the post above, during the Visual Studio debug, the line does get executed but ninjatrader does not execute the market buy.
          Attached Files
          Last edited by mintos; 05-03-2024, 12:55 PM.

          Comment


            #6
            Hello mintos,

            Typically we would compare the OrderState. I wonder if comparing the ErrorCode might be an issue.

            if (order.Name == "Stop Market Long" && order.OrderState == OrderState.Rejected)

            Does this change anything?

            Please also enable TraceOrders in State.Configure so we can look to see if the entry order is being ignored.

            Also, please add a print to the condition so that we know the code is being reached.

            Code:
            Print(order.ToString());
            
            if (order.Name == "Stop Market Long" && order.OrderState == OrderState.Rejected)
            {
            Print("Stop Market Long rejected, submitting Market Long");
            EnterLong(1, "Market Long");
            }
            Please provide the updated output text file.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi, those are all enabled and I have already added the Print statement which is what you see in the text document attached above. I edited the statement to match what you wrote and this is the new text document. As mentioned previously, the market buy is not executing.
              Code:
              orderId='7678191568524376b18d6c0eee5f1073' account='Playback101' name='Stop Market Long' orderState=Submitted instrument='NQ 06-24' orderAction=Buy orderType='Stop Market' limitPrice=0 stopPrice=17612 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=10 time='2024-05-02 13:20:01' gtd='2099-12-01' statementDate='2024-05-02'
              orderId='7678191568524376b18d6c0eee5f1073' account='Playback101' name='Stop Market Long' orderState=Rejected instrument='NQ 06-24' orderAction=Buy orderType='Stop Market' limitPrice=0 stopPrice=17612 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=10 time='2024-05-02 13:20:01' gtd='2099-12-01' statementDate='2024-05-02'
              Stop Market Long rejected, submitting Market Long
              ​
              Attached Files

              Comment


                #8
                Hello mintos,

                There is no TraceOrders information.

                May I confirm you have set TraceOrders = true; in State.Configure as directed?

                Have you compiled and re-run the script after changing this?

                Did you change to comparing the OrderState as mentioned in my previous post?

                May I have an export to test this on my end?
                Tools -> Export -> NinjaScript Add-on
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Click image for larger version

Name:	sn.png
Views:	16
Size:	25.7 KB
ID:	1302267
                  Hello,
                  Yes, the TraceOrders is set to true and yes it has been recompiled. I changed the orderstate script to match yours as directed. I have created a test script for you to use.

                  Comment


                    #10
                    Hello mintos,

                    It looks like you may have provided the wrong script.

                    This script is setting TraceOrders to true in State.SetDefaults on line 48.

                    May I confirm you have set TraceOrders to true in State.Configure as I have directed in posts # 6 and # 8?
                    May I have an export of that script, where this is set in State.Configure and not State.SetDefaults?


                    Also, I see that you are not using RealtimeErrorHandling.StopCancelCloseIgnoreRejects or RealtimeErrorHandling.IgnoreAllErrors.
                    This means if the strategy has a rejected order the strategy will be disabled and not continue running. (This will be shown in the TraceOrders output)
                    If you want to detect an order has been rejected, you must allow the strategy to continue running after a rejected order.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello, I have updated the script to have TraceOrders in State.Configure as well and attached to this post.
                      Attached Files

                      Comment


                        #12
                        Hello mintos,

                        The script you have provided in this latest post # 11 still does not set RealtimeErrorHandling to StopCancelCloseIgnoreRejects.
                        The strategy will be disabled if an order is rejected and a disable strategy is not able to submit further orders.
                        Set this in State.Configure. (If you set this in State.SetDefaults you will to remove the instance of the stratey and add a new instance to pull the new defaults)

                        I've tested the strategy you have provided on the ES 06-24 60 minute chart in playback and I am seeing a lot of information the output window.

                        I'm seeing messages for the strategy placing orders getting ignored in historical.

                        Strategy 'test/326217058': An order placed at '04/21/2024 15:30:00' has been ignored since the order was submitted before the strategy property BarsRequiredToTrade had been met.

                        I'm seeing output that an order was filled.

                        4/22/2024 1:30:00 AM Strategy 'test/326217058': Entered internal SubmitOrderManaged() method at 4/22/2024 1:30:00 AM: BarsInProgress=0 Action=Buy OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=5021.25 SignalName='Stop Market Long' FromEntrySignal=''
                        orderId='NT-00000-53' account='Playback101' name='Stop Market Long' orderState=Submitted instrument='ES 06-24' orderAction=Buy orderType='Stop Market' limitPrice=0 stopPrice=5021.25 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2024-04-22 01:30:00' gtd='2099-12-01' statementDate='2024-05-03'
                        orderId='NT-00000-53' account='Playback101' name='Stop Market Long' orderState=Accepted instrument='ES 06-24' orderAction=Buy orderType='Stop Market' limitPrice=0 stopPrice=5021.25 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2024-04-22 01:30:00' gtd='2099-12-01' statementDate='2024-05-03'
                        orderId='NT-00000-53' account='Playback101' name='Stop Market Long' orderState=Working instrument='ES 06-24' orderAction=Buy orderType='Stop Market' limitPrice=0 stopPrice=5021.25 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2024-04-22 01:30:00' gtd='2099-12-01' statementDate='2024-05-03'
                        orderId='NT-00000-53' account='Playback101' name='Stop Market Long' orderState=Filled instrument='ES 06-24' orderAction=Buy orderType='Stop Market' limitPrice=0 stopPrice=5021.25 quantity=1 tif=Gtc oco='' filled=1 averageFillPrice=5021.25 onBehalfOf='' id=-1 time='2024-04-22 01:30:00' gtd='2099-12-01' statementDate='2024-05-03'​

                        I'm seeing lots of orders being ignored due to the entries per direction being met.

                        4/22/2024 2:00:00 AM Strategy 'test/326217058': Ignored SubmitOrderManaged() method at 4/22/2024 2:00:00 AM: BarsInProgress=0 Action=Buy OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=5022.00 SignalName='Stop Market Long' FromEntrySignal='' Reason='Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties'
                        4/22/2024 2:30:00 AM Strategy 'test/326217058': Entered internal SubmitOrderManaged() method at 4/22/2024 2:30:00 AM: BarsInProgress=0 Action=Buy OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=5028.75 SignalName='Stop Market Long' FromEntrySignal=''​

                        Attached is the output text file generated from this strategy on my end.
                        NinjaScript Output 5_3_2024 1_42 PM.txt

                        To confirm, are you not seeing any of these TraceOrders messages in the output window after setting TraceOrders to true in State.Configure and re-running the script?
                        Attached Files
                        Chelsea B.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by kevinenergy, 02-17-2023, 12:42 PM
                        123 responses
                        2,882 views
                        1 like
                        Last Post SilverSurfer1  
                        Started by warpinator, Yesterday, 10:44 AM
                        2 responses
                        17 views
                        0 likes
                        Last Post warpinator  
                        Started by rayyyu12, Today, 03:59 PM
                        0 responses
                        9 views
                        0 likes
                        Last Post rayyyu12  
                        Started by calmcosmia, Today, 03:07 PM
                        4 responses
                        14 views
                        0 likes
                        Last Post calmcosmia  
                        Started by Tim-c, Today, 02:34 PM
                        1 response
                        10 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Working...
                        X