Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Disabled Strategies missing when looping account.Strategies

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

    Disabled Strategies missing when looping account.Strategies

    *** I think I posted this in the wrong place ..Can we move it to the correct forum? ***

    Hello friends!

    When I run this:

    Code:
    foreach (StrategyBase strategy in account.Strategies)
    {
    Print("Strat Category: " + strategy.Category + " Name: " + strategy.Name + " Instrument: " +strategy.Instrument.MasterInstrument.Name + " State: " + strategy.State);
    
    }
    The only print I get are from Strategies that are currently running (in strategyState == Historical or == Realtime, maybe == Active, but I've never seen that state).

    The strategies are listed correctly in ​Control Center, They work fine, as in, when Enabled, they take trades etc..

    If I enable a strategy from Control Center, I can see the print output information about that strategy as expected. If I disable the strategy, I see the message "Disabling NinjaScript strategy" and I can no longer see the strategy printed from the loop.

    Help! =)

    S.
    Last edited by scouratier; 08-22-2024, 05:16 AM.

    #2
    I'll also add, this page: https://ninjatrader.com/support/help...rategybase.htm

    Suggest that OnAccountStatusUpdate get events from enabling or disabling strategies. I've wired up things in my addon and I just do a simple print in OnAccountStatusUpdate and I never see strategy event being sent to it.

    Comment


      #3
      I did some a bit more work on my end. Attached a debugger. I looked at all my accounts and all listed strategies. I can confirm that in memory, disabled strategies do not show up.

      Comment


        #4
        I went ahead and made a quick addon to share for testing:
        Code:
        [HASHTAG="t3322"]region[/HASHTAG] Using declarations
        using System;
        using NinjaTrader.Cbi;
        
        #endregion
        
        //This namespace holds Add ons in this folder and is required. Do not change it.
        namespace NinjaTrader.NinjaScript.AddOns
        {
        
        public class ForumHelp : AddOnBase
        {
        // Timer Obj
        private System.Timers.Timer UpdateTimer;
        
        protected override void OnStateChange()
        {
        if (State == State.SetDefaults)
        {
        // listen to connection events
        Connection.ConnectionStatusUpdate += OnConnectionStatusUpdate;
        foreach (Connection connection in Connection.Connections)
        {
        Print("SetDefaults/Connections: " + connection.Options.Name + " Status: " + connection.Status);
        }
        
        // loop accounts
        foreach (Account account in Account.All)
        {
        if (account.Name == "Backtest") continue;
        account.AccountItemUpdate += OnAccountItemUpdate;
        account.PositionUpdate += OnPositionUpdate;
        
        // first update of AccountItems
        foreach (AccountItem item in Enum.GetValues(typeof(AccountItem)))
        {
        // this seems to work, so not going to print
        }
        
        // first update of strategies
        UpdateStrategyStates(account);
        }
        
        // Subscribe to static events. Remember to unsubscribe with -= when you are done
        Account.AccountStatusUpdate += OnAccountStatusUpdate;
        
        }
        else if (State == State.Configure)
        {
        UpdateTimer = new System.Timers.Timer(5000);
        UpdateTimer.Elapsed += OnTimerElapsed;
        UpdateTimer.Enabled = true;
        }
        else if (State == State.DataLoaded)
        {
        
        }
        else if (State == State.Terminated)
        {
        // loop accounts
        foreach (Account account in Account.All)
        {
        if (account.Name == "Backtest") continue;
        account.AccountItemUpdate -= OnAccountItemUpdate;
        account.PositionUpdate -= OnPositionUpdate;
        
        }
        Account.AccountStatusUpdate -= OnAccountStatusUpdate;
        
        Connection.ConnectionStatusUpdate -= OnConnectionStatusUpdate;
        // stop the timer
        if (UpdateTimer != null)
        {
        UpdateTimer.Enabled = false;
        UpdateTimer.Elapsed -= OnTimerElapsed;
        UpdateTimer.Stop();
        }
        }
        }
        
        private void OnTimerElapsed(object sender, EventArgs e)
        {
        lock (Account.All)
        {
        foreach (Account account in Account.All)
        {
        UpdateStrategyStates(account);
        }
        }
        }
        
        private void OnPositionUpdate(object sender, PositionEventArgs e)
        {
        Print("OnPositionUpdate: " + sender.ToString() + " : " + e.ToString());
        }
        
        private void OnAccountStatusUpdate(object sender, AccountStatusEventArgs e)
        {
        Print("OnAccountStatusUpdate: " + sender.ToString() + " : " + e.ToString());
        
        }
        
        public void UpdateStrategyStates(Account account)
        {
        foreach (StrategyBase strategy in account.Strategies)
        {
        Print("Strat Category: " + strategy.Category + " Name: " + strategy.Name + " Instrument: " + strategy.Instrument.MasterInstrument.Name + " State: " + strategy.State);
        }
        
        }
        private void OnAccountItemUpdate(object sender, AccountItemEventArgs e)
        {
        // this seems to work, so leaving it out
        //Print("OnAccountItemUpdate: " + e.AccountItem.ToString() + " : " + e.Value.ToString());
        }
        
        private void OnConnectionStatusUpdate(object sender, ConnectionStatusEventArgs e)
        {
        Print("OnConnectionStatusUpdate: " + sender.ToString() + " Connection Args:" + e.ToString());
        }
        
        
        }
        }

        Comment


          #5
          All added strategies in the strategies tab are saved in the ninjatrader.sqlite db file. Perhaps you can use a viewer to see what's going on in that file to get some clues

          Comment


            #6
            The strategy table is empty. Ahem. That seems odd.

            Comment


              #7
              Hello scouratier,

              Thank you for your post.

              The Account.Strategies collection only holds currently enabled strategies, so it is expected that a disabled strategy would not be included in the collection.

              Unfortunately, there are currently no supported means of getting a list of disabled strategies. However this thread will remain open for any members of the community who want to share unsupported solutions.

              Please let us know if you have any further questions. ​

              Comment


                #8
                Well, there's a discrepancy then (or at the very least, a need for clarification).

                From this page of the documentation:



                In the code sample, we can read.

                // This comprises all which are active, recovered upon last connect, or deactived since last connect
                This would mean that strategies that have been enabled / were enabled on last connect and are then disabled should still show up. That's not the case.

                Comment


                  #9
                  Hello scouratier,

                  Thanks for pointing this out.

                  This comment is indeed a mistake in the Help Guide. The collection will only contain enabled strategies and active ATMs. I've made our team aware and they have marked this for correction.

                  Apologies for any confusion caused by this error in the HG.

                  Comment


                    #10
                    Originally posted by backtester831 View Post
                    All added strategies in the strategies tab are saved in the ninjatrader.sqlite db file. Perhaps you can use a viewer to see what's going on in that file to get some clues
                    So went ahead and fixed this. Reflection issues were preventing the strategies from being saved to the DB. This solved another one of my problems where I needed to add strategies back to my charts on each restart, so that's great.

                    Disabled strats do not show up, confirming Gaby's post. Sad Face.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by argusthome, 03-08-2026, 10:06 AM
                    0 responses
                    60 views
                    0 likes
                    Last Post argusthome  
                    Started by NabilKhattabi, 03-06-2026, 11:18 AM
                    0 responses
                    39 views
                    0 likes
                    Last Post NabilKhattabi  
                    Started by Deep42, 03-06-2026, 12:28 AM
                    0 responses
                    21 views
                    0 likes
                    Last Post Deep42
                    by Deep42
                     
                    Started by TheRealMorford, 03-05-2026, 06:15 PM
                    0 responses
                    23 views
                    0 likes
                    Last Post TheRealMorford  
                    Started by Mindset, 02-28-2026, 06:16 AM
                    0 responses
                    51 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Working...
                    X