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

Handle Position and Orders after Strategy Restart

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

    Handle Position and Orders after Strategy Restart

    Hello

    After my strategy entered a position with a stop loss using the EnterLong and SetStopLoss methods it was disabled and then enabled again (while the position is still opened).
    What I want is to be able to manage my positionr with my strategy after it was restarted. For example:

    a. Know the Position.MarketPosition value
    b. Catch OnExecution event
    c. Catch OnOrderUpdate event

    How can I accomplish that?

    Thanks

    #2
    Hello kiss987,

    Thanks for opening the thread.

    Being able to resume the position created by your strategy would require changing the Start Behavior under Tools > Options > Strategies > NinjaScript.

    Immediately Submit will try to match any active orders that were made by the strategy, while Wait Until Flat will cancel any active orders that were previously made.

    I would suggest to review the documentation on syncing account positions for complete information on how these start behaviors work exactly. It's important to understand how these behaviors work when you decide to go live with your strategy. After reviewing the documentation, I would suggest to test each behavior by disabling and re-enabling a simple strategy that can enter a position quickly. The reading can be a little dense, so testing the behaviors can provide a good visual.

    The SampleOnOrderUpdate strategy is a good example that can be used to test start behaviors quickly. You could to set the BarsRequired to 0 and apply it to a 5 second data series on the simulated data feed to get some quick entries. You can then disable and enable the strategy from the Control Center to trigger the start behavior as well as the cancel-on-disable behaviors.

    The SampleOnOrderUpdate Strategy also demonstrates using OnExecution() and OnOrderUpdate() as well as Position.MarketPosition. It should be noted that these methods do not dictate the Start Behavior.

    Syncing Account Positions is documented here: https://ninjatrader.com/support/help..._positions.htm

    Additional information on Strategy vs. Account Position can be found here: https://ninjatrader.com/support/help..._account_p.htm

    SampleOnOrderUpdate strategy - http://ninjatrader.com/support/forum...ead.php?t=7499

    Here is a video I made demonstrating the Immediately Submit behavior with the SampleOnOrderUpdate strategy in NinjaTrader 8: https://www.screencast.com/t/FUCm2zymF8Cd

    Please let me know if you have any questions.
    JimNinjaTrader Customer Service

    Comment


      #3
      Thank you for your reply.

      When my strategy is set to "Immediately submit live working historical orders", can you please explain the difference between Sync account position = false and Sync account position = true? I'm not sure I understood it. Perhaps a short scenario explanation will help.

      Comment


        #4
        Hello kiss987,

        Thanks for the additional question.

        Here is the demonstration video as requested: https://www.screencast.com/t/UK26nmo5o

        The difference between Immediately Submit and Immediately Submit with Sync Account Position enabled follows the rules below:

        After the strategy is successful in cancelling and submitting any orders that required action it will check your current Account Position and compare it to your Strategy Position. On multi-instrument strategies it will perform this check for all instruments used by the strategy.

        • If the Account Position matches your Strategy Position, no reconciliatory order will be submitted. The strategy will then begin managing your Strategy Position immediately.
        • If the Account Position does not match your Strategy Position, NinjaTrader will submit a market order(s) to reconcile the Account Position to match your Strategy Position. The strategy will then begin managing your Strategy Position immediately.
        In simplest terms, this would mean if the Strategy's Position is not the same as the Account Position, the Strategy will submit an order to get the Account in sync with it. It is very important to understand that this is an additional live order that synchronizes the real account position to the position of the Strategy.

        Strategies are programmed to take profit based off of certain conditions and having a strategy that is out of sync with the account could cause trades that would be profitable to be unprofitable.
        JimNinjaTrader Customer Service

        Comment


          #5
          Hello and thank you for your reply.

          1. It is unclear to me what was the "Sync account position" on the video.

          2. More importantly, I would like to explain what I am trying to accomplish and why I fail to accomplish it.

          What I want is that if my strategy is disabled while having a position opened and then re-enabled it will continue exactly where it left. That is:
          a. the position will remain opened
          b. the Position.MarketPosition value will be automatically synced to the opened position


          For that purposes I have written a short strategy that all it does is enter a long position in case the Position.MarketPosition is set to flat. Otherwise it does nothing,

          a. The code is below.
          b. I set my parameters to "Immediately submit live working historical orders, Sync account position = true".

          The scenario I am trying to accomplish is that:

          1. Running the strategy while market is flat - the strategy will enter a long position
          2. Disabling the strategy - long position is remained open
          3. Restart the strategy - A. No further executions will be submitted since we are already in a long position. B. Position.MarketPosition will be automatically synced to Long.

          What happened to me is that steps 1-2 were OK but after I restarted my strategy on step 3, NT7 CLOSED the long position (instead of leaving it open) and after that since the Position.MarketPosition was set to flat, my strategy entered a long position.





          protected override void OnStartUp()
          {
          Print("startup " + Position.MarketPosition);
          }

          protected override void Initialize()
          {
          CalculateOnBarClose = false;
          }

          protected override void OnBarUpdate()
          {
          if (Position.MarketPosition == MarketPosition.Flat)
          {
          Print("enter long " + Position.MarketPosition);
          EnterLong();
          }
          }

          Comment


            #6
            Hello kiss987,

            The major takeaway from the Sync Account Position video is that Sync Account Position only does just that: syncing the account position to the strategy position. It does not sync the strategy position to the account position.

            In the video, I disabled the strategy and submitted market sell market orders to put the account position in a short position. This put the account position out of sync with the strategy position. When I re-enabled, the Sync Account Position behavior synced the account position to the strategy position by submitting an order to sync it.

            I suggest to test your strategy with Immediately Submit enabled and Sync Account Position to false, as it sounds like you do not want your strategy to submit another order to sync the account position to the strategy position.

            If you would like to reference the differences between the account position and the strategy position, please see the Account Position vs. Strategy Position link in post 2.

            Please let me know if I can be of additional help.
            Last edited by NinjaTrader_Jim; 07-27-2017, 06:51 AM.
            JimNinjaTrader Customer Service

            Comment


              #7
              hi

              1. It is unclear to me what was the "Sync account position" value on the video.

              2. It seems your last answer was cut off. At the end of the first paragraph you write "It does not " and then there's no continuation to the sentence.

              Thanks

              Comment


                #8
                Hello kiss987,

                I'm not sure what happened with my post there. I've edited the post with a complete answer explaining what Sync Account Position does and does not do.

                Was there any confusion behind my explanation of the video?
                In the video, I disabled the strategy and submitted market sell market orders to put the account position in a short position. This put the account position out of sync with the strategy position. When I re-enabled, the Sync Account Position behavior synced the account position to the strategy position by submitting an order to sync it.
                I have created another demonstration for you: https://www.screencast.com/t/1F9IbKjF62

                Keep in mind the order gets filled during the last few seconds when I re-enable the strategy and should not be confused with the Synchronize orders submitted by the Sync Account Position Behavior.
                Last edited by NinjaTrader_Jim; 07-27-2017, 01:24 PM.
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Hello

                  After entering long using EnterLong and restarting the strategy, can I exit the position using ExitLong?

                  Comment


                    #10
                    Hello kiss987,

                    Yes, this would be possible.

                    As long as the Start Behavior used (Immediately Submit) can match the order historically, the position will be resumed and can get closed out by ExitLong().

                    Remember that when we enable a strategy, it processes the data historically. The orders generated through the historical data get compared to existing real time orders and if they match the strategy resumes them.

                    * A previously generated order is considered to match an active strategy order when the order action, order type, quantity, limit price, and stop price are exactly identical.
                    Please let me know if I may be of further assistance.
                    JimNinjaTrader Customer Service

                    Comment


                      #11
                      1. When I submit an order and then restart my strategy, will the OnExecution and OnOrderUpdate events be triggered after the restart (for the order which was submitted before the restart)?

                      2. Can you please attach the strategy that you showed on




                      Thanks
                      Last edited by kiss987; 08-03-2017, 11:31 AM.

                      Comment


                        #12
                        Hello kiss987,

                        1. When I submit an order and then restart my strategy, will the OnExecution and OnOrderUpdate events be triggered after the restart (for the order which was submitted before the restart)?
                        Yes. As a test, you can add a print in OnExecution(). When you enable a strategy, you will see prints for all historical executions. The same can be said for order updates in OnOrderUpdate()

                        2. Can you please attach the strategy that you showed on
                        https://www.screencast.com/t/1F9IbKjF62
                        Done.
                        Attached Files
                        JimNinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Gerik, Today, 09:40 AM
                        1 response
                        6 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Started by RookieTrader, Today, 09:37 AM
                        1 response
                        10 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by alifarahani, Today, 09:40 AM
                        0 responses
                        5 views
                        0 likes
                        Last Post alifarahani  
                        Started by KennyK, 05-29-2017, 02:02 AM
                        3 responses
                        1,285 views
                        0 likes
                        Last Post NinjaTrader_Clayton  
                        Started by AttiM, 02-14-2024, 05:20 PM
                        11 responses
                        186 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Working...
                        X