Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Start Behavior

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

    Start Behavior

    Hi,

    I am struggling trying to understand the various Start Behavior options. I have searched, read and re-read the documentation and various posts here concerning this subject and I am still confused about the startup behavior options. I would appreciate some clarifications:

    1. I am using a fully unmanaged approach where my strategy handles all orders, positions, etc. Some of the options seem to be related to managed approaches. Am I correct about this, and if so, which options are not applicable to unmanaged approaches?

    2. Many of these options seem to attempt to cancel / match / somehow handle the situation and may sometimes result in live orders being submitted to the market. I cannot imagine any scenario in which I would want live orders submitted based on historical decisions that in real-time may leave me in an unfavorable position, or that may have been submitted in the historical context at prices that are no longer even close to current reality. What would be the purpose of such actions?

    3. In my mind, the only viable action that any of these options should take is to modify the internal strategy order and position state to match the real world orders and position - or partial variants thereof. This is especially true if one's live account has existing working orders and position.

    4. If I select 'Wait Until Flat' I expect it to do nothing until my real account position is flat, and I have verified this through testing. I currently have my strategy set to IsAdoptAccountPositionAware = true with 'Adopt Account Position' selected as the Startup Behavior. Now, what behavior should I expect from NT? In my testing I do see NT attempt to close out existing positions and cancel unfilled orders that were previously placed by my strategy in historical context. Nothing is submitted to the live account. Does this sound correct because the documentation leads you to believe that your strategy must be programmed to be "Adopt Account Position Aware' (IsAdoptAccountPositionAware = true), implying that it would have to handle outstanding orders and positions itself. From my testing, it seems my strategy does not need to be aware of anything - NT does all the work, even with an unmanaged approach!

    Thanks for your patience with this one. I know I have asked a lot questions.

    Charlie





    #2
    Hello ChazJ,

    I have a forum post that discusses start behavior you may find helpful.


    1. All of the start behaviors can apply to an unmanaged strategy.

    2. Any unmatched orders will automatically be cancelled. For wait until flat, any previous placed orders that are still working will be cancelled.
    For an order to be resumed, the Immediately Submit option must be used and the order must match, which generally requires intra-bar granularity.

    3. If you would like to change the strategy position to match the account position, this would be the IsAdoptAccountPositionAware property and Adopt account position start behavior.


    4. With IsAdoptAccountPositionAware and Adopt account position, with a new instance of the script, the strategy position when enabled will be changed to match the account position.
    The strategy needs to be aware if there is an account position and be able to place protective orders for the existing position.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks Chelsea. I've been through all that material before and it still is not clear. I guess I will need to perform some experiments to unlock my brain .

      On additional question - when are these behaviors executed? Is it really on startup (before the first bar) as the name would imply, or when a strategy transitions to real-time, or both?

      Charlie

      Comment


        #4
        Hello Charlie,

        The behavior occurs during transition to real-time after historical data is processed, as this historical position is used for the actions of the start behavior.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          OK, but I what happens to orders and positions held at the very start of the strategy?

          For example, my strategy was in a real-time state processing data from a live broker connection on account SIM101. I had a short position of six contracts and a working stop-loss order of six contracts when I disabled my strategy. I set a breakpoint on the very first line on OnBarUpdate() and re-enabled my strategy. From the Immediate Window I can see the positions in the strategy vs. the positions in the account:

          ? Positions
          {NinjaTrader.Cbi.Position[2]}
          [0]: {instrument='MES 06-20' account='Sim101' avgPrice=0 quantity=0 marketPosition=Flat statementDate='2020-04-28'}
          [1]: {instrument='MES 06-20' account='Sim101' avgPrice=0 quantity=0 marketPosition=Flat statementDate='2020-04-28'}
          ? PositionsAccount
          {NinjaTrader.Cbi.Position[2]}
          [0]: {instrument='MES 06-20' account='Sim101' avgPrice=2895.20833333333 quantity=6 marketPosition=Short statementDate='2020-04-28'}
          [1]: {instrument='MES 06-20' account='Sim101' avgPrice=2895.20833333333 quantity=6 marketPosition=Short statementDate='2020-04-28'}

          This makes sense.

          (Side question - why are there two identical entries? There is only a single copy of my strategy running on a single chart)

          The same holds for 'this.Orders' vs. 'Account.Orders'. These orders and positions exist when the strategy first begins executing. Are they just ignored? Are new orders matched up with previous orders as historical processing proceeds on the way to real-time? What if the new historical run does not produce the same orders and position as the previous run due to changing parameters or start date? Are any previous entries deleted or over-written?

          Charlie

          Comment


            #6
            Hello ChazJ,

            This depends on the start behavior selected and if intra-bar granularity is used and if the orders are matched.

            Are you specifically asking about adopt account position behavior?

            However, if you are printing on the first bar, I would expect the strategy position and account position to be different as the strategy has not yet had time to calculate a historical position.

            Regarding the prints, I would need to see the code in the script.
            Last edited by NinjaTrader_ChelseaB; 04-29-2020, 09:04 AM.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              The prints in my last post were from the immediate window in Visual Studio after I set the breakpoint. And you are correct, as you can see, the positions are different between the strategy and account, as expected. The AdoptAccountPosition is working as I would expect. When the strategy hits real-time, it adopts the account's position. But no so with orders.

              My question, then, surrounds the orders. Does NT try to match the historical orders to the orders in the account? And as for historical orders, there are two sets, the order list from the previous historical run, and the order list being created from the current historical run when the strategy is re-enabled. In all, there are three lists of orders. How does NT handle this?

              And how does intra-bar granularity enter into this? Maybe that's why I am losing the stop-loss order. I am using a 5-minute chart and Calculate on Close.

              Thanks,

              Charlie

              Comment


                #8
                Hello Charlie,

                Yes, NinjaTrader will attempt to match previously placed orders. This requires the order to match the submission time, name, and price.

                Intra-bar granularity is used for increased accuracy to ensure the orders are placed at the same price and time so these are matched.



                NinjaTrader cancels any orders that are not matched.

                Any orders placed by you would be placed.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  So, does it attempt to match the new orders to the existing strategy orders from the previous run, or to the Account orders?

                  You see, at the moment I disable my strategy, I have a working order in the live account (a stop-loss order). When I re-enable the strategy, my position is re-established within my strategy to match the live account, but the stop-loss order is lost. What do I need to do to recover and manage that order? I am tempted to scan the Account.Orders list to find it and then submit a matching order through the strategy, but I fear that will cause a duplicate order into the live account. Will NT check for the match and only update the strategy orders list and not submit the order to the market?

                  Charlie

                  Comment


                    #10
                    Hello Charlie,

                    Yes, by matching orders this does mean orders placed by the strategy in real-time on a previous run. (This would not be to the account orders)

                    With the order that is not being matched, are you seeing the order historically being placed at the same price, time, and with the same name internally in the script?
                    Are you seeing this order getting cancelled internally in the script?
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Chelsea,

                      Yes, it is cancelled by NT.

                      Here is a step-by-step description of what I am seeing (with Adopt Account Position set):

                      1. Historical processing completes and at the time it enters real-time I have a position open of two contracts and a stop-loss order for two contracts. NT cancels the stop-loss order and closes the strategy position for two contracts. It uses an order called 'Close position'.

                      2. I let the strategy run for a bit and it enters a new position for one contract and my strategy places a stop-loss order for one contract.

                      3. I disable the strategy. I have chart trader enabled and the chart still shows my position and stop-loss order.

                      4. I re-enable the strategy. When historical processing completes, I am back to having a position of two contracts and associated stop-loss orders active and NT cancels them again. At this instant in time I am at the condition I was in #1 above, EXCEPT the TIME is different. NT adopts my account position of one contract, but I am left with no stop-loss in place. The stop-loss order that was generated in #1 above, did not happen because the two contract position was still in effect at that point in time. If I let it run in real-time again it will eventually find an entry condition at some future time. But that does not help my current live position. In fact it could cause more confusion, leaving me with a position of 2 and a stop loss for one contract. Or if the signal is in the opposite direction, leave me with no position and a stop loss order in place.

                      5. I decided to insert Print statements on every BAR update to output the state of these two lists: this.Orders and Account.Orders.

                      6. This time after disabling and re-enabling my startegy, the first Print output shows the order state as it was at the time the strategy was disabled:

                      Bar# 1: Strategy Order - Name: STO, State=Filled, Quantity=1
                      Bar# 1: Strategy Order - Name: BTCDSL, State=Accepted, Quantity=1
                      Bar# 1: Account Order - Name: STO, State=Filled, Quantity=1
                      Bar# 1: Account Order - Name: BTCDSL, State=Accepted, Quantity=1

                      7. While NT processes the historical data again, the above output never changes, even as orders are placed, cancelled, and position changes - until transition occurs. It is interesting to note that the list 'this.Orders' does not contain any orders associated with the current historical run. That was a revelation for me, and is certainly good to know.

                      8. Once the strategy transitions to Realtime, my Print statements show:

                      Bar# 6034: Strategy Order - Name: STO, State=Filled, Quantity=1
                      Bar# 6034: Strategy Order - Name: BTCDSL, State=Cancelled, Quantity=1
                      Bar# 6034: Account Order - Name: STO, State=Filled, Quantity=1
                      Bar# 6034: Account Order - Name: BTCDSL, State=Cancelled, Quantity=1

                      So it looks like NT cancelled the order that had existed when the strategy was disabled and was re-enabled, rather than place it with the live account.

                      Charlie


                      Comment


                        #12
                        Hello ChazJ,

                        If an order does not have the same price, time, and with the same name, it will not be matched and will be cancelled.

                        You have stated the order has a different time. That order would be cancelled.

                        Disabling a strategy does not cause the position to be flattened.

                        You can choose, however, if orders get automatically cancelled when disabled with the 'Cancel entry orders when a strategy is disabled' and 'Cancel exit orders when a strategy is disabled' .
                        https://ninjatrader.com/support/help...riptProperties


                        Are you printing the time of the bar, values used in conditions, and order object in OnOrderUpdate() along with TraceOrders to know that order is matching from the previous real-time run and the next run on historical data?

                        Below is a link to a forum post that demonstrates using prints to understand behavior.
                        https://ninjatrader.com/support/foru...121#post791121

                        Do these prints have labels?

                        Can we see the saved output file?

                        Are you mentioning the orders are being matched but then are being cancelled after the strategy begins processing real-time data?
                        Last edited by NinjaTrader_ChelseaB; 05-05-2020, 07:56 AM.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          OK, then, the difference in time is what is causing this. I can see that, so it is working as designed. But why is it designed that way? Why does NT mess with my live account? If it is adopting my live account position shouldn't it also adopt the orders currently active in that account?

                          Charlie

                          Comment


                            #14
                            Hello Charlie,

                            NinjaTrader can't match a live order to a historical one unless it understands it is being submitted at the same time and is the same order. Otherwise it could be any random order. This is why intra-bar granularity is so important when trying to resume a strategy.

                            No, adopting a position does not mean automatically adopting orders. The strategy has to be aware that a position is about to be adopted and must use code to handle the orders. If the orders match from a previous run and are still working, they will be resumed and will not be cancelled.
                            If a position is being adopted, the strategy needs to be aware if a previous order was resumed and know whether these orders properly protect the account position and know if additional orders need to be submitted or if orders need to be manually cancelled.

                            Using IsAdoptAccountPositionAware does require advanced programming logic to use (which is why its an extra flag preventing the Start Behavior from being used until the script is unlocked and the advanced programmed logic added).
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Thanks, Chelsea.

                              It seems to me that this is a function that NT should perform automatically. I suspect there may be a valid reason it doesn't. I would be interested to know how this decision was arrived at by your design team.

                              Now my question is - how should this be handled? I have a boolean setting that causes the strategy to ignore all historical bars. Once the State reaches Real-Time, it begins processing with the strategy in a clean environment - no positions and no orders. At this point, I would assume that I need to submit a stop-loss order that exactly matches the order residing in the Account, correct? This will prevent NT from submitting it to the market? Or is there another way to sync up the strategy with the Account?

                              Charlie

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              116 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              61 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              40 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              44 views
                              0 likes
                              Last Post TheRealMorford  
                              Started by Mindset, 02-28-2026, 06:16 AM
                              0 responses
                              82 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Working...
                              X