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

Close Positions in a Controlled Way on Strategy Termination

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

    Close Positions in a Controlled Way on Strategy Termination

    A strategy starts and submits various orders that result in open positions. These open positions are active when the strategy is manually terminated from the Control Center.

    How does one "interrupt" the termination to gracefully close these open positions and do other expected termination activities?

    Thanks.
    Multi-Dimensional Managed Trading
    jeronymite
    NinjaTrader Ecosystem Vendor - Mizpah Software

    #2
    Hello jeronymite,

    Thank you for the post.

    I wanted to clarify, by interrupt and gracefully close do you mean to pause the standard termination that is near instant and perform some of your own logic to close the position?



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

    Comment


      #3
      Close Positions in a Controlled Way on Strategy Termination

      That's right, Jesse.

      Approximate sequence of events:
      1. Start strategy from Control Center; from within the strategy ...
      2. Enter Long/Short orders (EnterLong/EnterShort)
      3. Orders are filled and active
      4. Set StopLoss orders
      5. Manually disable strategy from Control Center
      6. ASAP the strategy disable is detected ...
      7. "Interrupt" the termination, only allowing it to resume on a given signal, if possible, so that all desired activities are guaranteed to have completed
      8. Perform various tasks within OnStop or OnTermination or some other method ...
      9. Close open positions and ensure all normal OnExecution and OnOrderUpdate etc activities occur
      10. Perform other housekeeping tasks including writing final records to files and closing filestreams etc
      11. ... etc ...
      12. Normal termination activities resume and strategy terminates as expected
      Thanks.
      Multi-Dimensional Managed Trading
      jeronymite
      NinjaTrader Ecosystem Vendor - Mizpah Software

      Comment


        #4
        Hello,

        Thank you for confirming.

        Generally, OnTermination should be sufficient for cleaning resources and doing other actions. Are you seeing that some action you are doing is not being completed in some case? The method body would need to complete all lines of code before the script is disabled so this should allow you to exit or write data if needed when clicking disable.

        You would not have the script remain enabled to receive further order updates for OnExecution and OnOrderUpdate as the script was willingly disabled. The final logic should be completed but this would not include any error handling logic you may have because you disabled the script.

        I will put in a feature request for a more advanced termination system, something that includes some sort of flag or bool to indicate the script can now be disabled.

        The alternate I could offer would be to make your own button to disable the strategy and then call to disable it when you are complete with your actions. You could append a button to the toolbar or make a chart panel as a few examples. This would be an advanced concept and is over the top compared to just clicking the standard disable button, but I believe this would be the way to go to align with your goal.

        There are a few examples of creating GUI elements at the following link if you are interested in seeing what would be required for creating your own button, nt7 is not well documented for this type of item but going forward with nt8 this is.



        I look forward to being of further assistance.
        Last edited by NinjaTrader_Jesse; 09-06-2017, 07:46 AM.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Close Positions in a Controlled Way on Strategy Termination

          Thanks, Jesse.

          I need to explicitly handle disabling of the strategy in Control Center to ensure that no matter what, that particular situation is handled correctly -- one cannot always control what a user will do and so one needs to code defensively to handle all possible situations.

          I find that I can do various "rundown" activities (e.g. writing to files, etc) in OnTermination without issue.

          The problem is closing open positions and ensuring the positions are closed in a "normal" and "expected" manner (including OnExecution and OnOrderUpdate) and all associated post-close activities are completed (e.g. writing trade results to file, etc). If one initiates the closing of all open positions (individually) from within OnStop, I find that by the time OnTermination completes, the positions have not exited and continue to be open and active even after the strategy is fully disabled.

          Hopefully, this makes it clearer. Thanks.
          Multi-Dimensional Managed Trading
          jeronymite
          NinjaTrader Ecosystem Vendor - Mizpah Software

          Comment


            #6
            Hello,

            Yes in this case if your strategy has special requirements, you would need to relay this to the end user as it is not a part of the platform currently for what you are trying to do.

            As noted you would either have the option of allowing OnTermination to proceed normally or to implement a custom disable for the strategy using your own means. Unfortunately, there is not a safe way I could provide to hold the strategies OnTermination as this is being called internally in a specific way.

            I have put in a feature request for a more advanced termination state but this would not likely be implemented in NT7. Very likely you would need to make a custom disable system and relay how to correctly use your strategy to end users.


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

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by elirion, Today, 01:36 AM
            2 responses
            12 views
            0 likes
            Last Post elirion
            by elirion
             
            Started by DJ888, 04-16-2024, 06:09 PM
            5 responses
            14 views
            0 likes
            Last Post NinjaTrader_Erick  
            Started by samish18, Yesterday, 08:31 AM
            4 responses
            14 views
            0 likes
            Last Post elirion
            by elirion
             
            Started by funk10101, Yesterday, 09:43 PM
            1 response
            14 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by TheWhiteDragon, 01-21-2019, 12:44 PM
            5 responses
            551 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Working...
            X