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 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
    Brandon H.NinjaTrader Customer Service

    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.
                  Brandon H.NinjaTrader Customer Service

                  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 cre8able, Yesterday, 01:16 PM
                    3 responses
                    11 views
                    0 likes
                    Last Post cre8able  
                    Started by ChartTourist, Today, 08:22 AM
                    0 responses
                    6 views
                    0 likes
                    Last Post ChartTourist  
                    Started by LiamTwine, Today, 08:10 AM
                    0 responses
                    2 views
                    0 likes
                    Last Post LiamTwine  
                    Started by Balage0922, Today, 07:38 AM
                    0 responses
                    5 views
                    0 likes
                    Last Post Balage0922  
                    Started by JoMoon2024, Today, 06:56 AM
                    0 responses
                    6 views
                    0 likes
                    Last Post JoMoon2024  
                    Working...
                    X