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

Cancel Pending

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

    Cancel Pending

    I am using "OnOrderUpdate() and OnExecution() methods" , as in the exemple in strategies samples, to submit protective orders but, sometimes , when conection is lost and take profit order is executed , the stop loss order is not canceled as you can see in the picture. It remain as Cancel Pending.
    How can I solve this ?

    Thanks

    #2
    Hello wagner4691,

    Thank you for the post.

    If you are physically not connected when the order event happens you would not receive it so the OnOrderUpdate/OnExecution would not be called to invoke your scripts logic.

    Are you cancelling the order yourself or was this tied by OCO?

    Was this cancelled on the account but did not reflect that in the platform after reconnecting or was the order still also active on the account?


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

    Comment


      #3
      The order should be canceled by strategy "OnOrderUpdate() and OnExecution() methods" , as in the exemple,
      The order still also active on the account
      Last edited by wagner4691; 05-22-2020, 12:30 PM.

      Comment


        #4
        It is only canceled in the account and in the platform when I turn off NT and turn on

        Comment


          #5
          Hello wagner4691,

          Thank you for the additional details.

          Yes in this situation the event would not have been observed for your logic to have cancelled the order when it was filled in realtime due to the disconnect. If the disconnect happened slightly after the fill you may see that the fill was observed to invoke the cancel however if the platform was unable to reach the broker nothing would have actually happened on the account.

          After restarting the platform and re enabling the script it should be able to reprocess this time using historical data and try to manage the position/account at that point which it sounds like that is what happened. If the script does not have a connection it won't be able to manage the orders or do other logic that is based on events as it would be waiting of the connection at that point. To avoid this type of situation would really require avoiding the connection loss, a private server could be used for that purpose to host the platform on a more stable connection if that were a ongoing problem.



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

          Comment


            #6
            Is it possible to change something in strategy to cancel the order ?

            Could I use this in some part of the strategy ?

            if (execution.Order.OrderState == OrderState.CancelPending)

            stopshortp = null;



            This is how I handle the protective orders.
            ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

            Shortp
            if (entryshortp != null && entryshortp == execution.Order)
            {
            if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
            {
            sumFilled4 += execution.Quantity;

            if (execution.Order.OrderState == OrderState.PartFilled)
            {
            stopshortp = ExitShortStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + SLOS, "SLV2", @"ShortP");
            targetshort = ExitShortLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - STP, "TPV2", @"ShortP");
            }

            else if (execution.Order.OrderState == OrderState.Filled && sumFilled4 == execution.Order.Filled)
            {
            stopshortp = ExitShortStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + SLOS, "SLV2", @"ShortP");
            targetshort = ExitShortLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - STP, "TPV2", @"ShortP");
            }


            if (execution.Order.OrderState != OrderState.PartFilled && sumFilled4 == execution.Order.Filled)
            {
            entryshortp = null;
            sumFilled4 = 0;
            }
            }
            }


            if ((stopshortp != null && stopshortp == execution.Order) || (targetshort != null && targetshort == execution.Order))
            {
            if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
            {
            stopshortp = null;
            targetshort = null;
            }
            }

            Comment


              #7
              Hello wagner4691,

              If the strategy became disabled or if the connection is not present you won't be able to do anything besides manually manage your account at that point. You would need internet to have the platform perform actions based on order events.

              You can try to have the strategy recalculate in case of a reconnect, it sounds like disabling and re enabling the strategy was a potential solution here so recalculation may be an option. You can see the following page which details the connection loss settings, selecting to have the strategy recalculate could potentially work here if the start behavior allows for that. If the connection is physically unable to reconnect, exceeds the max restart attempts or the strategy becomes disabled you would still need to manually manage the account position at that point.


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

              Comment


                #8
                As you can see in the log below , the strategy was not disable . The conection was lost less than 1 minute.

                My question is, when I have a Cancel Pending, how can I cancel using some command in the strategy ?

                Can use something like this ?

                if (execution.Order.OrderState == OrderState.CancelPending)

                stopshortp = null;


                22/05/2020 13:23 Position Instrument='WIN 06-20' Account='52857' Average price=82410 Quantity=0 Market position=Flat Operation=Remove
                22/05/2020 13:23 Execution Execution='72212:13363895:1' Instrument='WIN 06-20' Account='52857' Exchange=Default Price=82745 Quantity=1 Market position=Short Operation=Operation_Add Order='727421650405' Time='5/22/2020 1:23 PM'
                22/05/2020 13:23 Order Order='727421650405/52857' Name='TPC2' New state='Filled' Instrument='WIN 06-20' Action='Sell' Limit price=82745 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='' Filled=1 Fill price=82745 Error='No error' Native error=''
                22/05/2020 13:13 Position Instrument='WIN 06-20' Account='52857' Average price=82410 Quantity=1 Market position=Long Operation=Update
                22/05/2020 13:13 Default Email - Message sent successfully
                22/05/2020 13:13 Default Email - Message sent successfully
                22/05/2020 13:13 Default Email - Message sent successfully
                22/05/2020 13:13 Default Email - Message sent successfully
                22/05/2020 13:13 Order Order='727421650405/52857' Name='TPC2' New state='Working' Instrument='WIN 06-20' Action='Sell' Limit price=82745 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
                22/05/2020 13:13 Order Order='727421650405/52857' Name='TPC2' New state='Accepted' Instrument='WIN 06-20' Action='Sell' Limit price=82745 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
                22/05/2020 13:13 Order Order='727417963408/52857' Name='SLV2' New state='Filled' Instrument='WIN 06-20' Action='Buy to cover' Limit price=82880 Stop price=0 Quantity=1 Type='Stop Market' Time in force=DAY Oco='' Filled=1 Fill price=82510 Error='No error' Native error=''
                22/05/2020 13:13 Execution Execution='72212:13024015:2' Instrument='WIN 06-20' Account='52857' Exchange=Default Price=82510 Quantity=1 Market position=Long Operation=Update Order='727417963408' Time='5/22/2020 12:10 PM'
                22/05/2020 13:13 Connection My modalmais: Primary connection=Connected, Price feed=Connected
                22/05/2020 13:13 Order Order='727417963650/52857' Name='TPV2' New state='Cancelled' Instrument='WIN 06-20' Action='Buy to cover' Limit price=81660 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
                22/05/2020 13:13 Execution Execution='72212:13267347:2' Instrument='WIN 06-20' Account='52857' Exchange=Default Price=82410 Quantity=1 Market position=Long Operation=Update Order='727421649900' Time='5/22/2020 1:00 PM'
                22/05/2020 13:13 Position Instrument='WIN 06-20' Account='52857' Average price=81995 Quantity=1 Market position=Long Operation=Update
                22/05/2020 13:13 Order Order='727421649900/52857' Name='LongP' New state='Filled' Instrument='WIN 06-20' Action='Buy' Limit price=83015 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=1 Fill price=82410 Error='No error' Native error=''
                22/05/2020 13:13 Execution Execution='72212:12563820:1' Instrument='WIN 06-20' Account='52857' Exchange=Default Price=81995 Quantity=1 Market position=Short Operation=Update Order='727417962269' Time='5/22/2020 11:00 AM'
                22/05/2020 13:13 Order Order='727417962269/52857' Name='ShortP' New state='Filled' Instrument='WIN 06-20' Action='Sell short' Limit price=81395 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=1 Fill price=81995 Error='No error' Native error=''
                22/05/2020 13:13 Order Order='727421650309/52857' Name='SLC2' New state='Accepted' Instrument='WIN 06-20' Action='Sell' Limit price=81530 Stop price=81900 Quantity=1 Type='Stop Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
                22/05/2020 13:13 Order Order='727421650309/52857' Name='SLC2' New state='Working' Instrument='WIN 06-20' Action='Sell' Limit price=81530 Stop price=81900 Quantity=1 Type='Stop Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
                22/05/2020 13:12 Default Email - Message sent successfully
                22/05/2020 13:12 Connection My modalmais: Primary connection=Connection lost, Price feed=Connection lost
                22/05/2020 13:00 Default Email - Message sent successfully
                22/05/2020 13:00 Order Order='727421650405/52857' Name='TPC2' New state='Working' Instrument='WIN 06-20' Action='Sell' Limit price=82745 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
                22/05/2020 13:00 Order Order='727421650405/52857' Name='TPC2' New state='Accepted' Instrument='WIN 06-20' Action='Sell' Limit price=82745 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
                22/05/2020 13:00 Order Order='727421650405/52857' Name='TPC2' New state='Submitted' Instrument='WIN 06-20' Action='Sell' Limit price=82745 Stop price=0 Quantity=1 Type='Limit' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
                22/05/2020 13:00 Order Order='727421650309/52857' Name='SLC2' New state='Working' Instrument='WIN 06-20' Action='Sell' Limit price=81530 Stop price=81900 Quantity=1 Type='Stop Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
                22/05/2020 13:00 Order Order='727421650309/52857' Name='SLC2' New state='Submitted' Instrument='WIN 06-20' Action='Sell' Limit price=0 Stop price=81900 Quantity=1 Type='Stop Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
                22/05/2020 13:00 Order Order='727421650309/52857' Name='SLC2' New state='Accepted' Instrument='WIN 06-20' Action='Sell' Limit price=0 Stop price=81900 Quantity=1 Type='Stop Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
                22/05/2020 13:00 Execution Execution='72212:13267347:2' Instrument='WIN 06-20' Account='52857' Exchange=Default Price=82410 Quantity=1 Market position=Long Operation=Operation_Add Order='727421649900' Time='5/22/2020 1:00 PM'
                22/05/2020 13:00 Position Instrument='WIN 06-20' Account='52857' Average price=82410 Quantity=1 Market position=Long Operation=Operation_Add
                22/05/2020 13:00 Order Order='727421649900/52857' Name='LongP' New state='Filled' Instrument='WIN 06-20' Action='Buy' Limit price=83015 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=1 Fill price=82410 Error='No error' Native error=''
                22/05/2020 13:00 NinjaScript NinjaScript strategy 'SCALPER/201402763' submitting order
                22/05/2020 13:00 NinjaScript NinjaScript strategy 'SCALPER/201402763' submitting order
                22/05/2020 13:00 Order Order='727421649900/52857' Name='LongP' New state='Working' Instrument='WIN 06-20' Action='Buy' Limit price=83015 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
                22/05/2020 13:00 Order Order='727421649900/52857' Name='LongP' New state='Accepted' Instrument='WIN 06-20' Action='Buy' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
                22/05/2020 13:00 Order Order='727421649900/52857' Name='LongP' New state='Submitted' Instrument='WIN 06-20' Action='Buy' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=DAY Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
                22/05/2020 13:00 NinjaScript NinjaScript strategy 'SCALPER/201402763' submitting order

                Comment


                  #9
                  I understand it will not be triggerd imediately but if I use it in OnBarupdate for exemple, the order will be cancel when I have a new bar?

                  Comment


                    #10
                    Hello wagner4691,

                    If you had a cancel pending order which did not resolve by its self when the connection was back there is nothing further your strategy could do at that point, you would need to disable the strategy and manually manage the account if necessary. At that point you would generally need to restart the platform or connection to clear the pending order and then re synchronize with that has happened on the account. If connection losses are common for your connection you could look into using a virtual private server to host the platform on a more stable connection instead.

                    Please let me know if I may be of further assistance.
                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      I understand but , You should have some solution for that. I am using a VPS but sometimes it is a broker problem and NT should be able to handle with that. Sometimes I am not in front of my computer to turn NT off and turn it on to cancel pending orders. Is it possible to solve on the future?

                      Comment


                        #12
                        Sorry for so many questions. Is it possible to check the conecttion before cancel the exit order?
                        Last edited by wagner4691; 05-23-2020, 05:59 AM.

                        Comment


                          #13
                          Hello wagner4691,

                          Really all I could suggest here is not leave the platform completely unsupervised to make sure your PC is working correctly and that you are connected. If that VPS has disconnection problems you may want to try a different VPS. You can also try syncing the PC clock, if the clock is at all out of sync that can cause connection issues.

                          You can use the OnConnectionStatusUpdate override to monitor the connection status from your script however if you get frequent disconnects that is likely something you will need to troubleshoot directly rather that trying to make the script/platform accommodate for that. https://ninjatrader.com/support/help...nnectionstatus

                          Please let me know if I may be of further assistance.

                          JesseNinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by algospoke, Yesterday, 06:40 PM
                          2 responses
                          19 views
                          0 likes
                          Last Post algospoke  
                          Started by ghoul, Today, 06:02 PM
                          3 responses
                          14 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by jeronymite, 04-12-2024, 04:26 PM
                          3 responses
                          45 views
                          0 likes
                          Last Post jeronymite  
                          Started by Barry Milan, Yesterday, 10:35 PM
                          7 responses
                          21 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by AttiM, 02-14-2024, 05:20 PM
                          10 responses
                          181 views
                          0 likes
                          Last Post jeronymite  
                          Working...
                          X