Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

calling flatten from Indicator

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

    calling flatten from Indicator

    Account.Flatten(new [] { this.Instrument });

    is there a way when executing this command from a indicator that it can check that the flatten executed successfully and then execute other code?
    it appears that this call will freeze NT as it takes 'time' to execute the command. However it is not synchronous and once called it will return immediately and continue with processing other commands.
    Last edited by WHICKED; 09-26-2024, 11:16 AM.

    #2
    Hello WHICKED,

    There is not any kind of return for that method so that specifically would not be possible. You could write logic that has a delay which checks the account position collection after doing that to see if any positions on the instrument still exist.

    Comment


      #3
      what would be the best way to get the instrument position then close out all corresponding positions and any ATM strategies etc.

      calling .Flatten seems to cause issues and freezes when using it in a Indicator.

      as well Position.Close("","") also seems to freeze when called.

      Comment


        #4
        literally calling Flatten or Position.Close from an indicator - causes the entire NT to freeze and lock requiring a restart.

        Comment


          #5
          Hello WHICKED,

          What specific code are you using when you call Flatten? Are you using the addon framework to first find an account and save it to a variable? The code originally posted is the unsupported for as that uses the strategy Account property. Your code should look similar to:

          //find account:

          lock (Account.All)
          myAccount = Account.All.FirstOrDefault(a => a.Name == "Sim101");​

          //later

          myAccount.Flatten(new [] { Instrument.GetInstrument("ES 12-24") });

          This could also relate to how your script is operating logically in case it is calling that method more than 1 time.

          Comment


            #6
            ok the reason it is freezing is that I am needing to get the NinjaTrader.Gui.Tools.AccountSelector chartTraderAccountSelector = null;
            NinjaTrader.Gui.NinjaScript.AtmStrategy.AtmStrateg ySelector chartTraderATMSelector

            and

            myAccount = Account.All.FirstOrDefault(a => a.Name == chartTraderAccountSelector.SelectedAccount.Display Name);
            p = myAccount.GetPosition(instrumentId);

            however, since this is a indicator - it is on a different thread then the charttrader, gui, etc. so I MUST use a Dispatcher to get the information.

            However, I MUST be able to get the values back from those selectors by using this.Dispatcher.Invoke or ChartControl.Dispatcher.Invoke -- this call causes a deadlock issue and InvokeAsync won't work because it can not guarantee that it will get the information back in time.

            why is threading SUCH an issue with NT? is there a solution to this?​

            Comment


              #7
              Hello WHICKED,

              Using invoke is incorrect and causes freezing, you need to use InvokeAsync for any UI thread manipulations. InvokeAsync is the only correct way to use the UI so you would need to change how you have coded that portion of the script, all code that you need to use the UI value needs to go inside the dispatcher so it happens at a later time when the thread is available.

              This is not specific to NT, any C# application has the same stipulations for threading and UI access. You need to use the correct approach to execute code on different threads so it does not cause deadlocks.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              596 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              343 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
              556 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              554 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X