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

OnBarUpdate sample and null reset orders

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

    OnBarUpdate sample and null reset orders

    Hello

    This is my first post. I am learning a lot in this forum.

    I have created an strategy as OnBarUpdate sample. I just have copied with 4 different entries (Long1, Long2, Short1, and Short2):

    It runs basically ok but randomly order object are not set to null after execution, so strategy does not more entries in playback since that moment...

    Any idea how can I avoid that? Any other method or approach when using more than one entry?

    Thanks a lot

    #2
    Hello Rosario,

    Thank you for the post.

    To address what you are currently seeing you would very likely need to add some Prints into the script to further explore what specifically happens in that use case. If one of the conditions which manages the order object is not happening you could see it by using prints.

    An alternative would be to try looking at the following example which manages one order and targets. That would be good to contrast against to see if your script is missing something or potentially you have mixed up some of the order resetting logic/signal names etc. https://ninjatrader.com/support/help...and_onexec.htm

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

    Comment


      #3
      Hello Jesse

      The example you provide is the one I told in my post I have used.

      And I also have added prints, of course, and surprisingly, strategy randomly catches execution before order entry although time in prints is correct... In those cases, strategy does not more entries.

      1/11/2021 3:45:00 PM order S1 sent with Id : 0
      1/11/2021 3:45:06 PM cancel S1 no execution

      1/11/2021 3:46:00 PM order S1 sent with Id : 1
      1/11/2021 3:46:16 PM cancel S1 no execution

      1/11/2021 3:47:15 PM order S1 sent with Id : 2
      1/11/2021 3:47:29 PM START TRADE S1
      1/11/2021 3:47:29 PM execution S1 Id 2

      1/11/2021 3:49:15 PM order S1 sent with Id : 5
      1/11/2021 3:49:19 PM START TRADE S1
      1/11/2021 3:49:19 PM execution S1 Id 5

      1/11/2021 3:51:30 PM order S1 sent with Id : 8
      1/11/2021 3:51:31 PM cancel S1 no execution

      1/11/2021 3:52:45 PM order S1 sent with Id : 9
      1/11/2021 3:52:49 PM START TRADE S1
      1/11/2021 3:52:49 PM execution S1 Id 9

      1/11/2021 3:53:30 PM order S1 sent with Id : 12
      1/11/2021 3:53:33 PM START TRADE S1
      1/11/2021 3:53:33 PM execution S1 Id 12

      1/11/2021 3:54:45 PM order S1 sent with Id : 15
      1/11/2021 3:54:57 PM START TRADE S1
      1/11/2021 3:54:57 PM execution S1 Id 15

      1/11/2021 3:56:16 PM START TRADE S1
      1/11/2021 3:56:16 PM execution S1 Id 18
      1/11/2021 3:56:15 PM order S1 sent with Id : 18


      How can I manage this issue?

      Thanks

      Comment


        #4
        Hello Rosario,

        Thank you for the reply.

        Without knowing where you defined the prints or what was happening at that time I really couldn't provide much context here. This could relate to where you put the prints or it could relate to other changes you made to the sample. Do you see the same problem if you use the sample from the help guide without any modification other than adding prints? If you do then you could provide that sample with the print statements included so we could see where the prints are in the code to better understand the ordering.

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

        Comment


          #5
          Hello Jesse

          The code has mor than 2000 lines.

          protected override void OnBarUpdate()
          {
          if (Conditions && entryShortOrder == null)
          {
          entryShortOrder = EnterShortLimit(0, true, _lotSizeEntryOne, entryShortPrice, @"S1");
          Print(Time[1] + " order S1 sent with Id : " + entryShortOrder.Id);
          }

          OnOrderUpdate

          if (order.Name == "S1")
          {
          entryShortOrder = order;


          if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
          {
          entryShortOrder = null;
          sumShortFilled = 0;

          }
          }

          OnExecutionUpdate

          if (entryShortOrder != null && entryShortOrder == execution.Order)
          {
          if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
          {
          sumShortFilled += execution.Quantity;
          Print(Time[0] + execution.Time + " START TRADE S1 ");

          if (execution.Order.OrderState != OrderState.PartFilled && sumShortFilled == execution.Order.Filled)
          {
          entryShortOrder = null;
          sumShortFilled = 0;
          Print(Time[0] + " execution S1 Id " + entryShortOrder.Id);
          }
          }
          }

          Where do support suggest define other prints?
          Last edited by Rosario; 02-09-2021, 12:34 PM.

          Comment


            #6
            Hello Rosario,


            The code has more than 2000 lines.
            If the script is large its generally best to stop testing using that script and try a more simple test to isolate the issue. If you can't see the problem using the help guide sample without modification then you can use that as a comparison on what your code needs to look like to find problems.

            For the print being out of place that would relate to how you are resetting the variable and when OnBarUpdate is called. To further address that you would need to see when you reset the variable which allows the condition to become true again. From the excerpt here I can't see what that may be. You may also want to review the help guide notes, if you are using brokers like IB or Rithmic the events can be in different orders. https://ninjatrader.com/support/help...ub=onexecution


            I look forward to being of further assistance.

            JesseNinjaTrader Customer Service

            Comment


              #7
              Hello, Jesse

              I think I have found the problem. When there is a partial fill and the remaining is cancelled in OBU, in sample provided entryOrder does not get NULL.

              I have tried this in OBU, but obviously that is not the solution...

              protected override void OnBarUpdate()

              if (Conditions && entryOrder == null)
              {
              EnterLong(100, "MyEntry");
              ordL = true;
              }

              if (ordL == true
              && (Close[0] > cancelOrderPrice))
              {
              CancelOrder(entryOrder);
              entryOrder = null;
              ordL = false;
              }

              .What would be the best approach to solve this calling in OnBarUpdate or OnExecutionUpdate?

              Thanks
              Last edited by Rosario; 02-11-2021, 04:03 AM.

              Comment


                #8
                Hello Rosario,

                For any type of order change fill or other modification you would want to use the order events, either OnOrderUpdate or OnExecutionUpdate depending on what event you are looking for. OnBarUpdate is not associated with order events, its associated with bar data so that would not be a good location to look for order changes.

                OnOrderUpdate could be used or also OnExecututionUpdate depending on the event you are seeing happen. For cancels you can use a similar approach to what is shown in the sample you used, there is also a sample of cancel order and finding the cancel event here: https://ninjatrader.com/support/help...thod_to_ca.htm

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

                Comment


                  #9
                  Hello Jesse

                  I had already read sample provided and I have used it in several scripts where lot size entry is just one contract.

                  I am looking for advise from support team or any user to match ordinary samples ( OEU & Cancel & SampleMACrossOver) when lot size entry is bigger than one and partfilled execution are likely.

                  Any help will be very appreciated

                  Sincerely,

                  Comment


                    #10
                    Hello Rosario,

                    The sample for OnOrderUpdate that you linked does contain logic to handle part fills, that is anywhere the logic checks for OrderState.PartFilled. If you are seeing an issue when using that strategy I would need more specific details on what part you are having trouble with and what the specific situation was surrounding the fills.

                    For partial fills or using greater quantity you need to also adjust your logic for the different situations you encounter. You would do that by observing the various states of the orders or different events being passed into the overrides. Conditions checking things like OrderState.PartFilled can be used to delegate logic in those cases.

                    If your having a trouble with your full script then starting from a more simple place is always our suggestion. You could start by using the basic sample that we provide and use multiple quantity. Beyond that you would need to find an instrument where that partially fills to test it.



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

                    Comment


                      #11
                      Hello Jesse

                      Thanks for your quick response.

                      I suppose checking OrderState.PartFilled will be mandatory in order to pass entryOrder to null after cancel not-filled order. All I need to know is how and where to do, OOU? OEU?

                      For example, I have a long limit entry order (liveuntilcancel) Close[0] - 6 ticks, and this order will cancel if price goes to entryOrder + 12 ticks... I have a partial fill of 5 contracts, target and stop start, price goes up and cancel remaining...

                      How we pass entry Order to null for next trade?

                      I will test several combinations with OrderState.PartFilled in OOU and OEU and see results. If anyone has got a clue would be welcomed.

                      Sincerely,

                      Comment


                        #12
                        Hello Rosario,

                        Right, if you are dealing with orders that can part fill you will have to also account for that in your logic. You can generally follow what the OnOrderUpdate sample that you linked shows for handling part fills or how to check for them and which overrides are needed.

                        You would always have to use OnOrderUpdate or OnExecutionUpdate to manage your order variables. You should only set your variables to null at the appropriate times when the OnOrderUpdate or OnExecutionUpdate events that warrant that occur.

                        I look forward to being of further assistance.



                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Hello Jesse

                          I have followed your advise and try the simplest possible script. Print statements do not show me where the problem is, I attach strategy and output.
                          For any reason OnOrderUpdate and OnExecutionUpdate are called before OnBarUpdate, so entryLongOrder is not set to null after trade.

                          Date: 1/05/2021
                          Chart 15 seconds US Equities RTH trading hours
                          Trade 9:46 EST
                          Primary Instrument: YM 03-21
                          Secondary Instrument : ES 03-21

                          Enabling NinjaScript strategy 'LightCode V 1/224918013' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On each tick IsUnmanaged=False MaxRestarts=4 in 5 minutes
                          OOU 1/5/2021 3:46:31 PM
                          Submitted
                          OOU 1/5/2021 3:46:31 PM
                          Accepted
                          OOU 1/5/2021 3:46:31 PM
                          Working
                          ================================================== ========
                          OBU || LightCode V 1 || SENT L1 30220 || contracts: 1
                          OOU 1/5/2021 3:46:35 PM
                          Filled
                          OEU1/5/2021 3:46:35 PM START TRADE L1
                          OEU1/5/2021 3:46:35 PM execution.Order.OrderState == OrderState.Filled && sumLongFilled == execution.Order.Filled || 1
                          OEU1/5/2021 3:46:35 PM execution.Order.OrderState != OrderState.PartFilled && sumLongFilled == execution.Order.Filled || 1
                          entryLongOrder set to NULL
                          1/5/2021 3:47:46 PM END TRADE L1
                          ================================================== ========
                          OOU 1/5/2021 3:48:01 PM
                          Submitted
                          OOU 1/5/2021 3:48:01 PM
                          Accepted
                          OOU 1/5/2021 3:48:01 PM
                          Working
                          ================================================== ========
                          OBU || LightCode V 1 || SENT L1 30199 || contracts: 1
                          1/5/2021 3:48:15 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 3:48:30 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 3:48:45 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 3:49:00 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 3:49:15 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 3:49:30 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 3:49:45 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 3:50:00 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 3:50:15 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 3:50:30 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 3:50:45 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 3:51:00 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 3:51:15 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 3:51:30 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 3:51:45 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 3:52:00 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 3:52:15 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 3:52:30 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 3:52:45 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 3:53:00 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 3:53:15 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 3:53:30 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 3:53:45 PM || LightCode V 1 || - entryLongOrder != null
                          OOU 1/5/2021 3:53:53 PM
                          Filled
                          OEU1/5/2021 3:53:53 PM START TRADE L1
                          OEU1/5/2021 3:53:53 PM execution.Order.OrderState == OrderState.Filled && sumLongFilled == execution.Order.Filled || 1
                          OEU1/5/2021 3:53:53 PM execution.Order.OrderState != OrderState.PartFilled && sumLongFilled == execution.Order.Filled || 1
                          entryLongOrder set to NULL
                          1/5/2021 4:00:01 PM END TRADE L1
                          ================================================== ========
                          OOU 1/5/2021 4:05:46 PM
                          Submitted
                          OOU 1/5/2021 4:05:46 PM
                          Accepted
                          OOU 1/5/2021 4:05:46 PM
                          Working
                          OOU 1/5/2021 4:05:46 PM
                          Filled
                          OEU1/5/2021 4:05:46 PM START TRADE L1
                          OEU1/5/2021 4:05:46 PM execution.Order.OrderState == OrderState.Filled && sumLongFilled == execution.Order.Filled || 1
                          OEU1/5/2021 4:05:46 PM execution.Order.OrderState != OrderState.PartFilled && sumLongFilled == execution.Order.Filled || 1
                          entryLongOrder set to NULL
                          ================================================== ========
                          OBU || LightCode V 1 || SENT L1 30225 || contracts: 1
                          1/5/2021 4:06:00 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 4:06:11 PM END TRADE L1
                          ================================================== ========
                          1/5/2021 4:06:15 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 4:06:30 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 4:06:45 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 4:07:00 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 4:07:15 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 4:07:30 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 4:07:45 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 4:08:00 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 4:08:15 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 4:08:30 PM || LightCode V 1 || - entryLongOrder != null
                          1/5/2021 4:08:45 PM || LightCode V 1 || - entryLongOrder != null


                          Please, have a quick look and tell me what I am doing wrong.

                          Thanks in advance
                          Attached Files
                          Last edited by Rosario; 02-16-2021, 12:46 AM. Reason: I did not attach strategy

                          Comment


                            #14
                            Hello Rosario,


                            For any reason OnOrderUpdate and OnExecutionUpdate are called before OnBarUpdate, so entryLongOrder is not set to null after trade.
                            OnBarUpdate is not associated with your orders. It will be called at a specific frequency based on the series you used, that could come while you are still in a trade or any time around the trades events. Managing the order object would need to be done by using the order events like OnOrderUpdate or OnExecutionUpdate depending on the event you are looking for. OnBarUpdate can be used to check the variables you manage from the other events.

                            From your highlighted prints it appears that part worked as expected, the trade filled and the order object was set to null. After that happened the OBU logic was able to enter again:

                            OEU1/5/2021 4:05:46 PM START TRADE L1
                            OEU1/5/2021 4:05:46 PM execution.Order.OrderState == OrderState.Filled && sumLongFilled == execution.Order.Filled || 1
                            OEU1/5/2021 4:05:46 PM execution.Order.OrderState != OrderState.PartFilled && sumLongFilled == execution.Order.Filled || 1
                            entryLongOrder set to NULL

                            ================================================== ========
                            OBU || LightCode V 1 || SENT L1 30225 || contracts: 1


                            From what you selected in the prints here I am not certain what you are saying is the problem. Can you further explain what you are expecting to occur here?

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

                            Comment


                              #15
                              Hello Jesse

                              Thanks for your time and sorry for my english... I will try to explain better...

                              As I told you in post #3, strategy randomly catches execution before order entry. So, sometimes execution is called before OBU send the order, as you can see in prints attached. Strategy runs OnEachTick and print OBU is Time[1], so that line should be before OnOrderUpdate and OnExecutionUpdate but I can not explain why sometimes that does not happen...

                              Correct prints should be

                              OBU || LightCode V 1 || SENT L1 30225 || contracts: 1
                              1/5/2021 4:06:00 PM || LightCode V 1 || - entryLongOrder != null
                              OOU 1/5/2021 4:05:46 PM
                              Submitted
                              OOU 1/5/2021 4:05:46 PM
                              Accepted
                              OOU 1/5/2021 4:05:46 PM
                              Working
                              OOU 1/5/2021 4:05:46 PM
                              Filled
                              OEU1/5/2021 4:05:46 PM START TRADE L1
                              OEU1/5/2021 4:05:46 PM execution.Order.OrderState == OrderState.Filled && sumLongFilled == execution.Order.Filled || 1
                              OEU1/5/2021 4:05:46 PM execution.Order.OrderState != OrderState.PartFilled && sumLongFilled == execution.Order.Filled || 1
                              entryLongOrder set to NULL

                              Taking a look at simplified strategy file attached, can you tell me what I am doing wrong?

                              Thanks
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by inanazsocial, Today, 01:15 AM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_Jason  
                              Started by rocketman7, Today, 02:12 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post rocketman7  
                              Started by dustydbayer, Today, 01:59 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post dustydbayer  
                              Started by trilliantrader, 04-18-2024, 08:16 AM
                              5 responses
                              23 views
                              0 likes
                              Last Post trilliantrader  
                              Started by Davidtowleii, Today, 12:15 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post Davidtowleii  
                              Working...
                              X