Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Close ATM STrategy from indicator

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

    Close ATM STrategy from indicator

    HI
    I use buttons on a grid (NOT in charttrader) to handle my positions - the one thing I can't 'mimic' is the Close button.
    I can exit the position but the atmstrategy stop and profit orders remain. Its just annloing.

    In strategies there is AtmStrategyClose but I don't have access to that in an indicator - any help much appreciated.

    #2
    Hello Mindset,

    Thanks for your post.

    You could consider looping through <Account>.Strategies collection within your indicator and use CloseStrategy() to close out a strategy that is running.

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

    <Account>.Strategies: https://ninjatrader.com/support/help...gies_account.h tm
    CloseStrategy(): https://ninjatrader.com/support/help...sestrategy.htm

    And, see the attached example script demonstrating this concept.

    Let us know if we may assist further.
    Attached Files
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      ah so simple - thanks Brandon. Perfect solution to my issue.

      Comment


        #4
        Originally posted by Mindset View Post
        ah so simple - thanks Brandon. Perfect solution to my issue.
        Hi Mindset,

        I imported the CloseStrategyIndi indicator into NT8 but I don't see any indication of any sort on the chart ... just blank. So, please how do you use the indicator ?

        Regards.

        omololu

        Comment


          #5
          omololu
          you just need these two lines

          foreach (StrategyBase strat in myAccount.Strategies)
          {
          strat.CloseStrategy("Close Strategy");
          }

          Comment


            #6
            Brandon
            Occasionally I get
            Unhandled exception - Collection was modified enumeration operation may not execute.

            When I capture the error this is what I get
            28/4/2022 8:31:33 pm System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
            at System.ThrowHelper.ThrowInvalidOperationException( ExceptionResource resource)
            at System.Collections.Generic.List`1.Enumerator.MoveN extRare()
            at NinjaTrader.NinjaScript.Indicators.MyIndicators.Bu ttonsHorizontalDev.Button_Action(Int32 idx, Boolean bChangeStatus)

            Can you help me untangle / prevent this error?

            Comment


              #7
              Originally posted by Mindset View Post
              omololu
              you just need these two lines

              foreach (StrategyBase strat in myAccount.Strategies)
              {
              strat.CloseStrategy("Close Strategy");
              }
              Hi Mindset,

              Thanks for your reply.

              So, those two lines should be in my own indicator and which should be under the scripts that trigger a "CLOSE trade" option ... like a "Close" button ... Right ?

              Regards.

              omololu

              Comment


                #8
                yes but please see some errors cropping up above

                Comment


                  #9
                  Hello Mindset,

                  Thanks for your note.

                  In general, the error is caused when you are iterating a collection and modifying it at the same time.

                  You could consider using the <Account>.Flatten() method with the collection modified to something like a .ToList() to clone the list first so that it cannot be changed as the loop is running.

                  See the help guide documentation below for more information about <Account>.Flatten() and sample code.

                  <Account>.Flatten(): https://ninjatrader.com/support/help...ml?flatten.htm

                  For information about working with Lists in C#, you could do a quick Google search for something like 'working with lists C#' to find public documentation and sample code on the topic.

                  Let us know if we may assist further.
                  <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                  Comment


                    #10
                    BrandonH
                    Thanks for the flatten idea - it seems to resolve my issue ( well currently anyway!).
                    This is a little beyond me

                    "You could consider using the <Account>.Flatten() method with the collection modified to something like a .ToList() to clone the list first so that it cannot be changed as the loop is running."

                    The whole clone and list thing is a bit too deep for me.
                    But I have utilised the code in the help section you provided and certainly in Sim mode it works just fine.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    599 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    345 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    103 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    558 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    558 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X