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

Auto Close Code

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

    Auto Close Code

    I was exploring the strategy builder, and discovered the conditions and actions functionality. However the 'Order Management Folder,' does not provided an option to close all positions. Can someone share the code to 'close all positions,' based on pre-defined conditions? The Strategy Builder has limitations for this type of functionality.

    #2
    Hello PYEEDEVELOPER,

    Thanks for your post.

    A limitation of using the Strategy Builder is that there are no options available that close/flatten all positions.

    Something to consider is creating a condition checking if you are in a Long or Short market position and call your ExitLong/ExitShort exit order to exit the long or short position.

    An example of checking if you are in a Long position could be seen in the image attached.

    Otherwise, you would need to unlock the script from the Strategy Builder and manually code your strategy to use the Account.Flatten() method.

    See this help guide page for more information about Account.Flatten(): https://ninjatrader.com/support/help...t8/flatten.htm

    Let me know if I may assist further.
    Attached Files
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_BrandonH View Post
      Hello PYEEDEVELOPER,

      Thanks for your post.

      A limitation of using the Strategy Builder is that there are no options available that close/flatten all positions.

      Something to consider is creating a condition checking if you are in a Long or Short market position and call your ExitLong/ExitShort exit order to exit the long or short position.

      An example of checking if you are in a Long position could be seen in the image attached.

      Otherwise, you would need to unlock the script from the Strategy Builder and manually code your strategy to use the Account.Flatten() method.

      See this help guide page for more information about Account.Flatten(): https://ninjatrader.com/support/help...t8/flatten.htm

      Let me know if I may assist further.
      I have not found this code to work. I've got 2 positions open, and I've implemented your code (as referenced in the quote above)...and that code does not close the positions. It doesn't create any errors and it doesn't effect any closing of the 2 positions (both were entered using ChartTrader on the Sim101 account.

      I just want to "Flatten" all of the current positions and cancel all of the orders on an account. What's the best way to do that using an indicator?

      Comment


        #4
        Hello sbgtrading,

        Thanks for your note.

        The Strategy Builder screenshot would only apply to strategies, not custom indicators.

        To flatten an account on an instrument you could use the Account.Flatten() method.

        Note that if you have positions on multiple instruments on an account, you would need to use Account.Flatten() for each instrument that has a position open. See this 'Flatten a list of instruments' section of the help guide sample code for how this could be done.

        Please see the help guide documentation below for more information and sample code.

        Account: https://ninjatrader.com/support/help...ount_class.htm
        Account.Flatten(): https://ninjatrader.com/support/help...t8/flatten.htm

        Another option would be to use the Account.FlattenEverything() method in your script to flatten all positions on an account. Note that this method is not documented/supported and it would be up to your discretion to use it in your script.

        Let me know if I may assist further.
        Last edited by NinjaTrader_BrandonH; 02-28-2023, 01:49 PM.
        Brandon H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by lightsun47, Today, 03:51 PM
        0 responses
        4 views
        0 likes
        Last Post lightsun47  
        Started by 00nevest, Today, 02:27 PM
        1 response
        8 views
        0 likes
        Last Post 00nevest  
        Started by futtrader, 04-21-2024, 01:50 AM
        4 responses
        44 views
        0 likes
        Last Post futtrader  
        Started by Option Whisperer, Today, 09:55 AM
        1 response
        13 views
        0 likes
        Last Post bltdavid  
        Started by port119, Today, 02:43 PM
        0 responses
        9 views
        0 likes
        Last Post port119
        by port119
         
        Working...
        X